📄 dsusql.pas
字号:
{Component to work around current limitations of TUpdateSQL, such as:
* Dataset property tied to TBDEDataset instead of TDataset
* Lack of virtual methods for several key methods to make component descending easy
Written by Dan Miser. Published in Delpih 5 Developer's Guide, 8/99}
unit DSUSQL;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables;
type
TDSUpdateSQL = class(TUpdateSQL)
private
{ Private declarations }
FDatabaseName: string;
FSessionName: string;
FDataset: TDataset;
protected
{ Protected declarations }
procedure SetParams(UpdateKind: TUpdateKind); virtual;
public
{ Public declarations }
published
{ Published declarations }
procedure Apply(UpdateKind: TUpdateKind); override;
property Dataset: TDataset read FDataset write FDataset;
property DatabaseName: string read FDatabaseName write FDatabaseName;
property SessionName: string read FSessionName write FSessionName;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('DDG', [TDSUpdateSQL]);
end;
{ TDSUpdateSQL }
procedure TDSUpdateSQL.SetParams(UpdateKind: TUpdateKind);
var
I: Integer;
Old: Boolean;
Param: TParam;
PName: string;
Field: TField;
Value: Variant;
begin
if not Assigned(DataSet) then Exit;
with Query[UpdateKind] do
begin
for I := 0 to Params.Count - 1 do
begin
Param := Params[I];
PName := Param.Name;
Old := CompareText(Copy(PName, 1, 4), 'OLD_') = 0;
if Old then System.Delete(PName, 1, 4);
Field := DataSet.FindField(PName);
if not Assigned(Field) then Continue;
if Old then Param.AssignFieldValue(Field, Field.OldValue) else
begin
Value := Field.NewValue;
if VarIsEmpty(Value) then Value := Field.OldValue;
Param.AssignFieldValue(Field, Value);
end;
end;
end;
end;
// We don't want to call the base method here as the SetParams method
// of the base class is not virtual and would therefore rely on FDataset
// being of type TBDEDataset. ExecSQL is fine to call on the base class.
procedure TDSUpdateSQL.Apply(UpdateKind: TUpdateKind);
var
AUpdateKind: TUpdateKind;
begin
// Set up the queries to point to the proper DatabasName and SessionName.
// The act of getting the Query will create the TQuery if needed
for AUpdateKind := Low(TUpdateKind) to High(TUpdateKind) do
begin
Query[AUpdateKind].DatabaseName := DatabaseName;
Query[AUpdateKind].SessionName := SessionName;
end;
SetParams(UpdateKind);
ExecSQL(UpdateKind);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -