comparison m-toolbox/classes/@repogui/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.7 2011/04/08 08:56:26 hewitson Exp $
6 %
7
8 function cb_get_dbs(varargin)
9
10 % Handles
11 myh = varargin{1};
12 mainfig = varargin{end};
13 status = findobj(mainfig.handle, 'Tag', 'RepoguiStatusTxt');
14 set(status,'String','*** Retrieving databases...');
15
16 tables = {'collections', 'objmeta', 'objs', 'transactions', 'users'};
17
18 % Get conn
19 conn = mainfig.connection;
20 % Assume I don't need to open the connection
21 wasEmpty = 0;
22
23 % Open connection, if necessary
24 if isempty(conn)
25
26 % we opened the connection
27 wasEmpty = 1;
28
29 % have we a stored username and password
30 dbuser = mainfig.dbuser;
31 dbpass = mainfig.dbpass;
32
33 % get hostname
34 hostname = get(findobj(mainfig.handle, 'Tag','RepoguiServerEdit'), 'String');
35 if isempty(hostname)
36 set(status,'String','Impossible to connect: please select a server first');
37 error('### Please select a server to connect to.');
38 end
39
40 % verify if the server is reachable
41 try
42 java.net.InetAddress.getByName(hostname);
43 catch
44 set(status,'String','Impossible to connect: server not responding or no internet connection available');
45 error('### Please check internet connection and server availability.');
46 end
47
48 % connect
49 try
50 if ~isempty(dbuser) && ~isempty(dbpass)
51 conn = utils.mysql.connect(hostname, 'test', dbuser, dbpass);
52 else
53 [conn, dbpass] = utils.mysql.connect(hostname);
54 end
55 catch
56 set(status,'String','Impossible to connect: please check the provided username and password');
57 error('### Please check the provided username and password')
58 end
59
60 if ~ischar(conn) && ~ischar(dbpass)
61 if conn==-1 && dbpass==-1
62 % user cancelled
63 return;
64 end
65 end
66
67 if ~isa(conn, 'database')
68 error('### couldn''t connect to server');
69 end
70
71 % store the connection
72 mainfig.dbuser = conn.Username;
73 mainfig.dbpass = dbpass;
74 else
75 hostname = get(findobj(mainfig.handle, 'Tag','RepoguiServerEdit'), 'String');
76 end
77
78 % get the databases
79 q = 'show databases';
80 curs = exec(conn, q);
81 curs = fetch(curs);
82
83 ltpda_dbs = [];
84 % Now get the LTPDA databases
85 for j=1:length(curs.Data)
86 db = curs.Data{j};
87
88 if ~strcmp(db, 'information_schema')
89
90 q = sprintf('show tables from %s', db);
91 c = exec(conn, q);
92 c = fetch(c);
93 match = 0;
94 for l=1:length(c.Data)
95 for k=1:length(tables)
96 if strcmp(c.Data{l}, tables{k})
97 match = match + 1;
98 end
99 end
100 end
101 close(c);
102 if match == 5
103 % we have an LTPDA database
104 ltpda_dbs = [ltpda_dbs cellstr(db)];
105 end
106 end
107
108 end
109 close(curs);
110
111 % If I opened the connection, close it again
112 if wasEmpty
113 close(conn);
114 end
115
116 % Set the list
117 id = findobj(mainfig.handle, 'Tag', 'RepoguiDatabaseList');
118 set(id, 'String', ltpda_dbs, 'Value', 1);
119
120 % Get selected DB
121 dbh = findobj(mainfig.handle, 'Tag', 'RepoguiDatabaseList');
122 dbs = get(dbh, 'String');
123 db = dbs{1};
124
125 % set selected DB
126 dbh = findobj(mainfig.handle, 'Tag', 'RepoguiDatabaseEdit');
127 set(dbh, 'String', db);
128
129 set(status, 'String', ['Database list retrieved from ' hostname],'ForeGroundcolor','b');
130 end