diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/m-toolbox/classes/+utils/@jmysql/connect.m	Wed Nov 23 19:22:13 2011 +0100
@@ -0,0 +1,65 @@
+% CONNECT Connects to an LTPDA repository and returns the connection object.
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+% DESCRIPTION: Connects to an LTPDA repository and returns the connection
+% object. The provided credentials are stored in a persistent utility, and
+% reused when a connection to the same database and server is requested.
+%
+% If credentials for the requested database and server are not given, and are
+% not in the utility, the user is prompted for the missing informations.
+% Credentials are set to expire and to be entered again after a time defined
+% in the LTPDA user preferences.
+%
+% CALL:        conn = connect(hostname)
+%              conn = connect(hostname, dbname)
+%              conn = connect(hostname, dbname, dbuser, dbpass)
+%
+% VERSION:     $Id: connect.m,v 1.13 2011/04/01 08:36:49 hewitson Exp $
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+function varargout = connect(varargin)
+  
+  dbuser = '';
+  dbpass = '';
+  dbhost = '';
+  dbname = '';
+  
+  if nargin == 1
+    dbhost = varargin{1};
+  elseif nargin == 2
+    dbhost = varargin{1};
+    dbname = varargin{2};
+  elseif nargin == 3
+    dbhost = varargin{1};
+    dbname = varargin{2};
+  elseif nargin == 4
+    dbhost = varargin{1};
+    dbname = varargin{2};
+    dbuser = varargin{3};
+    dbpass = varargin{4};
+  elseif nargin == 5
+    dbhost = varargin{1};
+    dbname = varargin{2};
+    dbuser = varargin{3};
+    dbpass = varargin{4};
+  end
+  
+  rm = LTPDARepositoryManager();
+  conn = rm.findConnections(dbhost, dbname, dbuser, dbpass);
+  
+  if isempty(conn)
+    conn = rm.newConnection(dbhost, dbname, dbuser, dbpass);
+  end
+  
+  if ~isempty(dbpass) && isempty(char(conn.getPassword))
+    conn.setPassword(dbpass)
+  end
+  
+  % If we have more than one matching connection, let the user choose.
+  if numel(conn) > 1
+    conn = rm.manager.selectConnection([]);
+  end
+  
+  varargout{1} = conn(1);
+end