view m-toolbox/classes/+utils/@jmysql/getUserID.m @ 43:bc767aaa99a8

CVS Update
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Tue, 06 Dec 2011 11:09:25 +0100
parents f0afece42f48
children
line wrap: on
line source

% 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