Browse Source

* Implemented TPQConnection.RowsAffected
* Improved test for RowsAffected
* TIBConnection.RowsAffected now returns -1 on failure

git-svn-id: trunk@8976 -

joost 18 years ago
parent
commit
340acdea57

+ 4 - 3
packages/fcl-db/src/sqldb/interbase/ibconnection.pp

@@ -1174,10 +1174,11 @@ var info_request       : string;
     InsertedRows       : integer;
     InsertedRows       : integer;
     
     
 begin
 begin
-  SelectedRows:=0;
-  InsertedRows:=0;
+  SelectedRows:=-1;
+  InsertedRows:=-1;
 
 
-  with cursor as TIBCursor do
+  if assigned(cursor) then with cursor as TIBCursor do
+   if assigned(statement) then
     begin
     begin
     info_request := chr(isc_info_sql_records);
     info_request := chr(isc_info_sql_records);
     if isc_dsql_sql_info(@Status[0],@Statement,Length(info_request), @info_request[1],sizeof(resbuf),@resbuf) <> 0 then
     if isc_dsql_sql_info(@Status[0],@Statement,Length(info_request), @info_request[1],sizeof(resbuf),@resbuf) <> 0 then

+ 9 - 0
packages/fcl-db/src/sqldb/postgres/pqconnection.pp

@@ -65,6 +65,7 @@ type
     procedure UpdateIndexDefs(var IndexDefs : TIndexDefs;TableName : string); override;
     procedure UpdateIndexDefs(var IndexDefs : TIndexDefs;TableName : string); override;
     function GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string; override;
     function GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string; override;
     procedure LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField; cursor: TSQLCursor;ATransaction : TSQLTransaction); override;
     procedure LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField; cursor: TSQLCursor;ATransaction : TSQLTransaction); override;
+    function RowsAffected(cursor: TSQLCursor): TRowsCount; override;
   public
   public
     constructor Create(AOwner : TComponent); override;
     constructor Create(AOwner : TComponent); override;
     procedure CreateDB; override;
     procedure CreateDB; override;
@@ -868,6 +869,14 @@ begin
     end;
     end;
 end;
 end;
 
 
+function TPQConnection.RowsAffected(cursor: TSQLCursor): TRowsCount;
+begin
+  if assigned(cursor) and assigned((cursor as TPQCursor).res) then
+    Result := StrToIntDef(PQcmdTuples((cursor as TPQCursor).res),-1)
+  else
+    Result := -1;
+end;
+
 { TPQConnectionDef }
 { TPQConnectionDef }
 
 
 class function TPQConnectionDef.TypeName: String;
 class function TPQConnectionDef.TypeName: String;

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

@@ -899,6 +899,7 @@ procedure TTestFieldTypes.TestRowsAffected;
 begin
 begin
   with TSQLDBConnector(DBConnector) do
   with TSQLDBConnector(DBConnector) do
     begin
     begin
+    AssertEquals(-1,query.RowsAffected);
     Connection.ExecuteDirect('create table FPDEV2 (         ' +
     Connection.ExecuteDirect('create table FPDEV2 (         ' +
                               '  ID INT NOT NULL            , ' +
                               '  ID INT NOT NULL            , ' +
                               '  NAME VARCHAR(250),         ' +
                               '  NAME VARCHAR(250),         ' +
@@ -915,6 +916,11 @@ begin
     Query.SQL.Text := 'update FPDEV2 set NAME=''NewTest''';
     Query.SQL.Text := 'update FPDEV2 set NAME=''NewTest''';
     Query.ExecSQL;
     Query.ExecSQL;
     AssertEquals(2,query.RowsAffected);
     AssertEquals(2,query.RowsAffected);
+    Query.SQL.Text := 'select * from FPDEV2';
+    Query.Open;
+    AssertTrue(query.RowsAffected<>0); // It should return -1 or the number of selected rows.
+    query.Close;
+    AssertEquals(-1,query.RowsAffected);
     Query.SQL.Text := 'delete from FPDEV2';
     Query.SQL.Text := 'delete from FPDEV2';
     Query.ExecSQL;
     Query.ExecSQL;
     AssertEquals(2,query.RowsAffected);
     AssertEquals(2,query.RowsAffected);