Clover.NET coverage report - Coverage

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

File Stats: LOC: 278   Methods: 5
NCLOC: 171 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
Configuration\Statements\SqlGenerator.cs 92.9 % 97.5 % 100.0 % 96.1 %
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  
28   using System;
29   using System.Text;
30   using System.Collections.Specialized;
31  
32   using IBatisNet.DataMapper.Configuration.ParameterMapping;
33  
34   namespace IBatisNet.DataMapper.Configuration.Statements
35   {
36   /// <summary>
37   /// Summary description for SqlGenerator.
38   /// </summary>
39   public class SqlGenerator
40   {
41   /// <summary>
42   /// Creates SQL command text for a specified statement
43   /// </summary>
44   /// <param name="statement">The statement to build the SQL command text.</param>
45   /// <returns>The SQL command text for the statement.</returns>
46 850 public static string BuildQuery(IStatement statement)
47   {
48 850 string sqlText = string.Empty;
49  
50 850 if (statement is Select)
51   {
52 340 sqlText = BuildSelectQuery(statement);
53   }
54 510 else if (statement is Insert)
55   {
56 170 sqlText = BuildInsertQuery(statement);
57   }
58 340 else if (statement is Update)
59   {
60 170 sqlText = BuildUpdateQuery(statement);
61   }
62 170 else if (statement is Delete)
63   {
64 170 sqlText = BuildDeleteQuery(statement);
65   }
66  
67 850 return sqlText;
68   }
69  
70  
71   /// <summary>
72   /// Creates an select SQL command text for a specified statement
73   /// </summary>
74   /// <param name="statement">The statement to build the SQL command text.</param>
75   /// <returns>The SQL command text for the statement.</returns>
76 340 private static string BuildSelectQuery(IStatement statement)
77   {
78 340 StringBuilder output = new StringBuilder();
79 340 Select select = (Select) statement;
80 340 int columnCount = statement.ParameterMap.PropertiesList.Count;
81  
82 340 output.Append("SELECT ");
83  
84   // Create the list of columns
85 1360 for (int i = 0; i < columnCount; i++)
86   {
87 1020 ParameterProperty property = (ParameterProperty) statement.ParameterMap.PropertiesList[i];
88  
89 1020 if (i < (columnCount - 1))
90   {
91 680 output.Append("\t" + property.ColumnName + " as "+property.PropertyName+",");
92   }
93   else
94   {
95 340 output.Append("\t" + property.ColumnName + " as "+property.PropertyName);
96   }
97   }
98  
99 340 output.Append(" FROM ");
100 340 output.Append("\t" + select.Generate.Table + "");
101  
102   // Create the where clause
103  
104 340 string [] compositeKeyList = select.Generate.By.Split(new Char [] {','});
105 340 if (compositeKeyList.Length > 0 && select.Generate.By.Length>0)
106   {
107 170 output.Append(" WHERE ");
108  
109 340 for (int i = 0; i < compositeKeyList.Length; i++)
110   {
111 170 string columnName = compositeKeyList[i];
112  
113 170 if (i > 0)
114   {
115 0 output.Append("\tAND " + columnName + " = ?" );
116   }
117   else
118   {
119 170 output.Append("\t" + columnName + " = ?" );
120   }
121   }
122   }
123  
124   // 'Select All' case
125 340 if (statement.ParameterClass == null)
126   {
127   // The ParameterMap is just used to build the query
128   // to avoid problems later, we set it to null
129 170 statement.ParameterMap = null;
130   }
131  
132 340 return output.ToString();
133   }
134  
135  
136   /// <summary>
137   /// Creates an insert SQL command text for a specified statement
138   /// </summary>
139   /// <param name="statement">The statement to build the SQL command text.</param>
140   /// <returns>The SQL command text for the statement.</returns>
141 170 private static string BuildInsertQuery(IStatement statement)
142   {
143 170 StringBuilder output = new StringBuilder();
144 170 Insert insert = (Insert) statement;
145 170 int columnCount = statement.ParameterMap.PropertiesList.Count;
146  
147 170 output.Append("INSERT INTO " + insert.Generate.Table + " (");
148  
149   // Create the parameter list
150 510 for (int i = 0; i < columnCount; i++)
151   {
152 340 ParameterProperty property = (ParameterProperty) statement.ParameterMap.PropertiesList[i];
153  
154   // Append the column name as a parameter of the insert statement
155 340 if (i < (columnCount - 1))
156   {
157 170 output.Append("\t" + property.ColumnName + ",");
158   }
159   else
160   {
161 170 output.Append("\t" + property.ColumnName + "");
162   }
163   }
164  
165 170 output.Append(") VALUES (");
166  
167   // Create the values list
168 510 for (int i = 0; i < columnCount; i++)
169   {
170 340 ParameterProperty property = (ParameterProperty) statement.ParameterMap.PropertiesList[i];
171  
172   // Append the necessary line breaks and commas
173 340 if (i < (columnCount - 1))
174   {
175 170 output.Append("\t?,");
176   }
177   else
178   {
179 170 output.Append("\t?");
180   }
181   }
182  
183 170 output.Append(")");
184  
185 170 return output.ToString();
186   }
187  
188  
189   /// <summary>
190   /// Creates an update SQL command text for a specified statement
191   /// </summary>
192   /// <param name="statement">The statement to build the SQL command text.</param>
193   /// <returns>The SQL command text for the statement.</returns>
194 170 private static string BuildUpdateQuery(IStatement statement)
195   {
196 170 StringBuilder output = new StringBuilder();
197 170 Update update = (Update) statement;
198 170 int columnCount = statement.ParameterMap.PropertiesList.Count;
199 170 string[] keysList = update.Generate.By.Split(',');
200  
201 170 output.Append("UPDATE ");
202 170 output.Append("\t" + update.Generate.Table + " ");
203 170 output.Append("SET ");
204  
205   // Create the set statement
206 680 for (int i = 0; i < columnCount; i++)
207   {
208 510 ParameterProperty property = (ParameterProperty) statement.ParameterMap.PropertiesList[i];
209  
210   // Ignore key columns
211 510 if (update.Generate.By.IndexOf(property.ColumnName) < 0)
212   {
213 340 if (i < (columnCount-keysList.Length - 1))
214   {
215 170 output.Append("\t" + property.ColumnName + " = ?,");
216   }
217   else
218   {
219 170 output.Append("\t" + property.ColumnName + " = ? ");
220   }
221   }
222   }
223  
224 170 output.Append(" WHERE ");
225  
226   // Create the where clause
227 340 for (int i = 0; i < keysList.Length; i++)
228   {
229 170 string columnName = keysList[i];
230  
231 170 if (i > 0)
232   {
233 0 output.Append("\tAND " + columnName + " = ?");
234   }
235   else
236   {
237 170 output.Append("\t " + columnName + " = ?");
238   }
239   }
240  
241 170 return output.ToString();
242   }
243  
244   /// <summary>
245   /// Creates an delete SQL command text for a specified statement
246   /// </summary>
247   /// <param name="statement">The statement to build the SQL command text.</param>
248   /// <returns>The SQL command text for the statement.</returns>
249 170 private static string BuildDeleteQuery(IStatement statement)
250   {
251 170 StringBuilder output = new StringBuilder();
252 170 Delete delete = (Delete) statement;
253 170 string[] keysList = delete.Generate.By.Split(',');
254  
255 170 output.Append("DELETE FROM");
256 170 output.Append("\t" + delete.Generate.Table + "");
257 170 output.Append(" WHERE ");
258  
259   // Create the where clause
260 510 for (int i = 0; i < keysList.Length; i++)
261   {
262 340 string columnName = keysList[i].Trim();
263  
264 340 if (i > 0)
265   {
266 170 output.Append("\tAND " + columnName + " = ?");
267   }
268   else
269   {
270 170 output.Append("\t " + columnName + " = ?");
271   }
272   }
273  
274 170 return output.ToString();
275   }
276   }
277   }
278