32 |
|
{************************************************************************} |
33 |
|
|
34 |
|
unit IBUtils; |
35 |
+ |
{$IFDEF MSWINDOWS} |
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 |
44 |
< |
{$IFDEF WINDOWS } |
45 |
< |
Windows, |
46 |
< |
{$ELSE} |
47 |
< |
unix, |
48 |
< |
{$ENDIF} |
49 |
< |
Classes, SysUtils; |
48 |
> |
uses Classes, SysUtils; |
49 |
|
|
50 |
|
const |
51 |
|
CRLF = #13 + #10; |
257 |
|
|
258 |
|
function Max(n1, n2: Integer): Integer; |
259 |
|
function Min(n1, n2: Integer): Integer; |
260 |
< |
function RandomString(iLength: Integer): String; |
260 |
> |
function RandomString(iLength: Integer): AnsiString; |
261 |
|
function RandomInteger(iLow, iHigh: Integer): Integer; |
262 |
< |
function StripString(st: String; CharsToStrip: String): String; |
263 |
< |
function FormatIdentifier(Dialect: Integer; Value: String): String; |
264 |
< |
function FormatIdentifierValue(Dialect: Integer; Value: String): String; |
265 |
< |
function FormatIdentifierValueNC(Dialect: Integer; Value: String): String; |
266 |
< |
function ExtractIdentifier(Dialect: Integer; Value: String): String; |
267 |
< |
function QuoteIdentifier(Dialect: Integer; Value: String): String; |
268 |
< |
function QuoteIdentifierIfNeeded(Dialect: Integer; Value: String): String; |
269 |
< |
function Space2Underscore(s: string): string; |
271 |
< |
function SQLSafeString(const s: string): string; |
262 |
> |
function StripString(st: AnsiString; CharsToStrip: 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 |
290 |
|
result := n2; |
291 |
|
end; |
292 |
|
|
293 |
< |
function RandomString(iLength: Integer): String; |
293 |
> |
function RandomString(iLength: Integer): AnsiString; |
294 |
|
begin |
295 |
|
result := ''; |
296 |
|
while Length(result) < iLength do |
304 |
|
result := Trunc(Random(iHigh - iLow)) + iLow; |
305 |
|
end; |
306 |
|
|
307 |
< |
function StripString(st: String; CharsToStrip: String): String; |
307 |
> |
function StripString(st: AnsiString; CharsToStrip: AnsiString): AnsiString; |
308 |
|
var |
309 |
|
i: Integer; |
310 |
|
begin |
315 |
|
end; |
316 |
|
end; |
317 |
|
|
318 |
< |
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; |
318 |
> |
{Extracts SQL Identifier typically from a Dialect 3 encoding} |
319 |
|
|
320 |
< |
function FormatIdentifierValue(Dialect: Integer; Value: String): String; |
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: 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; |
339 |
> |
{Returns true if "w" is a Firebird SQL reserved word} |
340 |
|
|
341 |
< |
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; |
341 |
> |
function IsReservedWord(w: AnsiString): boolean; |
342 |
|
var i: integer; |
343 |
|
begin |
344 |
|
Result := true; |
348 |
|
Result := false; |
349 |
|
end; |
350 |
|
|
351 |
< |
function QuoteIdentifier(Dialect: Integer; Value: String): String; |
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 |
< |
function QuoteIdentifierIfNeeded(Dialect: Integer; Value: String): String; |
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 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 |
< |
((AnsiUpperCase(Value) <> Value) or IsReservedWord(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 |
< |
function Space2Underscore(s: string): string; |
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 |
< |
function SQLSafeString(const s: string): string; |
441 |
> |
{Reformats an SQL string with single quotes duplicated.} |
442 |
> |
|
443 |
> |
function SQLSafeString(const s: AnsiString): AnsiString; |
444 |
|
begin |
445 |
|
Result := StringReplace(s,'''','''''',[rfReplaceAll]); |
446 |
|
end; |