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 118 by tony, Mon Jan 22 13:58:14 2018 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines