function querybuilder()
%QUERYBUILDER Start visual SQL query builder.

%   Author(s): C.F.Garvin, 04-16-98
%   Copyright (c) 1984-2000 The MathWorks, Inc.
%   $Revision: 1.24 $   $Date: 2000/08/21 18:52:18 $

%GUI spacing parameters
dfp = get(0,'DefaultFigurePosition');
mfp = [560 420];    %Reference width and height
bspc = mean([5/mfp(2)*dfp(4) 5/mfp(1)*dfp(3)]);
bhgt = 20/mfp(2) * dfp(4);
bwid = 80/mfp(1) * dfp(3);
bwid2 = 90/mfp(1) * dfp(3);

%Focus dialog if already open and return
dlgobj = findobj('Tag','SQLDLG');
if ~isempty(dlgobj)
  figure(dlgobj);
  return
end

%Return valid data sources
datasources = getdatasources;     

if isempty(datasources)  %Empty datasources indicates no defined sources
  errordlg('No available data sources.');
  return
elseif ~iscell(datasources) & datasources == -1
  errordlg('Unable to open ODBC.INI.');
  return
end

%Build main window
f = figure('Name','Visual Query Builder','Numbertitle','off','Resizefcn','querycallbacks(''cleanupdlg'')',...
           'Menubar','none','Tag','SQLDLG','Closerequestfcn','querycallbacks(''Exit'')');
         
%Adjust size of dialog to fit uicontrols
p = get(gcf,'Position');
rgt = 6*(bspc+bwid);
top = p(4);
set(gcf,'Position',[p(1) p(2) rgt top]);

%Make frame for better visual display
uicontrol('Enable','off',...
          'Position',[bspc top-(bspc+6*bhgt) 4*bspc+6*bwid 6*bhgt-bspc]);
 
%Data Sources uicontrols
uicontrol('Style','text','String','Data source',...
   'Position',[2*bspc top-(bspc+bhgt) bwid bhgt]);
uicontrol('Style','listbox','String',datasources,'Callback','querycallbacks',...
  'Tag','sources','Tooltip','List of valid data sources on system',...
  'Max',2,'Value',[],...
  'Position',[2*bspc top-(6*bhgt) 2*bwid 5*bhgt-bspc]);

%Data Source table uicontrols
uicontrol('Style','text','String','Tables',...
   'Position',[3*bspc+2*bwid top-(bspc+bhgt) bwid bhgt]);
uicontrol('Style','listbox','String','','Max',2,...
   'Tag','tables','Callback','querycallbacks',...
   'Tooltip','Tables in selected data source',...
   'Position',[3*bspc+2*bwid top-(6*bhgt) 2*bwid 5*bhgt-bspc]);

%Table fieldnames
uicontrol('Style','text','String','Fields',...
   'Position',[4*bspc+4*bwid top-(bspc+bhgt) bwid bhgt]);
uicontrol('Style','listbox','String','',...
   'Tag','fields','Max',2,'Callback','buildquery',... 
   'Tooltip','Fields in selected table',...
   'Position',[4*bspc+4*bwid top-(6*bhgt) 2*bwid 5*bhgt-bspc]);
 
%Options panel
uicontrol('Enable','off',...
  'Position',[bspc top-(2*bspc+9*bhgt) 4*bspc+6*bwid 3*bspc+2*bhgt]);
uicontrol('Style','text','String','Advanced query options',...
  'Position',[2*bspc top-(2*bspc+7*bhgt) bwid bhgt]); 

%Select ALL or DISTINCT radio buttons
uicontrol('Style','radiobutton','String','All','Tag','AoD','Value',1,...
  'Callback','querycallbacks','Tooltip','Return ALL records',...
  'Userdata','ALL','Position',[2*bspc top-(bspc+8*bhgt) bwid bhgt]);
uicontrol('Style','radiobutton','String','Distinct','Tag','AoD','Value',0,...
  'Callback','querycallbacks','Tooltip','Return UNIQUE records',...
  'Userdata','DISTINCT','Position',[2*bspc top-(bspc+9*bhgt) bwid bhgt]);

