zeoslib  UNKNOWN
 All Files
ZAdoToken.pas
Go to the documentation of this file.
1 {*********************************************************}
2 { }
3 { Zeos Database Objects }
4 { String tokenizing classes for MySQL }
5 { }
6 { Originally written by Sergey Seroukhov }
7 { }
8 {*********************************************************}
9 
10 {@********************************************************}
11 { Copyright (c) 1999-2012 Zeos Development Group }
12 { }
13 { License Agreement: }
14 { }
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. }
20 { }
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. }
39 { }
40 { }
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) }
45 { }
46 { http://www.sourceforge.net/projects/zeoslib. }
47 { }
48 { }
49 { Zeos Development Group. }
50 {********************************************************@}
51 
52 unit ZAdoToken;
53 
54 interface
55 
56 {$I ZParseSql.inc}
57 
58 uses
59  Classes, SysUtils, ZTokenizer, ZGenericSqlToken, ZCompatibility;
60 
61 type
62  {** Implements a quote string state object. }
63  TZAdoSQLQuoteState = class (TZQuoteState)
64  public
65  function NextToken(Stream: TStream; FirstChar: Char;
66  Tokenizer: TZTokenizer): TZToken; override;
67 
68  function EncodeString(const Value: string; QuoteChar: Char): string; override;
69  function DecodeString(const Value: string; QuoteChar: Char): string; override;
70  end;
71 
72  {** Implements a default tokenizer object. }
73  TZAdoSQLTokenizer = class (TZGenericSQLTokenizer)
74  public
75  constructor Create;
76  end;
77 
78 implementation
79 
80 { TZAdoSQLQuoteState }
81 
82 {**
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.
86 
87  @return a quoted string token from a reader
88 }
89 function TZAdoSQLQuoteState.NextToken(Stream: TStream; FirstChar: Char;
90  Tokenizer: TZTokenizer): TZToken;
91 var
92  ReadChar: Char;
93  LastChar: Char;
94 begin
95  Result.Value := FirstChar;
96  LastChar := #0;
97  while Stream.Read(ReadChar, SizeOf(Char)) > 0 do
98  begin
99  if ((LastChar = FirstChar) and (ReadChar <> FirstChar)
100  and (FirstChar <> '[')) or ((FirstChar = '[') and (LastChar = ']')) then
101  begin
102  Stream.Seek(-SizeOf(Char), soFromCurrent);
103  Break;
104  end;
105  Result.Value := Result.Value + ReadChar;
106  if (LastChar = FirstChar) and (ReadChar = FirstChar) then
107  LastChar := #0
108  else LastChar := ReadChar;
109  end;
110 
111  if CharInSet(FirstChar, ['"', '[']) then
112  Result.TokenType := ttQuotedIdentifier
113  else Result.TokenType := ttQuoted;
114 end;
115 
116 {**
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.
121 }
122 function TZAdoSQLQuoteState.EncodeString(const Value: string; QuoteChar: Char): string;
123 begin
124  if QuoteChar = '[' then
125  Result := '[' + Value + ']'
126  else if CharInSet(QuoteChar, [#39, '"']) then
127  Result := QuoteChar + Value + QuoteChar
128  else Result := Value;
129 end;
130 
131 {**
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.
136 }
137 function TZAdoSQLQuoteState.DecodeString(const Value: string; QuoteChar: Char): string;
138 begin
139  Result := Value;
140  if Length(Value) >= 2 then
141  begin
142  if CharInSet(QuoteChar, [#39, '"']) and (Value[1] = QuoteChar)
143  and (Value[Length(Value)] = QuoteChar) then
144  begin
145  if Length(Value) > 2 then
146  Result := AnsiDequotedStr(Value, QuoteChar)
147  else Result := '';
148  end
149  else if (QuoteChar = '[') and (Value[1] = QuoteChar)
150  and (Value[Length(Value)] = ']') then
151  Result := Copy(Value, 2, Length(Value) - 2)
152  end;
153 end;
154 
155 
156 { TZAdoSQLTokenizer }
157 {**
158  Constructs a tokenizer with a default state table (as
159  described in the class comment).
160 }
161 constructor TZAdoSQLTokenizer.Create;
162 begin
163  EscapeState := TZEscapeState.Create;
164  NumberState := TZNumberState.Create;
165  QuoteState := TZAdoSQLQuoteState.Create;
166  WhitespaceState := TZWhitespaceState.Create;
167  CommentState := TZCppCommentState.Create;
168 
169  SymbolState := TZGenericSQLSymbolState.Create;
170  WordState := TZGenericSQLWordState.Create;
171 
172  SetCharacterState(#0, #32, WhitespaceState);
173  SetCharacterState(#33, #191, SymbolState);
174  SetCharacterState(#192, High(Char), WordState);
175 
176  SetCharacterState('a', 'z', WordState);
177  SetCharacterState('A', 'Z', WordState);
178  SetCharacterState('_', '_', WordState);
179  SetCharacterState('$', '$', WordState);
180  SetCharacterState('@', '@', WordState);
181 
182  SetCharacterState('0', '9', NumberState);
183  SetCharacterState('.', '.', NumberState);
184 
185  SetCharacterState('"', '"', QuoteState);
186  SetCharacterState(#39, #39, QuoteState);
187  SetCharacterState('[', '[', QuoteState);
188  SetCharacterState(']', ']', QuoteState);
189 
190  SetCharacterState('/', '/', CommentState);
191 
192 end;
193 
194 end.