Mercurial > hg > ltpda
diff m-toolbox/classes/+utils/@jmysql/dbquery.m @ 16:91f21a0aab35 database-connection-manager
Update utils.jquery
* * *
Update utils.jmysql.getsinfo
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Mon, 05 Dec 2011 16:20:06 +0100 |
parents | f0afece42f48 |
children |
line wrap: on
line diff
--- a/m-toolbox/classes/+utils/@jmysql/dbquery.m Mon Dec 05 16:20:06 2011 +0100 +++ b/m-toolbox/classes/+utils/@jmysql/dbquery.m Mon Dec 05 16:20:06 2011 +0100 @@ -1,27 +1,25 @@ -% DBQUERY query an AO repository database. -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -% DESCRIPTION: DBQUERY query an AO repository database. +function varargout = dbquery(conn, varargin) +% DBQUERY Query an AO repository database % % CALL: info = dbquery(conn); % info = dbquery(conn, query); % info = dbquery(conn, table, query); % % INPUTS: -% conn - a database connection object such as that returned by -% utils.jmysql.connect(). -% query - a valid MySQL query string -% table - a table name +% +% conn - a database connection object implementing java.sql.Connection +% query - a valid MySQL query string +% table - a table name % % OUTPUTS: -% info - the returned 'info' structure contains the fields from -% each matching record. +% +% info - structure containing fields from each matching record % % EXAMPLES: % -% >> info = utils.jmysql.dbquery(conn, 'select * from objmeta where id>1000 and id<2000'); -% >> info = utils.jmysql.dbquery(conn, 'ao', 'id>1000 and id<2000'); -% >> info = utils.jmysql.dbquery(conn, 'objmeta', 'name like "x12"'); +% >> info = utils.jmysql.dbquery(conn, 'SELECT * FROM objmeta WHERE id>1000 AND id<2000'); +% >> info = utils.jmysql.dbquery(conn, 'ao', 'id>1000 AND id<2000'); +% >> info = utils.jmysql.dbquery(conn, 'objmeta', 'name LIKE "x12"'); % >> info = utils.jmysql.dbquery(conn, 'users', 'username="aouser"'); % >> info = utils.jmysql.dbquery(conn, 'collections', 'id=3'); % >> info = utils.jmysql.dbquery(conn, 'collections', 'obj_ids="1,2"'); @@ -30,111 +28,65 @@ % % >> tables = utils.jmysql.dbquery(conn) % -% The 'tables' cell-array will contain a list of the tables in the database. +% The 'tables' cell-array will contain a list of the tables in the database % % >> info = utils.jmysql.dbquery(conn, query) % -% The 'info' cell-array will contain the results from the SQL query. +% The 'info' cell-array will contain the results from the SQL query % % VERSION: $Id: dbquery.m,v 1.2 2010/01/19 20:55:30 ingo Exp $ % -% HISTORY: 10-05-2007 M Hewitson -% Creation -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -function varargout = dbquery(varargin) - - conn = varargin{1}; - if ~isa(conn, 'mpipeline.repository.RepositoryConnection') - error('### First input must be a database connection object.'); + if ~isa(conn, 'java.sql.Connection') + error('### first argument should be a java.sql.Connection object'); end - if nargin == 1 - % get table list - info = getTableList(conn); - - elseif nargin == 2 - % execute query - info = simpleQuery(conn, varargin{2}); + switch nargin + case 1 + % get table list + try + info = utils.mysql.execute(conn, 'SHOW TABLES'); + catch ex + error('### failed to get table list. %s', ex.message); + end - elseif nargin == 3 - % query a table - table = varargin{2}; - query = varargin{3}; - info = runQuery(conn, table, query); - else - error('### Incorrect inputs.'); + case 2 + % execute query + try + info = utils.mysql.execute(conn, varargin{1}); + catch ex + error('### failed to execute query. %s', ex.message); + end + + case 3 + % query a table + table = varargin{1}; + query = varargin{2}; + info = runQuery(conn, table, query); + + otherwise + error('### incorrect inputs'); end varargout{1} = info; end -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Get table list -function info = getTableList(conn) +function info = getFieldList(conn, table) info = {}; - - % open a connection try - q = 'show tables'; - results = conn.query(q); - while results.next - info = [info {char(results.getString(1))}]; + rows = utils.mysql.execute(conn, sprintf('DESCRIBE `%s`', table)); + for kk = 1:size(rows, 1) + info = [info rows(kk,1)]; end - catch - error('### Failed to get table list.'); + catch ex + error('### failed to get field list. %s', ex.message); end end -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Get field list -function info = getFieldList(conn, table) - info = {}; - try - q = ['describe ' table]; - results = conn.query(q); - while results.next - info = [info {char(results.getObject(1))}]; - end - catch - error('### Failed to get field list.'); - end -end - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Get field list -function info = simpleQuery(conn, q) - - % open a connection +function info = runQuery(conn, table, query) info = {}; - try - results = conn.query(q); - mt = results.getMetaData; - Ncols = mt.getColumnCount; - row = 1; - while results.next - for kk=1:Ncols - info{row,kk} = convertValue(results.getObject(kk)); - end - row = row + 1; - end - - catch ME - disp(ME.message) - error('### Failed to execute query.'); - end -end - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Run a query -function info = runQuery(conn, table, query) - - % Run query - info = {}; - fields = getFieldList(conn, table); f = ''; fs = {}; @@ -143,22 +95,10 @@ f = [f fields{j} ',' ]; fs = [fs fields(j)]; end - q = sprintf('select %s from %s where %s', f(1:end-1), table, query); - disp(['** QUERY: ' q]); + q = sprintf('SELECT %s FROM %s WHERE %s', f(1:end-1), table, query); try - info = simpleQuery(conn, q); - catch - error('### Failed to query table.'); + info = utils.mysql.execute(conn, q); + catch ex + error('### failed to query table. %s', ex.message); end end - -function val = convertValue(val) - - switch class(val) - case 'java.sql.Timestamp' - val = char(val); - otherwise - end -end - -