ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/branches/udr/client/IBUtils.pas
(Generate patch)

Comparing ibx/trunk/fbintf/IBUtils.pas (file contents):
Revision 56 by tony, Mon Mar 6 10:20:02 2017 UTC vs.
Revision 120 by tony, Mon Jan 22 13:58:20 2018 UTC

# Line 33 | Line 33
33  
34   unit IBUtils;
35   {$IFDEF MSWINDOWS}
36 < {$DEFINE WINDOWS}
36 > {$DEFINE WINDOWS}
37   {$ENDIF}
38  
39   {$IFDEF FPC}
40   {$Mode Delphi}
41   {$codepage UTF8}
42 + {$define HASREQEX}
43   {$ENDIF}
44  
45 +
46   interface
47  
48   uses Classes, SysUtils;
# Line 258 | Line 260 | function Min(n1, n2: Integer): Integer;
260   function RandomString(iLength: Integer): AnsiString;
261   function RandomInteger(iLow, iHigh: Integer): Integer;
262   function StripString(st: AnsiString; CharsToStrip: AnsiString): AnsiString;
261 function FormatIdentifier(Dialect: Integer; Value: AnsiString): AnsiString;
262 function FormatIdentifierValue(Dialect: Integer; Value: AnsiString): AnsiString;
263 function FormatIdentifierValueNC(Dialect: Integer; Value: AnsiString): AnsiString;
263   function ExtractIdentifier(Dialect: Integer; Value: AnsiString): AnsiString;
264   function QuoteIdentifier(Dialect: Integer; Value: AnsiString): AnsiString;
265   function QuoteIdentifierIfNeeded(Dialect: Integer; Value: AnsiString): AnsiString;
266   function Space2Underscore(s: AnsiString): AnsiString;
267   function SQLSafeString(const s: AnsiString): AnsiString;
268 + function IsSQLIdentifier(Value: AnsiString): boolean;
269 + function ExtractConnectString(const CreateSQL: AnsiString; var ConnectString: AnsiString): boolean;
270  
271   implementation
272  
273 + {$IFDEF HASREQEX}
274 + uses RegExpr;
275 + {$ENDIF}
276 +
277   function Max(n1, n2: Integer): Integer;
278   begin
279    if (n1 > n2) then
# Line 310 | Line 315 | begin
315    end;
316   end;
317  
318 < function FormatIdentifier(Dialect: Integer; Value: AnsiString): AnsiString;
314 < begin
315 <  Value := Trim(Value);
316 <  if Dialect = 1 then
317 <    Value := AnsiUpperCase(Value)
318 <  else
319 <    if (Value <> '') and (Value[1] = '"') then
320 <      Value := '"' + StringReplace (TrimRight(Value), '"', '""', [rfReplaceAll]) + '"'
321 <    else
322 <      Value := AnsiUpperCase(Value);
323 <  Result := Value;
324 < end;
318 > {Extracts SQL Identifier typically from a  Dialect 3 encoding}
319  
320 < function FormatIdentifierValue(Dialect: Integer; Value: AnsiString): AnsiString;
320 > function ExtractIdentifier(Dialect: Integer; Value: AnsiString): AnsiString;
321   begin
322    Value := Trim(Value);
323    if Dialect = 1 then
# Line 342 | Line 336 | begin
336    Result := Value;
337   end;
338  
339 < function FormatIdentifierValueNC(Dialect: Integer; Value: AnsiString): AnsiString;
346 < begin
347 <  Value := Trim(Value);
348 <  if Dialect = 1 then
349 <    Value := AnsiUpperCase(Value)
350 <  else
351 <  begin
352 <    if (Value <> '') and (Value[1] = '"') then
353 <    begin
354 <      Delete(Value, 1, 1);
355 <      Delete(Value, Length(Value), 1);
356 <      Value := AnsiUpperCase(StringReplace (Value, '""', '"', [rfReplaceAll]));
357 <    end
358 <    else
359 <      Value := AnsiUpperCase(Value);
360 <  end;
361 <  Result := Value;
362 < end;
363 <
364 < function ExtractIdentifier(Dialect: Integer; Value: AnsiString): AnsiString;
365 < begin
366 <  Value := Trim(Value);
367 <  if Dialect = 1 then
368 <    Value := AnsiUpperCase(Value)
369 <  else
370 <  begin
371 <    if (Value <> '') and (Value[1] = '"') then
372 <    begin
373 <      Delete(Value, 1, 1);
374 <      Delete(Value, Length(Value), 1);
375 <      Value := StringReplace (Value, '""', '"', [rfReplaceAll]);
376 <    end
377 <    else
378 <      Value := AnsiUpperCase(Value);
379 <  end;
380 <  Result := Value;
381 < end;
339 > {Returns true if "w" is a Firebird SQL reserved word}
340  
341   function IsReservedWord(w: AnsiString): boolean;
342   var i: integer;
# Line 390 | Line 348 | begin
348       Result := false;
349   end;
350  
351 + {Format an SQL Identifier according to SQL Dialect}
352 +
353   function QuoteIdentifier(Dialect: Integer; Value: AnsiString): AnsiString;
354   begin
355    if Dialect = 1 then
356      Value := AnsiUpperCase(Trim(Value))
357    else
358 <    Value := '"' + Value + '"';
358 >    Value := '"' + StringReplace (Value, '""', '"', [rfReplaceAll]) + '"';
359    Result := Value;
360   end;
361  
362 + const
363 +  ValidSQLIdentifierChars = ['A'..'Z','a'..'z','0'..'9','_','$'];
364 +
365 + {Returns true if the value is a valid SQL Identifier - note lower case accepted}
366 +
367 + function IsSQLIdentifier(Value: AnsiString): boolean;
368 + var i: integer;
369 + begin
370 +  Result := false;
371 +  for i := 1 to Length(Value) do
372 +    if not (Value[i] in ValidSQLIdentifierChars) then Exit;
373 +  Result := true;
374 + end;
375 +
376 + {Extracts the Database Connect string from a Create Database Statement}
377 +
378 + {$IFDEF HASREQEX}
379 + function ExtractConnectString(const CreateSQL: AnsiString;
380 +  var ConnectString: AnsiString): boolean;
381 + var RegexObj: TRegExpr;
382 + begin
383 +  RegexObj := TRegExpr.Create;
384 +  try
385 +    {extact database file spec}
386 +    RegexObj.ModifierG := false; {turn off greedy matches}
387 +    RegexObj.ModifierI := true; {case insensitive match}
388 +    RegexObj.Expression := '^ *CREATE +(DATABASE|SCHEMA) +''(.*)''';
389 +    Result := RegexObj.Exec(CreateSQL);
390 +    if Result then
391 +      ConnectString := system.copy(CreateSQL,RegexObj.MatchPos[2],RegexObj.MatchLen[2]);
392 +  finally
393 +    RegexObj.Free;
394 +  end;
395 + end;
396 + {$ELSE}
397 + {cruder version of above for old versions of Delphi}
398 + function ExtractConnectString(const CreateSQL: AnsiString;
399 +  var ConnectString: AnsiString): boolean;
400 + var i: integer;
401 + begin
402 +  Result := false;
403 +  i := Pos('''',CreateSQL);
404 +  if i > 0 then
405 +  begin
406 +    ConnectString := CreateSQL;
407 +    delete(ConnectString,1,i);
408 +    i := Pos('''',ConnectString);
409 +    if i > 0 then
410 +    begin
411 +      delete(ConnectString,i,Length(ConnectString)-i+1);
412 +      Result := true;
413 +    end;
414 +  end;
415 + end;
416 + {$ENDIF}
417 +
418 + {Format an SQL Identifier according to SQL Dialect with encapsulation if necessary}
419 +
420   function QuoteIdentifierIfNeeded(Dialect: Integer; Value: AnsiString): AnsiString;
421   begin
422    if (Dialect = 3) and
423 <    ((AnsiUpperCase(Value) <> Value) or IsReservedWord(Value)) then
424 <     Result := '"' + Value + '"'
423 >    (IsReservedWord(Value) or not IsSQLIdentifier(Value) or (AnsiUpperCase(Value) <> Value)) then
424 >     Result := '"' + StringReplace (TrimRight(Value), '"', '""', [rfReplaceAll]) + '"'
425    else
426      Result := Value
427   end;
428  
429 + {Replaces unknown characters in a string with underscores}
430 +
431   function Space2Underscore(s: AnsiString): AnsiString;
432   var
433     k: integer;
434   begin
435       Result := s;
436       for k := 1 to Length(s) do
437 <         if not (Result[k] in ['0'..'9','A'..'Z','_','$'])  then
437 >         if not (Result[k] in ValidSQLIdentifierChars)  then
438              Result[k] := '_';
439   end;
440  
441 + {Reformats an SQL string with single quotes duplicated.}
442 +
443   function SQLSafeString(const s: AnsiString): AnsiString;
444   begin
445    Result := StringReplace(s,'''','''''',[rfReplaceAll]);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines