Browse Source

* Add possibility to specify parameters

Michaël Van Canneyt 2 years ago
parent
commit
1ee8bde72f

+ 20 - 2
packages/fcl-db/src/datadict/fpdatadict.pp

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

+ 17 - 1
packages/fcl-db/src/datadict/fpddsqldb.pp

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