From: Michael D. <mik...@us...> - 2004-08-20 02:00:29
|
Update of /cvsroot/nhibernate/nhibernate/src/NHibernate/Tool/hbm2ddl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4678/NHibernate/Tool/hbm2ddl Modified Files: SchemaExport.cs Log Message: No longer using Transaction for the scope of creating the tables in the db. This will help the users of Firebird. Index: SchemaExport.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Tool/hbm2ddl/SchemaExport.cs,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** SchemaExport.cs 11 Jul 2004 21:00:10 -0000 1.10 --- SchemaExport.cs 20 Aug 2004 02:00:20 -0000 1.11 *************** *** 1,7 **** using System; ! using System.IO; using System.Data; using System.Text; ! using System.Collections; using NHibernate.Cfg; using NHibernate.Connection; --- 1,8 ---- using System; ! using System.Collections; using System.Data; + using System.IO; using System.Text; ! using NHibernate.Cfg; using NHibernate.Connection; *************** *** 13,20 **** { /// <summary> ! /// Commandline tool to export table schema for a configured <c>Configuration</c> to the database /// </summary> /// <remarks> ! /// To compile run "csc SchemaExport.cs /reference:../../bin/debug/NHibernate.dll" /// </remarks> public class SchemaExport --- 14,22 ---- { /// <summary> ! /// Generates ddl to export table schema for a configured <c>Configuration</c> to the database /// </summary> /// <remarks> ! /// This Class can be used directly or the command line wrapper NHibernate.Tool.hbm2ddl.exe can be ! /// used when a dll can not be directly used. /// </remarks> public class SchemaExport *************** *** 30,33 **** --- 32,36 ---- /// Create a schema exported for a given Configuration /// </summary> + /// <param name="cfg">The NHibernate Configuration to generate the schema from.</param> public SchemaExport(Configuration cfg) : this(cfg, cfg.Properties) { *************** *** 38,43 **** /// database connection properties /// </summary> ! /// <param name="cfg"></param> ! /// <param name="connectionProperties"></param> public SchemaExport(Configuration cfg, IDictionary connectionProperties) { --- 41,46 ---- /// database connection properties /// </summary> ! /// <param name="cfg">The NHibernate Configuration to generate the schema from.</param> ! /// <param name="connectionProperties">The Properties to use when connecting to the Database.</param> public SchemaExport(Configuration cfg, IDictionary connectionProperties) { *************** *** 51,54 **** --- 54,59 ---- /// Set the output filename. The generated script will be written to this file /// </summary> + /// <param name="filename">The name of the file to output the ddl to.</param> + /// <returns>The SchemaExport object.</returns> public SchemaExport SetOutputFile(string filename) { *************** *** 60,63 **** --- 65,70 ---- /// Set the end of statement delimiter /// </summary> + /// <param name="delimiter">The end of statement delimiter.</param> + /// <returns>The SchemaExport object.</returns> public SchemaExport SetDelimiter(string delimiter) { *************** *** 68,71 **** --- 75,84 ---- /// Run the schema creation script /// </summary> + /// <param name="script"><c>true</c> if the ddl should be outputted in the Console.</param> + /// <param name="export"><c>true</c> if the ddl should be executed against the Database.</param> + /// <remarks> + /// This is a convenience method that calls <see cref="Execute(bool, bool, bool, bool)"/> and sets + /// the justDrop parameter to false and the format parameter to true. + /// </remarks> public void Create(bool script, bool export) { *************** *** 76,79 **** --- 89,98 ---- /// Run the drop schema script /// </summary> + /// <param name="script"><c>true</c> if the ddl should be outputted in the Console.</param> + /// <param name="export"><c>true</c> if the ddl should be executed against the Database.</param> + /// <remarks> + /// This is a convenience method that calls <see cref="Execute(bool, bool, bool, bool)"/> and sets + /// the justDrop and format parameter to true. + /// </remarks> public void Drop(bool script, bool export) { *************** *** 81,88 **** } ! private void Execute(bool script, bool export, bool justDrop, bool format) { IDbConnection connection = null; - IDbTransaction transaction = null; StreamWriter fileOutput = null; IConnectionProvider connectionProvider = null; --- 100,116 ---- } ! /// <summary> ! /// Executes the Export of the Schema. ! /// </summary> ! /// <param name="script"><c>true</c> if the ddl should be outputted in the Console.</param> ! /// <param name="export"><c>true</c> if the ddl should be executed against the Database.</param> ! /// <param name="justDrop"><c>true</c> if only the ddl to drop the Database objects should be executed.</param> ! /// <param name="format"><c>true</c> if the ddl should be nicely formatted instead of one statement per line.</param> ! /// <remarks> ! /// This method allows for both the drop and create ddl script to be executed. ! /// </remarks> ! public void Execute(bool script, bool export, bool justDrop, bool format) { IDbConnection connection = null; StreamWriter fileOutput = null; IConnectionProvider connectionProvider = null; *************** *** 114,118 **** connectionProvider = ConnectionProviderFactory.NewConnectionProvider(props); connection = connectionProvider.GetConnection(); - transaction = connection.BeginTransaction(); statement = connection.CreateCommand(); } --- 142,145 ---- *************** *** 122,126 **** try { ! string formatted = dropSQL[i]; if (delimiter!=null) formatted += delimiter; if (script) Console.WriteLine(formatted); --- 149,162 ---- try { ! string formatted; ! if(format) ! { ! formatted = Format( dropSQL[i] ); ! } ! else ! { ! formatted = dropSQL[i]; ! } ! if (delimiter!=null) formatted += delimiter; if (script) Console.WriteLine(formatted); *************** *** 130,134 **** statement.CommandText = dropSQL[i]; statement.CommandType = CommandType.Text; - statement.Transaction = transaction; statement.ExecuteNonQuery(); } --- 166,169 ---- *************** *** 147,151 **** try { ! string formatted = createSQL[j]; if (delimiter!=null) formatted += delimiter; if (script) Console.WriteLine(formatted); --- 182,194 ---- try { ! string formatted; ! if(format) ! { ! formatted = Format( createSQL[j] ); ! } ! else ! { ! formatted = createSQL[j]; ! } if (delimiter!=null) formatted += delimiter; if (script) Console.WriteLine(formatted); *************** *** 155,159 **** statement.CommandText = createSQL[j]; statement.CommandType = CommandType.Text; - statement.Transaction = transaction; statement.ExecuteNonQuery(); } --- 198,201 ---- *************** *** 166,185 **** } } - - // if the parameter export==false then there will be no transaction - // to commit. - if (transaction!=null) transaction.Commit(); } catch (Exception e) { - if (transaction != null) - { - try - { - transaction.Rollback(); - } - catch {} - } Console.Write(e.StackTrace); throw new HibernateException( e.Message ); --- 208,215 ---- *************** *** 215,227 **** /// <summary> ! /// Format an SQL statement using simple rules: ! /// a) Insert newline after each comma; ! /// b) Indent three spaces after each inserted newline; ! /// If the statement contains single/double quotes return unchanged, ! /// it is too complex and could be broken by simple formatting. /// </summary> ! private static string Format(string sql) { ! ! if ( sql.IndexOf("\"") > 0 || sql.IndexOf("'") > 0) { return sql; } --- 245,273 ---- /// <summary> ! /// Format an SQL statement using simple rules /// </summary> ! /// <param name="sql">The string containing the sql to format.</param> ! /// <returns>A string that contains formatted sql.</returns> ! /// <remarks> ! /// The simple rules to used when formatting are: ! /// <list type="number"> ! /// <item> ! /// <description>Insert a newline after each comma</description> ! /// </item> ! /// <item> ! /// <description>Indent three spaces after each inserted newline</description> ! /// </item> ! /// <item> ! /// <description> ! /// If the statement contains single/double quotes return unchanged because ! /// it is too complex and could be broken by simple formatting. ! /// </description> ! /// </item> ! /// </list> ! /// </remarks> ! private static string Format(string sql) ! { ! if ( sql.IndexOf("\"") > 0 || sql.IndexOf("'") > 0) ! { return sql; } *************** *** 229,234 **** string formatted; ! if ( sql.ToLower().StartsWith("create table") ) { ! StringBuilder result = new StringBuilder(60); StringTokenizer tokens = new StringTokenizer( sql, "(,)", true); --- 275,280 ---- string formatted; ! if ( sql.ToLower().StartsWith("create table") ) ! { StringBuilder result = new StringBuilder(60); StringTokenizer tokens = new StringTokenizer( sql, "(,)", true); *************** *** 236,241 **** int depth = 0; ! foreach(string tok in tokens) { ! if ( StringHelper.ClosedParen.Equals(tok) ) { depth--; if (depth==0) result.Append("\n"); --- 282,289 ---- int depth = 0; ! foreach(string tok in tokens) ! { ! if ( StringHelper.ClosedParen.Equals(tok) ) ! { depth--; if (depth==0) result.Append("\n"); *************** *** 243,247 **** result.Append(tok); if ( StringHelper.Comma.Equals(tok) && depth==1 ) result.Append("\n "); ! if ( StringHelper.OpenParen.Equals(tok) ) { depth++; if ( depth==1 ) result.Append("\n "); --- 291,296 ---- result.Append(tok); if ( StringHelper.Comma.Equals(tok) && depth==1 ) result.Append("\n "); ! if ( StringHelper.OpenParen.Equals(tok) ) ! { depth++; if ( depth==1 ) result.Append("\n "); *************** *** 250,312 **** formatted = result.ToString(); ! } else { formatted = sql; } return formatted; ! } ! ! public static void Main(string[] args) { ! try { ! Configuration cfg = new Configuration(); ! ! bool script = true; ! bool drop = false; ! bool export = true; ! string outputFile = null; ! string propFile = null; ! bool formatSQL = false; ! string delimiter = null; ! ! for ( int i=0; i<args.Length; i++ ) { ! if ( args[i].StartsWith("--") ) { ! if ( args[i].Equals("--quiet") ) { ! script = false; ! } else if ( args[i].Equals("--drop") ) { ! drop = true; ! } else if ( args[i].Equals("--text") ) { ! export = false; ! } else if ( args[i].Equals("--output=") ) { ! outputFile = args[i].Substring(13); ! } else if ( args[i].Equals("--format") ) { ! formatSQL = true; ! } else if ( args[i].Equals("--delimiter=") ) { ! delimiter = args[i].Substring(12); ! } else if ( args[i].Equals("--config=") ) { ! cfg.Configure( args[i].Substring(9) ); ! } ! } else { ! string filename = args[i]; ! if ( filename.EndsWith( ".dll") || filename.EndsWith( ".exe") ) { ! cfg.AddAssembly( filename ); ! } else { ! cfg.AddXmlFile(filename); ! } ! } ! } ! if (propFile!=null) { ! //TODO: load up a props file (xml based probably..) ! } else { ! new SchemaExport(cfg) ! .SetOutputFile(outputFile) ! .SetDelimiter(delimiter) ! .Execute(script, export, drop, formatSQL); ! } ! } catch(Exception e) { ! Console.Error.WriteLine("Error creating schema " + e.Message ); ! Console.Error.Write(e.StackTrace); ! } ! } ! } } --- 299,310 ---- formatted = result.ToString(); ! } ! else ! { formatted = sql; } return formatted; ! } } } |