#1 2017-12-07 08:48:44

noobies
Member
Registered: 2011-09-13
Posts: 139

FormatUTF8 \ problem

r := FormatUTF8('insert into ' + dbf_TFOMS4 + ' (SNILS_DOCT, POST_DOCT, OBR_SVED, CERT_NUM, CERT_DATE, PRVS) VALUES (?, ?, ?, ?, ?, ?)', [], [ss, post_rmis, edu_s, cer_num, cer_date, prvs_rmis], True);
database_dbf.ExecuteNoResult(r, []);

edu_s contains in text character \, after FormatUTF8, it turns out \\ and an error occurs when running ExecuteNoResult.
In this case, if you run the query without FormatUTF8 with the generated string with one \ error, it does not pass.
Why FormatUTF8 doubles \?

Last edited by noobies (2017-12-07 08:54:45)

Offline

#2 2017-12-07 10:08:28

Chaa
Member
Registered: 2011-03-26
Posts: 245

Re: FormatUTF8 \ problem

You set JSONFormat to True (last parameter), so FormatUTF8 used JSON rules for text escaping.

- if optional JSONFormat parameter is TRUE, ? parameters will be written
as JSON quoted strings, without : (...) : tokens, e.g. "quoted "" string"

Offline

#3 2017-12-07 12:30:31

noobies
Member
Registered: 2011-09-13
Posts: 139

Re: FormatUTF8 \ problem

To Chaa
Yea, u right, but i used execute to insert in dbf file, if i set false - no one sql command run (ado dont understand : (...) : tokens), if set Yes - symbol \ dubles sad
FormatUTF8 should 2 parameter array ([] - for default, [JSON] - for JSON, [none] - without any change or use [] for none and [Token] for current behavior)
Simple fix

r := FormatUTF8('insert into ' + dbf_TFOMS4 + ' (SNILS_DOCT, POST_DOCT, OBR_SVED, CERT_NUM, CERT_DATE, PRVS) VALUES (?, ?, ?, ?, ?, ?)', [], [ss, post_rmis, edu_s, cer_num, cer_date, prvs_rmis], True);
r := string(r).Replace('\\', '\', [rfReplaceAll]);
database_dbf.ExecuteNoResult(r, []);

but may be in foture ab can change parameter FormatUTF8 add option without any change

Last edited by noobies (2017-12-07 12:31:34)

Offline

#4 2017-12-07 15:15:51

Chaa
Member
Registered: 2011-03-26
Posts: 245

Re: FormatUTF8 \ problem

You can try without FormatUTF8 at all:

database_dbf.ExecuteNoResult('insert into ' + dbf_TFOMS4 
  + ' (SNILS_DOCT, POST_DOCT, OBR_SVED, CERT_NUM, CERT_DATE, PRVS) VALUES (?, ?, ?, ?, ?, ?)',
  [ss, post_rmis, edu_s, cer_num, cer_date, prvs_rmis]);

Offline

#5 2017-12-08 07:56:29

noobies
Member
Registered: 2011-09-13
Posts: 139

Re: FormatUTF8 \ problem

To Chaa
i cant, in real code many operation before i get final RawUTF8 string, and next i run ExecuteNoResult
but i find answer, i change source code SybCommons adding parameter JSONEscape: TTextWriterKind = twJSONEscape

function FormatUTF8(const Format: RawUTF8; const Args, Params: array of const;
  JSONFormat: boolean=false; JSONEscape: TTextWriterKind = twJSONEscape): RawUTF8; overload;

...

function FormatUTF8(const Format: RawUTF8; const Args, Params: array of const; JSONFormat: boolean; JSONEscape: TTextWriterKind): RawUTF8; overload;

      {$ifndef NOVARIANTS}
      if JSONFormat and (Params[P].VType=vtVariant) then
        VariantSaveJSON(Params[P].VVariant^,JSONEscape,tmp[tmpN]) else  //<==== change parameter
      {$endif}
      begin
        VarRecToUTF8(Params[P],tmp[tmpN]);
        wasString := not (Params[P].VType in NOTTOQUOTE[JSONFormat]);
        if wasString then
          if JSONFormat and (JSONEscape = twJSONEscape) then //<==== add additional parameter
            QuotedStrJSON(tmp[tmpN],tmp[tmpN]) else
            tmp[tmpN] := QuotedStr(pointer(tmp[tmpN]),'''');
        if not JSONFormat then begin
          inc(L,4); // space for :():
          include(inlin,tmpN);
        end;
      end;

i think it best solution on this time. Ty Chaa!

Offline

Board footer

Powered by FluxBB