* Add possibility to specify parameters

This commit is contained in:
Michaël Van Canneyt 2023-08-10 22:39:12 +02:00
parent 49e7a98ca1
commit 1ee8bde72f
2 changed files with 37 additions and 3 deletions

View File

@ -554,7 +554,8 @@ Type
{ TFPDDEngine }
TFPDDEngineCapability =(ecImport,ecCreateTable,ecViewTable, ecTableIndexes,
ecRunQuery, ecRowsAffected, ecSequences, ecDomains);
ecRunQuery, ecRowsAffected, ecSequences, ecDomains,
ecParams);
TFPDDEngineCapabilities = set of TFPDDEngineCapability;
{
to avoid dependencies on GUI elements in the data dictionary engines,
@ -598,7 +599,11 @@ Type
// Should not open the dataset.
Function ViewTable(Const TableName: String; DatasetOwner : TComponent) : TDataset; virtual;
// Run a non-select query. If possible, returns the number of modified records.
Function RunQuery(SQL : String) : Integer; Virtual;
Function RunQuery(SQL : String) : Integer; Virtual; overload;
// Run a non-select query. If possible, apply parameters and returns the number of modified records.
Function RunQuery(SQL : String; Params : TParams) : Integer; virtual; overload;
// Apply parameters to a dataset, if possible
Procedure ApplyParams(DS : TDataset; Params : TParams); virtual;
// Create a select query TDataset. Do not open the resulting dataset.
Function CreateQuery(SQL : String; DatasetOwner : TComponent) : TDataset; Virtual;
// Assign a select query and open the resulting dataset.
@ -761,6 +766,7 @@ Resourcestring
SErrViewTableNotSupported = 'Viewing tables is not supported by the "%s" engine.';
SErrRunQueryNotSupported = 'Running queries is not supported by the "%s" engine.';
SErrOpenQueryNotSupported = 'Running and opening SELECT queries is not supported by the "%s" engine.';
SErrApplyParamsNotSupported = 'Cannot apply parameters to a dataset in engine "%s".';
SErrSetQueryStatementNotSupported = 'Setting the SQL statement is not supported by the "%s" engine.';
SErrGetTableIndexDefsNotSupported = 'Getting index definitions of a table is not supported by the "%s" engine.';
SSavingFieldsFrom = 'Saving fields from %s';
@ -2019,6 +2025,18 @@ begin
Raise EDataDict.CreateFmt(SErrRunQueryNotSupported,[DBType]);
end;
function TFPDDEngine.RunQuery(SQL: String; Params: TParams): Integer;
begin
if Assigned(Params) then
Raise EDataDict.CreateFmt(SErrApplyParamsNotSupported,[DBType]);
Result:=RunQuery(SQL);
end;
procedure TFPDDEngine.ApplyParams(DS: TDataset; Params: TParams);
begin
Raise EDataDict.CreateFmt(SErrApplyParamsNotSupported,[DBType]);
end;
function TFPDDEngine.CreateQuery(SQL: String; DatasetOwner : TComponent): TDataset;
begin
Raise EDataDict.CreateFmt(SErrOpenQueryNotSupported,[DBType]);

View File

@ -50,7 +50,9 @@ Type
Function ImportFields(Table : TDDTableDef) : Integer; override;
Function ImportIndexes(Table : TDDTableDef) : Integer; override;
Function ViewTable(Const TableName: String; DatasetOwner : TComponent) : TDataset; override;
Function RunQuery(SQL : String) : Integer; override;
Function RunQuery(SQL : String) : Integer; override; overload;
Function RunQuery(SQL : String; Params : TParams) : Integer; override; overload;
Procedure ApplyParams(DS : TDataset; Params : TParams); virtual;
Function CreateQuery(SQL : String; DatasetOwner : TComponent) : TDataset; override;
Procedure SetQueryStatement(SQL : String; AQuery : TDataset); override;
Function GetTableIndexDefs(ATableName : String; Defs : TDDIndexDefs) : integer ; override;
@ -202,6 +204,12 @@ end;
function TSQLDBDDEngine.RunQuery(SQL: String): Integer;
begin
Result:=RunQuery(SQL,Nil)
end;
function TSQLDBDDEngine.RunQuery(SQL: String; Params: TParams): Integer;
Var
Q : TSQLQuery;
@ -209,11 +217,19 @@ begin
Q:=CreateSQLQuery(Nil);
Try
Q.SQL.Text:=SQL;
ApplyParams(Q,Params);
Q.ExecSQL;
Result:=0;
Finally
Q.Free;
end;
end;
procedure TSQLDBDDEngine.ApplyParams(DS: TDataset; Params: TParams);
begin
if DS is TSQLQuery then
TSQLQuery(DS).Params.Assign(Params);
end;
function TSQLDBDDEngine.CreateQuery(SQL: String; DatasetOwner: TComponent