% DBQUERY query an AO repository database.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: DBQUERY query an AO repository database.
%
% CALL:        info = dbquery(conn);
%              info = dbquery(conn, query);
%              info = dbquery(conn, table, query);
%
% INPUTS:
%           conn   - a database connection object such as that returned by
%                    utils.mysql.connect().
%           query  - a valid MySQL query string
%           table  - a table name
%
% OUTPUTS:
%           info - the returned 'info' structure contains the fields from
%                  each matching record.
%
% EXAMPLES:
%
%   >> info  = dbquery(conn, 'select * from objmeta where id>1000 and id<2000');
%   >> info  = dbquery(conn, 'ao',           'id>1000 and id<2000');
%   >> info  = dbquery(conn, 'objmeta',      'name like "x12"');
%   >> info  = dbquery(conn, 'users',        'username="aouser"');
%   >> info  = dbquery(conn, 'collections',  'id=3');
%   >> info  = dbquery(conn, 'collections',  'obj_ids="1,2"');
%   >> info  = dbquery(conn, 'transactions', 'user_id=3');
%   >> info  = dbquery(conn, 'transactions', 'obj_id=56');
%
%   >> info  = dbquery(conn)
%
%   The 'info' structure will contain a list of the tables in the database.
%
%   >> info  = dbquery(conn, query)
%
%   The 'info' structure will contain a list of records resulting from the SQL query.
%
% VERSION:     $Id: dbquery.m,v 1.2 2010/01/22 12:46:08 ingo Exp $
%
% HISTORY:     10-05-2007 M Hewitson
%                 Creation
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = dbquery(varargin)

  error('### Obsolete as of LTPDA version 2.2. Replaced my the utils class jmysql (utils.jmysql.%s)', mfilename());
  conn = varargin{1};
  if ~isa(conn, 'database')
    error('### First input must be a database connection object.');
  end

  if nargin == 1
    % get table list
    info = getTableList(conn);

  elseif nargin == 2
    % execute query
    info = simpleQuery(conn, varargin{2});

  elseif nargin == 3
    % query a table
    table    = varargin{2};
    query    = varargin{3};
    info = runQuery(conn, table, query);
  else
    error('### Incorrect inputs.');
  end

  varargout{1} = info;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get table list
function info = getTableList(conn)

  % open a connection
  try
    curs = exec(conn, 'show tables');
    curs = fetch(curs);
    info = curs.Data;
    close(curs);
  catch
    error('### Failed to get table list. Server returned: %s', curs.Message);
  end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get field list
function info = getFieldList(conn, table)

  try
    curs = exec(conn, sprintf('describe %s', table));
    curs = fetch(curs);
    info = curs.Data;
    close(curs);
  catch
    error('### Failed to get field list. Server returned: %s', curs.Message);
  end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get field list
function info = simpleQuery(conn, q)

  % open a connection

  try
    curs = exec(conn, sprintf('%s', q));
    curs = fetch(curs);
    info = curs.Data;
    close(curs);
  catch
    error('### Failed to execute query. Server returned: %s', curs.Message);
  end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Run a query
function info = runQuery(conn, table, query)

  % Run query
  info = [];

  fieldlist = getFieldList(conn, table);
  fields  = fieldlist(:,1);
  f = '';
  fs = {};
  for j=1:length(fields)
    % special cases
    f  = [f fields{j} ',' ];
    fs = [fs fields(j)];
  end
  q = sprintf('select %s  from %s where %s', f(1:end-1), table, query);
  disp(['** QUERY: ' q]);
  try
    curs = exec(conn, q);
    curs = fetch(curs);
    info = curs.Data;
    close(curs);
  catch
    error('### Failed to query table. Server returned: %s', curs.Message);
  end
end


