diff m-toolbox/classes/+utils/@jmysql/getUserID.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/m-toolbox/classes/+utils/@jmysql/getUserID.m	Wed Nov 23 19:22:13 2011 +0100
@@ -0,0 +1,43 @@
+% GETUSERID gets the user ID number corresponding to the given user name.
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+% DESCRIPTION: GETUSERID gets the user ID number corresponding to the
+%              user name that connected to the MySQL database.
+%
+% CALL:        userid = getUserID(conn)
+%
+% VERSION:     $Id: getUserID.m,v 1.1 2008/08/08 11:51:12 ingo Exp $
+%
+% HISTORY:     24-05-2007 M Hewitson
+%                 Creation
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+function [userid,dbuser] = getUserID(varargin)
+
+  userid = [];
+  conn = varargin{1};
+  if nargin > 1
+    username = varargin{2};
+  else
+    username = char(conn.getUsername);
+  end
+  try
+    if ~conn.isConnected
+      conn.openConnection;
+    end
+    if conn.isConnected
+      q    = sprintf('select id from users where username="%s"', username);
+      results = conn.query(q);
+      while results.next
+        userid = str2num(char(results.getString(1)));
+      end
+    end
+    dbuser = conn.getUsername;
+        
+  catch
+    warning('!!! Unable to retrieve user ID.');
+  end
+
+end
+