32 |
|
{************************************************************************} |
33 |
|
|
34 |
|
unit IBUtils; |
35 |
+ |
{$IFDEF MSWINDOWS} |
36 |
+ |
{$DEFINE WINDOWS} |
37 |
+ |
{$ENDIF} |
38 |
|
|
39 |
|
{$IFDEF FPC} |
40 |
|
{$Mode Delphi} |
43 |
|
|
44 |
|
interface |
45 |
|
|
46 |
< |
uses |
44 |
< |
{$IFDEF WINDOWS } |
45 |
< |
Windows, |
46 |
< |
{$ELSE} |
47 |
< |
unix, |
48 |
< |
{$ENDIF} |
49 |
< |
Classes, SysUtils; |
46 |
> |
uses Classes, SysUtils; |
47 |
|
|
48 |
|
const |
49 |
|
CRLF = #13 + #10; |
255 |
|
|
256 |
|
function Max(n1, n2: Integer): Integer; |
257 |
|
function Min(n1, n2: Integer): Integer; |
258 |
< |
function RandomString(iLength: Integer): String; |
258 |
> |
function RandomString(iLength: Integer): AnsiString; |
259 |
|
function RandomInteger(iLow, iHigh: Integer): Integer; |
260 |
< |
function StripString(st: String; CharsToStrip: String): String; |
261 |
< |
function FormatIdentifier(Dialect: Integer; Value: String): String; |
262 |
< |
function FormatIdentifierValue(Dialect: Integer; Value: String): String; |
263 |
< |
function FormatIdentifierValueNC(Dialect: Integer; Value: String): String; |
264 |
< |
function ExtractIdentifier(Dialect: Integer; Value: String): String; |
265 |
< |
function QuoteIdentifier(Dialect: Integer; Value: String): String; |
266 |
< |
function QuoteIdentifierIfNeeded(Dialect: Integer; Value: String): String; |
267 |
< |
function Space2Underscore(s: string): string; |
268 |
< |
function SQLSafeString(const s: string): string; |
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; |
264 |
> |
function ExtractIdentifier(Dialect: Integer; Value: AnsiString): AnsiString; |
265 |
> |
function QuoteIdentifier(Dialect: Integer; Value: AnsiString): AnsiString; |
266 |
> |
function QuoteIdentifierIfNeeded(Dialect: Integer; Value: AnsiString): AnsiString; |
267 |
> |
function Space2Underscore(s: AnsiString): AnsiString; |
268 |
> |
function SQLSafeString(const s: AnsiString): AnsiString; |
269 |
> |
function IsSQLIdentifier(Value: AnsiString): boolean; |
270 |
|
|
271 |
|
implementation |
272 |
|
|
286 |
|
result := n2; |
287 |
|
end; |
288 |
|
|
289 |
< |
function RandomString(iLength: Integer): String; |
289 |
> |
function RandomString(iLength: Integer): AnsiString; |
290 |
|
begin |
291 |
|
result := ''; |
292 |
|
while Length(result) < iLength do |
300 |
|
result := Trunc(Random(iHigh - iLow)) + iLow; |
301 |
|
end; |
302 |
|
|
303 |
< |
function StripString(st: String; CharsToStrip: String): String; |
303 |
> |
function StripString(st: AnsiString; CharsToStrip: AnsiString): AnsiString; |
304 |
|
var |
305 |
|
i: Integer; |
306 |
|
begin |
311 |
|
end; |
312 |
|
end; |
313 |
|
|
314 |
< |
function FormatIdentifier(Dialect: Integer; Value: String): String; |
314 |
> |
function FormatIdentifier(Dialect: Integer; Value: AnsiString): AnsiString; |
315 |
|
begin |
316 |
|
Value := Trim(Value); |
317 |
|
if Dialect = 1 then |
324 |
|
Result := Value; |
325 |
|
end; |
326 |
|
|
327 |
< |
function FormatIdentifierValue(Dialect: Integer; Value: String): String; |
327 |
> |
function FormatIdentifierValue(Dialect: Integer; Value: AnsiString): AnsiString; |
328 |
|
begin |
329 |
|
Value := Trim(Value); |
330 |
|
if Dialect = 1 then |
343 |
|
Result := Value; |
344 |
|
end; |
345 |
|
|
346 |
< |
function FormatIdentifierValueNC(Dialect: Integer; Value: String): String; |
346 |
> |
function FormatIdentifierValueNC(Dialect: Integer; Value: AnsiString): AnsiString; |
347 |
|
begin |
348 |
|
Value := Trim(Value); |
349 |
|
if Dialect = 1 then |
362 |
|
Result := Value; |
363 |
|
end; |
364 |
|
|
365 |
< |
function ExtractIdentifier(Dialect: Integer; Value: String): String; |
365 |
> |
function ExtractIdentifier(Dialect: Integer; Value: AnsiString): AnsiString; |
366 |
|
begin |
367 |
|
Value := Trim(Value); |
368 |
|
if Dialect = 1 then |
381 |
|
Result := Value; |
382 |
|
end; |
383 |
|
|
384 |
< |
function IsReservedWord(w: string): boolean; |
384 |
> |
function IsReservedWord(w: AnsiString): boolean; |
385 |
|
var i: integer; |
386 |
|
begin |
387 |
|
Result := true; |
391 |
|
Result := false; |
392 |
|
end; |
393 |
|
|
394 |
< |
function QuoteIdentifier(Dialect: Integer; Value: String): String; |
394 |
> |
function QuoteIdentifier(Dialect: Integer; Value: AnsiString): AnsiString; |
395 |
|
begin |
396 |
|
if Dialect = 1 then |
397 |
|
Value := AnsiUpperCase(Trim(Value)) |
400 |
|
Result := Value; |
401 |
|
end; |
402 |
|
|
403 |
< |
function QuoteIdentifierIfNeeded(Dialect: Integer; Value: String): String; |
403 |
> |
const |
404 |
> |
ValidSQLIdentifierChars = ['A'..'Z','a'..'z','0'..'9','_','$']; |
405 |
> |
|
406 |
> |
function IsSQLIdentifier(Value: AnsiString): boolean; |
407 |
> |
var i: integer; |
408 |
> |
begin |
409 |
> |
Result := false; |
410 |
> |
for i := 1 to Length(Value) do |
411 |
> |
if not (Value[i] in ValidSQLIdentifierChars) then Exit; |
412 |
> |
Result := true; |
413 |
> |
end; |
414 |
> |
|
415 |
> |
function QuoteIdentifierIfNeeded(Dialect: Integer; Value: AnsiString): AnsiString; |
416 |
|
begin |
417 |
|
if (Dialect = 3) and |
418 |
< |
((AnsiUpperCase(Value) <> Value) or IsReservedWord(Value)) then |
418 |
> |
(IsReservedWord(Value) or not IsSQLIdentifier(Value) or (AnsiUpperCase(Value) <> Value)) then |
419 |
|
Result := '"' + Value + '"' |
420 |
|
else |
421 |
|
Result := Value |
422 |
|
end; |
423 |
|
|
424 |
< |
function Space2Underscore(s: string): string; |
424 |
> |
function Space2Underscore(s: AnsiString): AnsiString; |
425 |
|
var |
426 |
|
k: integer; |
427 |
|
begin |
431 |
|
Result[k] := '_'; |
432 |
|
end; |
433 |
|
|
434 |
< |
function SQLSafeString(const s: string): string; |
434 |
> |
function SQLSafeString(const s: AnsiString): AnsiString; |
435 |
|
begin |
436 |
|
Result := StringReplace(s,'''','''''',[rfReplaceAll]); |
437 |
|
end; |