Mercurial > hg > ltpda
comparison m-toolbox/classes/@repogui2/cb_get_dbs.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 % CB_GET_DBS callback when the user clicks the 'get dbs' button. | |
2 % | |
3 % M Hewitson 22-09-08 | |
4 % | |
5 % $Id: cb_get_dbs.m,v 1.2 2011/04/08 08:56:36 hewitson Exp $ | |
6 % | |
7 | |
8 function cb_get_dbs(varargin) | |
9 | |
10 % Handles | |
11 myh = varargin{1}; | |
12 mainfig = varargin{end}; | |
13 | |
14 tables = {'collections', 'objmeta', 'objs', 'transactions', 'users'}; | |
15 | |
16 % Get conn | |
17 conn = mainfig.connection; | |
18 % Assume I don't need to open the connection | |
19 wasEmpty = 0; | |
20 | |
21 % Open connection, if necessary | |
22 if isempty(conn) | |
23 | |
24 % we opened the connection | |
25 wasEmpty = 1; | |
26 | |
27 % have we a stored username and password | |
28 dbuser = mainfig.dbuser; | |
29 dbpass = mainfig.dbpass; | |
30 | |
31 % get hostname | |
32 hostname = get(findobj(mainfig.handle, 'Tag','RepoguiServerEdit'), 'String'); | |
33 if isempty(hostname) | |
34 error('### Please select a server to connect to.'); | |
35 end | |
36 | |
37 % connect | |
38 if ~isempty(dbuser) && ~isempty(dbpass) | |
39 conn = utils.mysql.connect(hostname, 'test', dbuser, dbpass); | |
40 else | |
41 [conn, dbpass] = utils.mysql.connect(hostname); | |
42 end | |
43 if ~isa(conn, 'database') | |
44 error('### couldn''t connect to server'); | |
45 end | |
46 | |
47 % store the connection | |
48 mainfig.dbuser = conn.Username; | |
49 mainfig.dbpass = dbpass; | |
50 | |
51 end | |
52 | |
53 % get the databases | |
54 q = 'show databases'; | |
55 curs = exec(conn, q); | |
56 curs = fetch(curs); | |
57 | |
58 ltpda_dbs = []; | |
59 % Now get the LTPDA databases | |
60 for j=1:length(curs.Data) | |
61 db = curs.Data{j}; | |
62 | |
63 if ~strcmp(db, 'information_schema') | |
64 | |
65 q = sprintf('show tables from %s', db); | |
66 c = exec(conn, q); | |
67 c = fetch(c); | |
68 match = 0; | |
69 for l=1:length(c.Data) | |
70 for k=1:length(tables) | |
71 if strcmp(c.Data{l}, tables{k}) | |
72 match = match + 1; | |
73 end | |
74 end | |
75 end | |
76 close(c); | |
77 if match == 5 | |
78 % we have an LTPDA database | |
79 ltpda_dbs = [ltpda_dbs cellstr(db)]; | |
80 end | |
81 end | |
82 | |
83 end | |
84 close(curs); | |
85 | |
86 % If I opened the connection, close it again | |
87 if wasEmpty | |
88 close(conn); | |
89 end | |
90 | |
91 % Set the list | |
92 id = findobj(mainfig.handle, 'Tag', 'RepoguiDatabaseList'); | |
93 set(id, 'String', ltpda_dbs, 'Value', 1); | |
94 | |
95 % Get selected DB | |
96 dbh = findobj(mainfig.handle, 'Tag', 'RepoguiDatabaseList'); | |
97 dbs = get(dbh, 'String'); | |
98 db = dbs{1}; | |
99 | |
100 % set selected DB | |
101 dbh = findobj(mainfig.handle, 'Tag', 'RepoguiDatabaseEdit'); | |
102 set(dbh, 'String', db); | |
103 | |
104 end |