%EXECUTEQUERY Execute SQL query and return data to workspace.

%   Author(s): C.F.Garvin, 05-07-98
%   Copyright (c) 1984-2000 The MathWorks, Inc. 
%   $Revision: 1.14 $   $Date: 2000/01/20 14:06:46 $

%Perform error checking on current query statement
anse = querycallbacks('errors');
set(findobj('Type','figure'),'Pointer','watch')
if anse
  clear anse
  set(findobj('Type','figure'),'Pointer','arrow')
  return
end

%Get query string
qobj = findobj(gcf,'Tag','query');
querystr = get(qobj,'String');

%Get connection info
sobj = findobj(gcf,'Tag','sources');
sstr = get(sobj,'String');
sval = get(sobj,'Value');

%Get workspace variable
wobj = findobj(gcf,'Tag','wkvariable');
wkvar = get(wobj,'String');

%Exit if query or variable string is empty or if no data source chosen
if isempty(sval)
  errordlg('Please choose a data source.')
  clear anse qobj querystr sobj sstr sval wobj wkvar
  set(findobj('Type','figure'),'Pointer','arrow')
  return
end
if isempty(querystr)
  errordlg('No SQL statement found for execution.')
  clear anse qobj querystr sobj sstr sval wobj wkvar
  set(findobj('Type','figure'),'Pointer','arrow')
  return
end
if isempty(wkvar) | ~isnan(str2double(wkvar))
  errordlg('Please specify a valid MATLAB workspace variable for return data storage.')
  clear anse qobj querystr sobj sstr sval wobj wkvar
  set(findobj('Type','figure'),'Pointer','arrow')
  return
end

%Open connection
sourcestr = sstr{sval};
conn = loginconnect(sourcestr);

%Execute the statement
curs = exec(conn,querystr);

%Exit if error returned from query
if ~isempty(curs.Message)
  errordlg(curs.Message)
  close(conn)
  clear anse qobj querystr sobj sstr sval wobj wkvar sourcestr conn curs
  set(findobj('Type','figure'),'Pointer','arrow')
  return
end

%No data returned by query or bad query
ansr = struct(curs);
if isempty(ansr.ResultSet)
  errordlg('Invalid SQL statement')
  close(conn)
  clear anse qobj querystr sobj sstr sval wobj wkvar sourcestr conn curs ansr
  set(findobj('Type','figure'),'Pointer','arrow')
  return
end
  
%Fetch the data in 1000 records chunks to allow for interruption of fetch
stobj = findobj('Tag','vqbstatus');
set(stobj,'String','Executing Query. Type Control-C to cancel.')
querycallbacks('cleanupdialog')
set(findobj('Type','figure'),'Pointer','watch')
drawnow
ansf = [];
while 1   
  curs = fetch(curs,1280);
  if ~strcmp(curs.Data,'No Data')
    ansf = [ansf;curs.Data];
  else
    break
  end
end
set(stobj,'String','')
clear stobj

%Exit if error returned from fetch
if ~isempty(curs.Message)
  errordlg(curs.Message)
  close(curs)
  close(conn)
  clear anse qobj querystr sobj sstr sval wobj wkvar sourcestr conn curs ansr ansf 
  set(findobj('Type','figure'),'Pointer','arrow')
  return
end

%Close cursors and connections
close(curs)
close(conn)

%Store data in specified variable
eval([wkvar ' = ansf;'])
clear anse qobj querystr sobj sstr sval wobj wkvar sourcestr conn curs ansr ansf ans

querycallbacks('varlist')

set(findobj('Type','figure'),'Pointer','arrow')
