184人参与 • 2024-05-12 • Delphi
其实在前面想学习mormot1部分已经收集了很多关于crud的示例了,但感觉总是不通透,不能很好使用,一则mormot函数命令规则比较不同寻常,另外确实示例太少,其实代码注释倒是讲了很多。
procedure fillprepare(table: tormtable; achecktablename: tormchecktablename = ctnnocheck); overload;
fillprepare
程序是为了从 tormtable
结果中准备获取值。
fillrow(1..table.rowcount)
来获取任何一行的值。while rec.fillone do dosomethingwith(rec);
tormtable
被存储在一个受保护的内部字段 ftable
中。achecktablename
参数。在匹配到当前记录之前,任何待处理的 'tablename.'
前缀都将被去除。注意:这里的 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 语句中获取值。
fillrow(1..table.rowcount)
来获取任何一行的值。while rec.fillone do dosomethingwith(rec);
tormtable
,并将其存储在一个受保护的内部字段 ftable
中。asqlwhere
留空(''),则会尽快检索所有行(例如,通过绕过外部数据库的 sqlite3 虚拟表模块)。'name=:('arnaud'):'
)以提高服务器速度。请注意,你可以像这样使用 formatutf8()
:arec.fillprepare(client, formatutf8('salary>? and salary<?', [], [1000, 2000]));
或者直接使用带有 boundssqlwhere
参数数组的 fillprepare()
方法的重载版本。
fieldscsv
可用于指定必须检索哪些字段。fieldscsv=''
将检索所有简单的表字段,但如果你只需要访问一个或多个字段,并希望节省远程带宽,可以通过指定所需字段来实现。fieldscsv='*'
,它将检索所有字段,包括 blobs。fieldscsv
参数,因为任何缺失的字段都将保留先前的值。但是,在 fillprepare
之后可以安全地使用 batchupdate()
(将仅设置 id、tmodtime 和映射的字段)。function fillprepare(const aclient: irestorm; const formatsqlwhere: rawutf8; const boundssqlwhere: array of const; const fieldscsv: rawutf8 = ''): boolean; overload;
使用带有 '%' 参数的指定 where 子句准备获取值。
fillrow(1..table.rowcount)
来获取任何一行的值。while rec.fillone do dosomethingwith(rec);
tormtable
,并将其存储在一个受保护的内部字段 ftable
中。formatsqlwhere
语句中标识为 '?' 的绑定参数,这些参数应按照 boundssqlwhere
开放数组中提供的值的顺序进行排列。对于 tdatetime
类型,使用 datetosql
/datetimetosql
,或者直接将任何整数/双精度/货币/rawutf8 值绑定为请求的参数。paramssqlwhere
是 array of const
类型,并在 formatsqlwhere
语句中使用 '%',而现在它期望绑定参数为 '?'。fieldscsv
可用于指定必须检索哪些字段。fieldscsv=''
将检索所有简单的表字段,但如果你只需要访问一个或多个字段,并希望节省远程带宽,可以通过指定所需字段来实现。fieldscsv='*'
,它将检索所有字段,包括 blobs。fieldscsv
参数,因为任何缺失的字段都将保留先前的值。但是,在 fillprepare
之后可以安全地使用 batchupdate()
(将仅设置 id、tmodtime 和映射的字段)。function fillprepare(const aclient: irestorm; const formatsqlwhere: rawutf8; const paramssqlwhere, boundssqlwhere: array of const; const fieldscsv: rawutf8 = ''): boolean; overload;
使用带有 '%' 和 '?' 参数的指定 where 子句准备获取值。
fillrow(1..table.rowcount)
来获取任何一行的值。while rec.fillone do dosomethingwith(rec);
tormtable
,并将其存储在一个受保护的内部字段 ftable
中。formatsqlwhere
子句会将所有 '%' 字符替换为提供的 paramssqlwhere[]
数组中的值,并将所有 '?' 字符绑定为 boundssqlwhere[]
数组中的绑定参数值。fieldscsv
可用于指定必须检索哪些字段。fieldscsv=''
将检索所有简单的表字段,但如果你只需要访问一个或多个字段,并希望节省远程带宽,可以通过指定所需字段来实现。fieldscsv='*'
,它将检索所有字段,包括 blobs。fieldscsv
参数,因为任何缺失的字段都将保留先前的值。但是,在 fillprepare
之后可以安全地使用 batchupdate()
(将仅设置 id、tmodtime 和映射的字段)。这个函数结合了两种参数替换方式:'%' 字符的直接替换和 '?' 的绑定参数方式,为用户提供了更大的灵活性。但同时,也要求用户更仔细地管理参数以避免潜在的 sql 注入风险。
function fillprepare(const aclient: irestorm; const aids: array of tid; const fieldscsv: rawutf8 = ''): boolean; overload;
准备从一个id列表中获取值。
fillrow(1..table.rowcount)
来获取任何一行的值。while rec.fillone do dosomethingwith(rec);
tormtable
,并将其存储在一个受保护的内部字段 ftable
中。fieldscsv
可用于指定必须检索哪些字段。fieldscsv=''
将检索所有简单的表字段,但如果你只需要访问一个或多个字段,并希望节省带宽,可以通过指定所需字段来实现。fieldscsv='*'
,它将检索所有字段,包括blobs。fieldscsv
参数,因为任何缺失的字段都将保留先前的值。但在 fillprepare
之后可以安全地使用 batchupdate()
(只会设置id、tmodtime和映射的字段)。这个重载版本的 fillprepare
函数接收一个id数组作为参数,使得用户能够一次性检索多个特定id对应的记录,提高了数据检索的效率。同时,与上一个版本类似,它也支持通过 fieldscsv
参数来定制需要检索的字段,以满足不同的数据需求。
function fillpreparemany(const aclient: irestorm; const aformatsqljoin: rawutf8; const aparamssqljoin, aboundssqljoin: array of const): boolean;
准备遍历包含 tormmany
字段的join语句。
tormmany.dest
发布的字段现在将包含一个真正的 torm
实例,准备好用join语句的结果填充(这些实例将在 fillclose
时释放)——source
也将指向自身实例。aformatsqljoin
子句将为自动化的join语句定义一个where子句,包括 tormmany
发布的属性(及其嵌套属性)。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
关系。通过这种方式,用户可以方便地获取相关联的数据,并在应用程序中进行进一步处理。此函数还提供了参数化查询的功能,增强了查询的灵活性和安全性。
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论