Преглед изворни кода

* Raise an exception when a query is executed with a parameter set to null when this is not allowed + test

git-svn-id: trunk@13969 -
joost пре 16 година
родитељ
комит
f546247481

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

@@ -779,8 +779,9 @@ begin
     VSQLVar := @in_sqlda^.SQLvar[SQLVarNr];
     if AParams[ParNr].IsNull then
       begin
-      If Assigned(VSQLVar^.SQLInd) then
-        VSQLVar^.SQLInd^ := -1;
+      If not Assigned(VSQLVar^.SQLInd) then
+        DatabaseErrorFmt(SNullParamNotAllowed,[AParams[ParNr].Name],Self);
+      VSQLVar^.SQLInd^ := -1;
       end
     else
       begin

+ 20 - 0
packages/fcl-db/tests/testfieldtypes.pas

@@ -48,6 +48,7 @@ type
     procedure TestSetBlobAsMemoParam;
     procedure TestSetBlobAsBlobParam;
     procedure TestSetBlobAsStringParam;
+    procedure TestNonNullableParams;
     procedure TestGetIndexDefs;
     procedure TestDblQuoteEscComments;
     procedure TestpfInUpdateFlag; // bug 7565
@@ -929,6 +930,25 @@ begin
   TSQLDBConnector(DBConnector).Connection.ExecuteDirect('update FPDEV set name=''nothing'' where (1=0)');
 end;
 
+procedure TTestFieldTypes.TestNonNullableParams;
+var ASQLQuery : TSQLQuery;
+begin
+  // Check for an exception when a null value is stored into a non-nullable
+  // field using a parameter
+  // There was a bug in IBConnection so that in this case the last used value
+  // for the parameter was used.
+
+  // To make sure that any changes are cancelled in the case the test fails
+  TSQLDBConnector(DBConnector).GetNDataset(true,5);
+
+  ASQLQuery := TSQLDBConnector(DBConnector).Query;
+  ASQLQuery.SQL.text := 'update fpdev set ID=:ID1 where id = :ID2';
+  ASQLQuery.Params[0].Clear;
+  ASQLQuery.Params[1].AsInteger := 1;
+  AssertTrue(ASQLQuery.Params[0].IsNull);
+  AssertException(EDatabaseError, @ASQLQuery.ExecSQL);
+end;
+
 procedure TTestFieldTypes.TestStringLargerThen8192;
 
 var