半唐番 發表於 2024-7-6 22:08:00

mORMot模糊概念--FormatSQL-第1部分

<h1 id="mormot里面的模糊概念--formatsql第1部分">mORMot里面的模糊概念--FormatSQL第1部分</h1>
<p>mORMot 的 Fast Format 到底是% 还是 ? 作为参数!,先看看关键代码。</p>
<p>下面是代码原始注释</p>
<pre><code class="language-Pascal">function FormatSql(const Format: RawUtf8;const Args, Params: array of const): RawUtf8;
</code></pre>
<p>fast Format() function replacement, handling % but also ? inlined parameters</p>
<ul>
<li>will include Args[] for every % in Format</li>
<li>will include Params[] for every ? in Format, as "inlined" ORM or DB values,<br>
e.g. :(1234): for numbers, and😦'quoted '' string'): for text</li>
<li>note that, due to a Delphi compiler limitation, cardinal values should be<br>
type-casted to Int64() (otherwise the integer mapped value will be converted)</li>
<li>is a wrapper around FormatParams(Format, Args, Params, false, result);</li>
</ul>
<p>快速Format()函数替代,处理%和?内联参数</p>
<ul>
<li>Format中每个%都将包含Args[]</li>
<li>Format中每个?都将包含Params[],作为“内联”ORM或DB值,<br>
例如,数字用:(1234):表示,文本用:('quoted '' string'):表示</li>
<li>请注意,由于Delphi编译器的限制,基数值应转换为Int64()(否则映射的整数值将被转换)</li>
<li>是FormatParams(Format, Args, Params, false, result)的包装函数;</li>
</ul>
<p>注:</p>
<ol>
<li>“Args[] for every % in Format” 指的是,在Format字符串中,每一个百分号(%)都将对应一个Args数组中的元素,用于替换该百分号。</li>
<li>“Params[] for every ? in Format” 指的是,在Format字符串中,每一个问号(?)都将对应一个Params数组中的元素,这个元素可能是一个ORM(对象关系映射)值或者数据库值。</li>
<li>“cardinal values should be type-casted to Int64()” 指的是,由于Delphi编译器的某些限制,需要将基数值(无符号整数值)显式转换为Int64类型,以避免整数值的自动转换可能引发的问题。</li>
<li>“wrapper around FormatParams” 指的是,这个函数实质上是对另一个函数FormatParams的封装,使得调用更加简便,或者增加了额外的功能。</li>
</ol>
<pre><code class="language-Pascal">function FormatJson(const Format: RawUtf8; const Args, Params: array of const): RawUtf8;
</code></pre>
<p>fast Format() function replacement, handling % but also ? parameters as JSON</p>
<ul>
<li>will include Args[] for every % in Format</li>
<li>will include Params[] for every ? in Format, as their JSON value, with<br>
proper JSON double quotes and escaping for strings</li>
<li>note that, due to a Delphi compiler limitation, cardinal values should be<br>
type-casted to Int64() (otherwise the integer mapped value will be converted)</li>
<li>is a wrapper around FormatParams(Format, Args, Params, true, result);</li>
</ul>
<p>快速Format()函数的替代品,处理%同时也处理?参数作为JSON</p>
<ul>
<li>将为每个在Format中出现的%包含Args[]</li>
<li>将为每个在Format中出现的?包含Params[],作为它们的JSON值,字符串会使用适当的JSON双引号和转义字符</li>
<li>注意,由于Delphi编译器的限制,基数值应该被类型转换为Int64()(否则映射的整数值将被转换)</li>
<li>是FormatParams(Format, Args, Params, true, result)的包装函数;</li>
</ul>
<pre><code class="language-pascal">{$ifndef PUREMORMOT2}
   // rather call FormatSql() and FormatJson() functions
   // 更应该调用 FormatSql() and FormatJson() 使代码清晰
function FormatUtf8(const Format: RawUtf8; const Args, Params: array of const; JsonFormat: boolean = false): RawUtf8; overload;
{$endif PUREMORMOT2}
</code></pre>
<p>fast Format() function replacement, handling % and ? parameters</p>
<ul>
<li>call rather FormatSql() and FormatJson() wrappers instead</li>
<li>resulting string has no length limit and uses fast concatenation</li>
<li>any supplied TObject instance will be written as their class name</li>
</ul>
<p>快速Format()函数替代,处理%和?参数</p>
<ul>
<li>请改用FormatSql()和FormatJson()包装函数</li>
<li>生成的字符串没有长度限制,并使用快速连接</li>
<li>任何提供的TObject实例都将以其类名写入</li>
</ul>
<p>从下面两段示例可以看出他们的区别</p>
<pre><code class="language-Pascal">procedure TForm1.Button1Click(Sender :TObject);
var
SearchPhrase :RawUtf8;
begin//sql 文字替换
SearchPhrase:='abcde' ;
Edit2.Caption := mormot.core.json.FormatUtf8(' %   MATCH ? ORDER BY rank DESC ', ['SearchTable'], );    SearchPhrase:='abcde' ;
Edit1.Caption := mormot.core.json.FormatUtf8(' %   MATCH ? ORDER BY rank DESC ', ['SearchTable'], );
end;

procedure TForm1.Button2Click(Sender :TObject);
var
SearchPhrase :RawUtf8;
begin//json 文字替换
SearchPhrase:='abcde' ;
Edit2.Caption := mormot.core.json.FormatUtf8(' %   MATCH ? ORDER BY rank DESC ', ['SearchTable'], ,True);    SearchPhrase:='abcde' ;
Edit1.Caption := mormot.core.json.FormatUtf8(' %   MATCH ? ORDER BY rank DESC ', ['SearchTable'], ,True);
end;
</code></pre>
<p>上面的输出是:<br>
<code>SearchTable   MATCH :('abcde'): ORDER BY rank DESC</code><br>
<code>SearchTable   MATCH :(123): ORDER BY rank DESC</code></p>
<p>据说mormot 带上 <code>:( ): </code>是为了优化数据查询,估计是内存中吧!这并不是数据库的特性。</p>
<p>下面的输出是:<br>
<code>SearchTable   MATCH 123 ORDER BY rank DESC</code><br>
<code>SearchTable   MATCH "abcde" ORDER BY rank DESC</code></p><br><br>
来源:https://www.cnblogs.com/hieroly/p/18287378
頁: [1]
查看完整版本: mORMot模糊概念--FormatSQL-第1部分