home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2001 October
/
Chip_2001-10_cd1.bin
/
zkuste
/
delphi
/
kolekce
/
d6
/
FRCLX.ZIP
/
SOURCE
/
DBX
/
FR_DBXQuery.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
2001-07-08
|
10KB
|
370 lines
{******************************************}
{ }
{ FastReport CLX v2.4 - DBX components }
{ Query component }
{ }
{ Copyright (c) 1998-2001 by Tzyganenko A. }
{ }
{******************************************}
unit FR_DBXQuery;
interface
{$I FR.inc}
uses
Types, SysUtils, Classes, QGraphics, QStdCtrls, QControls, QForms,
QMenus, QDialogs, FR_Class, FR_Pars, DB, DBXpress, FR_DBXTable,
SqlExpr, FR_DBUtils;
type
TfrDBXQuery = class(TfrDBXDataSet)
private
FQuery: TSQLQuery;
FParams: TfrVariables;
procedure SQLEditor(Sender: TObject);
procedure ParamsEditor(Sender: TObject);
procedure ReadParams(Stream: TStream);
procedure WriteParams(Stream: TStream);
function GetParamKind(Index: Integer): TfrParamKind;
procedure SetParamKind(Index: Integer; Value: TfrParamKind);
function GetParamText(Index: Integer): String;
procedure SetParamText(Index: Integer; Value: String);
procedure BeforeOpenQuery(DataSet: TDataSet);
protected
procedure SetPropValue(Index: String; Value: Variant); override;
function GetPropValue(Index: String): Variant; override;
function DoMethod(MethodName: String; Par1, Par2, Par3: Variant): Variant; override;
public
constructor Create; override;
destructor Destroy; override;
procedure LoadFromStream(Stream: TStream); override;
procedure SaveToStream(Stream: TStream); override;
procedure DefineProperties; override;
procedure Loaded; override;
property Query: TSQLQuery read FQuery;
property ParamKind[Index: Integer]: TfrParamKind read GetParamKind write SetParamKind;
property ParamText[Index: Integer]: String read GetParamText write SetParamText;
end;
implementation
uses
FR_Utils, FR_Const, FR_DBSQLEdit, FR_DBXQueryParam, Variants;
{ TfrDBXQuery }
constructor TfrDBXQuery.Create;
begin
inherited Create;
FQuery := TSQLQuery.Create(frDialogForm);
FQuery.BeforeOpen := BeforeOpenQuery;
FDataSet := TSQLDataSet(FQuery);
FDataSource.DataSet := FDataSet;
FParams := TfrVariables.Create;
Component := FQuery;
BaseName := 'Query';
Bmp.LoadFromResourceName(hInstance, 'FR_DBXQUERY');
end;
destructor TfrDBXQuery.Destroy;
begin
FParams.Free;
inherited Destroy;
end;
procedure TfrDBXQuery.DefineProperties;
function GetMasterSource: String;
var
i: Integer;
sl: TStringList;
begin
Result := '';
sl := TStringList.Create;
frGetComponents(FQuery.Owner, TDataSet, sl, FQuery);
sl.Sort;
for i := 0 to sl.Count - 1 do
Result := Result + sl[i] + ';';
sl.Free;
end;
begin
inherited DefineProperties;
AddEnumProperty('DataSource', GetMasterSource, [Null]);
AddProperty('Params', [frdtHasEditor], ParamsEditor);
AddProperty('SQL', [frdtHasEditor], SQLEditor);
AddProperty('SQL.Count', [], nil);
end;
procedure TfrDBXQuery.SetPropValue(Index: String; Value: Variant);
var
d: TDataset;
begin
inherited SetPropValue(Index, Value);
Index := AnsiUpperCase(Index);
if Index = 'DATASOURCE' then
begin
d := frFindComponent(FQuery.Owner, Value) as TDataSet;
FQuery.DataSource := frGetDataSource(FQuery.Owner, d);
end
else if Index = 'SQL' then
FQuery.SQL.Text := Value
end;
function TfrDBXQuery.GetPropValue(Index: String): Variant;
begin
Index := AnsiUpperCase(Index);
Result := inherited GetPropValue(Index);
if Result <> Null then Exit;
if Index = 'DATASOURCE' then
Result := frGetDataSetName(FQuery.Owner, FQuery.DataSource)
else if Index = 'SQL' then
Result := FQuery.SQL.Text
else if Index = 'SQL.Count' then
Result := FQuery.SQL.Count;
end;
function TfrDBXQuery.DoMethod(MethodName: String; Par1, Par2, Par3: Variant): Variant;
begin
Result := inherited DoMethod(MethodName, Par1, Par2, Par3);
if Result = Null then
Result := LinesMethod(FQuery.SQL, MethodName, 'SQL', Par1, Par2, Par3);
if MethodName = 'EXECSQL' then
begin
BeforeOpenQuery(FQuery);
FQuery.ExecSQL
end;
end;
procedure TfrDBXQuery.LoadFromStream(Stream: TStream);
var
s: String;
begin
FFixupList.Clear;
inherited LoadFromStream(Stream);
FFixupList['DataBase'] := frReadString(Stream);
Prop['DataBase'] := FFixupList['DataBase'];
s := frReadString(Stream);
FFixupList['DataSource'] := s;
Prop['DataSource'] := FFixupList['DataSource'];
frReadMemo(Stream, FQuery.SQL);
FFixupList['Active'] := frReadBoolean(Stream);
ReadFields(Stream);
ReadParams(Stream);
try
FQuery.Active := FFixupList['Active'];
except;
end;
end;
procedure TfrDBXQuery.SaveToStream(Stream: TStream);
begin
inherited SaveToStream(Stream);
frWriteString(Stream, Prop['DataBase']);
frWriteString(Stream, Prop['DataSource']);
frWriteMemo(Stream, FQuery.SQL);
frWriteBoolean(Stream, FQuery.Active);
WriteFields(Stream);
WriteParams(Stream);
end;
procedure TfrDBXQuery.Loaded;
begin
Prop['DataSource'] := FFixupList['DataSource'];
inherited Loaded;
end;
procedure TfrDBXQuery.SQLEditor(Sender: TObject);
begin
with TfrDBSQLEditorForm.Create(nil) do
begin
Query := FQuery;
M1.Lines.Assign(FQuery.SQL);
{$IFDEF QBUILDER}
QBEngine := TfrQBDBXEngine.Create(nil);
TfrQBDBXEngine(QBEngine).Query := FQuery;
QBEngine.DatabaseName := Prop['Database'];
{$ENDIF}
if (ShowModal = mrOk) and ((Restrictions and frrfDontModify) = 0) then
begin
FQuery.SQL := M1.Lines;
frDesigner.Modified := True;
end;
{$IFDEF QBUILDER}
QBEngine.Free;
{$ENDIF}
Free;
end;
end;
procedure TfrDBXQuery.ParamsEditor(Sender: TObject);
var
Params: TParams;
ParamValues: TfrVariables;
begin
if FQuery.Params.Count > 0 then
begin
Params := TParams.Create;
Params.Assign(FQuery.Params);
ParamValues := TfrVariables.Create;
ParamValues.Assign(FParams);
with TfrDBXParamsForm.Create(nil) do
begin
QueryComp := Self;
Query := FQuery;
Caption := Self.Name + ' ' + (SParams);
if ShowModal = mrOk then
frDesigner.Modified := True
else
begin
FQuery.Params.Assign(Params);
FParams.Assign(ParamValues);
end;
Free;
end;
Params.Free;
ParamValues.Free;
end;
end;
function TfrDBXQuery.GetParamKind(Index: Integer): TfrParamKind;
begin
Result := pkValue;
if not FQuery.Params[Index].Bound then
Result := pkAssignFromMaster;
end;
procedure TfrDBXQuery.SetParamKind(Index: Integer; Value: TfrParamKind);
begin
if Value = pkAssignFromMaster then
begin
FQuery.Params[Index].Bound := False;
FParams.Delete(FParams.IndexOf(FQuery.Params[Index].Name));
end
else
begin
FQuery.Params[Index].Clear;
FQuery.Params[Index].Bound := True;
FParams[FQuery.Params[Index].Name] := '';
end;
end;
function TfrDBXQuery.GetParamText(Index: Integer): String;
begin
Result := '';
if ParamKind[Index] = pkValue then
Result := FParams[FQuery.Params[Index].Name];
end;
procedure TfrDBXQuery.SetParamText(Index: Integer; Value: String);
begin
if ParamKind[Index] = pkValue then
FParams[FQuery.Params[Index].Name] := Value;
end;
procedure TfrDBXQuery.ReadParams(Stream: TStream);
var
i: Integer;
w, n: Word;
begin
Stream.Read(n, 2);
for i := 0 to n - 1 do
with FQuery.Params[i] do
begin
Stream.Read(w, 2);
DataType := ParamTypes[w];
Stream.Read(w, 2);
ParamKind[i] := TfrParamKind(w);
ParamText[i] := frReadString(Stream);
end;
end;
procedure TfrDBXQuery.WriteParams(Stream: TStream);
var
i: Integer;
w: Word;
begin
w := FQuery.Params.Count;
Stream.Write(w, 2);
for i := 0 to FQuery.Params.Count - 1 do
with FQuery.Params[i] do
begin
for w := 0 to 10 do
if DataType = ParamTypes[w] then
break;
Stream.Write(w, 2);
w := Word(ParamKind[i]);
Stream.Write(w, 2);
frWriteString(Stream, ParamText[i]);
end;
end;
procedure TfrDBXQuery.BeforeOpenQuery(DataSet: TDataSet);
var
i: Integer;
SaveView: TfrView;
SavePage: TfrPage;
SaveBand: TfrBand;
function DefParamValue(Param: TParam): String;
begin
if Param.DataType in [ftDate, ftDateTime] then
Result := '01.01.00'
else if Param.DataType = ftTime then
Result := '00:00'
else
Result := '0';
end;
begin
SaveView := CurView;
CurView := nil;
SavePage := CurPage;
CurPage := ParentPage;
SaveBand := CurBand;
CurBand := nil;
i := 0;
try
while i < FQuery.Params.Count do
begin
if ParamKind[i] = pkValue then
if DocMode = dmPrinting then
FQuery.Params[i].Text := frParser.Calc(ParamText[i]) else
FQuery.Params[i].Text := DefParamValue(FQuery.Params[i]);
Inc(i);
end;
except
Memo.Clear;
Memo.Add(ParamText[i]);
CurView := Self;
raise;
end;
CurView := SaveView;
CurPage := SavePage;
CurBand := SaveBand;
end;
var
Bmp: TBitmap;
initialization
Bmp := TBitmap.Create;
Bmp.LoadFromResourceName(hInstance, 'FR_DBXQUERYCONTROL');
frRegisterControl(TfrDBXQuery, Bmp, (SInsertQuery));
finalization
frUnRegisterObject(TfrDBXQuery);
Bmp.Free;
end.