Clover.NET coverage report - Coverage for s2dao.net

Coverage timestamp: 2006年5月18日 15:09:15

File Stats: LOC: 298   Methods: 18
NCLOC: 249 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
Seasar.Dao.Parser\SqlTokenizerImpl.cs 93.2% 92.5% 83.3% 91.8%
coverage coverage
1   #region Copyright
2   /*
3   * Copyright 2005 the Seasar Foundation and the Others.
4   *
5   * Licensed under the Apache License, Version 2.0 (the "License");
6   * you may not use this file except in compliance with the License.
7   * You may obtain a copy of the License at
8   *
9   * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14   * either express or implied. See the License for the specific language
15   * governing permissions and limitations under the License.
16   */
17   #endregion
18  
19   using System;
20  
21   namespace Seasar.Dao.Parser
22   {
23   public class SqlTokenizerImpl : ISqlTokenizer
24   {
25   private string sql;
26   private int position = 0;
27   private string token;
28   private TokenType tokenType = TokenType.SQL;
29   private TokenType nextTokenType = TokenType.SQL;
30   private int bindVariableNum = 0;
31  
32 101 public SqlTokenizerImpl(string sql)
33   {
34 101 this.sql = sql;
35   }
36  
37   #region ISqlTokenizer メンバ
38  
39   public string Token
40   {
41 360 get
42   {
43 360 return token;
44   }
45   }
46  
47   public string Before
48   {
49 3 get
50   {
51 3 return sql.Substring(0, position);
52   }
53   }
54  
55   public string After
56   {
57 3 get
58   {
59 3 return sql.Substring(position);
60   }
61   }
62  
63 0 public int Position
64   {
65   get
66   {
67   return position;
68   }
69   }
70  
71   public TokenType TokenType
72   {
73 281 get
74   {
75 281 return tokenType;
76   }
77   }
78  
79 0 public TokenType NextTokenType
80   {
81   get { return NextTokenType; }
82   }
83  
84 391 public TokenType Next()
85   {
86 391 if(position >= sql.Length)
87   {
88 81 token = null;
89 81 tokenType = TokenType.EOF;
90 81 nextTokenType = TokenType.EOF;
91 81 return tokenType;
92   }
93 310 switch(nextTokenType)
94   {
95   case TokenType.SQL:
96 173 ParseSql();
97 173 break;
98   case TokenType.COMMENT:
99 109 ParseComment();
100 107 break;
101   case TokenType.ELSE:
102 8 ParseElse();
103 8 break;
104   case TokenType.BIND_VARIABLE:
105 20 ParseBindVariable();
106 20 break;
107   default:
108 0 ParseEof();
109 0 break;
110   }
111 308 return tokenType;
112   }
113  
114 87 public string SkipToken()
115   {
116 87 int index = sql.Length;
117 87 char quote = position < sql.Length ? sql.ToCharArray()[position] : '\0';
118 87 bool quoting = quote == '\'' || quote =='(';
119 87 if(quote == '(') quote = ')';
120  
121 522 for(int i = quoting ? position + 1 : position; i < sql.Length; ++i)
122   {
123 490 char c = sql.ToCharArray()[i];
124 490 if((Char.IsWhiteSpace(c) || c == ',' || c == ')' || c == '(')
125   && !quoting)
126   {
127 19 index = i;
128 19 break;
129   }
130 471 else if(c == '/' && i + 1 < sql.Length
131   && sql.ToCharArray()[i + 1] == '*')
132   {
133 4 index = i;
134 4 break;
135   }
136 467 else if(c == '-' && i + 1 < sql.Length
137   && sql.ToCharArray()[i + 1] == '-')
138   {
139 0 index = i;
140 0 break;
141   }
142 467 else if(quoting && quote == '\'' && c == '\''
143   && (i + 1 >= sql.Length || sql.ToCharArray()[i + 1] != '\''))
144   {
145 14 index = i + 1;
146 14 break;
147   }
148 453 else if(quoting && c == quote)
149   {
150 18 index = i + 1;
151 18 break;
152   }
153   }
154 87 token = sql.Substring(position, (index - position));
155 87 tokenType = TokenType.SQL;
156 87 nextTokenType = TokenType.SQL;
157 87 position = index;
158 87 return token;
159   }
160  
161 40 public string SkipWhitespace()
162   {
163 40 int index = SkipWhitespace(position);
164 40 token = sql.Substring(position, (index - position));
165 40 position = index;
166 40 return token;
167   }
168  
169   #endregion
170  
171 173 protected void ParseSql()
172   {
173 173 int commentStartPos = sql.IndexOf("/*", position);
174 173 int lineCommentStartPos = sql.IndexOf("--", position);
175 173 int bindVariableStartPos = sql.IndexOf("?", position);
176 173 int elseCommentStartPos = -1;
177 173 int elseCommentLength = -1;
178  
179 173 if(bindVariableStartPos < 0)
180 155 bindVariableStartPos = sql.IndexOf("@", position);
181 173 if(lineCommentStartPos >= 0)
182   {
183 19 int skipPos = SkipWhitespace(lineCommentStartPos + 2);
184 19 if(skipPos + 4 < sql.Length
185   && "ELSE" == sql.Substring(skipPos, ((skipPos + 4) - skipPos)))
186   {
187 19 elseCommentStartPos = lineCommentStartPos;
188 19 elseCommentLength = skipPos + 4 - lineCommentStartPos;
189   }
190   }
191 173 int nextStartPos = GetNextStartPos(commentStartPos,
192   elseCommentStartPos, bindVariableStartPos);
193 173 if(nextStartPos < 0)
194   {
195 36 token = sql.Substring(position);
196 36 nextTokenType = TokenType.EOF;
197 36 position = sql.Length;
198 36 tokenType = TokenType.SQL;
199   }
200   else
201   {
202 137 token = sql.Substring(position, nextStartPos - position);
203 137 tokenType = TokenType.SQL;
204 137 bool needNext = nextStartPos == position;
205 137 if(nextStartPos == commentStartPos)
206   {
207 109 nextTokenType = TokenType.COMMENT;
208 109 position = commentStartPos + 2;
209   }
210 28 else if(nextStartPos == elseCommentStartPos)
211   {
212 8 nextTokenType = TokenType.ELSE;
213 8 position = elseCommentStartPos + elseCommentLength;
214   }
215 20 else if(nextStartPos == bindVariableStartPos)
216   {
217 20 nextTokenType = TokenType.BIND_VARIABLE;
218 20 position = bindVariableStartPos;
219   }
220 137 if(needNext) Next();
221   }
222   }
223  
224 173 protected int GetNextStartPos(int commentStartPos, int elseCommentStartPos,
225   int bindVariableStartPos)
226   {
227 173 int nextStartPos = -1;
228 173 if(commentStartPos >= 0)
229 117 nextStartPos = commentStartPos;
230  
231 173 if(elseCommentStartPos >= 0
232   && (nextStartPos < 0 || elseCommentStartPos < nextStartPos))
233 8 nextStartPos = elseCommentStartPos;
234  
235 173 if(bindVariableStartPos >= 0
236   && (nextStartPos < 0 || bindVariableStartPos < nextStartPos))
237 20 nextStartPos = bindVariableStartPos;
238  
239 173 return nextStartPos;
240   }
241  
242   protected string NextBindVariableName
243   {
244 20 get { return "$" + ++bindVariableNum; }
245   }
246  
247 109 protected void ParseComment()
248   {
249 109 int commentEndPos = sql.IndexOf("*/", position);
250 109 if(commentEndPos < 0)
251 2 throw new TokenNotClosedRuntimeException("*/",
252   sql.Substring(position));
253  
254 107 token = sql.Substring(position, (commentEndPos - position));
255 107 nextTokenType = TokenType.SQL;
256 107 position = commentEndPos + 2;
257 107 tokenType = TokenType.COMMENT;
258   }
259  
260 20 protected void ParseBindVariable()
261   {
262 20 token = NextBindVariableName;
263 20 nextTokenType = TokenType.SQL;
264 20 position++;
265 20 tokenType = TokenType.BIND_VARIABLE;
266   }
267  
268 8 protected void ParseElse()
269   {
270 8 token = null;
271 8 nextTokenType = TokenType.SQL;
272 8 tokenType = TokenType.ELSE;
273   }
274  
275 0 protected void ParseEof()
276   {
277   token = null;
278   tokenType = TokenType.EOF;
279   nextTokenType = TokenType.EOF;
280   }
281  
282 59 private int SkipWhitespace(int position)
283   {
284 59 int index = sql.Length;
285 88 for(int i = position; i < sql.Length; ++i)
286   {
287 80 char c = sql.ToCharArray()[i];
288 80 if(!Char.IsWhiteSpace(c))
289   {
290 51 index = i;
291 51 break;
292   }
293   }
294 59 return index;
295   }
296   }
297   }
298