32 |
|
{************************************************************************} |
33 |
|
|
34 |
|
unit IBUtils; |
35 |
+ |
{$IFDEF MSWINDOWS} |
36 |
+ |
{$DEFINE WINDOWS} |
37 |
+ |
{$IF defined(CompilerVersion) and (CompilerVersion >= 28)} |
38 |
+ |
{Delphi XE7 onwards}} |
39 |
+ |
{$define HASREQEX} |
40 |
+ |
{$IFEND} |
41 |
+ |
{$ENDIF} |
42 |
|
|
43 |
|
{$IFDEF FPC} |
44 |
|
{$Mode Delphi} |
45 |
|
{$codepage UTF8} |
46 |
+ |
{$define HASREQEX} |
47 |
|
{$ENDIF} |
48 |
|
|
49 |
+ |
|
50 |
|
interface |
51 |
|
|
52 |
< |
uses |
44 |
< |
{$IFDEF WINDOWS } |
45 |
< |
Windows, |
46 |
< |
{$ELSE} |
47 |
< |
unix, |
48 |
< |
{$ENDIF} |
49 |
< |
Classes, SysUtils; |
52 |
> |
uses Classes, SysUtils; |
53 |
|
|
54 |
|
const |
55 |
|
CRLF = #13 + #10; |
58 |
|
TAB = #9; |
59 |
|
NULL_TERMINATOR = #0; |
60 |
|
|
61 |
< |
sqlReservedWords: array [0..166] of string = ( |
62 |
< |
'ADD','ADMIN','ALL','ALTER','AND','ANY','AS','AT','AVG','BEGIN','BETWEEN','BIGINT','BIT_LENGTH','BLOB','BOTH', |
63 |
< |
'BY','CASE','CAST','CHAR','CHAR_LENGTH','CHARACTER','CHARACTER_LENGTH','CHECK','CLOSE','COLLATE','COLUMN', |
64 |
< |
'COMMIT','CONNECT','CONSTRAINT','COUNT','CREATE','CROSS','CURRENT','CURRENT_CONNECTION','CURRENT_DATE', |
65 |
< |
'CURRENT_ROLE','CURRENT_TIME','CURRENT_TIMESTAMP','CURRENT_TRANSACTION','CURRENT_USER','CURSOR','DATE', |
66 |
< |
'DAY','DEC','DECIMAL','DECLARE','DEFAULT','DELETE','DISCONNECT','DISTINCT','DOUBLE','DROP','ELSE','END', |
67 |
< |
'ESCAPE','EXECUTE','EXISTS','EXTERNAL','EXTRACT','FETCH','FILTER','FLOAT','FOR','FOREIGN','FROM','FULL', |
68 |
< |
'FUNCTION','GDSCODE','GLOBAL','GRANT','GROUP','HAVING','HOUR','IN','INDEX','INNER','INSENSITIVE','INSERT', |
69 |
< |
'INT','INTEGER','INTO','IS','JOIN','LEADING','LEFT','LIKE','LONG','LOWER','MAX','MAXIMUM_SEGMENT','MERGE', |
70 |
< |
'MIN','MINUTE','MONTH','NATIONAL','NATURAL','NCHAR','NO','NOT','NULL','NUMERIC','OCTET_LENGTH','OF','ON', |
71 |
< |
'ONLY','OPEN','OR','ORDER','OUTER','PARAMETER','PLAN','POSITION','POST_EVENT','PRECISION','PRIMARY', |
72 |
< |
'PROCEDURE','RDB$DB_KEY','REAL','RECORD_VERSION','RECREATE','RECURSIVE','REFERENCES','RELEASE','RETURNING_VALUES', |
73 |
< |
'RETURNS','REVOKE','RIGHT','ROLLBACK','ROW_COUNT','ROWS','SAVEPOINT','SECOND','SELECT','SENSITIVE', |
74 |
< |
'SET','SIMILAR','SMALLINT','SOME','SQLCODE','SQLSTATE','START','SUM','TABLE','THEN','TIME', |
75 |
< |
'TIMESTAMP','TO','TRAILING','TRIGGER','TRIM','UNION','UNIQUE','UPDATE','UPPER','USER','USING', |
76 |
< |
'VALUE','VALUES','VARCHAR','VARIABLE','VARYING','VIEW','WHEN','WHERE','WHILE','WITH','YEAR'); |
61 |
> |
sqlReservedWords: array [0..197] of string = ( |
62 |
> |
'ADD', |
63 |
> |
'ADMIN', |
64 |
> |
'ALL', |
65 |
> |
'ALTER', |
66 |
> |
'AND', |
67 |
> |
'ANY', |
68 |
> |
'AS', |
69 |
> |
'AT', |
70 |
> |
'AVG', |
71 |
> |
'BEGIN', |
72 |
> |
'BETWEEN', |
73 |
> |
'BIGINT', |
74 |
> |
'BIT_LENGTH', |
75 |
> |
'BLOB', |
76 |
> |
'BOOLEAN', |
77 |
> |
'BOTH', |
78 |
> |
'BY', |
79 |
> |
'CASE', |
80 |
> |
'CAST', |
81 |
> |
'CHAR', |
82 |
> |
'CHAR_LENGTH', |
83 |
> |
'CHARACTER', |
84 |
> |
'CHARACTER_LENGTH', |
85 |
> |
'CHECK', |
86 |
> |
'CLOSE', |
87 |
> |
'COLLATE', |
88 |
> |
'COLUMN', |
89 |
> |
'COMMIT', |
90 |
> |
'CONNECT', |
91 |
> |
'CONSTRAINT', |
92 |
> |
'CORR', |
93 |
> |
'COUNT', |
94 |
> |
'COVAR_POP', |
95 |
> |
'COVAR_SAMP', |
96 |
> |
'CREATE', |
97 |
> |
'CROSS', |
98 |
> |
'CURRENT', |
99 |
> |
'CURRENT_CONNECTION', |
100 |
> |
'CURRENT_DATE', |
101 |
> |
'CURRENT_ROLE', |
102 |
> |
'CURRENT_TIME', |
103 |
> |
'CURRENT_TIMESTAMP', |
104 |
> |
'CURRENT_TRANSACTION', |
105 |
> |
'CURRENT_USER', |
106 |
> |
'CURSOR', |
107 |
> |
'DATE', |
108 |
> |
'DAY', |
109 |
> |
'DEC', |
110 |
> |
'DECIMAL', |
111 |
> |
'DECLARE', |
112 |
> |
'DEFAULT', |
113 |
> |
'DELETE', |
114 |
> |
'DELETING', |
115 |
> |
'DETERMINISTIC', |
116 |
> |
'DISCONNECT', |
117 |
> |
'DISTINCT', |
118 |
> |
'DOUBLE', |
119 |
> |
'DROP', |
120 |
> |
'ELSE', |
121 |
> |
'END', |
122 |
> |
'ESCAPE', |
123 |
> |
'EXECUTE', |
124 |
> |
'EXISTS', |
125 |
> |
'EXTERNAL', |
126 |
> |
'EXTRACT', |
127 |
> |
'FALSE', |
128 |
> |
'FETCH', |
129 |
> |
'FILTER', |
130 |
> |
'FLOAT', |
131 |
> |
'FOR', |
132 |
> |
'FOREIGN', |
133 |
> |
'FROM', |
134 |
> |
'FULL', |
135 |
> |
'FUNCTION', |
136 |
> |
'GDSCODE', |
137 |
> |
'GLOBAL', |
138 |
> |
'GRANT', |
139 |
> |
'GROUP', |
140 |
> |
'HAVING', |
141 |
> |
'HOUR', |
142 |
> |
'IN', |
143 |
> |
'INDEX', |
144 |
> |
'INNER', |
145 |
> |
'INSENSITIVE', |
146 |
> |
'INSERT', |
147 |
> |
'INSERTING', |
148 |
> |
'INT', |
149 |
> |
'INTEGER', |
150 |
> |
'INTO', |
151 |
> |
'IS', |
152 |
> |
'JOIN', |
153 |
> |
'LEADING', |
154 |
> |
'LEFT', |
155 |
> |
'LIKE', |
156 |
> |
'LONG', |
157 |
> |
'LOWER', |
158 |
> |
'MAX', |
159 |
> |
'MAXIMUM_SEGMENT', |
160 |
> |
'MERGE', |
161 |
> |
'MIN', |
162 |
> |
'MINUTE', |
163 |
> |
'MONTH', |
164 |
> |
'NATIONAL', |
165 |
> |
'NATURAL', |
166 |
> |
'NCHAR', |
167 |
> |
'NO', |
168 |
> |
'NOT', |
169 |
> |
'NULL', |
170 |
> |
'NUMERIC', |
171 |
> |
'OCTET_LENGTH', |
172 |
> |
'OF', |
173 |
> |
'OFFSET', |
174 |
> |
'ON', |
175 |
> |
'ONLY', |
176 |
> |
'OPEN', |
177 |
> |
'OR', |
178 |
> |
'ORDER', |
179 |
> |
'OUTER', |
180 |
> |
'OVER', |
181 |
> |
'PARAMETER', |
182 |
> |
'PLAN', |
183 |
> |
'POSITION', |
184 |
> |
'POST_EVENT', |
185 |
> |
'PRECISION', |
186 |
> |
'PRIMARY', |
187 |
> |
'PROCEDURE', |
188 |
> |
'RDB$DB_KEY', |
189 |
> |
'RDB$RECORD_VERSION', |
190 |
> |
'REAL', |
191 |
> |
'RECORD_VERSION', |
192 |
> |
'RECREATE', |
193 |
> |
'RECURSIVE', |
194 |
> |
'REFERENCES', |
195 |
> |
'REGR_AVGX', |
196 |
> |
'REGR_AVGY', |
197 |
> |
'REGR_COUNT', |
198 |
> |
'REGR_INTERCEPT', |
199 |
> |
'REGR_R2', |
200 |
> |
'REGR_SLOPE', |
201 |
> |
'REGR_SXX', |
202 |
> |
'REGR_SXY', |
203 |
> |
'REGR_SYY', |
204 |
> |
'RELEASE', |
205 |
> |
'RETURN', |
206 |
> |
'RETURNING_VALUES', |
207 |
> |
'RETURNS', |
208 |
> |
'REVOKE', |
209 |
> |
'RIGHT', |
210 |
> |
'ROLLBACK', |
211 |
> |
'ROW', |
212 |
> |
'ROW_COUNT', |
213 |
> |
'ROWS', |
214 |
> |
'SAVEPOINT', |
215 |
> |
'SCROLL', |
216 |
> |
'SECOND', |
217 |
> |
'SELECT', |
218 |
> |
'SENSITIVE', |
219 |
> |
'SET', |
220 |
> |
'SIMILAR', |
221 |
> |
'SMALLINT', |
222 |
> |
'SOME', |
223 |
> |
'SQLCODE', |
224 |
> |
'SQLSTATE', |
225 |
> |
'SQLSTATE', |
226 |
> |
'START', |
227 |
> |
'STDDEV_POP', |
228 |
> |
'STDDEV_SAMP', |
229 |
> |
'SUM', |
230 |
> |
'TABLE', |
231 |
> |
'THEN', |
232 |
> |
'TIME', |
233 |
> |
'TIMESTAMP', |
234 |
> |
'TO', |
235 |
> |
'TRAILING', |
236 |
> |
'TRIGGER', |
237 |
> |
'TRIM', |
238 |
> |
'TRUE', |
239 |
> |
'UNION', |
240 |
> |
'UNIQUE', |
241 |
> |
'UNKNOWN', |
242 |
> |
'UPDATE', |
243 |
> |
'UPDATING', |
244 |
> |
'UPPER', |
245 |
> |
'USER', |
246 |
> |
'USING', |
247 |
> |
'VALUE', |
248 |
> |
'VALUES', |
249 |
> |
'VAR_POP', |
250 |
> |
'VAR_SAMP', |
251 |
> |
'VARCHAR', |
252 |
> |
'VARIABLE', |
253 |
> |
'VARYING', |
254 |
> |
'VIEW', |
255 |
> |
'WHEN', |
256 |
> |
'WHERE', |
257 |
> |
'WHILE', |
258 |
> |
'WITH', |
259 |
> |
'YEAR' |
260 |
> |
); |
261 |
|
|
262 |
|
function Max(n1, n2: Integer): Integer; |
263 |
|
function Min(n1, n2: Integer): Integer; |
264 |
< |
function RandomString(iLength: Integer): String; |
264 |
> |
function RandomString(iLength: Integer): AnsiString; |
265 |
|
function RandomInteger(iLow, iHigh: Integer): Integer; |
266 |
< |
function StripString(st: String; CharsToStrip: String): String; |
267 |
< |
function FormatIdentifier(Dialect: Integer; Value: String): String; |
268 |
< |
function FormatIdentifierValue(Dialect: Integer; Value: String): String; |
269 |
< |
function FormatIdentifierValueNC(Dialect: Integer; Value: String): String; |
270 |
< |
function ExtractIdentifier(Dialect: Integer; Value: String): String; |
271 |
< |
function QuoteIdentifier(Dialect: Integer; Value: String): String; |
272 |
< |
function QuoteIdentifierIfNeeded(Dialect: Integer; Value: String): String; |
273 |
< |
function Space2Underscore(s: string): string; |
266 |
> |
function StripString(st: AnsiString; CharsToStrip: AnsiString): AnsiString; |
267 |
> |
function ExtractIdentifier(Dialect: Integer; Value: AnsiString): AnsiString; |
268 |
> |
function QuoteIdentifier(Dialect: Integer; Value: AnsiString): AnsiString; |
269 |
> |
function QuoteIdentifierIfNeeded(Dialect: Integer; Value: AnsiString): AnsiString; |
270 |
> |
function Space2Underscore(s: AnsiString): AnsiString; |
271 |
> |
function SQLSafeString(const s: AnsiString): AnsiString; |
272 |
> |
function IsSQLIdentifier(Value: AnsiString): boolean; |
273 |
> |
function ExtractConnectString(const CreateSQL: AnsiString; var ConnectString: AnsiString): boolean; |
274 |
|
|
275 |
|
implementation |
276 |
|
|
277 |
+ |
{$IFDEF HASREQEX} |
278 |
+ |
uses RegExpr; |
279 |
+ |
{$ENDIF} |
280 |
+ |
|
281 |
|
function Max(n1, n2: Integer): Integer; |
282 |
|
begin |
283 |
|
if (n1 > n2) then |
294 |
|
result := n2; |
295 |
|
end; |
296 |
|
|
297 |
< |
function RandomString(iLength: Integer): String; |
297 |
> |
function RandomString(iLength: Integer): AnsiString; |
298 |
|
begin |
299 |
|
result := ''; |
300 |
|
while Length(result) < iLength do |
308 |
|
result := Trunc(Random(iHigh - iLow)) + iLow; |
309 |
|
end; |
310 |
|
|
311 |
< |
function StripString(st: String; CharsToStrip: String): String; |
311 |
> |
function StripString(st: AnsiString; CharsToStrip: AnsiString): AnsiString; |
312 |
|
var |
313 |
|
i: Integer; |
314 |
|
begin |
319 |
|
end; |
320 |
|
end; |
321 |
|
|
322 |
< |
function FormatIdentifier(Dialect: Integer; Value: String): String; |
132 |
< |
begin |
133 |
< |
Value := Trim(Value); |
134 |
< |
if Dialect = 1 then |
135 |
< |
Value := AnsiUpperCase(Value) |
136 |
< |
else |
137 |
< |
if (Value <> '') and (Value[1] = '"') then |
138 |
< |
Value := '"' + StringReplace (TrimRight(Value), '"', '""', [rfReplaceAll]) + '"' |
139 |
< |
else |
140 |
< |
Value := AnsiUpperCase(Value); |
141 |
< |
Result := Value; |
142 |
< |
end; |
322 |
> |
{Extracts SQL Identifier typically from a Dialect 3 encoding} |
323 |
|
|
324 |
< |
function FormatIdentifierValue(Dialect: Integer; Value: String): String; |
324 |
> |
function ExtractIdentifier(Dialect: Integer; Value: AnsiString): AnsiString; |
325 |
|
begin |
326 |
|
Value := Trim(Value); |
327 |
|
if Dialect = 1 then |
340 |
|
Result := Value; |
341 |
|
end; |
342 |
|
|
343 |
< |
function FormatIdentifierValueNC(Dialect: Integer; Value: String): String; |
164 |
< |
begin |
165 |
< |
Value := Trim(Value); |
166 |
< |
if Dialect = 1 then |
167 |
< |
Value := AnsiUpperCase(Value) |
168 |
< |
else |
169 |
< |
begin |
170 |
< |
if (Value <> '') and (Value[1] = '"') then |
171 |
< |
begin |
172 |
< |
Delete(Value, 1, 1); |
173 |
< |
Delete(Value, Length(Value), 1); |
174 |
< |
Value := AnsiUpperCase(StringReplace (Value, '""', '"', [rfReplaceAll])); |
175 |
< |
end |
176 |
< |
else |
177 |
< |
Value := AnsiUpperCase(Value); |
178 |
< |
end; |
179 |
< |
Result := Value; |
180 |
< |
end; |
343 |
> |
{Returns true if "w" is a Firebird SQL reserved word} |
344 |
|
|
345 |
< |
function ExtractIdentifier(Dialect: Integer; Value: String): String; |
183 |
< |
begin |
184 |
< |
Value := Trim(Value); |
185 |
< |
if Dialect = 1 then |
186 |
< |
Value := AnsiUpperCase(Value) |
187 |
< |
else |
188 |
< |
begin |
189 |
< |
if (Value <> '') and (Value[1] = '"') then |
190 |
< |
begin |
191 |
< |
Delete(Value, 1, 1); |
192 |
< |
Delete(Value, Length(Value), 1); |
193 |
< |
Value := StringReplace (Value, '""', '"', [rfReplaceAll]); |
194 |
< |
end |
195 |
< |
else |
196 |
< |
Value := AnsiUpperCase(Value); |
197 |
< |
end; |
198 |
< |
Result := Value; |
199 |
< |
end; |
200 |
< |
|
201 |
< |
function IsReservedWord(w: string): boolean; |
345 |
> |
function IsReservedWord(w: AnsiString): boolean; |
346 |
|
var i: integer; |
347 |
|
begin |
348 |
|
Result := true; |
352 |
|
Result := false; |
353 |
|
end; |
354 |
|
|
355 |
< |
function QuoteIdentifier(Dialect: Integer; Value: String): String; |
355 |
> |
{Format an SQL Identifier according to SQL Dialect} |
356 |
> |
|
357 |
> |
function QuoteIdentifier(Dialect: Integer; Value: AnsiString): AnsiString; |
358 |
|
begin |
359 |
|
if Dialect = 1 then |
360 |
|
Value := AnsiUpperCase(Trim(Value)) |
361 |
|
else |
362 |
< |
Value := '"' + Value + '"'; |
362 |
> |
Value := '"' + StringReplace (Value, '""', '"', [rfReplaceAll]) + '"'; |
363 |
|
Result := Value; |
364 |
|
end; |
365 |
|
|
366 |
< |
function QuoteIdentifierIfNeeded(Dialect: Integer; Value: String): String; |
366 |
> |
const |
367 |
> |
ValidSQLIdentifierChars = ['A'..'Z','a'..'z','0'..'9','_','$']; |
368 |
> |
|
369 |
> |
{Returns true if the value is a valid SQL Identifier - note lower case accepted} |
370 |
> |
|
371 |
> |
function IsSQLIdentifier(Value: AnsiString): boolean; |
372 |
> |
var i: integer; |
373 |
> |
begin |
374 |
> |
Result := false; |
375 |
> |
for i := 1 to Length(Value) do |
376 |
> |
if not (Value[i] in ValidSQLIdentifierChars) then Exit; |
377 |
> |
Result := true; |
378 |
> |
end; |
379 |
> |
|
380 |
> |
{Extracts the Database Connect string from a Create Database Statement} |
381 |
> |
|
382 |
> |
{$IFDEF HASREQEX} |
383 |
> |
function ExtractConnectString(const CreateSQL: AnsiString; |
384 |
> |
var ConnectString: AnsiString): boolean; |
385 |
> |
var RegexObj: TRegExpr; |
386 |
> |
begin |
387 |
> |
RegexObj := TRegExpr.Create; |
388 |
> |
try |
389 |
> |
{extact database file spec} |
390 |
> |
RegexObj.ModifierG := false; {turn off greedy matches} |
391 |
> |
RegexObj.ModifierI := true; {case insensitive match} |
392 |
> |
RegexObj.Expression := '^ *CREATE +(DATABASE|SCHEMA) +''(.*)'''; |
393 |
> |
Result := RegexObj.Exec(CreateSQL); |
394 |
> |
if Result then |
395 |
> |
ConnectString := system.copy(CreateSQL,RegexObj.MatchPos[2],RegexObj.MatchLen[2]); |
396 |
> |
finally |
397 |
> |
RegexObj.Free; |
398 |
> |
end; |
399 |
> |
end; |
400 |
> |
{$ELSE} |
401 |
> |
{cruder version of above for old versions of Delphi} |
402 |
> |
function ExtractConnectString(const CreateSQL: AnsiString; |
403 |
> |
var ConnectString: AnsiString): boolean; |
404 |
> |
var i: integer; |
405 |
> |
begin |
406 |
> |
Result := false; |
407 |
> |
i := Pos('''',CreateSQL); |
408 |
> |
if i > 0 then |
409 |
> |
begin |
410 |
> |
ConnectString := CreateSQL; |
411 |
> |
delete(ConnectString,1,i); |
412 |
> |
i := Pos('''',ConnectString); |
413 |
> |
if i > 0 then |
414 |
> |
begin |
415 |
> |
delete(ConnectString,i,Length(ConnectString)-i+1); |
416 |
> |
Result := true; |
417 |
> |
end; |
418 |
> |
end; |
419 |
> |
end; |
420 |
> |
{$ENDIF} |
421 |
> |
|
422 |
> |
{Format an SQL Identifier according to SQL Dialect with encapsulation if necessary} |
423 |
> |
|
424 |
> |
function QuoteIdentifierIfNeeded(Dialect: Integer; Value: AnsiString): AnsiString; |
425 |
|
begin |
426 |
|
if (Dialect = 3) and |
427 |
< |
((AnsiUpperCase(Value) <> Value) or IsReservedWord(Value)) then |
428 |
< |
Result := '"' + Value + '"' |
427 |
> |
(IsReservedWord(Value) or not IsSQLIdentifier(Value) or (AnsiUpperCase(Value) <> Value)) then |
428 |
> |
Result := '"' + StringReplace (TrimRight(Value), '"', '""', [rfReplaceAll]) + '"' |
429 |
|
else |
430 |
|
Result := Value |
431 |
|
end; |
432 |
|
|
433 |
< |
function Space2Underscore(s: string): string; |
433 |
> |
{Replaces unknown characters in a string with underscores} |
434 |
> |
|
435 |
> |
function Space2Underscore(s: AnsiString): AnsiString; |
436 |
|
var |
437 |
|
k: integer; |
438 |
|
begin |
439 |
|
Result := s; |
440 |
|
for k := 1 to Length(s) do |
441 |
< |
if not (Result[k] in ['0'..'9','A'..'Z','_','$']) then |
441 |
> |
if not (Result[k] in ValidSQLIdentifierChars) then |
442 |
|
Result[k] := '_'; |
443 |
|
end; |
444 |
|
|
445 |
+ |
{Reformats an SQL string with single quotes duplicated.} |
446 |
+ |
|
447 |
+ |
function SQLSafeString(const s: AnsiString): AnsiString; |
448 |
+ |
begin |
449 |
+ |
Result := StringReplace(s,'''','''''',[rfReplaceAll]); |
450 |
+ |
end; |
451 |
|
|
452 |
|
end. |