it编程 > 编程语言 > Delphi

mORMot2 获取数据集1

184人参与 2024-05-12 Delphi

mormot2 获取数据集

其实在前面想学习mormot1部分已经收集了很多关于crud的示例了,但感觉总是不通透,不能很好使用,一则mormot函数命令规则比较不同寻常,另外确实示例太少,其实代码注释倒是讲了很多。


procedure fillprepare(table: tormtable; achecktablename: tormchecktablename = ctnnocheck); overload;

fillprepare 程序是为了从 tormtable 结果中准备获取值。

while rec.fillone do
  dosomethingwith(rec);

注意:这里的 dosomethingwith(rec); 是一个占位符,代表你可以对每一行记录 rec 执行某些操作。实际使用时,你需要将其替换为具体的业务逻辑代码。同时,rec 应该是一个已经定义好的记录类型变量,用于存储从 tormtable 中获取的数据。

另外,tormchecktablename 是一个枚举类型,ctnnocheck 是它的一个值,表示不进行表名检查。如果你需要检查表名,可以传递其他相应的枚举值给 achecktablename 参数。


function fillprepare(const aclient: irestorm; const asqlwhere: rawutf8 = ''; const fieldscsv: rawutf8 = ''; achecktablename: tormchecktablename = ctnnocheck): boolean; overload;

fillprepare 函数用于从 sql where 语句中获取值。

while rec.fillone do
  dosomethingwith(rec);
arec.fillprepare(client, formatutf8('salary>? and salary<?', [], [1000, 2000]));

或者直接使用带有 boundssqlwhere 参数数组的 fillprepare() 方法的重载版本。


function fillprepare(const aclient: irestorm; const formatsqlwhere: rawutf8; const boundssqlwhere: array of const; const fieldscsv: rawutf8 = ''): boolean; overload;

使用带有 '%' 参数的指定 where 子句准备获取值。

while rec.fillone do
  dosomethingwith(rec);

function fillprepare(const aclient: irestorm; const formatsqlwhere: rawutf8; const paramssqlwhere, boundssqlwhere: array of const; const fieldscsv: rawutf8 = ''): boolean; overload;

使用带有 '%' 和 '?' 参数的指定 where 子句准备获取值。

while rec.fillone do
  dosomethingwith(rec);

这个函数结合了两种参数替换方式:'%' 字符的直接替换和 '?' 的绑定参数方式,为用户提供了更大的灵活性。但同时,也要求用户更仔细地管理参数以避免潜在的 sql 注入风险。


function fillprepare(const aclient: irestorm; const aids: array of tid; const fieldscsv: rawutf8 = ''): boolean; overload;

准备从一个id列表中获取值。

while rec.fillone do
  dosomethingwith(rec);

这个重载版本的 fillprepare函数接收一个id数组作为参数,使得用户能够一次性检索多个特定id对应的记录,提高了数据检索的效率。同时,与上一个版本类似,它也支持通过 fieldscsv参数来定制需要检索的字段,以满足不同的数据需求。


function fillpreparemany(const aclient: irestorm; const aformatsqljoin: rawutf8; const aparamssqljoin, aboundssqljoin: array of const): boolean;

准备遍历包含 tormmany字段的join语句。

if aprod.fillpreparemany(database,
    'owner=? and categories.dest.name=? and (sizes.dest.name=? or sizes.dest.name=?)',
    [], ['mark', 'for boy', 'small', 'medium']) then
  while aprod.fillone do
    // 在这里,例如,aprod.categories.dest被实例化(并且categories.source=aprod)
    writeln(aprod.name, ' ', aprod.owner, ' ', aprod.categories.dest.name, ' ', aprod.sizes.dest.name);
// 你也可以使用aprod.filltable来填充一个网格,例如
// (不要忘记设置aprod.filltable.ownermustfree := false)

这将执行类似于以下的join select语句:

select p.*, c.*, s.*
from product p, category c, categories cc, size s, sizes ss

这个函数允许用户准备和执行复杂的join查询,同时自动处理 tormmany关系。通过这种方式,用户可以方便地获取相关联的数据,并在应用程序中进行进一步处理。此函数还提供了参数化查询的功能,增强了查询的灵活性和安全性。

(0)
打赏 微信扫一扫 微信扫一扫

您想发表意见!!点此发布评论

推荐阅读

mORMot 1.18 第07章 简单的读写操作

05-12

mORMot2 生成和解析 JSON

05-12

mORMot 1.18 第06章 概念

05-12

Delphi 实现刘谦春晚魔术

05-12

mORMot 1.18 第08章 Delphi中的服务器端JavaScript

05-12

delphi redisclient测试

05-12

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论