go和delphi对数据库查询数据基于结构的遍历

go和delphi对数据库查询数据基于结构的遍历

在结构面前,go的rows,delphi的Tdataset,都是和谐的。特意整理了2篇GO和DELPHI的对比,试图提示跨语言、平台 数据标准的真像。

GO没有Tdataset,但有rows

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
func UnitsQryPB(w http.ResponseWriter, r *http.Request) {
    // /pb/{service}/units/qry/{dbid}
    url := strings.Split(r.URL.Path, "/")
    dbid := url[5]
    db := getDB(dbid)
    if db == nil {
        return
    }
    sql := "select * from tunit"
    rows, err := db.Query(sql)
 
    defer rows.Close()
    defer db.Close()
 
    if err != nil {
        public.Log(err)
        return
    }
    var arr Unitss
    for rows.Next() {
        var dw Units
        rows.Scan(&dw.Unitid, &dw.Unitname)
        arr.UnitsArr = append(arr.UnitsArr, &dw)
    }
    data, _ := proto.Marshal(&arr)
    if public.UseGzip {
        data, _ = public.GZIPEn(data)
    }
    w.Write(data)
}

  DELPHI没有rows,但有Tdataset

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
function select(url: string; body: TBytes): TBytes;
var
  db: tdb;
  pool: tdbpool;
  arr: TArray<string>;
  serial: TgoProtocolBuffer;
  rows: TtunitArray;
  i: integer;
  res: TRes;
begin
  serial := TgoProtocolBuffer.Create;
  try
    try
      arr := url.Split(['/']);
      pool := GetDBPool(arr[4]);
      db := pool.Lock;
      db.qry.Close;
      db.qry.SQL.Clear;
      var where: string := '';
      if high(arr) >= 5 then
        where := ' where ' + TNetEncoding.URL.Decode(arr[5]);
      db.qry.SQL.Text := 'select * from tunit' + where;
      db.qry.Open;
      if db.qry.isempty then
      begin
        rows.status := 0;
        rows.exception := 'No found any data.';
        result := serial.Serialize<TtunitArray>(rows);
        exit;
      end;
      SetLength(rows.tunits, db.qry.RecordCount);
      db.qry.First;
      i := 0;
      while not db.qry.Eof do
      begin
        rows.tunits[i].unitid := db.qry.fieldbyname('unitid').asstring;
        rows.tunits[i].unitname := db.qry.fieldbyname('unitname').asstring;
        rows.status := 1;
        rows.message := 'success';
        inc(i);
        db.qry.Next;
      end;
      result := serial.Serialize<TtunitArray>(rows);
    except
      on E: Exception do
      begin
        res.status := 0;
        res.exception := E.message;
        result := serial.Serialize<TRes>(res);
      end;
    end;
  finally
    pool.Unlock(db);
    serial.Free;
  end;
end;

  

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