0001 function varargout = jmysql(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010 persistent driver connections
0011
0012
0013 if isempty(driver)
0014 driver = javaObjectEDT('com.mysql.jdbc.Driver');
0015 end
0016
0017
0018 MAX_CONNECTIONS = 15;
0019 if isempty(connections)
0020 connections = cell(1,MAX_CONNECTIONS);
0021 end
0022
0023
0024
0025
0026
0027 if nargin < 1
0028 action = 'status';
0029 else
0030 action = varargin{1};
0031 end
0032
0033
0034 if ~ischar(action)
0035
0036 conn_id = action;
0037
0038
0039 action = varargin{2};
0040 extra_arg_start = 3;
0041 else
0042
0043 conn_id = 0;
0044 extra_arg_start = 2;
0045 end
0046
0047
0048 conn_idx = conn_id+1;
0049
0050
0051 possible_actions = {'open','close','status','use','query'};
0052
0053 if ~ismember(action, possible_actions)
0054 query = action;
0055 action = 'query';
0056 end
0057
0058
0059 switch action
0060 case 'open'
0061
0062
0063 host = varargin{extra_arg_start};
0064 user = varargin{extra_arg_start+1};
0065 password = varargin{extra_arg_start+2};
0066
0067
0068 conn = connections{conn_idx};
0069 if isempty(conn) || conn.isClosed()
0070 try
0071
0072 url = ['jdbc:mysql://' host '/' ];
0073
0074
0075 properties = java.util.Properties();
0076 properties.setProperty('user', user);
0077 properties.setProperty('password', password);
0078
0079
0080 conn = driver.connect(url, properties);
0081
0082
0083 connections{conn_idx} = conn;
0084
0085
0086 varargout{1} = conn_id;
0087
0088 catch exception
0089 throw(MException('jmysql:ConnectionError', char(exception.message)));
0090 end
0091 end
0092
0093 case 'close'
0094
0095 conn = connections{conn_idx};
0096
0097
0098 conn.close();
0099
0100
0101 connections{conn_idx} = [];
0102
0103 case 'status'
0104 conn = connections{conn_idx};
0105 if ~isempty(conn) && ~conn.isClosed()
0106 varargout{1} = 0;
0107 else
0108 varargout{1} = -1;
0109 end
0110
0111 case {'use', 'query'}
0112 if strcmp(action,'use')
0113 db = varargin{extra_arg_start};
0114 query = ['use ' db];
0115 end
0116
0117
0118 conn = connections{conn_idx};
0119 if isempty(conn)
0120 error('Connection not open')
0121 end
0122
0123
0124 statement = conn.createStatement();
0125
0126
0127 if statement.execute(query)
0128
0129 resultset = statement.getResultSet();
0130
0131
0132 [outdata, varnames] = process_result_set(resultset);
0133
0134
0135 resultset.close()
0136 else
0137 outdata = {};
0138 varnames = {};
0139 end
0140
0141
0142 statement.close();
0143
0144
0145 if strcmp(action, 'query')
0146
0147
0148 varargout = outdata;
0149 end
0150 end
0151
0152 end
0153
0154 function [outdata, varnames] = process_result_set(resultset)
0155
0156 metadata = resultset.getMetaData();
0157 ncols = metadata.getColumnCount();
0158
0159
0160 resultset.last();
0161 nrows = resultset.getRow();
0162
0163
0164 varnames = cell(1,ncols);
0165 datatypes = cell(1,ncols);
0166 outdata = cell(1,ncols);
0167
0168
0169 for icol = 1:ncols
0170 varnames{icol} = char(metadata.getColumnLabel(icol));
0171 datatypes{icol} = char(metadata.getColumnTypeName(icol));
0172 end
0173
0174
0175 numeric_types = {'DECIMAL','BIGINT UNSIGNED', 'INT UNSIGNED'};
0176 datetypes = {'DATETIME','DATE','TIME'};
0177
0178
0179 for icol = 1:ncols
0180 switch datatypes{icol}
0181 case numeric_types
0182 outdata{icol} = nan(nrows,1);
0183 otherwise
0184 outdata{icol} = cell(nrows,1);
0185 end
0186 end
0187
0188
0189 if resultset.first()
0190 have_more_data = true;
0191 irow = 0;
0192
0193 while have_more_data
0194 irow = irow+1;
0195
0196
0197 for icol = 1:ncols
0198 value = resultset.getObject(icol);
0199
0200 if ~resultset.wasNull()
0201
0202 switch datatypes{icol}
0203 case numeric_types
0204 outdata{icol}(irow,1) = double(value);
0205 case datetypes
0206 outdata{icol}{irow,1} = char(value);
0207 otherwise
0208 outdata{icol}{irow,1} = value;
0209
0210 end
0211 end
0212 end
0213
0214
0215 if ~resultset.next()
0216 have_more_data = false;
0217 end
0218 end
0219 end
0220 end
0221