diff m-toolbox/classes/+utils/@jmysql/query.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children 91f21a0aab35
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/m-toolbox/classes/+utils/@jmysql/query.m	Wed Nov 23 19:22:13 2011 +0100
@@ -0,0 +1,79 @@
+% QUERY query an LTPDA repository.
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+% DESCRIPTION: QUERY query an LTPDA repository.
+%
+% CALL:                     results = query(conn, q)
+%              [results, col_names] = query(conn, q)
+%
+% INPUTS:
+%              conn - an mpipeline.repository.RepositoryConnection object,
+%                     as is returned by utils.jmysql.connect
+%              q    - a valid MySQL query string
+%
+% 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
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+
+%              (*) 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)
+  
+  prefs = getappdata(0, 'LTPDApreferences');
+  
+  if ~isa(varargin{1}, 'mpipeline.repository.RepositoryConnection')
+    error('### The first argument should be a RepositoryConnection');
+  end
+  
+  if ~ischar(varargin{2})
+    error('### The second argument should be a query string.');
+  end
+  
+  % Inputs
+  conn = varargin{1};
+  q    = varargin{2};
+  
+  % Outputs
+  results  = {};
+  colnames = {};
+  
+  % 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
+  if nargout > 0
+    varargout{1} = resultSet;
+    if nargout == 2
+      % Get column names from the meta data
+      rsm = resultSet.getMetaData;
+      Nc = rsm.getColumnCount;
+      colnames = cell(1,Nc);
+      for kk=1:Nc
+        colnames{kk} = char(rsm.getColumnName(kk));
+      end
+      varargout{2} = colnames;
+    end
+  end
+  
+end
+