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 107 by tony, Thu Jan 18 14:37:40 2018 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 311 | Line 319 | begin
319    end;
320   end;
321  
322 < function FormatIdentifier(Dialect: Integer; Value: AnsiString): AnsiString;
315 < begin
316 <  Value := Trim(Value);
317 <  if Dialect = 1 then
318 <    Value := AnsiUpperCase(Value)
319 <  else
320 <    if (Value <> '') and (Value[1] = '"') then
321 <      Value := '"' + StringReplace (TrimRight(Value), '"', '""', [rfReplaceAll]) + '"'
322 <    else
323 <      Value := AnsiUpperCase(Value);
324 <  Result := Value;
325 < 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 343 | Line 340 | begin
340    Result := Value;
341   end;
342  
343 < function FormatIdentifierValueNC(Dialect: Integer; Value: AnsiString): AnsiString;
347 < begin
348 <  Value := Trim(Value);
349 <  if Dialect = 1 then
350 <    Value := AnsiUpperCase(Value)
351 <  else
352 <  begin
353 <    if (Value <> '') and (Value[1] = '"') then
354 <    begin
355 <      Delete(Value, 1, 1);
356 <      Delete(Value, Length(Value), 1);
357 <      Value := AnsiUpperCase(StringReplace (Value, '""', '"', [rfReplaceAll]));
358 <    end
359 <    else
360 <      Value := AnsiUpperCase(Value);
361 <  end;
362 <  Result := Value;
363 < end;
364 <
365 < function ExtractIdentifier(Dialect: Integer; Value: AnsiString): AnsiString;
366 < begin
367 <  Value := Trim(Value);
368 <  if Dialect = 1 then
369 <    Value := AnsiUpperCase(Value)
370 <  else
371 <  begin
372 <    if (Value <> '') and (Value[1] = '"') then
373 <    begin
374 <      Delete(Value, 1, 1);
375 <      Delete(Value, Length(Value), 1);
376 <      Value := StringReplace (Value, '""', '"', [rfReplaceAll]);
377 <    end
378 <    else
379 <      Value := AnsiUpperCase(Value);
380 <  end;
381 <  Result := Value;
382 < end;
343 > {Returns true if "w" is a Firebird SQL reserved word}
344  
345   function IsReservedWord(w: AnsiString): boolean;
346   var i: integer;
# Line 391 | 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
# Line 412 | Line 377 | begin
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 <    (IsReservedWord(Value) or not IsSQLIdentifier(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