Index: ScriptRunner.cs
===================================================================
--- ScriptRunner.cs (revision 381)
+++ ScriptRunner.cs (working copy)
@@ -25,6 +25,7 @@
#endregion
using System;
+using System.Collections;
using System.Data;
using System.IO;
@@ -49,39 +50,95 @@
///
/// The dataSouce that will be used to run the script.
/// a path to an sql script file.
- public void RunScript(DataSource dataSource, string sqlScriptPath)
+ public void RunScript(DataSource dataSource, string sqlScriptPath) {
+ RunScript(dataSource, sqlScriptPath, true);
+ }
+
+ ///
+ /// Run an sql script
+ ///
+ /// The dataSouce that will be used to run the script.
+ /// a path to an sql script file.
+ /// parse out the statements in the sql script file.
+ public void RunScript(DataSource dataSource, string sqlScriptPath, bool doParse)
{
+ // Get script file
+ FileInfo fi = new FileInfo(sqlScriptPath);
+ string script = fi.OpenText().ReadToEnd();
+
+ ArrayList sqlStatements = new ArrayList();
+
+ if (doParse) {
+ switch(dataSource.Provider.Name) {
+ case "oracle9.2":
+ case "oracleClient":
+ sqlStatements = ParseScript(script);
+ break;
+ default:
+ sqlStatements.Add(script);
+ break;
+ }
+ }
+ else {
+ sqlStatements.Add(script);
+ }
+
+ try {
+ ExecuteStatements(dataSource, sqlStatements);
+ }
+ catch(System.Exception e) {
+ throw new IBatisNetException("Unable to execute the sql: " + fi.Name, e);
+ }
+ }
+
+ ///
+ /// Execute the given sql statements
+ ///
+ /// The dataSouce that will be used.
+ /// An ArrayList of sql statements to execute.
+ private void ExecuteStatements(DataSource dataSource, ArrayList sqlStatements) {
IDbConnection connection = dataSource.Provider.GetConnection();
-
connection.ConnectionString = dataSource.ConnectionString;
connection.Open();
IDbTransaction transaction = connection.BeginTransaction();
-
- // Create database structure
- FileInfo fi = new FileInfo(sqlScriptPath);
-
- string creationScript = fi.OpenText().ReadToEnd();
-
+
IDbCommand command = dataSource.Provider.GetCommand();
command.Connection = connection;
- command.Transaction = transaction;
+ command.Transaction = transaction;
- command.CommandText = creationScript;
-
- try
- {
- command.ExecuteNonQuery();
+ try {
+ foreach (string sqlStatement in sqlStatements) {
+ command.CommandText = sqlStatement;
+ command.ExecuteNonQuery();
+ }
transaction.Commit();
}
- catch(System.Exception e)
- {
+ catch(System.Exception e) {
transaction.Rollback();
- throw new IBatisNetException("Unable to execute the sql script " + sqlScriptPath, e);
+ throw (e);
}
- finally
- {
+ finally {
connection.Close();
}
}
+
+ ///
+ /// Parse and tokenize the sql script into multiple statements
+ ///
+ /// the script to parse
+ private ArrayList ParseScript(string script) {
+ ArrayList statements = new ArrayList();
+ StringTokenizer parser = new StringTokenizer(script, ";");
+ IEnumerator enumerator = parser.GetEnumerator();
+
+ while (enumerator.MoveNext()) {
+ string statement= ((string)enumerator.Current).Replace("\r\n",string.Empty);
+ if (statement != string.Empty) {
+ statements.Add(statement);
+ }
+ }
+
+ return statements;
+ }
}
}