1 {*********************************************************}
3 { Zeos Database Objects }
4 { String tokenizing classes for SQLite }
6 { Originally written by Sergey Seroukhov }
8 {*********************************************************}
10 {@********************************************************}
11 { Copyright (c) 1999-2012 Zeos Development Group }
13 { License Agreement: }
15 { This library is distributed in the hope that it will be }
16 { useful, but WITHOUT ANY WARRANTY; without even the }
17 { implied warranty of MERCHANTABILITY or FITNESS FOR }
18 { A PARTICULAR PURPOSE. See the GNU Lesser General }
19 { Public License for more details. }
21 { The source code of the ZEOS Libraries and packages are }
22 { distributed under the Library GNU General Public }
23 { License (see the file COPYING / COPYING.ZEOS) }
24 { with the following modification: }
25 { As a special exception, the copyright holders of this }
26 { library give you permission to link this library with }
27 { independent modules to produce an executable, }
28 { regardless of the license terms of these independent }
29 { modules, and to copy and distribute the resulting }
30 { executable under terms of your choice, provided that }
31 { you also meet, for each linked independent module, }
32 { the terms and conditions of the license of that module. }
33 { An independent module is a module which is not derived }
34 { from or based on this library. If you modify this }
35 { library, you may extend this exception to your version }
36 { of the library, but you are not obligated to do so. }
37 { If you do not wish to do so, delete this exception }
38 { statement from your version. }
41 { The project web site is located on: }
42 { http://zeos.firmos.at (FORUM) }
43 { http://sourceforge.net/p/zeoslib/tickets/ (BUGTRACKER)}
44 { svn://svn.code.sf.net/p/zeoslib/code-0/trunk (SVN) }
46 { http://www.sourceforge.net/projects/zeoslib. }
49 { Zeos Development Group. }
50 {********************************************************@}
59 Classes, {$IFDEF MSEgui}mclasses,{$ENDIF}
60 ZTokenizer, ZGenericSqlToken;
64 {** Implements a SQLite-specific number state object. }
65 TZSQLiteNumberState = class (TZNumberState)
67 function NextToken(Stream: TStream; FirstChar: Char;
68 Tokenizer: TZTokenizer): TZToken; override;
71 {** Implements a SQLite-specific quote string state object. }
72 TZSQLiteQuoteState = class (TZQuoteState)
74 function NextToken(Stream: TStream; FirstChar: Char;
75 Tokenizer: TZTokenizer): TZToken; override;
77 function EncodeString(const Value: string; QuoteChar: Char): string; override;
78 function DecodeString(const Value: string; QuoteChar: Char): string; override;
82 This state will either delegate to a comment-handling
83 state, or return a token with just a slash in it.
85 TZSQLiteCommentState = class (TZCppCommentState)
87 function NextToken(Stream: TStream; FirstChar: Char;
88 Tokenizer: TZTokenizer): TZToken; override;
91 {** Implements a symbol state object. }
92 TZSQLiteSymbolState = class (TZSymbolState)
97 {** Implements a word state object. }
98 TZSQLiteWordState = class (TZGenericSQLWordState)
103 {** Implements a default tokenizer object. }
104 TZSQLiteTokenizer = class (TZTokenizer)
111 uses SysUtils, ZCompatibility;
113 { TZSQLiteNumberState }
116 Return a number token from a reader.
117 @return a number token from a reader
119 function TZSQLiteNumberState.NextToken(Stream: TStream; FirstChar: Char;
120 Tokenizer: TZTokenizer): TZToken;
126 function ReadDecDigits: string;
130 while Stream.Read(LastChar, SizeOf(Char)) > 0 do
132 if CharInSet(LastChar, ['0'..'9']) then
134 Result := Result + LastChar;
139 Stream.Seek(-SizeOf(Char), soFromCurrent);
146 FloatPoint := FirstChar = '.';
147 Result.Value := FirstChar;
148 Result.TokenType := ttUnknown;
151 { Reads the first part of the number before decimal point }
152 if not FloatPoint then
154 Result.Value := Result.Value + ReadDecDigits;
155 FloatPoint := LastChar = '.';
158 Stream.Read(TempChar, SizeOf(Char));
159 Result.Value := Result.Value + TempChar;
163 { Reads the second part of the number after decimal point }
165 Result.Value := Result.Value + ReadDecDigits;
167 { Reads a power part of the number }
168 if CharInSet(LastChar, ['e','E']) then
170 Stream.Read(TempChar, SizeOf(Char));
171 Result.Value := Result.Value + TempChar;
174 Stream.Read(TempChar, SizeOf(Char));
175 if CharInSet(TempChar, ['0'..'9','-','+']) then
176 Result.Value := Result.Value + TempChar + ReadDecDigits
179 Result.Value := Copy(Result.Value, 1, Length(Result.Value) - 1);
180 Stream.Seek(-2*SizeOf(Char), soFromCurrent);
184 { Prepare the result }
185 if Result.Value = '.' then
187 if Tokenizer.SymbolState <> nil then
188 Result := Tokenizer.SymbolState.NextToken(Stream, FirstChar, Tokenizer);
193 Result.TokenType := ttFloat
194 else Result.TokenType := ttInteger;
198 { TZSQLiteQuoteState }
201 Return a quoted string token from a reader. This method
202 will collect characters until it sees a match to the
203 character that the tokenizer used to switch to this state.
205 @return a quoted string token from a reader
207 function TZSQLiteQuoteState.NextToken(Stream: TStream; FirstChar: Char;
208 Tokenizer: TZTokenizer): TZToken;
213 Result.Value := FirstChar;
215 while Stream.Read(ReadChar, SizeOf(Char)) > 0 do
217 if ((LastChar = FirstChar) and (ReadChar <> FirstChar)
218 and (FirstChar <> '[')) or ((FirstChar = '[') and (LastChar = ']')) then
220 Stream.Seek(-SizeOf(Char), soFromCurrent);
223 Result.Value := Result.Value + ReadChar;
224 if (LastChar = FirstChar) and (ReadChar = FirstChar) then
226 else LastChar := ReadChar;
229 if CharInSet(FirstChar, ['"', '[']) then
230 Result.TokenType := ttWord
231 else Result.TokenType := ttQuoted;
235 Encodes a string value.
236 @param Value a string value to be encoded.
237 @param QuoteChar a string quote character.
238 @returns an encoded string.
240 function TZSQLiteQuoteState.EncodeString(const Value: string; QuoteChar: Char): string;
242 if QuoteChar = '[' then
243 Result := '[' + Value + ']'
244 else if CharInSet(QuoteChar, [#39, '"']) then
245 Result := QuoteChar + Value + QuoteChar
246 else Result := Value;
250 Decodes a string value.
251 @param Value a string value to be decoded.
252 @param QuoteChar a string quote character.
253 @returns an decoded string.
255 function TZSQLiteQuoteState.DecodeString(const Value: string; QuoteChar: Char): string;
258 if Length(Value) >= 2 then
260 if CharInSet(QuoteChar, [#39, '"']) and (Value[1] = QuoteChar)
261 and (Value[Length(Value)] = QuoteChar) then
263 if Length(Value) > 2 then
264 Result := AnsiDequotedStr(Value, QuoteChar)
267 else if (QuoteChar = '[') and (Value[1] = QuoteChar)
268 and (Value[Length(Value)] = ']') then
269 Result := Copy(Value, 2, Length(Value) - 2)
273 { TZSQLiteCommentState }
276 Gets a SQLite specific comments like # or /* */.
277 @return either just a slash token, or the results of
278 delegating to a comment-handling state
280 function TZSQLiteCommentState.NextToken(Stream: TStream; FirstChar: Char;
281 Tokenizer: TZTokenizer): TZToken;
286 Result.Value := FirstChar;
287 Result.TokenType := ttUnknown;
289 if FirstChar = '-' then
291 ReadNum := Stream.Read(ReadChar, SizeOf(Char));
292 if (ReadNum > 0) and (ReadChar = '-') then
294 Result.TokenType := ttComment;
295 Result.Value := '--' + GetSingleLineComment(Stream);
300 Stream.Seek(-SizeOf(Char), soFromCurrent);
303 else if FirstChar = '/' then
305 ReadNum := Stream.Read(ReadChar, SizeOf(Char));
306 if (ReadNum > 0) and (ReadChar = '*') then
308 Result.TokenType := ttComment;
309 Result.Value := '/*' + GetMultiLineComment(Stream);
314 Stream.Seek(-SizeOf(Char), soFromCurrent);
318 if (Result.TokenType = ttUnknown) and (Tokenizer.SymbolState <> nil) then
319 Result := Tokenizer.SymbolState.NextToken(Stream, FirstChar, Tokenizer);
322 { TZSQLiteSymbolState }
325 Creates this SQLite-specific symbol state object.
327 constructor TZSQLiteSymbolState.Create;
340 { TZSQLiteWordState }
343 Constructs this SQLite-specific word state object.
345 constructor TZSQLiteWordState.Create;
347 SetWordChars(#0, #255, False);
348 SetWordChars('a', 'z', True);
349 SetWordChars('A', 'Z', True);
350 SetWordChars('0', '9', True);
351 SetWordChars('_', '_', True);
354 { TZSQLiteTokenizer }
357 Constructs a tokenizer with a default state table (as
358 described in the class comment).
360 constructor TZSQLiteTokenizer.Create;
362 EscapeState := TZEscapeState.Create;
363 WhitespaceState := TZWhitespaceState.Create;
365 SymbolState := TZSQLiteSymbolState.Create;
366 NumberState := TZSQLiteNumberState.Create;
367 QuoteState := TZSQLiteQuoteState.Create;
368 WordState := TZSQLiteWordState.Create;
369 CommentState := TZSQLiteCommentState.Create;
371 SetCharacterState(#0, #32, WhitespaceState);
372 SetCharacterState(#33, #191, SymbolState);
373 SetCharacterState(#192, High(Char), WordState);
375 SetCharacterState('a', 'z', WordState);
376 SetCharacterState('A', 'Z', WordState);
377 SetCharacterState('_', '_', WordState);
379 SetCharacterState('0', '9', NumberState);
380 SetCharacterState('.', '.', NumberState);
382 SetCharacterState('"', '"', QuoteState);
383 SetCharacterState(#39, #39, QuoteState);
384 SetCharacterState('[', '[', QuoteState);
385 SetCharacterState(']', ']', QuoteState);
387 SetCharacterState('/', '/', CommentState);
388 SetCharacterState('-', '-', CommentState);