% 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

