Esempio n. 1
0
 public IType GetIdentifierOrUniqueKeyType(ISessionFactoryImplementor factory)
 {
     if (uniqueKeyPropertyName == null)
     {
         return(factory.GetIdentifierType(associatedClass));
     }
     else
     {
         return(factory.GetPersister(associatedClass).GetPropertyType(uniqueKeyPropertyName));
     }
 }
Esempio n. 2
0
        public System.Type GetClassForPath(string rootAlias, string associationPath)
        {
            ISessionFactoryImplementor factory = session.Factory;

            System.Type clazz = (System.Type)classByAlias[rootAlias];
            IType       type  = (( IPropertyMapping )factory.GetPersister(clazz)).ToType(associationPath);

            if (!type.IsAssociationType)
            {
                throw new QueryException(string.Format("not an association path: {0}", associationPath));
            }

            return((( IAssociationType )type).GetAssociatedClass(factory));
        }
        private SqlString DiscriminatorWhereCondition(string alias)
        {
            InFragment frag = new InFragment()
                              .SetColumn(alias, DiscriminatorColumnName);

            System.Type[] subclasses = SubclassClosure;
            for (int i = 0; i < subclasses.Length; i++)
            {
                frag.AddValue(
                    (( IQueryable )factory.GetPersister(subclasses[i])).DiscriminatorSQLValue
                    );
            }

            return(frag.ToFragmentString());
        }
Esempio n. 4
0
        public override string ToString(object value, ISessionFactoryImplementor factory)
        {
            IClassPersister persister = factory.GetPersister(associatedClass);

            if (value == null)
            {
                return("null");
            }
            StringBuilder result = new StringBuilder();

            result.Append(StringHelper.Unqualify(NHibernateProxyHelper.GetClass(value).FullName));
            if (persister.HasIdentifierProperty)
            {
                result.Append('#')
                .Append(persister.IdentifierType.ToString(NHibernateProxyHelper.GetIdentifier(value, persister), factory));
            }

            return(result.ToString());
        }
Esempio n. 5
0
        /// <summary>
        /// Does the mapping, and Hibernate default semantics, specify that
        /// this association should be fetched by outer joining
        /// </summary>
        /// <param name="config"></param>
        /// <param name="type"></param>
        /// <param name="factory"></param>
        /// <returns></returns>
        protected bool IsJoinedFetchEnabledByDefault(OuterJoinFetchStrategy config, IAssociationType type, ISessionFactoryImplementor factory)
        {
            if (!type.IsEntityType && !type.IsPersistentCollectionType)
            {
                return(false);
            }
            else
            {
                switch (config)
                {
                case OuterJoinFetchStrategy.Eager:
                    return(true);

                case OuterJoinFetchStrategy.Lazy:
                    return(false);

                case OuterJoinFetchStrategy.Auto:
                    if (!factory.IsOuterJoinedFetchEnabled)
                    {
                        return(false);
                    }
                    if (type.IsEntityType)
                    {
                        EntityType      entityType = type as EntityType;
                        IClassPersister persister  = factory.GetPersister(entityType.AssociatedClass);
                        return(!persister.HasProxy || (
                                   entityType.IsOneToOne &&
                                   ((OneToOneType)entityType).IsNullable
                                   ));
                    }
                    else
                    {
                        return(false);
                    }

                default:
                    throw new ArgumentOutOfRangeException("config", config, "Unknown OJ strategy " + config.ToString());
                }
            }
        }
