mormot json orm
mormot json orm
//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.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/14848986.html