ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/branches/journaling/fbintf/IBUtils.pas
Revision: 45
Committed: Tue Dec 6 10:33:46 2016 UTC (7 years, 4 months ago) by tony
Content type: text/x-pascal
Original Path: ibx/trunk/fbintf/IBUtils.pas
File size: 8441 byte(s)
Log Message:
Committing updates for Release R2-0-0

File Contents

# Content
1 {************************************************************************}
2 { }
3 { Borland Delphi Visual Component Library }
4 { InterBase Express core components }
5 { }
6 { Copyright (c) 1998-2000 Inprise Corporation }
7 { }
8 { InterBase Express is based in part on the product }
9 { Free IB Components, written by Gregory H. Deatz for }
10 { Hoagland, Longo, Moran, Dunst & Doukas Company. }
11 { Free IB Components is used under license. }
12 { }
13 { The contents of this file are subject to the InterBase }
14 { Public License Version 1.0 (the "License"); you may not }
15 { use this file except in compliance with the License. You }
16 { may obtain a copy of the License at http://www.Inprise.com/IPL.html }
17 { Software distributed under the License is distributed on }
18 { an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either }
19 { express or implied. See the License for the specific language }
20 { governing rights and limitations under the License. }
21 { The Original Code was created by InterBase Software Corporation }
22 { and its successors. }
23 { Portions created by Inprise Corporation are Copyright (C) Inprise }
24 { Corporation. All Rights Reserved. }
25 { Contributor(s): Jeff Overcash }
26 { }
27 { IBX For Lazarus (Firebird Express) }
28 { Contributor: Tony Whyman, MWA Software http://www.mwasoftware.co.uk }
29 { Portions created by MWA Software are copyright McCallum Whyman }
30 { Associates Ltd 2011 }
31 { }
32 {************************************************************************}
33
34 unit IBUtils;
35
36 {$IFDEF FPC}
37 {$Mode Delphi}
38 {$codepage UTF8}
39 {$ENDIF}
40
41 interface
42
43 uses
44 {$IFDEF WINDOWS }
45 Windows,
46 {$ELSE}
47 unix,
48 {$ENDIF}
49 Classes, SysUtils;
50
51 const
52 CRLF = #13 + #10;
53 CR = #13;
54 LF = #10;
55 TAB = #9;
56 NULL_TERMINATOR = #0;
57
58 sqlReservedWords: array [0..166] of string = (
59 'ADD','ADMIN','ALL','ALTER','AND','ANY','AS','AT','AVG','BEGIN','BETWEEN','BIGINT','BIT_LENGTH','BLOB','BOTH',
60 'BY','CASE','CAST','CHAR','CHAR_LENGTH','CHARACTER','CHARACTER_LENGTH','CHECK','CLOSE','COLLATE','COLUMN',
61 'COMMIT','CONNECT','CONSTRAINT','COUNT','CREATE','CROSS','CURRENT','CURRENT_CONNECTION','CURRENT_DATE',
62 'CURRENT_ROLE','CURRENT_TIME','CURRENT_TIMESTAMP','CURRENT_TRANSACTION','CURRENT_USER','CURSOR','DATE',
63 'DAY','DEC','DECIMAL','DECLARE','DEFAULT','DELETE','DISCONNECT','DISTINCT','DOUBLE','DROP','ELSE','END',
64 'ESCAPE','EXECUTE','EXISTS','EXTERNAL','EXTRACT','FETCH','FILTER','FLOAT','FOR','FOREIGN','FROM','FULL',
65 'FUNCTION','GDSCODE','GLOBAL','GRANT','GROUP','HAVING','HOUR','IN','INDEX','INNER','INSENSITIVE','INSERT',
66 'INT','INTEGER','INTO','IS','JOIN','LEADING','LEFT','LIKE','LONG','LOWER','MAX','MAXIMUM_SEGMENT','MERGE',
67 'MIN','MINUTE','MONTH','NATIONAL','NATURAL','NCHAR','NO','NOT','NULL','NUMERIC','OCTET_LENGTH','OF','ON',
68 'ONLY','OPEN','OR','ORDER','OUTER','PARAMETER','PLAN','POSITION','POST_EVENT','PRECISION','PRIMARY',
69 'PROCEDURE','RDB$DB_KEY','REAL','RECORD_VERSION','RECREATE','RECURSIVE','REFERENCES','RELEASE','RETURNING_VALUES',
70 'RETURNS','REVOKE','RIGHT','ROLLBACK','ROW_COUNT','ROWS','SAVEPOINT','SECOND','SELECT','SENSITIVE',
71 'SET','SIMILAR','SMALLINT','SOME','SQLCODE','SQLSTATE','START','SUM','TABLE','THEN','TIME',
72 'TIMESTAMP','TO','TRAILING','TRIGGER','TRIM','UNION','UNIQUE','UPDATE','UPPER','USER','USING',
73 'VALUE','VALUES','VARCHAR','VARIABLE','VARYING','VIEW','WHEN','WHERE','WHILE','WITH','YEAR');
74
75 function Max(n1, n2: Integer): Integer;
76 function Min(n1, n2: Integer): Integer;
77 function RandomString(iLength: Integer): String;
78 function RandomInteger(iLow, iHigh: Integer): Integer;
79 function StripString(st: String; CharsToStrip: String): String;
80 function FormatIdentifier(Dialect: Integer; Value: String): String;
81 function FormatIdentifierValue(Dialect: Integer; Value: String): String;
82 function FormatIdentifierValueNC(Dialect: Integer; Value: String): String;
83 function ExtractIdentifier(Dialect: Integer; Value: String): String;
84 function QuoteIdentifier(Dialect: Integer; Value: String): String;
85 function QuoteIdentifierIfNeeded(Dialect: Integer; Value: String): String;
86 function Space2Underscore(s: string): string;
87
88 implementation
89
90 function Max(n1, n2: Integer): Integer;
91 begin
92 if (n1 > n2) then
93 result := n1
94 else
95 result := n2;
96 end;
97
98 function Min(n1, n2: Integer): Integer;
99 begin
100 if (n1 < n2) then
101 result := n1
102 else
103 result := n2;
104 end;
105
106 function RandomString(iLength: Integer): String;
107 begin
108 result := '';
109 while Length(result) < iLength do
110 result := result + IntToStr(RandomInteger(0, High(Integer)));
111 if Length(result) > iLength then
112 result := Copy(result, 1, iLength);
113 end;
114
115 function RandomInteger(iLow, iHigh: Integer): Integer;
116 begin
117 result := Trunc(Random(iHigh - iLow)) + iLow;
118 end;
119
120 function StripString(st: String; CharsToStrip: String): String;
121 var
122 i: Integer;
123 begin
124 result := '';
125 for i := 1 to Length(st) do begin
126 if AnsiPos(st[i], CharsToStrip) = 0 then
127 result := result + st[i];
128 end;
129 end;
130
131 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;
143
144 function FormatIdentifierValue(Dialect: Integer; Value: String): String;
145 begin
146 Value := Trim(Value);
147 if Dialect = 1 then
148 Value := AnsiUpperCase(Value)
149 else
150 begin
151 if (Value <> '') and (Value[1] = '"') then
152 begin
153 Delete(Value, 1, 1);
154 Delete(Value, Length(Value), 1);
155 Value := StringReplace (Value, '""', '"', [rfReplaceAll]);
156 end
157 else
158 Value := AnsiUpperCase(Value);
159 end;
160 Result := Value;
161 end;
162
163 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;
181
182 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;
202 var i: integer;
203 begin
204 Result := true;
205 for i := 0 to Length(sqlReservedWords) - 1 do
206 if w = sqlReservedWords[i] then
207 Exit;
208 Result := false;
209 end;
210
211 function QuoteIdentifier(Dialect: Integer; Value: String): String;
212 begin
213 if Dialect = 1 then
214 Value := AnsiUpperCase(Trim(Value))
215 else
216 Value := '"' + Value + '"';
217 Result := Value;
218 end;
219
220 function QuoteIdentifierIfNeeded(Dialect: Integer; Value: String): String;
221 begin
222 if (Dialect = 3) and
223 ((AnsiUpperCase(Value) <> Value) or IsReservedWord(Value)) then
224 Result := '"' + Value + '"'
225 else
226 Result := Value
227 end;
228
229 function Space2Underscore(s: string): string;
230 var
231 k: integer;
232 begin
233 Result := s;
234 for k := 1 to Length(s) do
235 if not (Result[k] in ['0'..'9','A'..'Z','_','$']) then
236 Result[k] := '_';
237 end;
238
239
240 end.