function varargout = query(conn, query, varargin)
% QUERY  Query an LTPDA repository.
%
% DEPRECATED! Use utils.mysql.execute instead!
%
% CALL:                [results] = query(conn, q, varargin)
%              [results, cnames] = query(conn, q, varargin)
%
% INPUTS:
%
%      conn  - database connection object implementing java.sql.Connection
%     query  - a valid SQL query
%  varargin  - optional query parameters
%
% OUTPUTS:
%
%   results  - a cell-array of the results
%    cnames  - a cell-array of the column names in the query
%
% VERSION: $Id: query.m,v 1.1 2009/07/27 19:46:21 hewitson Exp $
%

  warning('!!! deprecated. use utils.mysql.execute instead');
  
  if ~isa(conn, 'java.sql.Connection')
    error('### first argument should be a java.sql.Connection object');
  end
  
  stmt = conn.prepareStatement(query);
  for kk = 1:numel(varargin)
    stmt.setObject(kk, varargin{kk});
  end
  
  % execute query
  resultSet = stmt.executeQuery();
  
  % set outputs
  if nargout > 0
    varargout{1} = resultSet;
    if nargout == 2
      % get column names from the meta data
      rsm = resultSet.getMetaData;
      Nc = rsm.getColumnCount;
      colnames = cell(1,Nc);
      for kk=1:Nc
        colnames{kk} = char(rsm.getColumnName(kk));
      end
      varargout{2} = colnames;
    end
  end
  
end

