Mercurial > hg > ltpda
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 function rows = execute(conn, query, varargin) | |
2 % execute the given SQL query with optional parameters varargin | |
3 % through connection conn. the results are returned in a 2d cell | |
4 % array | |
5 % | |
6 % differs from similar riutines in utils.jmysql by the use of proper | |
7 % SQL escaping of the query arguments. the connection object should | |
8 % be an object implementing the java.sql.Connection interface. | |
9 | |
10 if nargin < 2 | |
11 error('### incorrect usage'); | |
12 end | |
13 if ~isjava(conn) | |
14 error('### invalid connection'); | |
15 end | |
16 | |
17 % return value | |
18 rows = {}; | |
19 | |
20 % build query | |
21 stmt = conn.prepareStatement(query); | |
22 for kk = 1:numel(varargin) | |
23 stmt.setObject(kk, varargin{kk}); | |
24 end | |
25 | |
26 % execute query | |
27 rs = stmt.executeQuery(); | |
28 | |
29 % get results into a cell array | |
30 ncols = rs.getMetaData.getColumnCount(); | |
31 row = 1; | |
32 while rs.next() | |
33 for kk = 1:ncols | |
34 rows{row,kk} = java2matlab(rs.getObject(kk)); | |
35 end | |
36 row = row + 1; | |
37 end | |
38 | |
39 end | |
40 | |
41 function val = java2matlab(val) | |
42 % matlab converts all base types. just add a conversion for datetime columns | |
43 switch class(val) | |
44 case 'java.sql.Timestamp' | |
45 val = time(plist('time', char(val), 'timezone', 'UTC')); | |
46 end | |
47 end |