Browse Source

* Patch from Andrey Gusev to implement TSQLQuery.GetRowsaffected + test

git-svn-id: trunk@8843 -
joost 18 năm trước cách đây
mục cha
commit
c39b663201

+ 6 - 0
packages/fcl-db/src/sqldb/mysql/mysqlconn.inc

@@ -115,6 +115,7 @@ Type
     procedure CommitRetaining(trans : TSQLHandle); override;
     procedure RollBackRetaining(trans : TSQLHandle); override;
     procedure UpdateIndexDefs(var IndexDefs : TIndexDefs;TableName : string); override;
+    function RowsAffected(cursor: TSQLCursor): TRowsCount; override;
   Public
     constructor Create(AOwner : TComponent); override;
     procedure CreateDB; override;
@@ -844,6 +845,11 @@ begin
   qry.free;
 end;
 
+function TConnectionName.RowsAffected(cursor: TSQLCursor): TRowsCount;
+begin
+  Result := (cursor as TCursorName).RowsAffected;
+end;
+
 constructor TConnectionName.Create(AOwner: TComponent);
 begin
   inherited Create(AOwner);

+ 23 - 0
packages/fcl-db/src/sqldb/sqldb.pp

@@ -26,6 +26,8 @@ type TSchemaType = (stNoSchema, stTables, stSysTables, stProcedures, stColumns,
      TConnOption = (sqSupportParams,sqEscapeSlash,sqEscapeRepeat);
      TConnOptions= set of TConnOption;
 
+     TRowsCount = LargeInt;
+
 type
   TSQLConnection = class;
   TSQLTransaction = class;
@@ -105,6 +107,7 @@ type
     procedure UpdateIndexDefs(var IndexDefs : TIndexDefs;TableName : string); virtual;
     function GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string; virtual;
     procedure LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction : TSQLTransaction); virtual; abstract;
+    function RowsAffected(cursor: TSQLCursor): TRowsCount; virtual;
   public
     property Handle: Pointer read GetHandle;
     destructor Destroy; override;
@@ -241,6 +244,7 @@ type
     procedure SetSchemaInfo( SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string); virtual;
     property Prepared : boolean read IsPrepared;
     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
+    function RowsAffected: TRowsCount; virtual;
   protected
       
     // redeclared data set properties
@@ -394,6 +398,7 @@ type
 
     procedure FreeFldBuffers(cursor : TSQLCursor); override;
     function LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer; out CreateBlob : boolean) : boolean; override;
+    function RowsAffected(cursor: TSQLCursor): TRowsCount; override;
     function GetTransactionHandle(trans : TSQLHandle): pointer; override;
     function Commit(trans : TSQLHandle) : boolean; override;
     function RollBack(trans : TSQLHandle) : boolean; override;
@@ -554,6 +559,10 @@ begin
   qry.free;
 end;
 
+function TSQLConnection.RowsAffected(cursor: TSQLCursor): TRowsCount;
+begin
+  Result := -1;
+end;
 
 procedure TSQLConnection.GetTableNames(List: TStrings; SystemTables: Boolean);
 begin
@@ -917,6 +926,14 @@ begin
   result := (Database as tSQLConnection).LoadField(FCursor,FieldDef,buffer, Createblob)
 end;
 
+function TCustomSQLQuery.RowsAffected: TRowsCount;
+begin
+  Result := -1;
+  if not Assigned(Database) then Exit;
+  //assert(Database is TSQLConnection);
+  Result := (Database as TSQLConnection).RowsAffected(FCursor);
+end;
+
 procedure TCustomSQLQuery.InternalAddRecord(Buffer: Pointer; AAppend: Boolean);
 begin
   // not implemented - sql dataset
@@ -1855,6 +1872,12 @@ begin
   Result:=FProxy.LoadField(cursor, FieldDef, buffer, CreateBlob);
 end;
 
+function TSQLConnector.RowsAffected(cursor: TSQLCursor): TRowsCount;
+begin
+  CheckProxy;
+  Result := FProxy.RowsAffected(cursor);
+end;
+
 function TSQLConnector.GetTransactionHandle(trans: TSQLHandle): pointer;
 begin
   CheckProxy;

+ 28 - 0
packages/fcl-db/tests/testsqlfieldtypes.pas

@@ -26,6 +26,7 @@ type
     procedure TearDown; override;
     procedure RunTest; override;
   published
+    procedure TestRowsAffected;
     procedure TestStringsReplace;
     procedure TestCircularParams;
     procedure Test11Params;
@@ -870,6 +871,33 @@ begin
     inherited RunTest;
 end;
 
+procedure TTestFieldTypes.TestRowsAffected;
+begin
+  with TSQLDBConnector(DBConnector) do
+    begin
+    Connection.ExecuteDirect('create table FPDEV2 (         ' +
+                              '  ID INT NOT NULL            , ' +
+                              '  NAME VARCHAR(250),         ' +
+                              '  PRIMARY KEY (ID)           ' +
+                              ')                            ');
+    Query.SQL.Text := 'insert into FPDEV2(ID,NAME) values (1,''test1'')';
+    Query.ExecSQL;
+    AssertEquals(1,query.RowsAffected);
+    Query.SQL.Text := 'insert into FPDEV2(ID,NAME) values (2,''test2'')';
+    Query.ExecSQL;
+    AssertEquals(1,query.RowsAffected);
+    Query.SQL.Text := 'update FPDEV2 set NAME=''NewTest''';
+    Query.ExecSQL;
+    AssertEquals(2,query.RowsAffected);
+    Query.SQL.Text := 'delete from FPDEV2';
+    Query.ExecSQL;
+    AssertEquals(2,query.RowsAffected);
+    Query.SQL.Text := 'delete from FPDEV2';
+    Query.ExecSQL;
+    AssertEquals(0,query.RowsAffected);
+    end;
+end;
+
 procedure TTestFieldTypes.TestStringsReplace;
 begin
   AssertEquals('dit is een string',StringsReplace('dit was een string',['was'],['is'],[]));