Esempio n. 6
0
 public IJoinable GetJoinable(ISessionFactoryImplementor factory)
 {
     return(( IJoinable )factory.GetPersister(associatedClass));
 }
		public AbstractCollectionPersister( Mapping.Collection collection, ISessionFactoryImplementor factory )
		{
			this.factory = factory;
			dialect = factory.Dialect;
			//sqlExceptionConverter = factory.SQLExceptionConverter;
			collectionType = collection.CollectionType;
			role = collection.Role;
			ownerClass = collection.OwnerClass;
			Alias alias = new Alias( "__" );

			sqlOrderByString = collection.OrderBy;
			hasOrder = sqlOrderByString != null;
			sqlOrderByStringTemplate = hasOrder ? Template.RenderOrderByStringTemplate( sqlOrderByString, dialect ) : null;

			sqlWhereString = collection.Where;
			hasWhere = sqlWhereString != null;
			sqlWhereStringTemplate = hasWhere ? Template.RenderWhereStringTemplate( sqlWhereString, dialect ) : null;

			hasOrphanDelete = collection.OrphanDelete;

			batchSize = collection.BatchSize;

			cache = collection.Cache;

			keyType = collection.Key.Type;
			int keySpan = collection.Key.ColumnSpan;
			keyColumnNames = new string[keySpan];
			string[ ] keyAliases = new string[keySpan];
			int k = 0;
			foreach( Column col in collection.Key.ColumnCollection )
			{
				keyColumnNames[ k ] = col.GetQuotedName( dialect );
				keyAliases[ k ] = col.Alias( dialect );
				k++;
			}
			keyColumnAliases = alias.ToAliasStrings( keyAliases, dialect );
			//unquotedKeyColumnNames = StringHelper.Unquote( keyColumnAliases );
			ISet distinctColumns = new HashedSet();
			CheckColumnDuplication( distinctColumns, collection.Key.ColumnCollection );

			//isSet = collection.IsSet;
			//isSorted = collection.IsSorted;
			primitiveArray = collection.IsPrimitiveArray;
			array = collection.IsArray;

			IValue element = collection.Element;
			int elementSpan = element.ColumnSpan;
			ICollection iter = element.ColumnCollection;
			Table table = collection.CollectionTable;
			enableJoinedFetch = element.OuterJoinFetchSetting;
			elementType = element.Type;

			if( !collection.IsOneToMany )
			{
				CheckColumnDuplication( distinctColumns, element.ColumnCollection );
			}

			if( elementType.IsEntityType )
			{
				elementPersister = factory.GetPersister( ( ( EntityType ) elementType ).AssociatedClass );
			}
			else
			{
				elementPersister = null;
			}

			qualifiedTableName = table.GetQualifiedName( dialect, factory.DefaultSchema );
			string[ ] aliases = new string[elementSpan];
			elementColumnNames = new string[elementSpan];
			int j = 0;
			foreach( Column col in iter )
			{
				elementColumnNames[ j ] = col.GetQuotedName( dialect );
				aliases[ j ] = col.Alias( dialect );
				j++;
			}

			elementColumnAliases = alias.ToAliasStrings( aliases, dialect );

			IType selectColumns;
			string[ ] selectType;

			hasIndex = collection.IsIndexed;

			if( hasIndex )
			{
				IndexedCollection indexedCollection = ( IndexedCollection ) collection;

				indexType = indexedCollection.Index.Type;
				int indexSpan = indexedCollection.Index.ColumnSpan;
				indexColumnNames = new string[indexSpan];

				string[ ] indexAliases = new string[indexSpan];
				int i = 0;
				foreach( Column indexCol in indexedCollection.Index.ColumnCollection )
				{
					indexAliases[ i ] = indexCol.Alias( dialect );
					indexColumnNames[ i ] = indexCol.GetQuotedName( dialect );
					i++;
				}
				selectType = indexColumnNames;
				selectColumns = indexType;
				indexColumnAliases = alias.ToAliasStrings( indexAliases, dialect );
				CheckColumnDuplication( distinctColumns, indexedCollection.Index.ColumnCollection );
			}
			else
			{
				indexType = null;
				indexColumnNames = null;
				indexColumnAliases = null;
				selectType = elementColumnNames;
				selectColumns = elementType;
			}

			hasIdentifier = collection.IsIdentified;

			if( hasIdentifier )
			{
				if( collection.IsOneToMany )
				{
					throw new MappingException( "one-to-many collections with identifiers are not supported." );
				}
				IdentifierCollection idColl = ( IdentifierCollection ) collection;
				identifierType = idColl.Identifier.Type;

				Column col = null;
				foreach( Column column in idColl.Identifier.ColumnCollection )
				{
					col = column;
					break;
				}

				identifierColumnName = col.GetQuotedName( dialect );
				selectType = new string[ ] {identifierColumnName};
				selectColumns = identifierType;
				identifierColumnAlias = alias.ToAliasString( col.Alias( dialect ), dialect );
				unquotedIdentifierColumnName = identifierColumnAlias;
				identifierGenerator = idColl.Identifier.CreateIdentifierGenerator( dialect );
				CheckColumnDuplication( distinctColumns, idColl.Identifier.ColumnCollection );
			}
			else
			{
				identifierType = null;
				identifierColumnName = null;
				identifierColumnAlias = null;
				unquotedIdentifierColumnName = null;
				identifierGenerator = null;
			}

			rowSelectColumnNames = selectType;
			rowSelectType = selectColumns;

			sqlDeleteString = GenerateDeleteString();
			sqlInsertRowString = GenerateInsertRowString();
			sqlUpdateRowString = GenerateUpdateRowString();
			sqlDeleteRowString = GenerateDeleteRowString();
			isLazy = collection.IsLazy;

			isInverse = collection.IsInverse;

			if( collection.IsArray )
			{
				elementClass = ( ( Array ) collection ).ElementClass;
			}
			else
			{
				// for non-arrays, we don't need to know the element class
				elementClass = null;
			}

			initializer = CreateCollectionInitializer( factory );

			if( elementType.IsComponentType )
			{
				elementPropertyMapping = new CompositeElementPropertyMapping( elementColumnNames, ( IAbstractComponentType ) elementType, factory );
			}
			else if( !elementType.IsEntityType )
			{
				elementPropertyMapping = new ElementPropertyMapping( elementColumnNames, elementType );
			}
			else
			{
				IClassPersister persister = factory.GetPersister( ( ( EntityType ) elementType ).AssociatedClass );
				// Not all classpersisters implement IPropertyMapping!
				if( persister is IPropertyMapping )
				{
					elementPropertyMapping = ( IPropertyMapping ) persister;
				}
				else
				{
					elementPropertyMapping = new ElementPropertyMapping( elementColumnNames, elementType );
				}
			}
		}
		/// <summary>
		/// Does the mapping, and Hibernate default semantics, specify that
		/// this association should be fetched by outer joining
		/// </summary>
		/// <param name="config"></param>
		/// <param name="type"></param>
		/// <param name="factory"></param>
		/// <returns></returns>
		protected bool IsJoinedFetchEnabledByDefault( OuterJoinFetchStrategy config, IAssociationType type, ISessionFactoryImplementor factory )
		{
			if ( !type.IsEntityType && !type.IsPersistentCollectionType )
			{
				return false;
			}
			else
			{
				switch ( config )
				{
					case OuterJoinFetchStrategy.Eager:
						return true;

					case OuterJoinFetchStrategy.Lazy:
						return false;

					case OuterJoinFetchStrategy.Auto:
						if ( !factory.IsOuterJoinedFetchEnabled )
						{
							return false;
						}
						if ( type.IsEntityType )
						{
							EntityType entityType = type as EntityType;
							IClassPersister persister = factory.GetPersister( entityType.AssociatedClass );
							return !persister.HasProxy || (
								entityType.IsOneToOne &&
								( (OneToOneType) entityType).IsNullable
								);
						}
						else
						{
							return false;
						}

					default:
						throw new ArgumentOutOfRangeException( "config", config, "Unknown OJ strategy " + config.ToString() );
				}
			}
		}
		protected void CreateUniqueKeyLoaders( ISessionFactoryImplementor factory )
		{
			// TODO: Does not handle components, or properties of a joined subclass
			for( int i = 0; i < propertyNames.Length; i++ )
			{
				string[ ] columns = ( string[ ] ) uniqueKeyColumns[ propertyNames[ i ] ];
				if( columns != null )
				{
					IType uniqueKeyType = propertyTypes[ i ];
					if( uniqueKeyType.IsEntityType )
					{
						System.Type clazz = ( ( EntityType ) uniqueKeyType ).AssociatedClass;
						uniqueKeyType = factory.GetPersister( clazz ).IdentifierType;
					}
					uniqueKeyLoaders.Add( propertyNames[ i ], new EntityLoader( this, columns, uniqueKeyType, 1, factory ) );
				}
			}
		}
 private static IQueryable GetPropertyMapping(System.Type persistentClass, ISessionFactoryImplementor sessionFactory)
 {
     return(( IQueryable )sessionFactory.GetPersister(persistentClass));
 }
		private static IQueryable GetPropertyMapping( System.Type persistentClass, ISessionFactoryImplementor sessionFactory )
		{
			return ( IQueryable ) sessionFactory.GetPersister( persistentClass );
		}
