Mercurial > hg > ltpda
comparison m-toolbox/classes/+utils/@mysql/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 % CONNECT connects to an LTPDA repository and returns the connection object. | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % DESCRIPTION: CONNECT connects to an LTPDA repository and returns the | |
5 % connection object. | |
6 % | |
7 % CONNECT stores a persistent set of logins where a login has | |
8 % the fields: | |
9 % 'hostname' - the hostname of the MySQL server | |
10 % 'db' - the name of the database | |
11 % 'user' - the username for connection to this db | |
12 % 'passwd' - the password for this user | |
13 % | |
14 % If a login for the requested hostname and db doesn't exist, | |
15 % or if the existing login is too old (*) then the user is | |
16 % prompted to log in again. | |
17 % | |
18 % (*) the expiry time for MySQL logins is set to 30s. | |
19 % | |
20 % CALL: conn = connect(hostname) % connects to 'test' database | |
21 % conn = connect(hostname, dbname) % dialog prompt for username and password | |
22 % [conn, password] = connect(hostname, dbname) % return password to caller | |
23 % conn = connect(hostname, dbname, dbuser, dbpass) % connect with explicit username and password | |
24 % | |
25 % VERSION: $Id: connect.m,v 1.8 2010/01/22 12:46:08 ingo Exp $ | |
26 % | |
27 % HISTORY: 24-05-2007 M Hewitson | |
28 % Creation | |
29 % | |
30 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
31 | |
32 | |
33 % (*) the expiry time for MySQL logins is a property of the | |
34 % LTPDA Toolbox and can be set in the LTPDA Preferences. | |
35 | |
36 | |
37 function varargout = connect(varargin) | |
38 | |
39 error('### Obsolete as of LTPDA version 2.2. Replaced my the utils class jmysql (utils.jmysql.%s)', mfilename()); | |
40 if nargin < 1 | |
41 error('### Incorrect inputs') | |
42 end | |
43 | |
44 persistent logins; | |
45 | |
46 prefs = getappdata(0, 'LTPDApreferences'); | |
47 expTime = prefs.repository.loginExpiry; | |
48 | |
49 dbuser = ''; | |
50 dbpass = ''; | |
51 | |
52 force = false; | |
53 if nargin == 1 | |
54 dbhost = varargin{1}; | |
55 dbname = 'test'; | |
56 elseif nargin == 2 | |
57 dbhost = varargin{1}; | |
58 dbname = varargin{2}; | |
59 elseif nargin == 3 | |
60 dbhost = varargin{1}; | |
61 dbname = varargin{2}; | |
62 force = varargin{3}; | |
63 elseif nargin == 4 | |
64 dbhost = varargin{1}; | |
65 dbname = varargin{2}; | |
66 dbuser = varargin{3}; | |
67 dbpass = varargin{4}; | |
68 elseif nargin == 5 | |
69 dbhost = varargin{1}; | |
70 dbname = varargin{2}; | |
71 dbuser = varargin{3}; | |
72 dbpass = varargin{4}; | |
73 force = varargin{5}; | |
74 end | |
75 | |
76 if isempty(logins) | |
77 % Prompt for username and password | |
78 [dbuser, dbpass] = getlogin(dbhost, dbname, dbuser, dbpass, force); | |
79 | |
80 % in the case the user clicks cancel | |
81 if ~ischar(dbuser) && ~ischar(dbpass) | |
82 if (dbuser == -1 && dbpass == -1) | |
83 varargout{1} = -1; | |
84 varargout{2} = -1; | |
85 return | |
86 end | |
87 end | |
88 | |
89 % Create a new login | |
90 login.hostname = dbhost; | |
91 login.db = dbname; | |
92 login.user = dbuser; | |
93 login.passwd = dbpass; | |
94 login.ts = now; | |
95 | |
96 % store this login | |
97 logins = [logins login]; | |
98 | |
99 else | |
100 % look for a login to this hostname/db | |
101 loginFound = false; | |
102 for kk=1:numel(logins) | |
103 login = logins(kk); | |
104 dt = (now - login.ts) * 86400; % [s] | |
105 if strcmp(login.hostname, dbhost) && strcmp(login.db, dbname) | |
106 if dt < expTime | |
107 % get the username and password from here | |
108 dbuser = login.user; | |
109 dbpass = login.passwd; | |
110 logins(kk).ts = now; | |
111 loginFound = true; | |
112 break; | |
113 end | |
114 end | |
115 end | |
116 | |
117 % If we didn't find a login then create a new one | |
118 if force || isempty(dbuser) || isempty(dbpass) | |
119 [dbuser, dbpass] = getlogin(dbhost, dbname, dbuser, dbpass, force); | |
120 | |
121 % Create a new login | |
122 login.hostname = dbhost; | |
123 login.db = dbname; | |
124 login.user = dbuser; | |
125 login.passwd = dbpass; | |
126 login.ts = now; | |
127 | |
128 % store this login | |
129 if ~loginFound | |
130 logins = [logins login]; | |
131 end | |
132 end | |
133 | |
134 end | |
135 | |
136 %% return if we don't have a dbuser | |
137 if isempty(dbuser) | |
138 return | |
139 end | |
140 | |
141 %% Database settings | |
142 dbdriver = getappdata(0, 'mysql_driver'); | |
143 dburl = sprintf('jdbc:mysql://%s/%s',dbhost,dbname); | |
144 | |
145 disp(sprintf('** Connecting to %s as %s...', dbhost, dbuser)) | |
146 | |
147 conn = database(dbname,dbuser,dbpass,dbdriver,dburl); | |
148 disp('** Connection status:') | |
149 disp(ping(conn)) | |
150 | |
151 % Set outputs | |
152 if nargout > 0 | |
153 varargout{1} = conn; | |
154 if nargout == 2 | |
155 varargout{2} = dbpass; | |
156 end | |
157 end | |
158 | |
159 end | |
160 | |
161 function [dbuser, dbpass] = getlogin(dbhost, dbname, dbuser, dbpass, force) | |
162 | |
163 % first try defaults | |
164 if isempty(dbuser) | |
165 dbuser = getappdata(0, 'ltpda_repo_user'); | |
166 end | |
167 if isempty(dbpass) | |
168 dbpass = getappdata(0, 'ltpda_repo_pass'); | |
169 end | |
170 | |
171 % then log-in dialog | |
172 if force || isempty(dbuser) || isempty(dbpass) | |
173 | |
174 ld = logindialog.LoginDialog([], true); | |
175 ld.setVisible(true) | |
176 if ~ld.isCancelled | |
177 dbuser = char(ld.getUsername); | |
178 dbpass = [char(ld.getPassword)]'; % for some reason we get a column here, so transpose. | |
179 end | |
180 | |
181 if isempty(dbuser) || isempty(dbpass) | |
182 warning('!!! Login process cancelled.'); | |
183 dbuser = -1; | |
184 dbpass = -1; | |
185 return | |
186 end | |
187 end | |
188 | |
189 end |