Mercurial > hg > ltpda
diff m-toolbox/classes/+utils/@jmysql/execute.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/m-toolbox/classes/+utils/@jmysql/execute.m Wed Nov 23 19:22:13 2011 +0100 @@ -0,0 +1,47 @@ +function rows = execute(conn, query, varargin) + % execute the given SQL query with optional parameters varargin + % through connection conn. the results are returned in a 2d cell + % array + % + % differs from similar riutines in utils.jmysql by the use of proper + % SQL escaping of the query arguments. the connection object should + % be an object implementing the java.sql.Connection interface. + + if nargin < 2 + error('### incorrect usage'); + end + if ~isjava(conn) + error('### invalid connection'); + end + + % return value + rows = {}; + + % build query + stmt = conn.prepareStatement(query); + for kk = 1:numel(varargin) + stmt.setObject(kk, varargin{kk}); + end + + % execute query + rs = stmt.executeQuery(); + + % get results into a cell array + ncols = rs.getMetaData.getColumnCount(); + row = 1; + while rs.next() + for kk = 1:ncols + rows{row,kk} = java2matlab(rs.getObject(kk)); + end + row = row + 1; + end + +end + +function val = java2matlab(val) + % matlab converts all base types. just add a conversion for datetime columns + switch class(val) + case 'java.sql.Timestamp' + val = time(plist('time', char(val), 'timezone', 'UTC')); + end +end