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; |
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 |
315 |
|
end; |
316 |
|
end; |
317 |
|
|
318 |
< |
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; |
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 |
336 |
|
Result := Value; |
337 |
|
end; |
338 |
|
|
339 |
< |
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; |
339 |
> |
{Returns true if "w" is a Firebird SQL reserved word} |
340 |
|
|
341 |
|
function IsReservedWord(w: AnsiString): boolean; |
342 |
|
var i: integer; |
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 |
370 |
+ |
Result := false; |
371 |
+ |
for i := 1 to Length(Value) do |
372 |
+ |
if not (Value[i] in ValidSQLIdentifierChars) then Exit; |
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 |
< |
((AnsiUpperCase(Value) <> Value) or IsReservedWord(Value)) then |
425 |
< |
Result := '"' + Value + '"' |
424 |
> |
(IsReservedWord(Value) or not IsSQLIdentifier(Value) or (AnsiUpperCase(Value) <> Value)) then |
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]); |