comparison m-toolbox/classes/+utils/@jmysql/connect.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children 91f21a0aab35
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: Connects to an LTPDA repository and returns the connection
5 % object. The provided credentials are stored in a persistent utility, and
6 % reused when a connection to the same database and server is requested.
7 %
8 % If credentials for the requested database and server are not given, and are
9 % not in the utility, the user is prompted for the missing informations.
10 % Credentials are set to expire and to be entered again after a time defined
11 % in the LTPDA user preferences.
12 %
13 % CALL: conn = connect(hostname)
14 % conn = connect(hostname, dbname)
15 % conn = connect(hostname, dbname, dbuser, dbpass)
16 %
17 % VERSION: $Id: connect.m,v 1.13 2011/04/01 08:36:49 hewitson Exp $
18 %
19 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
20
21 function varargout = connect(varargin)
22
23 dbuser = '';
24 dbpass = '';
25 dbhost = '';
26 dbname = '';
27
28 if nargin == 1
29 dbhost = varargin{1};
30 elseif nargin == 2
31 dbhost = varargin{1};
32 dbname = varargin{2};
33 elseif nargin == 3
34 dbhost = varargin{1};
35 dbname = varargin{2};
36 elseif nargin == 4
37 dbhost = varargin{1};
38 dbname = varargin{2};
39 dbuser = varargin{3};
40 dbpass = varargin{4};
41 elseif nargin == 5
42 dbhost = varargin{1};
43 dbname = varargin{2};
44 dbuser = varargin{3};
45 dbpass = varargin{4};
46 end
47
48 rm = LTPDARepositoryManager();
49 conn = rm.findConnections(dbhost, dbname, dbuser, dbpass);
50
51 if isempty(conn)
52 conn = rm.newConnection(dbhost, dbname, dbuser, dbpass);
53 end
54
55 if ~isempty(dbpass) && isempty(char(conn.getPassword))
56 conn.setPassword(dbpass)
57 end
58
59 % If we have more than one matching connection, let the user choose.
60 if numel(conn) > 1
61 conn = rm.manager.selectConnection([]);
62 end
63
64 varargout{1} = conn(1);
65 end