%Pushbuttons for advanced query statements
uicontrol('String','Where...','Callback','querycallbacks',...
  'Tooltip','Data search conditions','Tag','Where...',...
  'Position',[9*bspc+bwid top-(bspc+8*bhgt) bwid2 bhgt]);
uicontrol('String','Group by...','Callback','querycallbacks',...
  'Tooltip','Group data','Tag','Group By...',...
  'Position',[10*bspc+bwid+bwid2 top-(bspc+8*bhgt) bwid2 bhgt]);
uicontrol('String','Having...','Callback','querycallbacks',...
  'Tooltip','Data group conditions','Tag','Having...',...
  'Position',[11*bspc+bwid+2*bwid2 top-(bspc+8*bhgt) bwid2 bhgt]);
uicontrol('String','Order by...','Callback','querycallbacks',...
  'Tooltip','Sort data','Tag','Order By...',...
  'Position',[12*bspc+bwid+3*bwid2 top-(bspc+8*bhgt) bwid2 bhgt]);

%Build listboxes for visual queue that clause has been built
tmpstr = 'set(gcbo,''Value'',[])';
uicontrol('Style','edit','Tag','wherelist','Max',2,'Callback',tmpstr,...
  'Tooltip','Current data search conditions',...
  'Position',[9*bspc+bwid top-(bspc+9*bhgt) bwid2 bhgt]);
uicontrol('Style','edit','Tag','groupbylist','Max',2,'Callback',tmpstr,...
  'Tooltip','Current data grouping settings',...
  'Position',[10*bspc+bwid+bwid2 top-(bspc+9*bhgt) bwid2 bhgt]);
uicontrol('Style','edit','Tag','havinglist','Max',2,'Callback',tmpstr,...
  'Tooltip','Current data group search conditions',...
  'Position',[11*bspc+bwid+2*bwid2 top-(bspc+9*bhgt) bwid2 bhgt]);
uicontrol('Style','edit','Tag','orderbylist','Max',2,'Callback',tmpstr,...
  'Tooltip','Current data sorting settings',...
  'Position',[12*bspc+bwid+3*bwid2 top-(bspc+9*bhgt) bwid2 bhgt]);

%Build SQL statement and output variable uicontrols
uicontrol('Enable','off',...
  'Position',[bspc top-(2*bspc+14*bhgt) 4*bspc+6*bwid 2*bspc+4*bhgt]);
uicontrol('Style','text','String','SQL statement',...
  'Position',[2*bspc top-(3*bspc+10*bhgt) bwid bhgt]);  
uicontrol('Style','edit','String','','Tag','query',...
  'Tooltip','SQL statement for execution',...
  'Position',[2*bspc top-(3*bspc+11*bhgt) 2*bspc+6*bwid bhgt]);
uicontrol('Style','text','String','MATLAB workspace variable',...
  'Position',[2*bspc top-(5*bspc+12*bhgt) bwid bhgt]);  
uicontrol('Style','edit','String','','Tag','wkvariable',...
  'Tooltip',' MATLAB workspace variable for return data storage',...
  'Position',[2*bspc top-(5*bspc+13*bhgt) bwid bhgt]);
uicontrol('Style','text','Foregroundcolor','blue','Tag','vqbstatus',...
  'Fontweight','bold',...
  'Position',[2*bspc+2*bwid top-(5*bspc+12*bhgt) bwid bhgt]);
uicontrol('String','Execute','Callback','executequery',...
  'Tooltip','Execute shown SQL statement','Tag','Execute',...
  'Position',[4*bspc+5*bwid top-(5*bspc+13*bhgt) bwid bhgt]);

%Build list box for return data display
uicontrol('Enable','off',...
  'Position',[bspc top-(7*bspc+19*bhgt) 4*bspc+6*bwid 6*bhgt-bspc]);
uicontrol('Style','text','String','Data',...
  'Position',[2*bspc top-(7*bspc+14*bhgt) bwid bhgt]);
