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; |
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; |
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; |
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; |
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 |
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 |
|
(IsReservedWord(Value) or not IsSQLIdentifier(Value) or (AnsiUpperCase(Value) <> Value)) then |
398 |
< |
Result := '"' + Value + '"' |
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]); |