mormot json orm

mormot json orm

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
//cxg 2021-6-2  mormot json序列主从表
 
unit service.tables;
 
interface
 
uses
  mormot, sysutils, yn.log, yn.Unidac, yn.UnidacPool, SynCrtSock, Classes,
  SynCommons, SynVirtualDataSet;
 
type
  TGoods = class(mORMot.TSQLRecord)  //主表
  private
    fGoodsId: RawUTF8;
    fGoodsName: RawUTF8;
  published
    property GoodsId: RawUTF8 read fGoodsId write fGoodsId;
    property GoodsName: RawUTF8 read fGoodsName write fGoodsName;
  end;
 
  TUnits = class(mORMot.TSQLRecord) //从表
  private
    fUnitId: RawUTF8;
    fUnitName: RawUTF8;
  published
    property UnitId: RawUTF8 read fUnitId write fUnitId;
    property UnitName: RawUTF8 read fUnitName write fUnitName;
  end;
 
function serial(const url: sockstring): SockString;
 
procedure deserial(const json: string);
 
implementation
 
procedure deserial(const json: string);
var
  goods: TGoods;
  units: TUnits;
  js: TDocVariantData;
  sGoods, sUnits, tmp: string;
begin
  js.InitJSON(json);
  sGoods := utf8toansi(js.GetValueByPath(['goods']));  //主表
  goods := TGoods.CreateAndFillPrepare(sGoods);
  while goods.FillOne do
  begin
    tmp := goods.GoodsId;
    tmp := goods.GoodsName;
  end;
  goods.Free;
  sUnits := utf8toansi(js.GetValueByPath(['units']));   //从表
  units := TUnits.CreateAndFillPrepare(sUnits);
  while units.FillOne do
  begin
    tmp := units.UnitId;
    tmp := units.UnitName;
  end;
  units.Free;
end;
 
function serial(const url: sockstring): SockString;
//get url: /tables/{accountno}
var
  goods: TGoods;
  units: TUnits;
  sGoods, sUnits: string;
  jo: Variant;
  url2: tstringlist;
  pool: tunidacpool;
  db: tunidac;
begin
  pool := nil;
  db := nil;
  url2 := nil;
  try
    try
      url2 := tstringlist.Create;
      url2.Delimiter := '/';
      url2.DelimitedText := url;
      pool := getdbpool(url2[2]);
      db := pool.Lock;
  //生成主表json
      sGoods := '[';
      db.qry.Close;
      db.qry.SQL.Clear;
      db.qry.SQL.Add('select goodsid,goodsname from tgoods');
      db.qry.Open;
      db.qry.First;
      while not db.qry.Eof do
      begin
        TGoods.AutoFree(goods);
        goods.GoodsId := db.qry.fieldbyname('goodsid').AsString;
        goods.GoodsName := db.qry.fieldbyname('goodsname').AsString;
        if sGoods <> '[' then
          sGoods := sGoods + ',' + goods.GetJSONValues(true, false, soselect)
        else
          sGoods := sGoods + goods.GetJSONValues(true, false, soselect);
        db.qry.Next;
      end;
      sGoods := sGoods + ']';
  //生成从表json
      sUnits := '[';
      db.qry.Close;
      db.qry.SQL.Clear;
      db.qry.SQL.Add('select * from tunit');
      db.qry.Open;
      db.qry.First;
      while not db.qry.Eof do
      begin
        tunits.AutoFree(units);
        units.UnitId := db.qry.fieldbyname('unitid').AsString;
        units.UnitName := db.qry.fieldbyname('unitname').AsString;
        if sUnits <> '[' then
          sUnits := sUnits + ',' + goods.GetJSONValues(true, false, soselect)
        else
          sUnits := sUnits + goods.GetJSONValues(true, false, soselect);
        db.qry.Next;
      end;
      sUnits := sUnits + ']';
  //生成主从表json
      TDocVariant.New(jo);
      jo.goods := _JSon(sGoods);
      jo.units := _JSon(sUnits);
      result := ansitoutf8(VariantSaveJSON(jo));
  //{"mTable":[{"BillId":"1","operator":"name1"},{"BillId":"2","operator":"name2"}],"dTable":[{"ProductId":"1","ProductName":"p1"},{"ProductId":"2","ProductName":"p2"}]}
    except
      on e: exception do
      begin
        result := '{"err":"' + e.Message + '"}';
        log('service.tables.serial()' + e.Message);
      end;
    end;
  finally
    pool.Unlock(db);
    url2.Free;
  end;
end;
 
end.

  

posted @   delphi中间件  阅读(34)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2019-06-04 TynSerial流的序列(还原)
2019-06-04 TynSerial基本数据类型序列(还原)
2019-06-04 TynSerial序列(还原)TFDMemTable
2019-06-04 TynSerial图片序列(还原)
2019-06-04 TynSerial文件序列(还原)
2019-06-04 TynSerial序列(还原)TClientDataSet
2019-06-04 TynSerial结构体序列(还原)
点击右上角即可分享
微信分享提示