Browse Source

* Fix for bug #0021687 by Domingo Alvarez Duarte

git-svn-id: trunk@20770 -
michael 13 years ago
parent
commit
3112cfbdd3
1 changed files with 20 additions and 16 deletions
  1. 20 16
      packages/fcl-db/src/json/fpjsondataset.pp

+ 20 - 16
packages/fcl-db/src/json/fpjsondataset.pp

@@ -221,6 +221,8 @@ type
     Function CreateRow : TJSONData; override;
     Function CreateRow : TJSONData; override;
   end;
   end;
 
 
+  EJSONDataset = Class(EDatabaseError);
+  
 implementation
 implementation
 
 
 uses dateutils, jsonparser;
 uses dateutils, jsonparser;
@@ -355,7 +357,7 @@ begin
   If Assigned(FMetaData) then
   If Assigned(FMetaData) then
     MetaDataToFieldDefs;
     MetaDataToFieldDefs;
   if (FieldDefs.Count=0) then
   if (FieldDefs.Count=0) then
-    Raise Exception.Create('No fields found');
+    Raise EJSONDataset.Create('No fields found');
 end;
 end;
 
 
 procedure TBaseJSONDataSet.FreeData;
 procedure TBaseJSONDataSet.FreeData;
@@ -402,7 +404,7 @@ begin
     if MapUnknownToStringType then
     if MapUnknownToStringType then
       Result:=ftString
       Result:=ftString
     else
     else
-      Raise Exception.CreateFmt('Unknown JSON data type : %s',[s]);
+      Raise EJSONDataset.CreateFmt('Unknown JSON data type : %s',[s]);
 end;
 end;
 
 
 Function  TExtJSJSONDataSet.GetStringFieldLength(F : TJSONObject; AName : String; AIndex : Integer) : integer;
 Function  TExtJSJSONDataSet.GetStringFieldLength(F : TJSONObject; AName : String; AIndex : Integer) : integer;
@@ -418,7 +420,7 @@ begin
     begin
     begin
     Result:=StrToIntDef(trim(F.Items[i].AsString),-1);
     Result:=StrToIntDef(trim(F.Items[i].AsString),-1);
     if (Result=-1) then
     if (Result=-1) then
-      Raise Exception.CreateFmt('Invalid maximum length specifier for field %s : %s',[AName,F.Items[i].AsString])
+      Raise EJSONDataset.CreateFmt('Invalid maximum length specifier for field %s : %s',[AName,F.Items[i].AsString])
     end
     end
   else
   else
     begin
     begin
@@ -456,7 +458,7 @@ begin
       else
       else
         begin
         begin
         FreeAndNil(D);
         FreeAndNil(D);
-        Raise Exception.Create('Not a valid ExtJS JSON data packet');
+        Raise EJSONDataset.Create('Not a valid ExtJS JSON data packet');
         end;
         end;
       N:='rows';
       N:='rows';
       // Check metadata
       // Check metadata
@@ -464,23 +466,23 @@ begin
       if (I<>-1) then
       if (I<>-1) then
         begin
         begin
         If (O.Items[i].JSONType<>jtObject) then
         If (O.Items[i].JSONType<>jtObject) then
-          Raise Exception.Create('Invalid ExtJS JSON metaData in data packet.');
+          Raise EJSONDataset.Create('Invalid ExtJS JSON metaData in data packet.');
         Metadata:=O.Objects['metaData'];
         Metadata:=O.Objects['metaData'];
         O.Extract(I);
         O.Extract(I);
         I:=Metadata.IndexOfName('root');
         I:=Metadata.IndexOfName('root');
         If (I<>-1) then
         If (I<>-1) then
           begin
           begin
           if (MetaData.Items[i].JSONType<>jtString) then
           if (MetaData.Items[i].JSONType<>jtString) then
-            Raise Exception.Create('Invalid ExtJS JSON root element in metaData.');
+            Raise EJSONDataset.Create('Invalid ExtJS JSON root element in metaData.');
           N:=MetaData.Strings['root'];
           N:=MetaData.Strings['root'];
           end;
           end;
         end;
         end;
       // Check rows
       // Check rows
       I:=O.IndexOfName(N);
       I:=O.IndexOfName(N);
       if (I=-1) then
       if (I=-1) then
-        Raise Exception.Create('Missing rows in data packet');
+        Raise EJSONDataset.Create('Missing rows in data packet');
       if (O.Items[i].JSONType<>jtArray) then
       if (O.Items[i].JSONType<>jtArray) then
-        Raise Exception.Create('Rows element must be an array');
+        Raise EJSONDataset.Create('Rows element must be an array');
       Rows:=O.Items[i] as TJSONArray;
       Rows:=O.Items[i] as TJSONArray;
       O.Extract(I);
       O.Extract(I);
       OwnsData:=True;
       OwnsData:=True;
