1 {*********************************************************}
3 { Zeos Database Objects }
4 { String tokenizing classes for MySQL }
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, SysUtils, ZTokenizer, ZGenericSqlToken, ZCompatibility;
62 {** Implements a quote string state object. }
63 TZAdoSQLQuoteState = class (TZQuoteState)
65 function NextToken(Stream: TStream; FirstChar: Char;
66 Tokenizer: TZTokenizer): TZToken; override;
68 function EncodeString(const Value: string; QuoteChar: Char): string; override;
69 function DecodeString(const Value: string; QuoteChar: Char): string; override;
72 {** Implements a default tokenizer object. }
73 TZAdoSQLTokenizer = class (TZGenericSQLTokenizer)
80 { TZAdoSQLQuoteState }
83 Return a quoted string token from a reader. This method
84 will collect characters until it sees a match to the
85 character that the tokenizer used to switch to this state.
87 @return a quoted string token from a reader
89 function TZAdoSQLQuoteState.NextToken(Stream: TStream; FirstChar: Char;
90 Tokenizer: TZTokenizer): TZToken;
95 Result.Value := FirstChar;
97 while Stream.Read(ReadChar, SizeOf(Char)) > 0 do
99 if ((LastChar = FirstChar) and (ReadChar <> FirstChar)
100 and (FirstChar <> '[')) or ((FirstChar = '[') and (LastChar = ']')) then
102 Stream.Seek(-SizeOf(Char), soFromCurrent);
105 Result.Value := Result.Value + ReadChar;
106 if (LastChar = FirstChar) and (ReadChar = FirstChar) then
108 else LastChar := ReadChar;
111 if CharInSet(FirstChar, ['"', '[']) then
112 Result.TokenType := ttQuotedIdentifier
113 else Result.TokenType := ttQuoted;
117 Encodes a string value.
118 @param Value a string value to be encoded.
119 @param QuoteChar a string quote character.
120 @returns an encoded string.
122 function TZAdoSQLQuoteState.EncodeString(const Value: string; QuoteChar: Char): string;
124 if QuoteChar = '[' then
125 Result := '[' + Value + ']'
126 else if CharInSet(QuoteChar, [#39, '"']) then
127 Result := QuoteChar + Value + QuoteChar
128 else Result := Value;
132 Decodes a string value.
133 @param Value a string value to be decoded.
134 @param QuoteChar a string quote character.
135 @returns an decoded string.
137 function TZAdoSQLQuoteState.DecodeString(const Value: string; QuoteChar: Char): string;
140 if Length(Value) >= 2 then
142 if CharInSet(QuoteChar, [#39, '"']) and (Value[1] = QuoteChar)
143 and (Value[Length(Value)] = QuoteChar) then
145 if Length(Value) > 2 then
146 Result := AnsiDequotedStr(Value, QuoteChar)
149 else if (QuoteChar = '[') and (Value[1] = QuoteChar)
150 and (Value[Length(Value)] = ']') then
151 Result := Copy(Value, 2, Length(Value) - 2)
156 { TZAdoSQLTokenizer }
158 Constructs a tokenizer with a default state table (as
159 described in the class comment).
161 constructor TZAdoSQLTokenizer.Create;
163 EscapeState := TZEscapeState.Create;
164 NumberState := TZNumberState.Create;
165 QuoteState := TZAdoSQLQuoteState.Create;
166 WhitespaceState := TZWhitespaceState.Create;
167 CommentState := TZCppCommentState.Create;
169 SymbolState := TZGenericSQLSymbolState.Create;
170 WordState := TZGenericSQLWordState.Create;
172 SetCharacterState(#0, #32, WhitespaceState);
173 SetCharacterState(#33, #191, SymbolState);
174 SetCharacterState(#192, High(Char), WordState);
176 SetCharacterState('a', 'z', WordState);
177 SetCharacterState('A', 'Z', WordState);
178 SetCharacterState('_', '_', WordState);
179 SetCharacterState('$', '$', WordState);
180 SetCharacterState('@', '@', WordState);
182 SetCharacterState('0', '9', NumberState);
183 SetCharacterState('.', '.', NumberState);
185 SetCharacterState('"', '"', QuoteState);
186 SetCharacterState(#39, #39, QuoteState);
187 SetCharacterState('[', '[', QuoteState);
188 SetCharacterState(']', ']', QuoteState);
190 SetCharacterState('/', '/', CommentState);