258 |
|
function RandomString(iLength: Integer): AnsiString; |
259 |
|
function RandomInteger(iLow, iHigh: Integer): Integer; |
260 |
|
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; |
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 |
311 |
|
end; |
312 |
|
end; |
313 |
|
|
314 |
< |
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; |
314 |
> |
{Extracts SQL Identifier typically from a Dialect 3 encoding} |
315 |
|
|
316 |
< |
function FormatIdentifierValue(Dialect: Integer; Value: AnsiString): AnsiString; |
316 |
> |
function ExtractIdentifier(Dialect: Integer; Value: AnsiString): AnsiString; |
317 |
|
begin |
318 |
|
Value := Trim(Value); |
319 |
|
if Dialect = 1 then |
332 |
|
Result := Value; |
333 |
|
end; |
334 |
|
|
335 |
< |
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; |
335 |
> |
{Returns true if "w" is a Firebird SQL reserved word} |
336 |
|
|
337 |
|
function IsReservedWord(w: AnsiString): boolean; |
338 |
|
var i: integer; |
344 |
|
Result := false; |
345 |
|
end; |
346 |
|
|
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 |
+ |
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 |
+ |
{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 |
+ |
{Reformats an SQL string with single quotes duplicated.} |
416 |
+ |
|
417 |
|
function SQLSafeString(const s: AnsiString): AnsiString; |
418 |
|
begin |
419 |
|
Result := StringReplace(s,'''','''''',[rfReplaceAll]); |