32 |
|
{************************************************************************} |
33 |
|
|
34 |
|
unit IBUtils; |
35 |
+ |
{$IFDEF MSWINDOWS} |
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 |
44 |
< |
{$IFDEF WINDOWS } |
45 |
< |
Windows, |
46 |
< |
{$ELSE} |
47 |
< |
unix, |
48 |
< |
{$ENDIF} |
49 |
< |
Classes, SysUtils; |
52 |
> |
uses Classes, SysUtils; |
53 |
|
|
54 |
|
const |
55 |
|
CRLF = #13 + #10; |
261 |
|
|
262 |
|
function Max(n1, n2: Integer): Integer; |
263 |
|
function Min(n1, n2: Integer): Integer; |
264 |
< |
function RandomString(iLength: Integer): String; |
264 |
> |
function RandomString(iLength: Integer): AnsiString; |
265 |
|
function RandomInteger(iLow, iHigh: Integer): Integer; |
266 |
< |
function StripString(st: String; CharsToStrip: String): String; |
267 |
< |
function FormatIdentifier(Dialect: Integer; Value: String): String; |
268 |
< |
function FormatIdentifierValue(Dialect: Integer; Value: String): String; |
269 |
< |
function FormatIdentifierValueNC(Dialect: Integer; Value: String): String; |
270 |
< |
function ExtractIdentifier(Dialect: Integer; Value: String): String; |
271 |
< |
function QuoteIdentifier(Dialect: Integer; Value: String): String; |
272 |
< |
function QuoteIdentifierIfNeeded(Dialect: Integer; Value: String): String; |
273 |
< |
function Space2Underscore(s: string): string; |
271 |
< |
function SQLSafeString(const s: string): string; |
266 |
> |
function StripString(st: AnsiString; CharsToStrip: 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 |
294 |
|
result := n2; |
295 |
|
end; |
296 |
|
|
297 |
< |
function RandomString(iLength: Integer): String; |
297 |
> |
function RandomString(iLength: Integer): AnsiString; |
298 |
|
begin |
299 |
|
result := ''; |
300 |
|
while Length(result) < iLength do |
308 |
|
result := Trunc(Random(iHigh - iLow)) + iLow; |
309 |
|
end; |
310 |
|
|
311 |
< |
function StripString(st: String; CharsToStrip: String): String; |
311 |
> |
function StripString(st: AnsiString; CharsToStrip: AnsiString): AnsiString; |
312 |
|
var |
313 |
|
i: Integer; |
314 |
|
begin |
319 |
|
end; |
320 |
|
end; |
321 |
|
|
322 |
< |
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; |
322 |
> |
{Extracts SQL Identifier typically from a Dialect 3 encoding} |
323 |
|
|
324 |
< |
function FormatIdentifierValue(Dialect: Integer; Value: String): String; |
324 |
> |
function ExtractIdentifier(Dialect: Integer; Value: AnsiString): AnsiString; |
325 |
|
begin |
326 |
|
Value := Trim(Value); |
327 |
|
if Dialect = 1 then |
340 |
|
Result := Value; |
341 |
|
end; |
342 |
|
|
343 |
< |
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; |
343 |
> |
{Returns true if "w" is a Firebird SQL reserved word} |
344 |
|
|
345 |
< |
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; |
385 |
< |
|
386 |
< |
function IsReservedWord(w: string): boolean; |
345 |
> |
function IsReservedWord(w: AnsiString): boolean; |
346 |
|
var i: integer; |
347 |
|
begin |
348 |
|
Result := true; |
352 |
|
Result := false; |
353 |
|
end; |
354 |
|
|
355 |
< |
function QuoteIdentifier(Dialect: Integer; Value: String): String; |
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 |
< |
function QuoteIdentifierIfNeeded(Dialect: Integer; Value: String): String; |
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 |
< |
function Space2Underscore(s: string): string; |
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 |
< |
function SQLSafeString(const s: string): string; |
445 |
> |
{Reformats an SQL string with single quotes duplicated.} |
446 |
> |
|
447 |
> |
function SQLSafeString(const s: AnsiString): AnsiString; |
448 |
|
begin |
449 |
|
Result := StringReplace(s,'''','''''',[rfReplaceAll]); |
450 |
|
end; |