Browse Source

fcl-db: dataset: after rev.26661 some tests are broken. They use locate in form: Locate('ID', VarArrayOf([1])); So one field with one element array.
This patch takes into account:
- most common situation is, when we assign simple variant (not array) to one field, so optimise code to it
- when assign array to one field, do not restrict array dimensions on "dataset level" , but postpone it to "field level" and let field raise SFieldValueError if it cannot handle supplied variant value.

git-svn-id: trunk@26746 -

lacak 11 years ago
parent
commit
f88b0474c9
1 changed files with 18 additions and 30 deletions
  1. 18 30
      packages/fcl-db/src/base/dataset.inc

+ 18 - 30
packages/fcl-db/src/base/dataset.inc

@@ -2289,41 +2289,29 @@ begin
   end;
   end;
 end;
 end;
 
 
-procedure TDataset.SetFieldValues(const Fieldname: string; Value: Variant);
+procedure TDataset.SetFieldValues(const FieldName: string; Value: Variant);
 
 
 var
 var
-  i : Integer;
+  i, l, h : Integer;
   FieldList: TList;
   FieldList: TList;
-  hb, lb: integer;
 begin
 begin
-  FieldList := TList.Create;
-  try
-    GetFieldList( FieldList, FieldName );
-    if VarIsArray( Value ) then
-    begin
-      if ( FieldList.Count > 1 ) then
-      begin
-        if ( VarArrayDimCount( Value ) <> 1 ) then
-          DatabaseErrorFmt('Variant Array Dimension Mismatch: Expected 1, got %d',[VarArrayDimCount( Value )],Self);
-        hb := VarArrayHighBound( Value, 1 );
-        lb := VarArrayLowBound( Value, 1 );
-        if hb - lb + 1 <> FieldList.Count then
-          DatabaseErrorFmt('Variant Array Value Count Mismatch: Expected %d, got %d',[FieldList.Count, hb - lb + 1],Self);
-        for i := 0 to FieldList.Count -1 do
-          TField(FieldList[i]).Value := Value[i+lb];
-      end
+  if VarIsArray(Value) then begin
+    FieldList := TList.Create;
+    try
+      GetFieldList(FieldList, FieldName);
+      l := VarArrayLowBound(Value, 1);
+      h := VarArrayHighBound(Value, 1);
+      if (FieldList.Count = 1) and (l < h) then
+        // Allow for a field type that can deal with an array
+        FieldByName(FieldName).Value := Value
       else
       else
-        // Allow for a field type that can deal with an array.
-        FieldByName(Fieldname).Value := Value;
-    end
-    else
-    if FieldList.Count = 1 then
-      FieldByName( Fieldname ).Value := Value
-    else
-      DatabaseErrorFmt('Field Count Mismatch: Expected 1, got %d',[FieldList.Count],Self);
-  finally
-    FieldList.Free;
-  end;
+        for i := 0 to FieldList.Count - 1 do
+          TField(FieldList[i]).Value := Value[l+i];
+    finally
+      FieldList.Free;
+    end;
+  end else
+    FieldByName(FieldName).Value := Value;
 end;
 end;
 
 
 Function TDataset.Locate(const KeyFields: string; const KeyValues: Variant; Options: TLocateOptions) : boolean;
 Function TDataset.Locate(const KeyFields: string; const KeyValues: Variant; Options: TLocateOptions) : boolean;