diff m-toolbox/classes/+utils/@jmysql/query.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/query.m	Mon Dec 05 16:20:06 2011 +0100
+++ b/m-toolbox/classes/+utils/@jmysql/query.m	Mon Dec 05 16:20:06 2011 +0100
@@ -1,70 +1,44 @@
-% QUERY query an LTPDA repository.
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+function varargout = query(conn, query, varargin)
+% QUERY  Query an LTPDA repository.
 %
-% DESCRIPTION: QUERY query an LTPDA repository.
+% DEPRECATED! Use utils.mysql.execute instead!
 %
-% CALL:                     results = query(conn, q)
-%              [results, col_names] = query(conn, q)
+% CALL:                [results] = query(conn, q, varargin)
+%              [results, cnames] = query(conn, q, varargin)
 %
 % INPUTS:
-%              conn - an mpipeline.repository.RepositoryConnection object,
-%                     as is returned by utils.jmysql.connect
-%              q    - a valid MySQL query string
+%
+%      conn  - database connection object implementing java.sql.Connection
+%     query  - a valid SQL query
+%  varargin  - optional query parameters
 %
 % OUTPUTS:
-%                results - a cell-array of the results
-%              col_names - a cell-array of the column names in the query
-%
-% VERSION:     $Id: query.m,v 1.1 2009/07/27 19:46:21 hewitson Exp $
 %
-% HISTORY:     24-05-2007 M Hewitson
-%                 Creation
+%   results  - a cell-array of the results
+%    cnames  - a cell-array of the column names in the query
 %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
+% VERSION: $Id: query.m,v 1.1 2009/07/27 19:46:21 hewitson Exp $
+%
 
-%              (*) the expiry time for MySQL logins is a property of the
-%              LTPDA Toolbox and can be set in the LTPDA Preferences.
-
-
-function varargout = query(varargin)
+  warning('!!! deprecated. use utils.mysql.execute instead');
   
-  prefs = getappdata(0, 'LTPDApreferences');
-  
-  if ~isa(varargin{1}, 'mpipeline.repository.RepositoryConnection')
-    error('### The first argument should be a RepositoryConnection');
+  if ~isa(conn, 'java.sql.Connection')
+    error('### first argument should be a java.sql.Connection object');
   end
   
-  if ~ischar(varargin{2})
-    error('### The second argument should be a query string.');
+  stmt = conn.prepareStatement(query);
+  for kk = 1:numel(varargin)
+    stmt.setObject(kk, varargin{kk});
   end
   
-  % Inputs
-  conn = varargin{1};
-  q    = varargin{2};
-  
-  % Outputs
-  results  = {};
-  colnames = {};
+  % execute query
+  resultSet = stmt.executeQuery();
   
-  % Check connection
-  if ~conn.isConnected
-    conn.openConnection
-  end
-  
-  if ~conn.isConnected
-    conn.display;
-    error('### Failed to open connection');
-    return
-  end
-  
-  resultSet = conn.query(q);
-    
-  % Set outputs
+  % set outputs
   if nargout > 0
     varargout{1} = resultSet;
     if nargout == 2
-      % Get column names from the meta data
+      % get column names from the meta data
       rsm = resultSet.getMetaData;
       Nc = rsm.getColumnCount;
       colnames = cell(1,Nc);