Esempio n. 12
0
        public AbstractCollectionPersister(Mapping.Collection collection, ISessionFactoryImplementor factory)
        {
            this.factory = factory;
            dialect      = factory.Dialect;
            //sqlExceptionConverter = factory.SQLExceptionConverter;
            collectionType = collection.CollectionType;
            role           = collection.Role;
            ownerClass     = collection.OwnerClass;
            Alias alias = new Alias("__");

            sqlOrderByString         = collection.OrderBy;
            hasOrder                 = sqlOrderByString != null;
            sqlOrderByStringTemplate = hasOrder ? Template.RenderOrderByStringTemplate(sqlOrderByString, dialect) : null;

            sqlWhereString         = collection.Where;
            hasWhere               = sqlWhereString != null;
            sqlWhereStringTemplate = hasWhere ? Template.RenderWhereStringTemplate(sqlWhereString, dialect) : null;

            hasOrphanDelete = collection.OrphanDelete;

            batchSize = collection.BatchSize;

            cache = collection.Cache;

            keyType = collection.Key.Type;
            int keySpan = collection.Key.ColumnSpan;

            keyColumnNames = new string[keySpan];
            string[] keyAliases = new string[keySpan];
            int      k          = 0;

            foreach (Column col in collection.Key.ColumnCollection)
            {
                keyColumnNames[k] = col.GetQuotedName(dialect);
                keyAliases[k]     = col.Alias(dialect);
                k++;
            }
            keyColumnAliases = alias.ToAliasStrings(keyAliases, dialect);
            //unquotedKeyColumnNames = StringHelper.Unquote( keyColumnAliases );
            ISet distinctColumns = new HashedSet();

            CheckColumnDuplication(distinctColumns, collection.Key.ColumnCollection);

            //isSet = collection.IsSet;
            //isSorted = collection.IsSorted;
            primitiveArray = collection.IsPrimitiveArray;
            array          = collection.IsArray;

            IValue      element     = collection.Element;
            int         elementSpan = element.ColumnSpan;
            ICollection iter        = element.ColumnCollection;
            Table       table       = collection.CollectionTable;

            enableJoinedFetch = element.OuterJoinFetchSetting;
            elementType       = element.Type;

            if (!collection.IsOneToMany)
            {
                CheckColumnDuplication(distinctColumns, element.ColumnCollection);
            }

            if (elementType.IsEntityType)
            {
                elementPersister = factory.GetPersister((( EntityType )elementType).AssociatedClass);
            }
            else
            {
                elementPersister = null;
            }

            qualifiedTableName = table.GetQualifiedName(dialect, factory.DefaultSchema);
            string[] aliases = new string[elementSpan];
            elementColumnNames = new string[elementSpan];
            int j = 0;

            foreach (Column col in iter)
            {
                elementColumnNames[j] = col.GetQuotedName(dialect);
                aliases[j]            = col.Alias(dialect);
                j++;
            }

            elementColumnAliases = alias.ToAliasStrings(aliases, dialect);

            IType selectColumns;

            string[] selectType;

            hasIndex = collection.IsIndexed;

            if (hasIndex)
            {
                IndexedCollection indexedCollection = ( IndexedCollection )collection;

                indexType = indexedCollection.Index.Type;
                int indexSpan = indexedCollection.Index.ColumnSpan;
                indexColumnNames = new string[indexSpan];

                string[] indexAliases = new string[indexSpan];
                int      i            = 0;
                foreach (Column indexCol in indexedCollection.Index.ColumnCollection)
                {
                    indexAliases[i]     = indexCol.Alias(dialect);
                    indexColumnNames[i] = indexCol.GetQuotedName(dialect);
                    i++;
                }
                selectType         = indexColumnNames;
                selectColumns      = indexType;
                indexColumnAliases = alias.ToAliasStrings(indexAliases, dialect);
                CheckColumnDuplication(distinctColumns, indexedCollection.Index.ColumnCollection);
            }
            else
            {
                indexType          = null;
                indexColumnNames   = null;
                indexColumnAliases = null;
                selectType         = elementColumnNames;
                selectColumns      = elementType;
            }

            hasIdentifier = collection.IsIdentified;

            if (hasIdentifier)
            {
                if (collection.IsOneToMany)
                {
                    throw new MappingException("one-to-many collections with identifiers are not supported.");
                }
                IdentifierCollection idColl = ( IdentifierCollection )collection;
                identifierType = idColl.Identifier.Type;

                Column col = null;
                foreach (Column column in idColl.Identifier.ColumnCollection)
                {
                    col = column;
                    break;
                }

                identifierColumnName         = col.GetQuotedName(dialect);
                selectType                   = new string[] { identifierColumnName };
                selectColumns                = identifierType;
                identifierColumnAlias        = alias.ToAliasString(col.Alias(dialect), dialect);
                unquotedIdentifierColumnName = identifierColumnAlias;
                identifierGenerator          = idColl.Identifier.CreateIdentifierGenerator(dialect);
                CheckColumnDuplication(distinctColumns, idColl.Identifier.ColumnCollection);
            }
            else
            {
                identifierType               = null;
                identifierColumnName         = null;
                identifierColumnAlias        = null;
                unquotedIdentifierColumnName = null;
                identifierGenerator          = null;
            }

            rowSelectColumnNames = selectType;
            rowSelectType        = selectColumns;

            sqlDeleteString    = GenerateDeleteString();
            sqlInsertRowString = GenerateInsertRowString();
            sqlUpdateRowString = GenerateUpdateRowString();
            sqlDeleteRowString = GenerateDeleteRowString();
            isLazy             = collection.IsLazy;

            isInverse = collection.IsInverse;

            if (collection.IsArray)
            {
                elementClass = (( Array )collection).ElementClass;
            }
            else
            {
                // for non-arrays, we don't need to know the element class
                elementClass = null;
            }

            initializer = CreateCollectionInitializer(factory);

            if (elementType.IsComponentType)
            {
                elementPropertyMapping = new CompositeElementPropertyMapping(elementColumnNames, ( IAbstractComponentType )elementType, factory);
            }
            else if (!elementType.IsEntityType)
            {
                elementPropertyMapping = new ElementPropertyMapping(elementColumnNames, elementType);
            }
            else
            {
                IClassPersister persister = factory.GetPersister((( EntityType )elementType).AssociatedClass);
                // Not all classpersisters implement IPropertyMapping!
                if (persister is IPropertyMapping)
                {
                    elementPropertyMapping = ( IPropertyMapping )persister;
                }
                else
                {
                    elementPropertyMapping = new ElementPropertyMapping(elementColumnNames, elementType);
                }
            }
        }