Fixing Brail Templates for VS Net wizards.
[castle.git] / ActiveRecord / Castle.ActiveRecord / Framework / ARSchemaCreator.cs
blob696503879d601e2ec88deee4394b08eadc3f913f
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.ActiveRecord.Framework.Internal
17 using System;
18 using System.Collections;
19 using System.Collections.Specialized;
20 using System.Data;
21 using System.Diagnostics;
22 using System.IO;
24 using NHibernate.Cfg;
25 using NHibernate.Connection;
26 using NHibernate.Dialect;
28 /// <summary>
29 /// Used to execute a script file to create/update/drop
30 /// a database schema. Inspired on NHibernate SchemaExport class.
31 /// </summary>
32 public class ARSchemaCreator
34 private readonly IDictionary connectionProperties;
35 private readonly Dialect dialect;
37 /// <summary>
38 /// Initializes a new instance of the <see cref="ARSchemaCreator"/> class.
39 /// </summary>
40 /// <param name="config">The config.</param>
41 public ARSchemaCreator(Configuration config)
43 this.connectionProperties = config.Properties;
44 this.dialect = Dialect.GetDialect(connectionProperties);
47 /// <summary>
48 /// Executes the specified script file.
49 /// </summary>
50 /// <param name="scriptFileName">Name of the script file.</param>
51 public void Execute(String scriptFileName)
53 String[] parts = OpenFileAndStripContents(scriptFileName);
55 ExecuteScript(parts);
58 private void ExecuteScript(String[] parts)
60 IConnectionProvider connectionProvider =
61 ConnectionProviderFactory.NewConnectionProvider(CreateConnectionProperties());
63 try
65 using(IDbConnection connection = connectionProvider.GetConnection())
67 ExecuteScriptParts(connection, parts);
70 catch(Exception ex)
72 throw new ActiveRecordException("Could not execute schema script", ex);
76 /// <summary>
77 /// Executes the script parts.
78 /// </summary>
79 /// <param name="connection">The connection.</param>
80 /// <param name="parts">The parts.</param>
81 public static void ExecuteScriptParts(IDbConnection connection, String[] parts)
83 using(IDbCommand statement = connection.CreateCommand())
85 foreach(String part in parts)
87 try
89 statement.CommandText = part;
90 statement.CommandType = CommandType.Text;
91 statement.ExecuteNonQuery();
93 catch(Exception ex)
95 // Ignored, but we output it
97 Console.WriteLine(String.Format("SQL: {0} \r\nthrew {1}. Ignoring", part, ex.Message));
103 /// <summary>
104 /// Opens the file and return an array of seperate commands that it contains
105 /// </summary>
106 /// <param name="scriptFileName">Name of the script file.</param>
107 /// <returns></returns>
108 public static String[] OpenFileAndStripContents(String scriptFileName)
110 if (scriptFileName == null)
112 throw new ArgumentNullException("scriptFileName");
115 scriptFileName = Normalize(scriptFileName);
117 if (!File.Exists(scriptFileName))
119 throw new ArgumentException("File name could not be found: " + scriptFileName, "scriptFileName");
122 using(StreamReader reader = new StreamReader(File.OpenRead(scriptFileName)))
124 String contents = reader.ReadToEnd();
126 String[] parts = contents.Split(';');
128 if (parts.Length == 1)
130 parts = SplitString(contents, "GO");
133 return parts;
137 private static String[] SplitString(String sqlScript, String split)
139 ArrayList parts = new ArrayList();
141 int searchFrom = -1;
142 int lastIndex = 0;
144 for(;; )
146 searchFrom = sqlScript.IndexOf(split, searchFrom + 1);
148 if (searchFrom != -1)
150 parts.Add(sqlScript.Substring(lastIndex, searchFrom - lastIndex));
152 lastIndex = searchFrom + split.Length;
154 else
156 if (searchFrom == -1 && lastIndex != 0)
158 parts.Add(sqlScript.Substring(lastIndex));
161 break;
165 return (String[]) parts.ToArray(typeof(String));
168 private IDictionary CreateConnectionProperties()
170 IDictionary props = new HybridDictionary();
172 foreach(DictionaryEntry entry in dialect.DefaultProperties)
174 props[entry.Key] = entry.Value;
177 if (connectionProperties != null)
179 foreach(DictionaryEntry entry in connectionProperties)
181 props[entry.Key] = entry.Value;
185 return props;
188 private static string Normalize(string fileName)
190 if (!Path.IsPathRooted(fileName))
192 return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
194 else
196 return fileName;