Clover.NET coverage report - Coverage

Coverage timestamp: Friday, May 20, 2005 9:17:00 PM

File Stats: LOC: 210   Methods: 6
NCLOC: 117 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
Configuration\Sql\SimpleDynamic\SimpleDynamicSql.cs 75.0 % 92.7 % 100.0 % 88.9 %
coverage coverage
1  
2   #region Apache Notice
3   /*****************************************************************************
4   * $Header: $
5   * $Revision: $
6   * $Date: $
7   *
8   * iBATIS.NET Data Mapper
9   * Copyright (C) 2004 - Gilles Bayon
10   *
11   *
12   * Licensed under the Apache License, Version 2.0 (the "License");
13   * you may not use this file except in compliance with the License.
14   * You may obtain a copy of the License at
15   *
16   * http://www.apache.org/licenses/LICENSE-2.0
17   *
18   * Unless required by applicable law or agreed to in writing, software
19   * distributed under the License is distributed on an "AS IS" BASIS,
20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   * See the License for the specific language governing permissions and
22   * limitations under the License.
23   *
24   ********************************************************************************/
25   #endregion
26  
27   #region Imports
28   using System;
29   using System.Collections;
30   using System.Text;
31  
32   using IBatisNet.Common;
33   using IBatisNet.DataMapper.Configuration.Sql;
34   using IBatisNet.DataMapper.Configuration.Statements;
35   using IBatisNet.DataMapper.Scope;
36   using IBatisNet.DataMapper.Exceptions;
37   using IBatisNet.DataMapper.TypeHandlers;
38   using IBatisNet.Common.Utilities;
39   using IBatisNet.Common.Utilities.Objects;
40   #endregion
41  
42  
43   namespace IBatisNet.DataMapper.Configuration.Sql.SimpleDynamic
44   {
45   /// <summary>
46   /// Summary description for SimpleDynamicSql.
47   /// </summary>
48   internal class SimpleDynamicSql : ISql
49   {
50  
51   #region private
52  
53   private const string ELEMENT_TOKEN = "$";
54  
55   private string _simpleSqlStatement = string.Empty;
56   private IStatement _statement = null ;
57   private TypeHandlerFactory _typeHandlerFactory = null;
58  
59   #endregion
60  
61   #region Constructor (s) / Destructor
62   /// <summary>
63   /// Constructor
64   /// </summary>
65   /// <param name="sqlStatement">The sql statement.</param>
66   /// <param name="statement"></param>
67   /// <param name="typeHandlerFactory"></param>
68 854 internal SimpleDynamicSql(TypeHandlerFactory typeHandlerFactory,string sqlStatement, IStatement statement)
69   {
70 854 _simpleSqlStatement = sqlStatement;
71 854 _statement = statement;
72 854 _typeHandlerFactory = typeHandlerFactory;
73   }
74   #endregion
75  
76  
77   #region Methods
78   /// <summary>
79   ///
80   /// </summary>
81   /// <param name="parameterObject"></param>
82   /// <returns></returns>
83 4 public string GetSql(object parameterObject)
84   {
85 4 return ProcessDynamicElements(parameterObject);
86   }
87  
88   /// <summary>
89   ///
90   /// </summary>
91   /// <param name="sqlStatement"></param>
92   /// <returns></returns>
93 20279 public static bool IsSimpleDynamicSql(string sqlStatement)
94   {
95 20279 return ( (sqlStatement != null) && (sqlStatement.IndexOf(ELEMENT_TOKEN) > -1) );
96   }
97  
98   /// <summary>
99   ///
100   /// </summary>
101   /// <param name="parameterObject"></param>
102   /// <returns></returns>
103 9 private string ProcessDynamicElements(object parameterObject)
104   {
105   // define which character is seperating fields
106 9 char[] splitter = ELEMENT_TOKEN.ToCharArray();
107  
108 9 StringTokenizer parser = new StringTokenizer(_simpleSqlStatement, ELEMENT_TOKEN, true);
109  
110 9 StringBuilder newSql = new StringBuilder();
111  
112 9 string token = null;
113 9 string lastToken = null;
114  
115 9 IEnumerator enumerator = parser.GetEnumerator();
116  
117 55 while (enumerator.MoveNext())
118   {
119 46 token = ((string)enumerator.Current);
120  
121 46 if (ELEMENT_TOKEN.Equals(lastToken))
122   {
123 13 if (ELEMENT_TOKEN.Equals(token))
124   {
125 0 newSql.Append(ELEMENT_TOKEN);
126 0 token = null;
127   }
128   else
129   {
130 13 object value = null;
131 13 if (parameterObject != null)
132   {
133 13 if ( _typeHandlerFactory.IsSimpleType( parameterObject.GetType() ) == true)
134   {
135 4 value = parameterObject;
136   }
137   else
138   {
139 9 value = ObjectProbe.GetPropertyValue(parameterObject, token);
140   }
141   }
142 13 if (value != null)
143   {
144 13 newSql.Append(value.ToString());
145   }
146  
147 13 enumerator.MoveNext();
148 13 token = ((string)enumerator.Current);
149  
150 13 if (!ELEMENT_TOKEN.Equals(token))
151   {
152 0 throw new DataMapperException("Unterminated dynamic element in sql (" + _simpleSqlStatement + ").");
153   }
154 13 token = null;
155   }
156   }
157   else
158   {
159 33 if (!ELEMENT_TOKEN.Equals(token))
160   {
161 20 newSql.Append(token);
162   }
163   }
164  
165 46 lastToken = token;
166   }
167  
168 9 return newSql.ToString();
169   }
170  
171  
172   #region ISql Members
173  
174   /// <summary>
175   ///
176   /// </summary>
177   /// <param name="parameterObject"></param>
178   /// <param name="session"></param>
179   /// <returns></returns>
180 5 public RequestScope GetRequestScope(object parameterObject, IDalSession session)
181   {
182 5 string sqlStatement = ProcessDynamicElements(parameterObject);
183  
184 5 RequestScope request = new RequestScope();
185  
186 5 request.ParameterMap = _statement.ParameterMap;
187 5 request.ResultMap = _statement.ResultMap;
188 5 request.PreparedStatement = BuildPreparedStatement(session, request, sqlStatement);
189  
190 5 return request;
191   }
192  
193   /// <summary>
194   /// Build the PreparedStatement
195   /// </summary>
196   /// <param name="session"></param>
197   /// <param name="request"></param>
198   /// <param name="sqlStatement"></param>
199 5 private PreparedStatement BuildPreparedStatement(IDalSession session, RequestScope request, string sqlStatement)
200   {
201 5 PreparedStatementFactory factory = new PreparedStatementFactory( session, request, _statement, sqlStatement);
202 5 return factory.Prepare();
203   }
204   #endregion
205  
206   #endregion
207  
208   }
209   }
210