File: sqldbrestdataset.pp

package info (click to toggle)
lazarus 2.2.6%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 219,980 kB
  • sloc: pascal: 1,944,919; xml: 357,634; makefile: 270,608; cpp: 57,115; sh: 3,249; java: 609; perl: 297; sql: 222; ansic: 137
file content (153 lines) | stat: -rw-r--r-- 3,352 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
unit sqldbrestdataset;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, db, fpjson, fpjsondataset;

Type

  { TSQLDBRestDataset }

  TSQLDBRestDataset = Class(TBaseJSONDataset)
  private
  Protected
    function GetStringFieldLength(F: TJSONObject; AName: String; AIndex: Integer): integer;virtual;
    function StringToFieldType(S: String): TFieldType; virtual;
    Function CreateFieldMapper: TJSONFieldMapper; override;
    Procedure MetaDataToFieldDefs; override;
  Public
    procedure LoadFromStream(S: TStream);
  end;

implementation

type
  PRecInfo = ^TRecInfo;
  TRecInfo = record
    Index: Integer;
    Bookmark: Longint;
    BookmarkFlag: TBookmarkFlag;
  end;


procedure TSQLDBRestDataset.LoadFromStream(S: TStream);

Var
  D : TJSONData;
  O : TJSONObject absolute D;
  N : String;
  I : Integer;

begin
  D:=GetJSON(S);
  try
    if (D.JSONType<>jtObject) then
      Raise EJSONDataset.Create('Not a valid JSON data packet');
    N:='data';
    // Check metadata
    I:=O.IndexOfName('metaData');
    if (I<>-1) then
      begin
      If (O.Items[i].JSONType<>jtObject) then
        Raise EJSONDataset.Create('Invalid JSON metaData in data packet.');
      Metadata:=O.Objects['metaData'];
      O.Extract(I);
      end;
    // Check rows
    I:=O.IndexOfName(N);
    if (I=-1) then
      Raise EJSONDataset.Create('Missing data in data packet');
    if (O.Items[i].JSONType<>jtArray) then
      Raise EJSONDataset.Create('Rows element must be an array');
    Rows:=O.Items[i] as TJSONArray;
    O.Extract(I);
    OwnsData:=True;
  finally
    D.Free;
  end;
end;

function TSQLDBRestDataset.StringToFieldType(S: String): TFieldType;

begin
  if (s='int') then
    Result:=ftInteger
  else if (s='bigint') then
      Result:=ftLargeInt
  else if (s='float') then
    Result:=ftFloat
  else if (s='bool') then
    Result:=ftString // Buggy TJSONDataset in 3.0.4
  else if (s='date') then
    Result:=ftDate
  else if (s='datetime') then
    Result:=ftDateTime
  else if (s='time') then
    Result:=ftTime
  else if (s='blob') then
    Result:=ftBlob
  else if (s='string') then
    Result:=ftString
  else
    if MapUnknownToStringType then
      Result:=ftString
    else
      Raise EJSONDataset.CreateFmt('Unknown JSON data type : %s',[s]);
end;

function TSQLDBRestDataset.CreateFieldMapper: TJSONFieldMapper;
begin
  Result:=TJSONObjectFieldMapper.Create;
end;


function TSQLDBRestDataset.GetStringFieldLength(F: TJSONObject; AName: String;
  AIndex: Integer): integer;

Var
  I,L : Integer;
  D : TJSONData;

begin
  Result:=F.Get('maxLen',0);
  if (Result=0) then
    Result:=255;
end;

procedure TSQLDBRestDataset.MetaDataToFieldDefs;
Var
  A : TJSONArray;
  F : TJSONObject;
  I,FS : Integer;
  N,D: String;
  ft: TFieldType;

begin
  FieldDefs.Clear;
  A:=Metadata.Get('fields',TJSONArray(Nil));
  if A=Nil then
    Raise EJSONDataset.Create('Invalid metadata object');
  For I:=0 to A.Count-1 do
    begin
    F:=A.Objects[i];
    N:=F.Get('name','');
    If N='' then
      Raise EJSONDataset.CreateFmt('Field definition %d in has no or invalid name property',[i]);
    D:=F.Get('type','');
    If (D='') then
      ft:=ftstring
    else
     ft:=StringToFieldType(String(D));
    if (ft=ftString) then
      fs:=GetStringFieldLength(F,N,I)
    else
      fs:=0;
    FieldDefs.Add(N,ft,fs);
    end;
end;

end.