comparison m-toolbox/classes/@repogui/cb_connect.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_CONNECT callback when the user clicks connect button.
2 %
3 % M Hewitson 22-09-08
4 %
5 % $Id: cb_connect.m,v 1.6 2011/04/08 08:56:26 hewitson Exp $
6 %
7
8 function cb_connect(varargin)
9
10 myh = varargin{1};
11 mainfig = varargin{end};
12 status = findobj(mainfig.handle, 'Tag', 'RepoguiStatusTxt');
13 panels = mainfig.panels;
14
15 % Get conn
16 conn = mainfig.connection;
17
18 if isempty(conn)
19
20 % get hostname
21 hostname = get(findobj(mainfig.handle, 'Tag','RepoguiServerEdit'), 'String');
22
23 % get the database
24 db = get(findobj(mainfig.handle, 'Tag','RepoguiDatabaseEdit'), 'String');
25
26 if isempty(hostname)
27 set(status,'String','Impossible to connect: please select a server first','ForeGroundcolor','r');
28 error('### Please select a server first.');
29 elseif isempty(db)
30 set(status,'String','Impossible to connect: please select a database first','ForeGroundcolor','r');
31 error('### Please select a database first.');
32 end
33
34 % check for username and password
35 dbuser = mainfig.dbuser;
36 dbpass = mainfig.dbpass;
37
38 % verify if the server is reachable
39 try
40 java.net.InetAddress.getByName(hostname);
41 catch
42 set(status,'String','Impossible to connect: server not responding or no internet connection available','ForeGroundcolor','r');
43 error('### Please check internet connection and server availability.');
44 end
45
46 % connect
47 try
48 if ~isempty(dbpass) && ~isempty(dbuser)
49 conn = utils.mysql.connect(hostname, db, dbuser, dbpass, true);
50 else
51 [conn, dbpass] = utils.mysql.connect(hostname, db, true);
52 end
53 catch
54 set(status,'String','Impossible to connect: please check the provided username and password','ForeGroundcolor','r');
55 error('### Please check the provided username and password')
56 end
57
58
59 if ~ischar(conn) && ~ischar(dbpass)
60 if conn==-1 && dbpass==-1
61 % user cancelled
62 return;
63 end
64 end
65
66 if isa(conn, 'database')
67 mainfig.connection = conn;
68 mainfig.username = conn.Username;
69 set(myh, 'String', 'disconnect');
70 set(status, 'String', sprintf('connected to %s on %s as %s', db, hostname, conn.Username),'ForeGroundcolor','b');
71
72 % Set tables on query panel
73 buildConditions(mainfig);
74
75 % store the connection
76 mainfig.dbuser = conn.Username;
77 mainfig.dbpass = dbpass;
78 else
79 set(status, 'String', 'not connected','ForeGroundcolor','r');
80 error('Invalid connection.');
81 end
82 else
83 % disconnect
84 close(conn);
85 conn = [];
86 disp('* disconnected.');
87 mainfig.connection = conn;
88 mainfig.username = '';
89 set(myh, 'String', 'connect');
90 set(status, 'String', 'not connected','ForeGroundcolor','r');
91 mainfig.dbuser = '';
92 mainfig.dbpass = '';
93 end
94
95 end