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 47 by tony, Mon Jan 9 15:31:51 2017 UTC vs.
Revision 117 by tony, Mon Jan 22 13:58:11 2018 UTC

# Line 32 | Line 32
32   {************************************************************************}
33  
34   unit IBUtils;
35 + {$IFDEF MSWINDOWS}
36 + {$DEFINE WINDOWS}
37 + {$ENDIF}
38  
39   {$IFDEF FPC}
40   {$Mode Delphi}
# Line 40 | Line 43 | unit IBUtils;
43  
44   interface
45  
46 < uses
44 < {$IFDEF WINDOWS }
45 <  Windows,
46 < {$ELSE}
47 <  unix,
48 < {$ENDIF}
49 <  Classes, SysUtils;
46 > uses Classes, SysUtils;
47  
48   const
49    CRLF = #13 + #10;
# Line 258 | Line 255 | const
255  
256   function Max(n1, n2: Integer): Integer;
257   function Min(n1, n2: Integer): Integer;
258 < function RandomString(iLength: Integer): String;
258 > function RandomString(iLength: Integer): AnsiString;
259   function RandomInteger(iLow, iHigh: Integer): Integer;
260 < function StripString(st: String; CharsToStrip: String): String;
261 < function FormatIdentifier(Dialect: Integer; Value: String): String;
262 < function FormatIdentifierValue(Dialect: Integer; Value: String): String;
263 < function FormatIdentifierValueNC(Dialect: Integer; Value: String): String;
264 < function ExtractIdentifier(Dialect: Integer; Value: String): String;
265 < function QuoteIdentifier(Dialect: Integer; Value: String): String;
266 < function QuoteIdentifierIfNeeded(Dialect: Integer; Value: String): String;
267 < function Space2Underscore(s: string): string;
271 < function SQLSafeString(const s: string): string;
260 > function StripString(st: AnsiString; CharsToStrip: AnsiString): AnsiString;
261 > function ExtractIdentifier(Dialect: Integer; Value: AnsiString): AnsiString;
262 > function QuoteIdentifier(Dialect: Integer; Value: AnsiString): AnsiString;
263 > function QuoteIdentifierIfNeeded(Dialect: Integer; Value: AnsiString): AnsiString;
264 > function Space2Underscore(s: AnsiString): AnsiString;
265 > function SQLSafeString(const s: AnsiString): AnsiString;
266 > function IsSQLIdentifier(Value: AnsiString): boolean;
267 > function ExtractConnectString(const CreateSQL: AnsiString; var ConnectString: AnsiString): boolean;
268  
269   implementation
270  
271 + uses RegExpr;
272 +
273   function Max(n1, n2: Integer): Integer;
274   begin
275    if (n1 > n2) then
# Line 288 | Line 286 | begin
286      result := n2;
287   end;
288  
289 < function RandomString(iLength: Integer): String;
289 > function RandomString(iLength: Integer): AnsiString;
290   begin
291    result := '';
292    while Length(result) < iLength do
# Line 302 | Line 300 | begin
300    result := Trunc(Random(iHigh - iLow)) + iLow;
301   end;
302  
303 < function StripString(st: String; CharsToStrip: String): String;
303 > function StripString(st: AnsiString; CharsToStrip: AnsiString): AnsiString;
304   var
305    i: Integer;
306   begin
# Line 313 | Line 311 | begin
311    end;
312   end;
313  
314 < function FormatIdentifier(Dialect: Integer; Value: String): String;
317 < begin
318 <  Value := Trim(Value);
319 <  if Dialect = 1 then
320 <    Value := AnsiUpperCase(Value)
321 <  else
322 <    if (Value <> '') and (Value[1] = '"') then
323 <      Value := '"' + StringReplace (TrimRight(Value), '"', '""', [rfReplaceAll]) + '"'
324 <    else
325 <      Value := AnsiUpperCase(Value);
326 <  Result := Value;
327 < end;
314 > {Extracts SQL Identifier typically from a  Dialect 3 encoding}
315  
316 < function FormatIdentifierValue(Dialect: Integer; Value: String): String;
316 > function ExtractIdentifier(Dialect: Integer; Value: AnsiString): AnsiString;
317   begin
318    Value := Trim(Value);
319    if Dialect = 1 then
# Line 345 | Line 332 | begin
332    Result := Value;
333   end;
334  
335 < function FormatIdentifierValueNC(Dialect: Integer; Value: String): String;
349 < begin
350 <  Value := Trim(Value);
351 <  if Dialect = 1 then
352 <    Value := AnsiUpperCase(Value)
353 <  else
354 <  begin
355 <    if (Value <> '') and (Value[1] = '"') then
356 <    begin
357 <      Delete(Value, 1, 1);
358 <      Delete(Value, Length(Value), 1);
359 <      Value := AnsiUpperCase(StringReplace (Value, '""', '"', [rfReplaceAll]));
360 <    end
361 <    else
362 <      Value := AnsiUpperCase(Value);
363 <  end;
364 <  Result := Value;
365 < end;
366 <
367 < function ExtractIdentifier(Dialect: Integer; Value: String): String;
368 < begin
369 <  Value := Trim(Value);
370 <  if Dialect = 1 then
371 <    Value := AnsiUpperCase(Value)
372 <  else
373 <  begin
374 <    if (Value <> '') and (Value[1] = '"') then
375 <    begin
376 <      Delete(Value, 1, 1);
377 <      Delete(Value, Length(Value), 1);
378 <      Value := StringReplace (Value, '""', '"', [rfReplaceAll]);
379 <    end
380 <    else
381 <      Value := AnsiUpperCase(Value);
382 <  end;
383 <  Result := Value;
384 < end;
335 > {Returns true if "w" is a Firebird SQL reserved word}
336  
337 < function IsReservedWord(w: string): boolean;
337 > function IsReservedWord(w: AnsiString): boolean;
338   var i: integer;
339   begin
340       Result := true;
# Line 393 | Line 344 | begin
344       Result := false;
345   end;
346  
347 < function QuoteIdentifier(Dialect: Integer; Value: String): String;
347 > {Format an SQL Identifier according to SQL Dialect}
348 >
349 > function QuoteIdentifier(Dialect: Integer; Value: AnsiString): AnsiString;
350   begin
351    if Dialect = 1 then
352      Value := AnsiUpperCase(Trim(Value))
353    else
354 <    Value := '"' + Value + '"';
354 >    Value := '"' + StringReplace (Value, '""', '"', [rfReplaceAll]) + '"';
355    Result := Value;
356   end;
357  
358 < function QuoteIdentifierIfNeeded(Dialect: Integer; Value: String): String;
358 > const
359 >  ValidSQLIdentifierChars = ['A'..'Z','a'..'z','0'..'9','_','$'];
360 >
361 > {Returns true if the value is a valid SQL Identifier - note lower case accepted}
362 >
363 > function IsSQLIdentifier(Value: AnsiString): boolean;
364 > var i: integer;
365 > begin
366 >  Result := false;
367 >  for i := 1 to Length(Value) do
368 >    if not (Value[i] in ValidSQLIdentifierChars) then Exit;
369 >  Result := true;
370 > end;
371 >
372 > {Extracts the Database Connect string from a Create Database Statement}
373 >
374 > function ExtractConnectString(const CreateSQL: AnsiString;
375 >  var ConnectString: AnsiString): boolean;
376 > var RegexObj: TRegExpr;
377 > begin
378 >  RegexObj := TRegExpr.Create;
379 >  try
380 >    {extact database file spec}
381 >    RegexObj.ModifierG := false; {turn off greedy matches}
382 >    RegexObj.ModifierI := true; {case insensitive match}
383 >    RegexObj.Expression := '^ *CREATE +(DATABASE|SCHEMA) +''(.*)''';
384 >    Result := RegexObj.Exec(CreateSQL);
385 >    if Result then
386 >      ConnectString := system.copy(CreateSQL,RegexObj.MatchPos[2],RegexObj.MatchLen[2]);
387 >  finally
388 >    RegexObj.Free;
389 >  end;
390 > end;
391 >
392 > {Format an SQL Identifier according to SQL Dialect with encapsulation if necessary}
393 >
394 > function QuoteIdentifierIfNeeded(Dialect: Integer; Value: AnsiString): AnsiString;
395   begin
396    if (Dialect = 3) and
397 <    ((AnsiUpperCase(Value) <> Value) or IsReservedWord(Value)) then
398 <     Result := '"' + Value + '"'
397 >    (IsReservedWord(Value) or not IsSQLIdentifier(Value) or (AnsiUpperCase(Value) <> Value)) then
398 >     Result := '"' + StringReplace (TrimRight(Value), '"', '""', [rfReplaceAll]) + '"'
399    else
400      Result := Value
401   end;
402  
403 < function Space2Underscore(s: string): string;
403 > {Replaces unknown characters in a string with underscores}
404 >
405 > function Space2Underscore(s: AnsiString): AnsiString;
406   var
407     k: integer;
408   begin
409       Result := s;
410       for k := 1 to Length(s) do
411 <         if not (Result[k] in ['0'..'9','A'..'Z','_','$'])  then
411 >         if not (Result[k] in ValidSQLIdentifierChars)  then
412              Result[k] := '_';
413   end;
414  
415 < function SQLSafeString(const s: string): string;
415 > {Reformats an SQL string with single quotes duplicated.}
416 >
417 > function SQLSafeString(const s: AnsiString): AnsiString;
418   begin
419    Result := StringReplace(s,'''','''''',[rfReplaceAll]);
420   end;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines