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 115 by tony, Fri Jan 19 09:23:39 2018 UTC vs.
Revision 121 by tony, Mon Jan 22 13:58:23 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 311 | Line 315 | begin
315    end;
316   end;
317  
318 < 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;
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 343 | Line 336 | begin
336    Result := Value;
337   end;
338  
339 < 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;
339 > {Returns true if "w" is a Firebird SQL reserved word}
340  
341   function IsReservedWord(w: AnsiString): boolean;
342   var i: integer;
# Line 391 | 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
# Line 412 | Line 373 | begin
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 Delphi. Older versions lack regular expression
398 + handling.}
399 + function ExtractConnectString(const CreateSQL: AnsiString;
400 +  var ConnectString: AnsiString): boolean;
401 + var i: integer;
402 + begin
403 +  Result := false;
404 +  i := Pos('''',CreateSQL);
405 +  if i > 0 then
406 +  begin
407 +    ConnectString := CreateSQL;
408 +    delete(ConnectString,1,i);
409 +    i := Pos('''',ConnectString);
410 +    if i > 0 then
411 +    begin
412 +      delete(ConnectString,i,Length(ConnectString)-i+1);
413 +      Result := true;
414 +    end;
415 +  end;
416 + end;
417 + {$ENDIF}
418 +
419 + {Format an SQL Identifier according to SQL Dialect with encapsulation if necessary}
420 +
421   function QuoteIdentifierIfNeeded(Dialect: Integer; Value: AnsiString): AnsiString;
422   begin
423    if (Dialect = 3) and
424      (IsReservedWord(Value) or not IsSQLIdentifier(Value) or (AnsiUpperCase(Value) <> Value)) then
425 <     Result := '"' + Value + '"'
425 >     Result := '"' + StringReplace (TrimRight(Value), '"', '""', [rfReplaceAll]) + '"'
426    else
427      Result := Value
428   end;
429  
430 + {Replaces unknown characters in a string with underscores}
431 +
432   function Space2Underscore(s: AnsiString): AnsiString;
433   var
434     k: integer;
435   begin
436       Result := s;
437       for k := 1 to Length(s) do
438 <         if not (Result[k] in ['0'..'9','A'..'Z','_','$'])  then
438 >         if not (Result[k] in ValidSQLIdentifierChars)  then
439              Result[k] := '_';
440   end;
441  
442 + {Reformats an SQL string with single quotes duplicated.}
443 +
444   function SQLSafeString(const s: AnsiString): AnsiString;
445   begin
446    Result := StringReplace(s,'''','''''',[rfReplaceAll]);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines