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

海利鸟發表於2024-07-06

mORMot裡面的模糊概念--FormatSQL第1部分

mORMot 的 Fast Format 到底是% 還是 ? 作為引數!,先看看關鍵程式碼。

下面是程式碼原始註釋

function FormatSql(const Format: RawUtf8;  const Args, Params: array of const): RawUtf8;

fast Format() function replacement, handling % but also ? inlined parameters

  • will include Args[] for every % in Format
  • will include Params[] for every ? in Format, as "inlined" ORM or DB values,
    e.g. :(1234): for numbers, and 😦'quoted '' string'): for text
  • note that, due to a Delphi compiler limitation, cardinal values should be
    type-casted to Int64() (otherwise the integer mapped value will be converted)
  • is a wrapper around FormatParams(Format, Args, Params, false, result);

快速Format()函式替代,處理%和?內聯引數

  • Format中每個%都將包含Args[]
  • Format中每個?都將包含Params[],作為“內聯”ORM或DB值,
    例如,數字用:(1234):表示,文字用:('quoted '' string'):表示
  • 請注意,由於Delphi編譯器的限制,基數值應轉換為Int64()(否則對映的整數值將被轉換)
  • 是FormatParams(Format, Args, Params, false, result)的包裝函式;

注:

  1. “Args[] for every % in Format” 指的是,在Format字串中,每一個百分號(%)都將對應一個Args陣列中的元素,用於替換該百分號。
  2. “Params[] for every ? in Format” 指的是,在Format字串中,每一個問號(?)都將對應一個Params陣列中的元素,這個元素可能是一個ORM(物件關係對映)值或者資料庫值。
  3. “cardinal values should be type-casted to Int64()” 指的是,由於Delphi編譯器的某些限制,需要將基數值(無符號整數值)顯式轉換為Int64型別,以避免整數值的自動轉換可能引發的問題。
  4. “wrapper around FormatParams” 指的是,這個函式實質上是對另一個函式FormatParams的封裝,使得呼叫更加簡便,或者增加了額外的功能。
function FormatJson(const Format: RawUtf8; const Args, Params: array of const): RawUtf8;

fast Format() function replacement, handling % but also ? parameters as JSON

  • will include Args[] for every % in Format
  • will include Params[] for every ? in Format, as their JSON value, with
    proper JSON double quotes and escaping for strings
  • note that, due to a Delphi compiler limitation, cardinal values should be
    type-casted to Int64() (otherwise the integer mapped value will be converted)
  • is a wrapper around FormatParams(Format, Args, Params, true, result);

快速Format()函式的替代品,處理%同時也處理?引數作為JSON

  • 將為每個在Format中出現的%包含Args[]
  • 將為每個在Format中出現的?包含Params[],作為它們的JSON值,字串會使用適當的JSON雙引號和跳脫字元
  • 注意,由於Delphi編譯器的限制,基數值應該被型別轉換為Int64()(否則對映的整數值將被轉換)
  • 是FormatParams(Format, Args, Params, true, result)的包裝函式;
{$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}

fast Format() function replacement, handling % and ? parameters

  • call rather FormatSql() and FormatJson() wrappers instead
  • resulting string has no length limit and uses fast concatenation
  • any supplied TObject instance will be written as their class name

快速Format()函式替代,處理%和?引數

  • 請改用FormatSql()和FormatJson()包裝函式
  • 生成的字串沒有長度限制,並使用快速連線
  • 任何提供的TObject例項都將以其類名寫入

從下面兩段示例可以看出他們的區別

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]);    SearchPhrase:='abcde' ;
  Edit1.Caption := mormot.core.json.FormatUtf8(' %   MATCH ? ORDER BY rank DESC ', ['SearchTable'], [123]);
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'], [SearchPhrase],True);    SearchPhrase:='abcde' ;
  Edit1.Caption := mormot.core.json.FormatUtf8(' %   MATCH ? ORDER BY rank DESC ', ['SearchTable'], [123],True);
end;

上面的輸出是:
SearchTable MATCH :('abcde'): ORDER BY rank DESC
SearchTable MATCH :(123): ORDER BY rank DESC

據說mormot 帶上 :( ): 是為了最佳化資料查詢,估計是記憶體中吧!這並不是資料庫的特性。

下面的輸出是:
SearchTable MATCH 123 ORDER BY rank DESC
SearchTable MATCH "abcde" ORDER BY rank DESC

相關文章