% RESULTSTOCELL converts a java sql ResultSet to a cell-array
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: RESULTSTOCELL converts a java sql ResultSet to a cell-array.
%
% CALL:        [results, colnames] = utils.jmysql.resultsToCell(resultSet);
%
% INPUTS:
%              resultSet - a result set like that returned from
%                          utils.jmysql.query
%
% OUTPUTS:
%              results      - a cell array of results
%              col_names    - a cell array of column names
%
%
% VERSION:     $Id: resultsToCell.m,v 1.1 2009/07/27 19:46:21 hewitson Exp $
%
% HISTORY:     24-05-2007 M Hewitson
%                 Creation
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



function varargout = resultsToCell(varargin)
  
  prefs = getappdata(0, 'LTPDApreferences');
  
  if ~isa(varargin{1}, 'java.sql.ResultSet')
    error('### The first argument should be a ResultSet');
  end
  
  % Inputs
  resultSet  = varargin{1};
  
  os = mpipeline.repository.RepositoryConnection.resultSetToObjectArray(resultSet);
  disp(sprintf('*** converted result set: [%dx%d]', numel(os), numel(os(1))));
  
  % Set outputs
  if nargout > 0
    varargout{1} = cell(os);
    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

