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; |
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 |
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; |
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 |
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 old versions of Delphi} |
398 |
+ |
function ExtractConnectString(const CreateSQL: AnsiString; |
399 |
+ |
var ConnectString: AnsiString): boolean; |
400 |
+ |
var i: integer; |
401 |
+ |
begin |
402 |
+ |
Result := false; |
403 |
+ |
i := Pos('''',CreateSQL); |
404 |
+ |
if i > 0 then |
405 |
+ |
begin |
406 |
+ |
ConnectString := CreateSQL; |
407 |
+ |
delete(ConnectString,1,i); |
408 |
+ |
i := Pos('''',ConnectString); |
409 |
+ |
if i > 0 then |
410 |
+ |
begin |
411 |
+ |
delete(ConnectString,i,Length(ConnectString)-i+1); |
412 |
+ |
Result := true; |
413 |
+ |
end; |
414 |
+ |
end; |
415 |
+ |
end; |
416 |
+ |
{$ENDIF} |
417 |
+ |
|
418 |
+ |
{Format an SQL Identifier according to SQL Dialect with encapsulation if necessary} |
419 |
+ |
|
420 |
|
function QuoteIdentifierIfNeeded(Dialect: Integer; Value: AnsiString): AnsiString; |
421 |
|
begin |
422 |
|
if (Dialect = 3) and |
423 |
< |
(IsReservedWord(Value) or not IsSQLIdentifier(Value)) then |
424 |
< |
Result := '"' + Value + '"' |
423 |
> |
(IsReservedWord(Value) or not IsSQLIdentifier(Value) or (AnsiUpperCase(Value) <> Value)) then |
424 |
> |
Result := '"' + StringReplace (TrimRight(Value), '"', '""', [rfReplaceAll]) + '"' |
425 |
|
else |
426 |
|
Result := Value |
427 |
|
end; |
428 |
|
|
429 |
+ |
{Replaces unknown characters in a string with underscores} |
430 |
+ |
|
431 |
|
function Space2Underscore(s: AnsiString): AnsiString; |
432 |
|
var |
433 |
|
k: integer; |
434 |
|
begin |
435 |
|
Result := s; |
436 |
|
for k := 1 to Length(s) do |
437 |
< |
if not (Result[k] in ['0'..'9','A'..'Z','_','$']) then |
437 |
> |
if not (Result[k] in ValidSQLIdentifierChars) then |
438 |
|
Result[k] := '_'; |
439 |
|
end; |
440 |
|
|
441 |
+ |
{Reformats an SQL string with single quotes duplicated.} |
442 |
+ |
|
443 |
|
function SQLSafeString(const s: AnsiString): AnsiString; |
444 |
|
begin |
445 |
|
Result := StringReplace(s,'''','''''',[rfReplaceAll]); |