Clover.NET coverage report - Coverage

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

File Stats: LOC: 187   Methods: 5
NCLOC: 119 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
Utilities\ScriptRunner.cs 20.0 % 46.3 % 60.0 % 43.5 %
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   using System;
28   using System.Collections;
29   using System.Data;
30   using System.IO;
31  
32   using IBatisNet.Common.Exceptions;
33  
34   namespace IBatisNet.Common.Utilities
35   {
36   /// <summary>
37   /// Description résumée de ScriptRunner.
38   /// </summary>
39   public class ScriptRunner
40   {
41   /// <summary>
42   /// Constructor
43   /// </summary>
44 708 public ScriptRunner()
45   {
46   }
47  
48   /// <summary>
49   /// Run an sql script
50   /// </summary>
51   /// <param name="dataSource">The dataSouce that will be used to run the script.</param>
52   /// <param name="sqlScriptPath">a path to an sql script file.</param>
53 0 public void RunScript(DataSource dataSource, string sqlScriptPath) {
54   RunScript(dataSource, sqlScriptPath, true);
55   }
56  
57   /// <summary>
58   /// Run an sql script
59   /// </summary>
60   /// <param name="dataSource">The dataSouce that will be used to run the script.</param>
61   /// <param name="sqlScriptPath">a path to an sql script file.</param>
62   /// <param name="doParse">parse out the statements in the sql script file.</param>
63 708 public void RunScript(DataSource dataSource, string sqlScriptPath, bool doParse)
64   {
65   // Get script file
66 708 FileInfo fi = new FileInfo(sqlScriptPath);
67 708 string script = fi.OpenText().ReadToEnd();
68  
69 708 ArrayList sqlStatements = new ArrayList();
70  
71 708 if (doParse) {
72 609 switch(dataSource.Provider.Name) {
73   case "oracle9.2":
74   case "oracle10.1":
75   case "oracleClient1.0":
76   case "ByteFx":
77   case "MySql":
78 0 sqlStatements = ParseScript(script);
79 0 break;
80   case "OleDb1.1":
81 0 if (dataSource.ConnectionString.IndexOf("Microsoft.Jet.OLEDB")>0)
82   {
83   // Access
84   sqlStatements = ParseScript(script);
85   }
86   else
87   {
88   sqlStatements.Add(script);
89   }
90 0 break;
91   default:
92 609 sqlStatements.Add(script);
93 609 break;
94   }
95   }
96   else {
97 99 switch(dataSource.Provider.Name) {
98   case "oracle9.2":
99   case "oracle10.1":
100   case "oracleClient1.0":
101   case "ByteFx":
102   case "MySql":
103 0 script = script.Replace("\r\n"," ");
104 0 script = script.Replace("\t"," ");
105 0 sqlStatements.Add(script);
106 0 break;
107   case "OleDb1.1":
108 0 if (dataSource.ConnectionString.IndexOf("Microsoft.Jet.OLEDB")>0)
109   {
110   // Access
111   script = script.Replace("\r\n"," ");
112   script = script.Replace("\t"," ");
113   sqlStatements.Add(script);
114   }
115   else
116   {
117   sqlStatements.Add(script);
118   }
119 0 break;
120   default:
121 99 sqlStatements.Add(script);
122 99 break;
123   }
124   }
125  
126 708 try {
127 708 ExecuteStatements(dataSource, sqlStatements);
128   }
129   catch(System.Exception e) {
130 0 throw new IBatisNetException("Unable to execute the sql: " + fi.Name, e);
131   }
132   }
133  
134   /// <summary>
135   /// Execute the given sql statements
136   /// </summary>
137   /// <param name="dataSource">The dataSouce that will be used.</param>
138   /// <param name="sqlStatements">An ArrayList of sql statements to execute.</param>
139 708 private void ExecuteStatements(DataSource dataSource, ArrayList sqlStatements) {
140 708 IDbConnection connection = dataSource.Provider.GetConnection();
141 708 connection.ConnectionString = dataSource.ConnectionString;
142 708 connection.Open();
143 708 IDbTransaction transaction = connection.BeginTransaction();
144  
145 708 IDbCommand command = dataSource.Provider.GetCommand();
146  
147 708 command.Connection = connection;
148 708 command.Transaction = transaction;
149  
150 708 try {
151 708 foreach (string sqlStatement in sqlStatements) {
152 708 command.CommandText = sqlStatement;
153 708 command.ExecuteNonQuery();
154   }
155 708 transaction.Commit();
156   }
157   catch(System.Exception e) {
158 0 transaction.Rollback();
159 0 throw (e);
160   }
161   finally {
162 708 connection.Close();
163   }
164   }
165  
166   /// <summary>
167   /// Parse and tokenize the sql script into multiple statements
168   /// </summary>
169   /// <param name="script">the script to parse</param>
170 0 private ArrayList ParseScript(string script) {
171   ArrayList statements = new ArrayList();
172   StringTokenizer parser = new StringTokenizer(script, ";");
173   IEnumerator enumerator = parser.GetEnumerator();
174  
175   while (enumerator.MoveNext()) {
176   string statement= ((string)enumerator.Current).Replace("\r\n"," ");
177   statement = statement.Trim();
178   if (statement != string.Empty) {
179   statements.Add(statement);
180   }
181   }
182  
183   return statements;
184   }
185   }
186   }
187