From: <fab...@us...> - 2008-12-16 20:16:20
|
Revision: 3959 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3959&view=rev Author: fabiomaulo Date: 2008-12-16 20:16:16 +0000 (Tue, 16 Dec 2008) Log Message: ----------- Refactoring of SchemaAction matters Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Cfg/Configuration.cs trunk/nhibernate/src/NHibernate/Cfg/Mappings.cs trunk/nhibernate/src/NHibernate/Mapping/Table.cs trunk/nhibernate/src/NHibernate/Tool/hbm2ddl/SchemaExport.cs trunk/nhibernate/src/NHibernate/Tool/hbm2ddl/SchemaUpdate.cs trunk/nhibernate/src/NHibernate/Tool/hbm2ddl/SchemaValidator.cs Modified: trunk/nhibernate/src/NHibernate/Cfg/Configuration.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Cfg/Configuration.cs 2008-12-16 18:39:12 UTC (rev 3958) +++ trunk/nhibernate/src/NHibernate/Cfg/Configuration.cs 2008-12-16 20:16:16 UTC (rev 3959) @@ -671,11 +671,11 @@ { foreach (var table in TableMappings) { - if (table.IsPhysicalTable && table.SchemaDrop) + if (table.IsPhysicalTable && IncludeAction(table.SchemaActions, SchemaAction.Drop)) { foreach (var fk in table.ForeignKeyIterator) { - if (fk.HasPhysicalConstraint && fk.ReferencedTable.SchemaDrop) + if (fk.HasPhysicalConstraint && IncludeAction(fk.ReferencedTable.SchemaActions, SchemaAction.Drop)) { script.Add(fk.SqlDropString(dialect, defaultCatalog, defaultSchema)); } @@ -686,7 +686,7 @@ foreach (var table in TableMappings) { - if (table.IsPhysicalTable && table.SchemaDrop) + if (table.IsPhysicalTable && IncludeAction(table.SchemaActions, SchemaAction.Drop)) { script.Add(table.SqlDropString(dialect, defaultCatalog, defaultSchema)); } @@ -708,6 +708,11 @@ return script.ToArray(); } + public static bool IncludeAction(SchemaAction actionsSource, SchemaAction includedAction) + { + return (actionsSource & includedAction) != SchemaAction.None; + } + /// <summary> /// Generate DDL for creating tables /// </summary> @@ -723,7 +728,7 @@ foreach (var table in TableMappings) { - if (table.IsPhysicalTable && table.SchemaExport) + if (table.IsPhysicalTable && IncludeAction(table.SchemaActions, SchemaAction.Export)) { script.Add(table.SqlCreateString(dialect, mapping, defaultCatalog, defaultSchema)); script.AddRange(table.SqlCommentStrings(dialect, defaultCatalog, defaultSchema)); @@ -732,7 +737,7 @@ foreach (var table in TableMappings) { - if (table.IsPhysicalTable && table.SchemaExport) + if (table.IsPhysicalTable && IncludeAction(table.SchemaActions, SchemaAction.Export)) { if (!dialect.SupportsUniqueConstraintInCreateAlterTable) { @@ -755,7 +760,7 @@ { foreach (var fk in table.ForeignKeyIterator) { - if (fk.HasPhysicalConstraint && fk.ReferencedTable.SchemaExport) + if (fk.HasPhysicalConstraint && IncludeAction(fk.ReferencedTable.SchemaActions, SchemaAction.Export)) { script.Add(fk.SqlCreateString(dialect, mapping, defaultCatalog, defaultSchema)); } @@ -1920,7 +1925,7 @@ var script = new List<string>(50); foreach (var table in TableMappings) { - if (table.IsPhysicalTable && table.SchemaUpdate) + if (table.IsPhysicalTable && IncludeAction(table.SchemaActions, SchemaAction.Update)) { ITableMetadata tableInfo = databaseMetadata.GetTableMetadata(table.Name, table.Schema ?? defaultSchema, table.Catalog ?? defaultCatalog, table.IsQuoted); @@ -1941,7 +1946,7 @@ foreach (var table in TableMappings) { - if (table.IsPhysicalTable && table.SchemaUpdate) + if (table.IsPhysicalTable && IncludeAction(table.SchemaActions, SchemaAction.Update)) { ITableMetadata tableInfo = databaseMetadata.GetTableMetadata(table.Name, table.Schema, table.Catalog, table.IsQuoted); @@ -1950,7 +1955,7 @@ { foreach (var fk in table.ForeignKeyIterator) { - if (fk.HasPhysicalConstraint && fk.ReferencedTable.SchemaUpdate) + if (fk.HasPhysicalConstraint && IncludeAction(fk.ReferencedTable.SchemaActions, SchemaAction.Update)) { bool create = tableInfo == null || @@ -1997,10 +2002,10 @@ string defaultCatalog = PropertiesHelper.GetString(Environment.DefaultCatalog, properties, null); string defaultSchema = PropertiesHelper.GetString(Environment.DefaultSchema, properties, null); - var iter = this.TableMappings; + var iter = TableMappings; foreach (var table in iter) { - if (table.IsPhysicalTable && table.SchemaValidate) + if (table.IsPhysicalTable && IncludeAction(table.SchemaActions, SchemaAction.Validate)) { /*NH Different Implementation : TableMetadata tableInfo = databaseMetadata.getTableMetadata( @@ -2011,7 +2016,7 @@ ITableMetadata tableInfo = databaseMetadata.GetTableMetadata( table.Name, table.Schema??defaultSchema, - table.Catalog,//??defaultCatalog, + table.Catalog??defaultCatalog, table.IsQuoted); if (tableInfo == null) throw new HibernateException("Missing table: " + table.Name); Modified: trunk/nhibernate/src/NHibernate/Cfg/Mappings.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Cfg/Mappings.cs 2008-12-16 18:39:12 UTC (rev 3958) +++ trunk/nhibernate/src/NHibernate/Cfg/Mappings.cs 2008-12-16 20:16:16 UTC (rev 3959) @@ -258,10 +258,7 @@ table.Schema = schema; table.Catalog = catalog; table.Subselect = subselect; - table.SchemaDrop = SchemaActionRequested(schemaAction, "drop"); - table.SchemaUpdate = SchemaActionRequested(schemaAction, "update"); - table.SchemaExport = SchemaActionRequested(schemaAction, "export"); - table.SchemaValidate = SchemaActionRequested(schemaAction, "validate"); + table.SchemaActions = GetSchemaActions(schemaAction); tables[key] = table; } else @@ -273,9 +270,46 @@ return table; } - private static bool SchemaActionRequested(string schemaAction, string check) + private static SchemaAction GetSchemaActions(string schemaAction) { - return string.IsNullOrEmpty(schemaAction) || schemaAction.Contains("all") || schemaAction.Contains(check); + if (string.IsNullOrEmpty(schemaAction)) + { + return SchemaAction.All; + } + else + { + SchemaAction sa = SchemaAction.None; + string[] acts = schemaAction.Split(new[] {',', ' '}); + foreach (var s in acts) + { + switch (s.ToLowerInvariant()) + { + case "": + case "all": + sa |= SchemaAction.All; + break; + case "drop": + sa |= SchemaAction.Drop; + break; + case "update": + sa |= SchemaAction.Update; + break; + case "export": + sa |= SchemaAction.Export; + break; + case "validate": + sa |= SchemaAction.Validate; + break; + case "none": + sa |= SchemaAction.None; + break; + default: + throw new MappingException( + string.Format("Invalid schema-export value; Expected(all drop update export validate none), Found ({0})", s)); + } + } + return sa; + } } public Table AddDenormalizedTable(string schema, string catalog, string name, bool isAbstract, string subselect, Table includedTable) Modified: trunk/nhibernate/src/NHibernate/Mapping/Table.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Mapping/Table.cs 2008-12-16 18:39:12 UTC (rev 3958) +++ trunk/nhibernate/src/NHibernate/Mapping/Table.cs 2008-12-16 20:16:16 UTC (rev 3959) @@ -9,6 +9,17 @@ namespace NHibernate.Mapping { + [Flags] + public enum SchemaAction + { + None = 0, + Drop = 1, + Update= 2, + Export= 4, + Validate= 8, + All = Drop | Update | Export | Validate + } + /// <summary> /// Represents a Table in a database that an object gets mapped against. /// </summary> @@ -84,10 +95,7 @@ private string subselect; private string rowId; private bool isSchemaQuoted; - private bool schemaDrop = true; - private bool schemaUpdate = true; - private bool schemaExport = true; - private bool schemaValidate = true; + private SchemaAction schemaActions = SchemaAction.All; /// <summary> @@ -924,30 +932,12 @@ get { return !IsSubselect && !IsAbstractUnionTable; } } - public bool SchemaDrop + public SchemaAction SchemaActions { - get { return schemaDrop; } - set { schemaDrop = value; } + get { return schemaActions; } + set { schemaActions = value; } } - public bool SchemaUpdate - { - get { return schemaUpdate; } - set { schemaUpdate = value; } - } - - public bool SchemaExport - { - get { return schemaExport; } - set { schemaExport = value; } - } - - public bool SchemaValidate - { - get { return schemaValidate; } - set { schemaValidate = value; } - } - public string RowId { get { return rowId; } Modified: trunk/nhibernate/src/NHibernate/Tool/hbm2ddl/SchemaExport.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Tool/hbm2ddl/SchemaExport.cs 2008-12-16 18:39:12 UTC (rev 3958) +++ trunk/nhibernate/src/NHibernate/Tool/hbm2ddl/SchemaExport.cs 2008-12-16 20:16:16 UTC (rev 3959) @@ -19,23 +19,19 @@ /// </remarks> public class SchemaExport { - private readonly string[] dropSQL; - private readonly string[] createSQL; + private static readonly ILog log = LogManager.GetLogger(typeof (SchemaExport)); private readonly IDictionary<string, string> connectionProperties; - private string outputFile = null; + private readonly string[] createSQL; private readonly Dialect.Dialect dialect; - private string delimiter = null; + private readonly string[] dropSQL; + private string delimiter; + private string outputFile; - private static readonly ILog log = LogManager.GetLogger(typeof(SchemaExport)); - /// <summary> /// 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) - { - } + public SchemaExport(Configuration cfg) : this(cfg, cfg.Properties) {} /// <summary> /// Create a schema exporter for the given Configuration, with the given @@ -107,7 +103,7 @@ } private void Execute(Action<string> scriptAction, bool export, bool format, bool throwOnError, TextWriter exportOutput, - IDbCommand statement, string sql) + IDbCommand statement, string sql) { try { @@ -169,8 +165,8 @@ /// This overload is provided mainly to enable use of in memory databases. /// It does NOT close the given connection! /// </remarks> - public void Execute(bool script, bool export, bool justDrop, bool format, - IDbConnection connection, TextWriter exportOutput) + public void Execute(bool script, bool export, bool justDrop, bool format, IDbConnection connection, + TextWriter exportOutput) { if (script) { @@ -182,8 +178,8 @@ } } - public void Execute(Action<string> scriptAction, bool export, bool justDrop, bool format, - IDbConnection connection, TextWriter exportOutput) + public void Execute(Action<string> scriptAction, bool export, bool justDrop, bool format, IDbConnection connection, + TextWriter exportOutput) { IDbCommand statement = null; @@ -236,10 +232,8 @@ } } } - } - /// <summary> /// Executes the Export of the Schema. /// </summary> @@ -261,21 +255,22 @@ Execute(null, export, justDrop, format); } } + public void Execute(Action<string> scriptAction, bool export, bool justDrop, bool format) { IDbConnection connection = null; StreamWriter fileOutput = null; IConnectionProvider connectionProvider = null; - Dictionary<string, string> props = new Dictionary<string, string>(); - foreach (KeyValuePair<string, string> de in dialect.DefaultProperties) + var props = new Dictionary<string, string>(); + foreach (var de in dialect.DefaultProperties) { props[de.Key] = de.Value; } if (connectionProperties != null) { - foreach (KeyValuePair<string, string> de in connectionProperties) + foreach (var de in connectionProperties) { props[de.Key] = de.Value; } @@ -314,7 +309,6 @@ connectionProvider.Dispose(); } } - } /// <summary> @@ -350,8 +344,8 @@ if (StringHelper.StartsWithCaseInsensitive(sql, "create table")) { - StringBuilder result = new StringBuilder(60); - StringTokenizer tokens = new StringTokenizer(sql, "(,)", true); + var result = new StringBuilder(60); + var tokens = new StringTokenizer(sql, "(,)", true); int depth = 0; Modified: trunk/nhibernate/src/NHibernate/Tool/hbm2ddl/SchemaUpdate.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Tool/hbm2ddl/SchemaUpdate.cs 2008-12-16 18:39:12 UTC (rev 3958) +++ trunk/nhibernate/src/NHibernate/Tool/hbm2ddl/SchemaUpdate.cs 2008-12-16 20:16:16 UTC (rev 3959) @@ -8,26 +8,22 @@ namespace NHibernate.Tool.hbm2ddl { - public class SchemaUpdate { - private static readonly ILog log = LogManager.GetLogger(typeof(SchemaUpdate)); - private readonly IConnectionHelper connectionHelper; + private static readonly ILog log = LogManager.GetLogger(typeof (SchemaUpdate)); private readonly Configuration configuration; + private readonly IConnectionHelper connectionHelper; private readonly Dialect.Dialect dialect; private readonly List<Exception> exceptions; - public SchemaUpdate(Configuration cfg) - : this(cfg, cfg.Properties) - { - } + public SchemaUpdate(Configuration cfg) : this(cfg, cfg.Properties) {} public SchemaUpdate(Configuration cfg, IDictionary<string, string> connectionProperties) { configuration = cfg; - dialect = NHibernate.Dialect.Dialect.GetDialect(connectionProperties); - Dictionary<string, string> props = new Dictionary<string, string>(dialect.DefaultProperties); - foreach (KeyValuePair<string, string> prop in connectionProperties) + dialect = Dialect.Dialect.GetDialect(connectionProperties); + var props = new Dictionary<string, string>(dialect.DefaultProperties); + foreach (var prop in connectionProperties) { props[prop.Key] = prop.Value; } @@ -39,17 +35,24 @@ { configuration = cfg; dialect = settings.Dialect; - connectionHelper = new SuppliedConnectionProviderConnectionHelper( - settings.ConnectionProvider - ); + connectionHelper = new SuppliedConnectionProviderConnectionHelper(settings.ConnectionProvider); exceptions = new List<Exception>(); } + /// <summary> + /// Returns a List of all Exceptions which occured during the export. + /// </summary> + /// <returns></returns> + public IList<Exception> Exceptions + { + get { return exceptions; } + } + public static void Main(string[] args) { try { - Configuration cfg = new Configuration(); + var cfg = new Configuration(); bool script = true; // If true then execute db updates, otherwise just generate and display updates @@ -80,8 +83,7 @@ else if (args[i].StartsWith("--naming=")) { cfg.SetNamingStrategy( - (INamingStrategy)Activator.CreateInstance(ReflectHelper.ClassForName(args[i].Substring(9))) - ); + (INamingStrategy) Activator.CreateInstance(ReflectHelper.ClassForName(args[i].Substring(9)))); } } else @@ -204,14 +206,5 @@ } } } - - /// <summary> - /// Returns a List of all Exceptions which occured during the export. - /// </summary> - /// <returns></returns> - public IList<Exception> Exceptions - { - get { return exceptions; } - } } -} +} \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate/Tool/hbm2ddl/SchemaValidator.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Tool/hbm2ddl/SchemaValidator.cs 2008-12-16 18:39:12 UTC (rev 3958) +++ trunk/nhibernate/src/NHibernate/Tool/hbm2ddl/SchemaValidator.cs 2008-12-16 20:16:16 UTC (rev 3959) @@ -1,11 +1,7 @@ using System; using System.Collections.Generic; -using System.Data; using System.Data.Common; -using System.Data.SqlClient; -using System.Text; using log4net; -using log4net.Repository.Hierarchy; using NHibernate.Cfg; using NHibernate.Util; @@ -13,30 +9,28 @@ { public class SchemaValidator { - - private static readonly ILog log = LogManager.GetLogger(typeof(SchemaValidator)); - private readonly IConnectionHelper connectionHelper; + private static readonly ILog log = LogManager.GetLogger(typeof (SchemaValidator)); private readonly Configuration configuration; - private Dialect.Dialect dialect; + private readonly IConnectionHelper connectionHelper; + private readonly Dialect.Dialect dialect; - public SchemaValidator(Configuration cfg) : - this(cfg, cfg.Properties) - { - } + public SchemaValidator(Configuration cfg) : this(cfg, cfg.Properties) {} public SchemaValidator(Configuration cfg, IDictionary<string, string> connectionProperties) { - this.configuration = cfg; + configuration = cfg; dialect = Dialect.Dialect.GetDialect(connectionProperties); IDictionary<string, string> props = new Dictionary<string, string>(dialect.DefaultProperties); foreach (var prop in connectionProperties) + { props[prop.Key] = prop.Value; + } connectionHelper = new ManagedProviderConnectionHelper(props); } public SchemaValidator(Configuration cfg, Settings settings) { - this.configuration = cfg; + configuration = cfg; dialect = settings.Dialect; connectionHelper = new SuppliedConnectionProviderConnectionHelper(settings.ConnectionProvider); } @@ -45,33 +39,33 @@ { try { - Configuration cfg = new Configuration(); + var cfg = new Configuration(); - String propFile = null; + //string propFile = null; for (int i = 0; i < args.Length; i++) { if (args[i].StartsWith("--")) { - if (args[i].StartsWith("--properties=")) + //if (args[i].StartsWith("--properties=")) + //{ + // propFile = args[i].Substring(13); + //} + //else + if (args[i].StartsWith("--config=")) { - propFile = args[i].Substring(13); - } - else if (args[i].StartsWith("--config=")) - { cfg.Configure(args[i].Substring(9)); } else if (args[i].StartsWith("--naming=")) { cfg.SetNamingStrategy( - (INamingStrategy)Activator.CreateInstance(ReflectHelper.ClassForName(args[i].Substring(9)))); + (INamingStrategy) Activator.CreateInstance(ReflectHelper.ClassForName(args[i].Substring(9)))); } } else { cfg.AddFile(args[i]); } - } /* NH: No props file for .NET if ( propFile != null ) { @@ -93,32 +87,31 @@ /** * Perform the validations. */ + public void Validate() { log.Info("Running schema validator"); - DbConnection connection = null; try { - DatabaseMetadata meta; try { log.Info("fetching database metadata"); connectionHelper.Prepare(); - connection = connectionHelper.Connection; + DbConnection connection = connectionHelper.Connection; meta = new DatabaseMetadata(connection, dialect, false); } catch (Exception sqle) { log.Error("could not get database metadata", sqle); - throw sqle; + throw; } configuration.ValidateSchema(dialect, meta); } catch (Exception e) { log.Error("could not complete schema validation", e); - throw e; + throw; } finally { @@ -130,8 +123,7 @@ { log.Error("Error closing connection", e); } - } } } -} +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |