Mercurial > hg > ltpda
annotate m-toolbox/classes/+utils/@jmysql/dbquery.m @ 34:03d92954b939 database-connection-manager
Improve look of LTPDAPreferences diaolog
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Mon, 05 Dec 2011 16:20:06 +0100 |
parents | 91f21a0aab35 |
children |
rev | line source |
---|---|
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
1 function varargout = dbquery(conn, varargin) |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
2 % DBQUERY Query an AO repository database |
0 | 3 % |
4 % CALL: info = dbquery(conn); | |
5 % info = dbquery(conn, query); | |
6 % info = dbquery(conn, table, query); | |
7 % | |
8 % INPUTS: | |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
9 % |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
10 % conn - a database connection object implementing java.sql.Connection |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
11 % query - a valid MySQL query string |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
12 % table - a table name |
0 | 13 % |
14 % OUTPUTS: | |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
15 % |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
16 % info - structure containing fields from each matching record |
0 | 17 % |
18 % EXAMPLES: | |
19 % | |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
20 % >> info = utils.jmysql.dbquery(conn, 'SELECT * FROM objmeta WHERE id>1000 AND id<2000'); |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
21 % >> info = utils.jmysql.dbquery(conn, 'ao', 'id>1000 AND id<2000'); |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
22 % >> info = utils.jmysql.dbquery(conn, 'objmeta', 'name LIKE "x12"'); |
0 | 23 % >> info = utils.jmysql.dbquery(conn, 'users', 'username="aouser"'); |
24 % >> info = utils.jmysql.dbquery(conn, 'collections', 'id=3'); | |
25 % >> info = utils.jmysql.dbquery(conn, 'collections', 'obj_ids="1,2"'); | |
26 % >> info = utils.jmysql.dbquery(conn, 'transactions', 'user_id=3'); | |
27 % >> info = utils.jmysql.dbquery(conn, 'transactions', 'obj_id=56'); | |
28 % | |
29 % >> tables = utils.jmysql.dbquery(conn) | |
30 % | |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
31 % The 'tables' cell-array will contain a list of the tables in the database |
0 | 32 % |
33 % >> info = utils.jmysql.dbquery(conn, query) | |
34 % | |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
35 % The 'info' cell-array will contain the results from the SQL query |
0 | 36 % |
37 % VERSION: $Id: dbquery.m,v 1.2 2010/01/19 20:55:30 ingo Exp $ | |
38 % | |
39 | |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
40 if ~isa(conn, 'java.sql.Connection') |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
41 error('### first argument should be a java.sql.Connection object'); |
0 | 42 end |
43 | |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
44 switch nargin |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
45 case 1 |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
46 % get table list |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
47 try |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
48 info = utils.mysql.execute(conn, 'SHOW TABLES'); |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
49 catch ex |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
50 error('### failed to get table list. %s', ex.message); |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
51 end |
0 | 52 |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
53 case 2 |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
54 % execute query |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
55 try |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
56 info = utils.mysql.execute(conn, varargin{1}); |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
57 catch ex |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
58 error('### failed to execute query. %s', ex.message); |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
59 end |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
60 |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
61 case 3 |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
62 % query a table |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
63 table = varargin{1}; |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
64 query = varargin{2}; |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
65 info = runQuery(conn, table, query); |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
66 |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
67 otherwise |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
68 error('### incorrect inputs'); |
0 | 69 end |
70 | |
71 varargout{1} = info; | |
72 end | |
73 | |
74 | |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
75 function info = getFieldList(conn, table) |
0 | 76 info = {}; |
77 try | |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
78 rows = utils.mysql.execute(conn, sprintf('DESCRIBE `%s`', table)); |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
79 for kk = 1:size(rows, 1) |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
80 info = [info rows(kk,1)]; |
0 | 81 end |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
82 catch ex |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
83 error('### failed to get field list. %s', ex.message); |
0 | 84 end |
85 end | |
86 | |
87 | |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
88 function info = runQuery(conn, table, query) |
0 | 89 info = {}; |
90 fields = getFieldList(conn, table); | |
91 f = ''; | |
92 fs = {}; | |
93 for j=1:length(fields) | |
94 % special cases | |
95 f = [f fields{j} ',' ]; | |
96 fs = [fs fields(j)]; | |
97 end | |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
98 q = sprintf('SELECT %s FROM %s WHERE %s', f(1:end-1), table, query); |
0 | 99 try |
16
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
100 info = utils.mysql.execute(conn, q); |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
101 catch ex |
91f21a0aab35
Update utils.jquery
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
102 error('### failed to query table. %s', ex.message); |
0 | 103 end |
104 end |