170 lines
5.4 KiB
ObjectPascal
170 lines
5.4 KiB
ObjectPascal
unit SQLAccessMain;
|
|
|
|
interface
|
|
|
|
uses
|
|
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
|
|
Dialogs, uDADriverManager, uDAEngine, uDAADODriver, uDAClasses, StdCtrls,
|
|
DB, uDADataTable, uDACDSDataTable, uDAADODataTable, ExtCtrls,
|
|
ComCtrls, uDAScriptingProvider;
|
|
|
|
type
|
|
TSQLAccessMainForm = class(TForm)
|
|
DriverManager: TDADriverManager;
|
|
DAADODriver: TDAADODriver;
|
|
PageControl1: TPageControl;
|
|
TabSheet1: TTabSheet;
|
|
TabSheet2: TTabSheet;
|
|
Label1: TLabel;
|
|
Label2: TLabel;
|
|
Label3: TLabel;
|
|
ExecuteButton: TButton;
|
|
Memo: TMemo;
|
|
rgConnections: TRadioGroup;
|
|
eID: TEdit;
|
|
eCompanyName: TEdit;
|
|
ePhoneNumber: TEdit;
|
|
ProcessSQLButton: TButton;
|
|
Memo1: TMemo;
|
|
Memo2: TMemo;
|
|
rgProcessors: TRadioGroup;
|
|
cbOrderBy: TComboBox;
|
|
Label4: TLabel;
|
|
DAConnectionManager: TDAConnectionManager;
|
|
Panel1: TPanel;
|
|
DASchema: TDASchema;
|
|
procedure ExecuteButtonClick(Sender: TObject);
|
|
procedure FormCreate(Sender: TObject);
|
|
procedure ProcessSQLButtonClick(Sender: TObject);
|
|
|
|
private
|
|
{ Private declarations }
|
|
public
|
|
{ Public declarations }
|
|
end;
|
|
|
|
var
|
|
SQLAccessMainForm: TSQLAccessMainForm;
|
|
|
|
implementation
|
|
|
|
uses uDAInterfaces, uDAMacroProcessors, uDAMacros;
|
|
|
|
{$R *.dfm}
|
|
|
|
procedure TSQLAccessMainForm.ExecuteButtonClick(Sender: TObject);
|
|
var
|
|
connname: string;
|
|
conn: IDAConnection;
|
|
ds: IDADataset;
|
|
i: integer;
|
|
orderby: integer;
|
|
begin
|
|
{ In this example we are treating two different SQL queries like they were the same one.
|
|
One takes data from the SHIPPERS table when the other from the CUSTOMERS one. The field mappings
|
|
that are used when building the where clause isolate the developer from the SQL details and allow
|
|
you to build fairly complex where clauses without the need to do string concatenations in code.
|
|
|
|
Examine the two sql statements associated with the Customers dataset for the two connections for
|
|
more details. The code below transparently works for both.
|
|
|
|
IMPORTANT: Keep in mind the SQL property of a dataset will always and only reflect the statement you define
|
|
in the statement and the dynamic WHERE clause will only be merged to it when opening the dataset.
|
|
}
|
|
|
|
Memo.Lines.Add('---------------');
|
|
Memo.Lines.Add('');
|
|
try
|
|
connname := Trim(Copy(rgConnections.Items[rgConnections.ItemIndex], 1, Pos('-', rgConnections.Items[rgConnections.ItemIndex]) - 1));
|
|
conn := DAConnectionManager.NewConnection(connname, TRUE);
|
|
|
|
ds := DASchema.NewDataset(conn, 'Customers');
|
|
|
|
{ Note: we could have also created the dataset along with the field mappings and statements manually via code.
|
|
In some extremely dynamic situations that might be preferrable. }
|
|
|
|
Memo.Lines.Add('Mappings');
|
|
for i := 0 to ds.FieldCount - 1 do
|
|
Memo.Lines.Add(ds.fields[i].TableField + ' --> ' + ds.fields[i].Name);
|
|
Memo.Lines.Add('');
|
|
|
|
ds.Where.AddCondition('ID', cLike, eID.Text);
|
|
|
|
if (eCompanyName.Text <> '') and (ds.Where.Clause <> '') then ds.Where.AddOperator(opAND);
|
|
|
|
ds.Where.AddCondition('CompanyName', cDifferent, eCompanyName.Text);
|
|
|
|
if (ePhoneNumber.Text <> '') and (ds.Where.Clause <> '') then ds.Where.AddOperator(opOR);
|
|
|
|
ds.Where.AddCondition('PhoneNumber', cEqual, ePhoneNumber.Text);
|
|
|
|
orderby := POS('order by', lowercase(ds.SQL));
|
|
|
|
if (orderby = 0) then begin
|
|
if (cbOrderBy.Text <> '') then ds.SQL := ds.SQL + ' ORDER BY ' + ds.Fieldbyname(cbOrderBy.Text).TableField;
|
|
end
|
|
else begin
|
|
if (cbOrderBy.Text = '') then ds.SQL := copy(ds.sql, 1, orderby - 1)
|
|
else
|
|
ds.SQL := copy(ds.sql, 1, orderby - 1) + ' ORDER BY ' + ds.Fieldbyname(cbOrderBy.Text).TableField;
|
|
end;
|
|
ds.Open;
|
|
finally
|
|
Memo.Lines.Add('SQL >>> ' + ds.SQL);
|
|
Memo.Lines.Add('WHERE >>> ' + ds.Where.Clause);
|
|
end;
|
|
end;
|
|
|
|
procedure TSQLAccessMainForm.FormCreate(Sender: TObject);
|
|
var
|
|
i: integer;
|
|
conn: IDAConnection;
|
|
ds: IDADataset;
|
|
begin
|
|
with DAConnectionManager do
|
|
for i := 0 to (Connections.Count - 1) do begin
|
|
rgConnections.Items.Add(Connections[i].Name + ' - ' + Connections[i].Description);
|
|
end;
|
|
|
|
rgConnections.ItemIndex := 0;
|
|
|
|
conn := DAConnectionManager.NewConnection('NorthwindCustomers');
|
|
ds := DASchema.NewDataset(conn, 'Customers');
|
|
cbOrderBy.Items.Add('');
|
|
for i := 0 to (ds.FieldCount - 1) do begin
|
|
cbOrderBy.Items.Add(ds.Fields[i].Name);
|
|
end;
|
|
end;
|
|
|
|
procedure TSQLAccessMainForm.ProcessSQLButtonClick(Sender: TObject);
|
|
var
|
|
processor: TDASQLMacroProcessor;
|
|
savedShortDateFormat: string;
|
|
SavedDateSeparator, savedTimeSeparator: Char;
|
|
begin
|
|
case rgProcessors.ItemIndex of
|
|
0: processor := TDAMSSQLMacroProcessor.Create;
|
|
1: processor := TDAIBMacroProcessor.Create;
|
|
2: processor := TDAOracleMacroProcessor.Create;
|
|
3: processor := TDADBISAMMacroProcessor.Create;
|
|
else Exit;
|
|
end;
|
|
savedShortDateFormat := ShortDateFormat;
|
|
savedDateSeparator := DateSeparator;
|
|
savedTimeSeparator := TimeSeparator;
|
|
try
|
|
ShortDateFormat := 'MM/DD/YYYY';
|
|
DateSeparator := '/';
|
|
TimeSeparator := ':';
|
|
Memo2.Lines.Text := processor.Eval(Memo1.Lines.Text)
|
|
finally
|
|
ShortDateFormat := savedShortDateFormat;
|
|
DateSeparator := savedDateSeparator;
|
|
TimeSeparator := savedTimeSeparator;
|
|
processor.Free;
|
|
end;
|
|
end;
|
|
|
|
end.
|
|
|