Mercurial > hg > ltpda
annotate 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 |
rev | line source |
---|---|
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
1 function varargout = query(conn, query, varargin) |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
2 % QUERY Query an LTPDA repository. |
0 | 3 % |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
4 % DEPRECATED! Use utils.mysql.execute instead! |
0 | 5 % |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
6 % CALL: [results] = query(conn, q, varargin) |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
7 % [results, cnames] = query(conn, q, varargin) |
0 | 8 % |
9 % INPUTS: | |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
10 % |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
11 % conn - database connection object implementing java.sql.Connection |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
12 % query - a valid SQL query |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
13 % varargin - optional query parameters |
0 | 14 % |
15 % OUTPUTS: | |
16 % | |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
17 % results - a cell-array of the results |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
18 % cnames - a cell-array of the column names in the query |
0 | 19 % |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
20 % VERSION: $Id: query.m,v 1.1 2009/07/27 19:46:21 hewitson Exp $ |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
21 % |
0 | 22 |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
23 warning('!!! deprecated. use utils.mysql.execute instead'); |
0 | 24 |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
25 if ~isa(conn, 'java.sql.Connection') |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
26 error('### first argument should be a java.sql.Connection object'); |
0 | 27 end |
28 | |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
29 stmt = conn.prepareStatement(query); |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
30 for kk = 1:numel(varargin) |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
31 stmt.setObject(kk, varargin{kk}); |
0 | 32 end |
33 | |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
34 % execute query |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
35 resultSet = stmt.executeQuery(); |
0 | 36 |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
37 % set outputs |
0 | 38 if nargout > 0 |
39 varargout{1} = resultSet; | |
40 if nargout == 2 | |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
41 % get column names from the meta data |
0 | 42 rsm = resultSet.getMetaData; |
43 Nc = rsm.getColumnCount; | |
44 colnames = cell(1,Nc); | |
45 for kk=1:Nc | |
46 colnames{kk} = char(rsm.getColumnName(kk)); | |
47 end | |
48 varargout{2} = colnames; | |
49 end | |
50 end | |
51 | |
52 end | |
53 |