@@ -576,22 +578,22 @@ begin
   FieldDefs.Clear;
   FieldDefs.Clear;
   I:=FMetadata.IndexOfName('fields');
   I:=FMetadata.IndexOfName('fields');
   if (I=-1) or (FMetaData.Items[i].JSONType<>jtArray) then
   if (I=-1) or (FMetaData.Items[i].JSONType<>jtArray) then
-    Raise Exception.Create('Invalid metadata object');
+    Raise EJSONDataset.Create('Invalid metadata object');
   A:=FMetadata.Arrays['fields'];
   A:=FMetadata.Arrays['fields'];
   For I:=0 to A.Count-1 do
   For I:=0 to A.Count-1 do
     begin
     begin
     If (A.Types[i]<>jtObject) then
     If (A.Types[i]<>jtObject) then
-      Raise Exception.CreateFmt('Field definition %d in metadata (%s) is not an object',[i,A[i].AsJSON]);
+      Raise EJSONDataset.CreateFmt('Field definition %d in metadata (%s) is not an object',[i,A[i].AsJSON]);
     F:=A.Objects[i];
     F:=A.Objects[i];
     J:=F.IndexOfName('name');
     J:=F.IndexOfName('name');
     If (J=-1) or (F.Items[J].JSONType<>jtString) then
     If (J=-1) or (F.Items[J].JSONType<>jtString) then
-      Raise Exception.CreateFmt('Field definition %d in has no or invalid name property',[i]);
+      Raise EJSONDataset.CreateFmt('Field definition %d in has no or invalid name property',[i]);
     N:=F.Items[J].AsString;
     N:=F.Items[J].AsString;
     J:=F.IndexOfName('type');
     J:=F.IndexOfName('type');
     If (J=-1) then
     If (J=-1) then
       ft:=ftstring
       ft:=ftstring
     else If (F.Items[J].JSONType<>jtString) then
     else If (F.Items[J].JSONType<>jtString) then
-      Raise Exception.CreateFmt('Field definition %d in has invalid type property',[i])
+      Raise EJSONDataset.CreateFmt('Field definition %d in has invalid type property',[i])
     else
     else
       ft:=StringToFieldType(F.Items[J].asString);
       ft:=StringToFieldType(F.Items[J].asString);
     if (ft=ftString) then
     if (ft=ftString) then
@@ -640,7 +642,7 @@ begin
       ftLargeInt,
       ftLargeInt,
       ftword: t:='int';
       ftword: t:='int';
     else
     else
-      Raise Exception.CreateFmt('Unsupported field type : %s',[GetEnumName(TypeInfo(TFieldType),Ord(FieldDefs[i].DataType))]);
+      Raise EJSONDataset.CreateFmt('Unsupported field type : %s',[GetEnumName(TypeInfo(TFieldType),Ord(FieldDefs[i].DataType))]);
     end; // case
     end; // case
     O.Strings['type']:=t;
     O.Strings['type']:=t;
     if M<>0 then
     if M<>0 then
@@ -1015,7 +1017,7 @@ begin
       Move(LI,Buffer^,SizeOf(LI));
       Move(LI,Buffer^,SizeOf(LI));
       end;
       end;
   else
   else
-    Raise Exception.CreateFmt('Unsupported field type : %s',[GetEnumName(TypeInfo(TFieldType),Ord(Field.DataType))]);
+    Raise EJSONDataset.CreateFmt('Unsupported field type : %s',[GetEnumName(TypeInfo(TFieldType),Ord(Field.DataType))]);
   end; // case
   end; // case
 end;
 end;
 
 
@@ -1066,7 +1068,7 @@ begin
       F:=TJSONInt64Number.Create(PInt64(Buffer)^);
       F:=TJSONInt64Number.Create(PInt64(Buffer)^);
       end;
       end;
   else
   else
-    Raise Exception.CreateFmt('Unsupported field type : %s',[GetEnumName(TypeInfo(TFieldType),Ord(Field.DataType))]);
+    Raise EJSONDataset.CreateFmt('Unsupported field type : %s',[GetEnumName(TypeInfo(TFieldType),Ord(Field.DataType))]);
   end; // case
   end; // case
   if (F=Nil) then
   if (F=Nil) then
     F:=TJSONNull.Create;
     F:=TJSONNull.Create;
@@ -1084,8 +1086,10 @@ end;
 procedure TBaseJSONDataSet.SetRecNo(Value: Integer);
 procedure TBaseJSONDataSet.SetRecNo(Value: Integer);
 begin
 begin
   if (Value < 0) or (Value > FCurrentList.Count) then
   if (Value < 0) or (Value > FCurrentList.Count) then
-    raise Exception.Create ('SetRecNo: out of range');
+    raise EJSONDataset.CreateFmt('SetRecNo: index %d out of range',[Value]);
   FCurrent := Value - 1;
   FCurrent := Value - 1;
+  Resync([]); 
+  DoAfterScroll;
 end;
 end;
 
 
 constructor TBaseJSONDataSet.Create(AOwner: TComponent);
 constructor TBaseJSONDataSet.Create(AOwner: TComponent);