uicontrol('Style','text','String','Workspace variable',...
  'Position',[2*bspc top-(7*bspc+15*bhgt) bwid bhgt]);
uicontrol('Style','text','String','Size',...
  'Position',[2*bspc+2.25*bwid top-(7*bspc+15*bhgt) bwid bhgt]);
uicontrol('Style','text','String','Memory (bytes)',...
  'Position',[2*bspc+4*bwid top-(7*bspc+15*bhgt) bwid bhgt]);
uicontrol('Style','listbox','Tag','querydata','Fontname',get(0,'FixedWidthFontName'),'Max',2,...
  'Tooltip','List of variables created by Visual Query Builder queries',...
  'Callback','querycallbacks','Userdata',0,...
  'Position',[2*bspc top-(6*bspc+19*bhgt) 2*bspc+6*bwid 4*bhgt]);

%Build uicontrols to store clause data
uicontrol('Visible','off','Tag','whereclausedata');
uicontrol('Visible','off','Tag','orderbyclausedata');
uicontrol('Visible','off','Tag','havingclausedata');
uicontrol('Visible','off','Tag','groupbyclausedata');
uicontrol('Visible','off','Tag','saveflag');

%Build Query menu
qm = uimenu('Label','&Query','Accelerator','Q');
uimenu(qm,'Label','&Execute','Tag','Execute','Callback','executequery');
uimenu(qm,'Label','&Load...','Tag','Load','Callback','querycallbacks');
uimenu(qm,'Label','&Save...','Tag','Save','Callback','querycallbacks');
uimenu(qm,'Label','E&xit','Tag','Exit','Callback','querycallbacks');
uimenu(qm,'Label','&Preferences...','Tag','Preferences',...
  'Separator','on','Callback','querycallbacks');

%Build data display menu
dm = uimenu('Label','&Display','Accelerator','D');
uimenu(dm,'Label','&Data...','Tag','Data',...
  'Callback','eval([''showdatacallbacks(''''data'''','' get(findobj(''Tag'',''wkvariable''),''String'') '')''],'''')');
uimenu(dm,'Label','&Chart...','Tag','Chart',...
  'Callback','eval([''showdatacallbacks(''''chart'''','' get(findobj(''Tag'',''wkvariable''),''String'') '')''],'''')');

%Simple report supported for Windows and SOL2
if any(strcmp({'PCWIN','SOL2'},computer))
  repenable = 'on';
else
  repenable = 'off';
end

if (exist('rptgen') & exist('report')) %check for report generator
  repgenenable = 'on';
  repenable = 'on';
else
  repgenenable = 'off';
end

uimenu(dm,'Label','&Report...','Tag','Report','Enable',repenable,...
  'Callback','eval([''showdatacallbacks(''''report'''','' get(findobj(''Tag'',''wkvariable''),''String'') '')''],'''')');
uimenu(dm,'Label','Report &Generator...','Tag','ReportGenerator',...
  'Enable',repgenenable,'Callback','showdatacallbacks(''repgen'')');

%Build Help Menu
hm = uimenu('Label','&Help','Accelerator','H');
uimenu(hm,'Label','&Visual Query Builder Help','Tag','Help','Callback','qbhelp(''ALL'')');
uimenu(hm,'Label','Database &Toolbox Help','Tag','Help','Callback','qbhelp(''TOOLBOX'')');
if ~isunix   %Currently no demo for UNIX
  uimenu(hm,'Label','&Demos','Callback','vqbdemo','Separator','on');
end
uimenu(hm,'Label','&About Database Toolbox','Tag','Help','Callback','qbhelp(''AboutDialog'')','Separator','on');
  
%Create objects to store usernames and passwords
L = length(datasources);
udata = cell(1,L);
uicontrol('Visible','off','Tag','UserNames','Userdata',udata);
uicontrol('Visible','off','Tag','Passwords','Userdata',udata);

%Display current workspace variables in listbox
querycallbacks('varlist')

%Visual cleanup of gui
querycallbacks('cleanupdialog')
