view m-toolbox/classes/+utils/@jmysql/getUserID.m @ 11:9174aadb93a5 database-connection-manager

Add LTPDA Repository utility functions into utils.repository
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 16:20:06 +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