Update of /cvsroot/nhibernate/nhibernate/src/NHibernate/Cfg In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5548/nhibernate/src/NHibernate/Cfg Modified Files: Binder.cs Configuration.cs Mappings.cs Added Files: DefaultNamingStrategy.cs INamingStrategy.cs ImprovedNamingStrategy.cs Log Message: Various refactorings on the way to 2.1 querying capability Index: Mappings.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Cfg/Mappings.cs,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** Mappings.cs 31 Dec 2004 15:54:08 -0000 1.11 --- Mappings.cs 1 Mar 2005 16:24:44 -0000 1.12 *************** *** 18,21 **** --- 18,22 ---- private IDictionary tables; private IDictionary queries; + private IDictionary sqlqueries; private IList secondPasses; private IDictionary imports; *************** *** 23,28 **** --- 24,37 ---- private string defaultCascade; private bool autoImport; + private IList propertyReferences; private string defaultAccess; private IDictionary caches; + private INamingStrategy namingStrategy; + + private class UniquePropertyReference + { + public System.Type ReferencedClass; + public string PropertyName; + } /// <summary> *************** *** 33,48 **** /// <param name="tables"></param> /// <param name="queries"></param> /// <param name="imports"></param> /// <param name="caches"></param> /// <param name="secondPasses"></param> ! internal Mappings( IDictionary classes, IDictionary collections, IDictionary tables, IDictionary queries, IDictionary imports, IDictionary caches, IList secondPasses ) { this.classes = classes; this.collections = collections; this.queries = queries; this.tables = tables; this.imports = imports; this.caches = caches; this.secondPasses = secondPasses; } --- 42,63 ---- /// <param name="tables"></param> /// <param name="queries"></param> + /// <param name="sqlqueries"></param> /// <param name="imports"></param> /// <param name="caches"></param> /// <param name="secondPasses"></param> ! /// <param name="propertyReferences"></param> ! /// <param name="namingStrategy"></param> ! internal Mappings( IDictionary classes, IDictionary collections, IDictionary tables, IDictionary queries, IDictionary sqlqueries, IDictionary imports, IDictionary caches, IList secondPasses, IList propertyReferences, INamingStrategy namingStrategy ) { this.classes = classes; this.collections = collections; this.queries = queries; + this.sqlqueries = sqlqueries; this.tables = tables; this.imports = imports; this.caches = caches; this.secondPasses = secondPasses; + this.propertyReferences = propertyReferences; + this.namingStrategy = namingStrategy; } *************** *** 69,78 **** public void AddClass( PersistentClass persistentClass ) { ! object old = classes[ persistentClass.PersistentClazz ]; if( old != null ) { ! log.Warn( "duplicate class mapping: " + persistentClass.PersistentClazz.Name ); } ! classes[ persistentClass.PersistentClazz ] = persistentClass; } --- 84,93 ---- public void AddClass( PersistentClass persistentClass ) { ! object old = classes[ persistentClass.MappedClass ]; if( old != null ) { ! log.Warn( "duplicate class mapping: " + persistentClass.MappedClass.Name ); } ! classes[ persistentClass.MappedClass ] = persistentClass; } *************** *** 94,97 **** --- 109,126 ---- /// /// </summary> + /// <param name="referencedClass"></param> + /// <param name="propertyName"></param> + public void AddUniquePropertyReference( System.Type referencedClass, string propertyName ) + { + UniquePropertyReference upr = new UniquePropertyReference(); + upr.ReferencedClass = referencedClass; + upr.PropertyName = propertyName; + + propertyReferences.Add( upr ); + } + + /// <summary> + /// + /// </summary> /// <param name="type"></param> /// <returns></returns> *************** *** 104,107 **** --- 133,144 ---- /// /// </summary> + public INamingStrategy NamingStrategy + { + get { return namingStrategy; } + } + + /// <summary> + /// + /// </summary> /// <param name="role"></param> /// <returns></returns> *************** *** 185,188 **** --- 222,233 ---- } + private void CheckQueryExists( string name ) + { + if ( queries.Contains( name ) || sqlqueries.Contains( name ) ) + { + throw new MappingException( string.Format( "Duplicate query named: {0}", name ) ); + } + } + /// <summary> /// *************** *** 192,200 **** public void AddQuery( string name, string query ) { ! object old = queries[ name ]; ! if( old != null ) ! { ! log.Warn( "duplicate query name: " + name ); ! } queries[ name ] = query; } --- 237,241 ---- public void AddQuery( string name, string query ) { ! CheckQueryExists( name ); queries[ name ] = query; } *************** *** 204,207 **** --- 245,259 ---- /// </summary> /// <param name="name"></param> + /// <param name="query"></param> + public void AddSQLQuery( string name, NamedSQLQuery query ) + { + CheckQueryExists( name ); + sqlqueries[ name ] = query; + } + + /// <summary> + /// + /// </summary> + /// <param name="name"></param> /// <returns></returns> public string GetQuery( string name ) --- NEW FILE: DefaultNamingStrategy.cs --- using NHibernate.Util; namespace NHibernate.Cfg { /// <summary> /// The default <cref name="INamingStrategy"/> /// </summary> /// <remarks>See <cref name="ImprovedNamingStrategy"/> for a better alternative</remarks> public class DefaultNamingStrategy : INamingStrategy { /// <summary> /// The singleton instance /// </summary> public static readonly INamingStrategy Instance = new DefaultNamingStrategy(); private DefaultNamingStrategy() { } #region INamingStrategy Members /// <summary> /// Return the unqualified class name /// </summary> /// <param name="className"></param> /// <returns></returns> public string ClassToTableName(string className) { return StringHelper.Unqualify( className ); } /// <summary> /// Return the unqualified property name /// </summary> /// <param name="propertyName"></param> /// <returns></returns> public string PropertyToColumnName(string propertyName) { return StringHelper.Unqualify( propertyName ); } /// <summary> /// Return the argument /// </summary> /// <param name="tableName"></param> /// <returns></returns> public string TableName(string tableName) { return tableName; } /// <summary> /// Return the argument /// </summary> /// <param name="columnName"></param> /// <returns></returns> public string ColumnName(string columnName) { return columnName; } /// <summary> /// Return the unqualified property name /// </summary> /// <param name="className"></param> /// <param name="propertyName"></param> /// <returns></returns> public string PropertyToTableName(string className, string propertyName) { return StringHelper.Unqualify( propertyName ); } #endregion } } --- NEW FILE: ImprovedNamingStrategy.cs --- using System; using System.Text; using NHibernate.Util; namespace NHibernate.Cfg { /// <summary> /// Summary description for ImprovedNamingStrategy. /// </summary> public class ImprovedNamingStrategy : INamingStrategy { /// <summary> /// The singleton instance /// </summary> public static readonly INamingStrategy Instance = new ImprovedNamingStrategy(); private ImprovedNamingStrategy() { } #region INamingStrategy Members /// <summary> /// Return the unqualified class name, mixed case converted to underscores /// </summary> /// <param name="className"></param> /// <returns></returns> public string ClassToTableName(string className) { return AddUnderscores( StringHelper.Unqualify( className ) ); } /// <summary> /// Return the full property path with underscore seperators, mixed case converted to underscores /// </summary> /// <param name="propertyName"></param> /// <returns></returns> public string PropertyToColumnName(string propertyName) { return AddUnderscores( StringHelper.Unqualify( propertyName ) ); } /// <summary> /// Convert mixed case to underscores /// </summary> /// <param name="tableName"></param> /// <returns></returns> public string TableName(string tableName) { return AddUnderscores( tableName ); } /// <summary> /// Convert mixed case to underscores /// </summary> /// <param name="columnName"></param> /// <returns></returns> public string ColumnName(string columnName) { return AddUnderscores( columnName ); } /// <summary> /// Return the full property path prefixed by the unqualified class name, with underscore seperators, mixed case converted to underscores /// </summary> /// <param name="className"></param> /// <param name="propertyName"></param> /// <returns></returns> public string PropertyToTableName(string className, string propertyName) { return AddUnderscores( StringHelper.Unqualify( propertyName ) ); } #endregion private string AddUnderscores( string name ) { char[] chars = name.Replace( '.', '_' ).ToCharArray(); StringBuilder buf = new StringBuilder( chars.Length ) ; char prev = 'a'; foreach( char c in chars ) { if ( c != '_' && char.IsUpper(c) && !char.IsUpper(prev) ) { buf.Append( '_' ); } buf.Append( char.ToLower( c ) ); } return buf.ToString(); } } } Index: Binder.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Cfg/Binder.cs,v retrieving revision 1.38 retrieving revision 1.39 diff -C2 -d -r1.38 -r1.39 *** Binder.cs 14 Feb 2005 03:29:05 -0000 1.38 --- Binder.cs 1 Mar 2005 16:24:43 -0000 1.39 *************** *** 24,34 **** public static void BindClass(XmlNode node, PersistentClass model, Mappings mapping) { - string className = node.Attributes["name"] == null ? null : node.Attributes["name"].Value; ! // class try { ! model.PersistentClazz = ReflectHelper.ClassForName(className); } [...1756 lines suppressed...] } *************** *** 1459,1466 **** { public CollectionTypePrimitiveArray(string xmlTag) : base(xmlTag) { } ! public override Mapping.Collection Create(XmlNode node, string prefix, PersistentClass owner, Mappings mappings) { PrimitiveArray array = new PrimitiveArray(owner); ! Binder.BindArray(node, array, prefix, mappings); return array; } --- 1594,1601 ---- { public CollectionTypePrimitiveArray(string xmlTag) : base(xmlTag) { } ! public override Mapping.Collection Create(XmlNode node, string prefix, string path, PersistentClass owner, Mappings mappings) { PrimitiveArray array = new PrimitiveArray(owner); ! Binder.BindArray(node, array, prefix, path, mappings); return array; } Index: Configuration.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Cfg/Configuration.cs,v retrieving revision 1.31 retrieving revision 1.32 diff -C2 -d -r1.31 -r1.32 *** Configuration.cs 21 Feb 2005 19:17:39 -0000 1.31 --- Configuration.cs 1 Mar 2005 16:24:43 -0000 1.32 *************** *** 39,47 **** --- 39,51 ---- private Hashtable tables = new Hashtable(); private Hashtable namedQueries = new Hashtable(); + private Hashtable namedSqlQueries = new Hashtable(); private ArrayList secondPasses = new ArrayList(); + private ArrayList propertyReferences = new ArrayList(); private IInterceptor interceptor = emptyInterceptor; private IDictionary properties = Environment.Properties; private IDictionary caches = new Hashtable(); + private INamingStrategy namingStrategy = DefaultNamingStrategy.Instance; + private XmlSchema mappingSchema; private XmlSchema cfgSchema; *************** *** 70,73 **** --- 74,78 ---- tables = new Hashtable(); namedQueries = new Hashtable(); + namedSqlQueries = new Hashtable(); secondPasses = new ArrayList(); interceptor = emptyInterceptor; *************** *** 141,145 **** public Mappings CreateMappings() { ! return new Mappings( classes, collections, tables, namedQueries, imports, caches, secondPasses ); } --- 146,150 ---- public Mappings CreateMappings() { ! return new Mappings( classes, collections, tables, namedQueries, namedSqlQueries, imports, caches, secondPasses, propertyReferences, namingStrategy ); } *************** *** 692,695 **** --- 697,716 ---- /// <summary> + /// The named SQL queries + /// </summary> + public IDictionary NamedSQLQueries + { + get { return namedSqlQueries; } + } + + /// <summary> + /// Naming strategy for tables and columns + /// </summary> + public INamingStrategy NamingStrategy + { + get { return namingStrategy; } + } + + /// <summary> /// Instantitate a new <c>ISessionFactory</c>, using the properties and mappings in this /// configuration. The <c>ISessionFactory</c> will be immutable, so changes made to the *************** *** 833,837 **** } } ! } --- 854,868 ---- } } ! } ! ! /// <summary> ! /// Set a custom naming strategy ! /// </summary> ! /// <param name="namingStrategy">the NamingStrategy to set</param> ! /// <returns></returns> ! public Configuration SetNamingStrategy( INamingStrategy namingStrategy ) ! { ! this.namingStrategy = namingStrategy; ! return this; } *************** *** 1006,1009 **** --- 1037,1064 ---- get { return imports; } } + + // HACK: SHould really implement IMapping, but simpler than dealing with the cascades + + /// <summary> + /// + /// </summary> + /// <param name="objectClass"></param> + /// <returns></returns> + public string GetIdentifierPropertyName( System.Type objectClass ) + { + return null; + } + + /// <summary> + /// + /// </summary> + /// <param name="persistentClass"></param> + /// <param name="propertyName"></param> + /// <returns></returns> + public IType GetPropertyType( System.Type persistentClass, string propertyName ) + { + return null; + } + } } \ No newline at end of file --- NEW FILE: INamingStrategy.cs --- using System; namespace NHibernate.Cfg { /// <summary> /// A set of rules for determining the physical column and table names given the information in the mapping /// document. May be used to implement project-scoped naming standards for database objects. /// </summary> public interface INamingStrategy { /// <summary> /// Return a table name for an entity class /// </summary> /// <param name="className">the fully-qualified class name</param> /// <returns>a table name</returns> string ClassToTableName( string className ); /// <summary> /// Return a column name for a property path expression /// </summary> /// <param name="propertyName">a property path</param> /// <returns>a column name</returns> string PropertyToColumnName( string propertyName ); /// <summary> /// Alter the table name given in the mapping document /// </summary> /// <param name="tableName">a table name</param> /// <returns>a table name</returns> string TableName( string tableName ); /// <summary> /// Alter the column name given in the mapping document /// </summary> /// <param name="columnName">a column name</param> /// <returns>a column name</returns> string ColumnName( string columnName ); /// <summary> /// Return a table name for a collection /// </summary> /// <param name="className">the fully-qualified name of the owning entity class</param> /// <param name="propertyName">a property path</param> /// <returns>a table name</returns> string PropertyToTableName( string className, string propertyName ); } } |