public SimpleEntityLoader( ILoadable persister, SqlString sql, LockMode lockMode )
		{
			this.persister = new ILoadable[ ] {persister};
			this.idType = persister.IdentifierType;
			this.sql = sql;
			this.lockMode = new LockMode[ ] {lockMode};
			PostInstantiate();
		}
Example #2
0
		protected void InitPersisters(IList<OuterJoinableAssociation> associations, LockMode lockMode)
		{
			int joins = CountEntityPersisters(associations);
			int collections = CountCollectionPersisters(associations);

			collectionOwners = collections == 0 ? null : new int[collections];
			collectionPersisters = collections == 0 ? null : new ICollectionPersister[collections];
			collectionSuffixes = BasicLoader.GenerateSuffixes(joins + 1, collections);

			persisters = new ILoadable[joins];
			aliases = new String[joins];
			owners = new int[joins];
			ownerAssociationTypes = new EntityType[joins];
			lockModeArray = ArrayHelper.FillArray(lockMode, joins);

			int i = 0;
			int j = 0;

			foreach (OuterJoinableAssociation oj in associations)
			{
				if (!oj.IsCollection)
				{
					persisters[i] = (ILoadable)oj.Joinable;
					aliases[i] = oj.RHSAlias;
					owners[i] = oj.GetOwner(associations);
					ownerAssociationTypes[i] = (EntityType)oj.JoinableType;
					i++;
				}
				else
				{
					IQueryableCollection collPersister = (IQueryableCollection)oj.Joinable;

					if (oj.JoinType == JoinType.LeftOuterJoin)
					{
						//it must be a collection fetch
						collectionPersisters[j] = collPersister;
						collectionOwners[j] = oj.GetOwner(associations);
						j++;
					}

					if (collPersister.IsOneToMany)
					{
						persisters[i] = (ILoadable)collPersister.ElementPersister;
						aliases[i] = oj.RHSAlias;
						i++;
					}
				}
			}

			if (ArrayHelper.IsAllNegative(owners))
				owners = null;

			if (collectionOwners != null && ArrayHelper.IsAllNegative(collectionOwners))
				collectionOwners = null;
		}
Example #3
0
		private void Initialize(SelectClause selectClause)
		{
			IList<FromElement> fromElementList = selectClause.FromElementsForLoad;

			_hasScalars = selectClause.IsScalarSelect;
			_scalarColumnNames = selectClause.ColumnNames;
			//sqlResultTypes = selectClause.getSqlResultTypes();
			_queryReturnTypes = selectClause.QueryReturnTypes;

			_selectNewTransformer = HolderInstantiator.CreateSelectNewTransformer(selectClause.Constructor, selectClause.IsMap, selectClause.IsList);
			_queryReturnAliases = selectClause.QueryReturnAliases;

			IList<FromElement> collectionFromElements = selectClause.CollectionFromElements;
			if (collectionFromElements != null && collectionFromElements.Count != 0)
			{
				int length = collectionFromElements.Count;
				_collectionPersisters = new IQueryableCollection[length];
				_collectionOwners = new int[length];
				_collectionSuffixes = new string[length];

				for (int i = 0; i < length; i++)
				{
					FromElement collectionFromElement = collectionFromElements[i];
					_collectionPersisters[i] = collectionFromElement.QueryableCollection;
					_collectionOwners[i] = fromElementList.IndexOf(collectionFromElement.Origin);
					//				collectionSuffixes[i] = collectionFromElement.getColumnAliasSuffix();
					//				collectionSuffixes[i] = Integer.toString( i ) + "_";
					_collectionSuffixes[i] = collectionFromElement.CollectionSuffix;
				}
			}

			int size = fromElementList.Count;
			_entityPersisters = new IQueryable[size];
			_entityEagerPropertyFetches = new bool[size];
			_entityAliases = new String[size];
			_sqlAliases = new String[size];
			_sqlAliasSuffixes = new String[size];
			_includeInSelect = new bool[size];
			_owners = new int[size];
			_ownerAssociationTypes = new EntityType[size];

			for (int i = 0; i < size; i++)
			{
				FromElement element = fromElementList[i];
				_entityPersisters[i] = (IQueryable) element.EntityPersister;

				if (_entityPersisters[i] == null)
				{
					throw new InvalidOperationException("No entity persister for " + element);
				}

				_entityEagerPropertyFetches[i] = element.IsAllPropertyFetch;
				_sqlAliases[i] = element.TableAlias;
				_entityAliases[i] = element.ClassAlias;
				_sqlAliasByEntityAlias.Add(_entityAliases[i], _sqlAliases[i]);
				// TODO should we just collect these like with the collections above?
				_sqlAliasSuffixes[i] = (size == 1) ? "" : i + "_";
				//			sqlAliasSuffixes[i] = element.getColumnAliasSuffix();
				_includeInSelect[i] = !element.IsFetch;
				if (_includeInSelect[i])
				{
					_selectLength++;
				}

				_owners[i] = -1; //by default
				if (element.IsFetch)
				{
					if (element.IsCollectionJoin || element.QueryableCollection != null)
					{
						// This is now handled earlier in this method.
					}
					else if (element.DataType.IsEntityType)
					{
						var entityType = (EntityType) element.DataType;
						if (entityType.IsOneToOne)
						{
							_owners[i] = fromElementList.IndexOf(element.Origin);
						}
						_ownerAssociationTypes[i] = entityType;
					}
				}
			}

			//NONE, because its the requested lock mode, not the actual! 
			_defaultLockModes = ArrayHelper.Fill(LockMode.None, size);
		}
Example #4
0
        public CustomLoader(
            ICustomQuery customQuery,
            ISessionFactoryImplementor factory)
            : base(factory)
        {
            this.sql = customQuery.SQL;
            this.querySpaces.AddAll(customQuery.QuerySpaces);
            this.namedParameterBindPoints = customQuery.NamedParameterBindPoints;

            IList entityPersisters = new ArrayList();
            IList entityOwners = new ArrayList();
            IList entityAliases = new ArrayList();

            IList collectionPersisters = new ArrayList();
            IList collectionOwners = new ArrayList();
            IList collectionAliases = new ArrayList();

            IList lockModes = new ArrayList();
            IList resultColumnProcessors = new ArrayList();
            IList nonScalarReturnList = new ArrayList();
            IList resultTypes = new ArrayList();
            IList specifiedAliases = new ArrayList();

            int returnableCounter = 0;
            bool hasScalars = false;

            foreach (IReturn rtn in customQuery.CustomQueryReturns)
            {
                if (rtn is ScalarReturn)
                {
                    ScalarReturn scalarRtn = (ScalarReturn) rtn;
                    resultTypes.Add(scalarRtn.Type);
                    specifiedAliases.Add(scalarRtn.ColumnAlias);
                    resultColumnProcessors.Add(
                        new ScalarResultColumnProcessor(
                            scalarRtn.ColumnAlias,
                            scalarRtn.Type
                            )
                        );
                    hasScalars = true;
                }
                else if (rtn is RootReturn)
                {
                    RootReturn rootRtn = (RootReturn) rtn;
                    IQueryable persister = (IQueryable) factory.GetEntityPersister(rootRtn.EntityName);
                    entityPersisters.Add(persister);
                    lockModes.Add(rootRtn.LockMode);
                    resultColumnProcessors.Add(new NonScalarResultColumnProcessor(returnableCounter++));
                    nonScalarReturnList.Add(rtn);
                    entityOwners.Add(-1);
                    resultTypes.Add(persister.Type);
                    specifiedAliases.Add(rootRtn.Alias);
                    entityAliases.Add(rootRtn.EntityAliases);
                    querySpaces.AddAll(persister.QuerySpaces);
                }
                else if (rtn is CollectionReturn)
                {
                    CollectionReturn collRtn = (CollectionReturn) rtn;
                    String role = collRtn.OwnerEntityName + "." + collRtn.OwnerProperty;
                    IQueryableCollection persister = (IQueryableCollection) factory.GetCollectionPersister(role);
                    collectionPersisters.Add(persister);
                    lockModes.Add(collRtn.LockMode);
                    resultColumnProcessors.Add(new NonScalarResultColumnProcessor(returnableCounter++));
                    nonScalarReturnList.Add(rtn);
                    collectionOwners.Add(-1);
                    resultTypes.Add(persister.Type);
                    specifiedAliases.Add(collRtn.Alias);
                    collectionAliases.Add(collRtn.CollectionAliases);
                    // determine if the collection elements are entities...
                    IType elementType = persister.ElementType;
                    if (elementType.IsEntityType)
                    {
                        IQueryable elementPersister = (IQueryable) ((EntityType) elementType).GetAssociatedJoinable(factory);
                        entityPersisters.Add(elementPersister);
                        entityOwners.Add(-1);
                        entityAliases.Add(collRtn.ElementEntityAliases);
                        querySpaces.AddAll(elementPersister.QuerySpaces);
                    }
                }
                else if (rtn is EntityFetchReturn)
                {
                    EntityFetchReturn fetchRtn = (EntityFetchReturn) rtn;
                    NonScalarReturn ownerDescriptor = fetchRtn.Owner;
                    int ownerIndex = nonScalarReturnList.IndexOf(ownerDescriptor);
                    entityOwners.Add(ownerIndex);
                    lockModes.Add(fetchRtn.LockMode);
                    IQueryable ownerPersister = DetermineAppropriateOwnerPersister(ownerDescriptor);
                    EntityType fetchedType = (EntityType) ownerPersister.GetPropertyType(fetchRtn.OwnerProperty);
                    string entityName = fetchedType.GetAssociatedEntityName(Factory);
                    IQueryable persister = (IQueryable) factory.GetEntityPersister(entityName);
                    entityPersisters.Add(persister);
                    nonScalarReturnList.Add(rtn);
                    specifiedAliases.Add(fetchRtn.Alias);
                    entityAliases.Add(fetchRtn.EntityAliases);
                    querySpaces.AddAll(persister.QuerySpaces);
                }
                else if (rtn is CollectionFetchReturn)
                {
                    CollectionFetchReturn fetchRtn = (CollectionFetchReturn) rtn;
                    NonScalarReturn ownerDescriptor = fetchRtn.Owner;
                    int ownerIndex = nonScalarReturnList.IndexOf(ownerDescriptor);
                    collectionOwners.Add(ownerIndex);
                    lockModes.Add(fetchRtn.LockMode);
                    IQueryable ownerPersister = DetermineAppropriateOwnerPersister(ownerDescriptor);
                    String role = ownerPersister.EntityName + '.' + fetchRtn.OwnerProperty;
                    IQueryableCollection persister = (IQueryableCollection) factory.GetCollectionPersister(role);
                    collectionPersisters.Add(persister);
                    nonScalarReturnList.Add(rtn);
                    specifiedAliases.Add(fetchRtn.Alias);
                    collectionAliases.Add(fetchRtn.CollectionAliases);
                    // determine if the collection elements are entities...
                    IType elementType = persister.ElementType;
                    if (elementType.IsEntityType)
                    {
                        IQueryable elementPersister = (IQueryable) ((EntityType) elementType).GetAssociatedJoinable(factory);
                        entityPersisters.Add(elementPersister);
                        entityOwners.Add(ownerIndex);
                        entityAliases.Add(fetchRtn.ElementEntityAliases);
                        querySpaces.AddAll(elementPersister.QuerySpaces);
                    }
                }
                else
                {
                    throw new HibernateException("unexpected custom query return type : " + rtn.GetType().FullName);
                }
            }

            this.entityPersisters = new IQueryable[entityPersisters.Count];
            for (int i = 0; i < entityPersisters.Count; i++)
            {
                this.entityPersisters[i] = (IQueryable) entityPersisters[i];
            }
            this.entityOwners = ArrayHelper.ToIntArray(entityOwners);
            this.entityAliases = new IEntityAliases[entityAliases.Count];
            for (int i = 0; i < entityAliases.Count; i++)
            {
                this.entityAliases[i] = (IEntityAliases) entityAliases[i];
            }

            this.collectionPersisters = new IQueryableCollection[collectionPersisters.Count];
            for (int i = 0; i < collectionPersisters.Count; i++)
            {
                this.collectionPersisters[i] = (IQueryableCollection) collectionPersisters[i];
            }
            this.collectionOwners = ArrayHelper.ToIntArray(collectionOwners);
            this.collectionAliases = new ICollectionAliases[collectionAliases.Count];
            for (int i = 0; i < collectionAliases.Count; i++)
            {
                this.collectionAliases[i] = (ICollectionAliases) collectionAliases[i];
            }

            this.lockModes = new LockMode[lockModes.Count];
            for (int i = 0; i < lockModes.Count; i++)
            {
                this.lockModes[i] = (LockMode) lockModes[i];
            }

            this.resultTypes = ArrayHelper.ToTypeArray(resultTypes);
            this.transformerAliases = ArrayHelper.ToStringArray(specifiedAliases);

            this.rowProcessor = new ResultRowProcessor(
                hasScalars,
                (ResultColumnProcessor[]) ArrayHelper.ToArray(resultColumnProcessors, typeof(ResultColumnProcessor))
                );
        }
		public CustomLoader(ICustomQuery customQuery, ISessionFactoryImplementor factory) : base(factory)
		{
			sql = customQuery.SQL;
			querySpaces.AddAll(customQuery.QuerySpaces);
			namedParameterBindPoints = customQuery.NamedParameterBindPoints;

			List<IQueryable> entitypersisters = new List<IQueryable>();
			List<int> entityowners = new List<int>();
			List<IEntityAliases> entityaliases = new List<IEntityAliases>();

			List<IQueryableCollection> collectionpersisters = new List<IQueryableCollection>();
			List<int> collectionowners = new List<int>();
			List<ICollectionAliases> collectionaliases = new List<ICollectionAliases>();

			List<LockMode> lockmodes = new List<LockMode>();
			List<IResultColumnProcessor> resultColumnProcessors = new List<IResultColumnProcessor>();
			List<IReturn> nonScalarReturnList = new List<IReturn>();
			List<IType> resulttypes = new List<IType>();
			List<string> specifiedAliases = new List<string>();

			int returnableCounter = 0;
			bool hasScalars = false;

			foreach (IReturn rtn in customQuery.CustomQueryReturns)
			{
				if (rtn is ScalarReturn)
				{
					ScalarReturn scalarRtn = (ScalarReturn) rtn;
					resulttypes.Add(scalarRtn.Type);
					specifiedAliases.Add(scalarRtn.ColumnAlias);
					resultColumnProcessors.Add(new ScalarResultColumnProcessor(scalarRtn.ColumnAlias, scalarRtn.Type));
					hasScalars = true;
				}
				else if (rtn is RootReturn)
				{
					RootReturn rootRtn = (RootReturn) rtn;
					IQueryable persister = (IQueryable) factory.GetEntityPersister(rootRtn.EntityName);
					entitypersisters.Add(persister);
					lockmodes.Add(rootRtn.LockMode);
					resultColumnProcessors.Add(new NonScalarResultColumnProcessor(returnableCounter++));
					nonScalarReturnList.Add(rtn);
					entityowners.Add(-1);
					resulttypes.Add(persister.Type);
					specifiedAliases.Add(rootRtn.Alias);
					entityaliases.Add(rootRtn.EntityAliases);
					querySpaces.AddAll(persister.QuerySpaces);
				}
				else if (rtn is CollectionReturn)
				{
					CollectionReturn collRtn = (CollectionReturn) rtn;
					string role = collRtn.OwnerEntityName + "." + collRtn.OwnerProperty;
					IQueryableCollection persister = (IQueryableCollection) factory.GetCollectionPersister(role);
					collectionpersisters.Add(persister);
					lockmodes.Add(collRtn.LockMode);
					resultColumnProcessors.Add(new NonScalarResultColumnProcessor(returnableCounter++));
					nonScalarReturnList.Add(rtn);
					collectionowners.Add(-1);
					resulttypes.Add(persister.Type);
					specifiedAliases.Add(collRtn.Alias);
					collectionaliases.Add(collRtn.CollectionAliases);
					// determine if the collection elements are entities...
					IType elementType = persister.ElementType;
					if (elementType.IsEntityType)
					{
						IQueryable elementPersister = (IQueryable) ((EntityType) elementType).GetAssociatedJoinable(factory);
						entitypersisters.Add(elementPersister);
						entityowners.Add(-1);
						entityaliases.Add(collRtn.ElementEntityAliases);
						querySpaces.AddAll(elementPersister.QuerySpaces);
					}
				}
				else if (rtn is EntityFetchReturn)
				{
					EntityFetchReturn fetchRtn = (EntityFetchReturn) rtn;
					NonScalarReturn ownerDescriptor = fetchRtn.Owner;
					int ownerIndex = nonScalarReturnList.IndexOf(ownerDescriptor);
					entityowners.Add(ownerIndex);
					lockmodes.Add(fetchRtn.LockMode);
					IQueryable ownerPersister = DetermineAppropriateOwnerPersister(ownerDescriptor);
					EntityType fetchedType = (EntityType) ownerPersister.GetPropertyType(fetchRtn.OwnerProperty);
					string entityName = fetchedType.GetAssociatedEntityName(Factory);
					IQueryable persister = (IQueryable) factory.GetEntityPersister(entityName);
					entitypersisters.Add(persister);
					nonScalarReturnList.Add(rtn);
					specifiedAliases.Add(fetchRtn.Alias);
					entityaliases.Add(fetchRtn.EntityAliases);
					querySpaces.AddAll(persister.QuerySpaces);
				}
				else if (rtn is CollectionFetchReturn)
				{
					CollectionFetchReturn fetchRtn = (CollectionFetchReturn) rtn;
					NonScalarReturn ownerDescriptor = fetchRtn.Owner;
					int ownerIndex = nonScalarReturnList.IndexOf(ownerDescriptor);
					collectionowners.Add(ownerIndex);
					lockmodes.Add(fetchRtn.LockMode);
					IQueryable ownerPersister = DetermineAppropriateOwnerPersister(ownerDescriptor);
					string role = ownerPersister.EntityName + '.' + fetchRtn.OwnerProperty;
					IQueryableCollection persister = (IQueryableCollection) factory.GetCollectionPersister(role);
					collectionpersisters.Add(persister);
					nonScalarReturnList.Add(rtn);
					specifiedAliases.Add(fetchRtn.Alias);
					collectionaliases.Add(fetchRtn.CollectionAliases);
					// determine if the collection elements are entities...
					IType elementType = persister.ElementType;
					if (elementType.IsEntityType)
					{
						IQueryable elementPersister = (IQueryable) ((EntityType) elementType).GetAssociatedJoinable(factory);
						entitypersisters.Add(elementPersister);
						entityowners.Add(ownerIndex);
						entityaliases.Add(fetchRtn.ElementEntityAliases);
						querySpaces.AddAll(elementPersister.QuerySpaces);
					}
				}
				else
				{
					throw new HibernateException("unexpected custom query return type : " + rtn.GetType().FullName);
				}
			}

			entityPersisters = entitypersisters.ToArray();
			entityOwners = entityowners.ToArray();
			entityAliases = entityaliases.ToArray();
			collectionPersisters = collectionpersisters.ToArray();
			collectionOwners = collectionowners.ToArray();
			collectionAliases = collectionaliases.ToArray();
			lockModes = lockmodes.ToArray();
			resultTypes = resulttypes.ToArray();
			transformerAliases = specifiedAliases.ToArray();
			rowProcessor = new ResultRowProcessor(hasScalars, resultColumnProcessors.ToArray());
		}
Example #6
0
		protected void InitFromWalker(JoinWalker walker)
		{
			persisters = walker.Persisters;
			collectionPersisters = walker.CollectionPersisters;
			ownerAssociationTypes = walker.OwnerAssociationTypes;
			lockModeArray = walker.LockModeArray;
			suffixes = walker.Suffixes;
			collectionSuffixes = walker.CollectionSuffixes;
			owners = walker.Owners;
			collectionOwners = walker.CollectionOwners;
			sql = walker.SqlString;
			aliases = walker.Aliases;
		}
Example #7
0
		public CustomLoader(ICustomQuery customQuery, ISessionFactoryImplementor factory) : base(factory)
		{
			this.sql = customQuery.SQL;
			this.querySpaces.AddAll(customQuery.QuerySpaces);
			this.parametersSpecifications = customQuery.CollectedParametersSpecifications.ToList();

			var entityPersisters = new List<IEntityPersister>();
			var entityOwners = new List<int>();
			var entityAliases = new List<IEntityAliases>();
			var collectionPersisters = new List<ICollectionPersister>();
			var collectionOwners = new List<int>();
			var collectionAliases = new List<ICollectionAliases>();
			var lockModes = new List<LockMode>();
			var resultColumnProcessors = new List<IResultColumnProcessor>();
			var resultTypes = new List<IType>();
			var transformerAliases = new List<string>();

			int returnableCounter = 0;
			bool hasScalars = false;

			foreach (IReturn rtn in customQuery.CustomQueryReturns)
			{
				transformerAliases.Add(rtn.Alias);

				if (rtn is ScalarReturn)
				{
					resultTypes.Add(rtn.Type);
					resultColumnProcessors.Add(new ScalarResultColumnProcessor(rtn.Alias, rtn.Type));
					hasScalars = true;
					continue;
				}

				var nonScalarRtn = rtn as NonScalarReturn;
				if (nonScalarRtn != null)
				{
					lockModes.Add(nonScalarRtn.LockMode);

					var ownerIndex = nonScalarRtn.Owner != null
						? entityPersisters.IndexOf(nonScalarRtn.Owner.EntityPersister)
						: -1;
					if (nonScalarRtn.EntityPersister != null)
					{
						entityPersisters.Add(nonScalarRtn.EntityPersister);
						entityAliases.Add(nonScalarRtn.EntityAliases);
						entityOwners.Add(ownerIndex);
						querySpaces.AddAll(nonScalarRtn.EntityPersister.QuerySpaces);
					}
					if (nonScalarRtn.CollectionPersister != null)
					{
						collectionPersisters.Add(nonScalarRtn.CollectionPersister);
						collectionAliases.Add(nonScalarRtn.CollectionAliases);
						collectionOwners.Add(ownerIndex);
					}
					if (nonScalarRtn.Owner == null)
					{
						resultTypes.Add(nonScalarRtn.Type);
						resultColumnProcessors.Add(new NonScalarResultColumnProcessor(returnableCounter++));
					}

					continue;
				}
					
				throw new HibernateException("unexpected custom query return type : " + rtn.GetType().FullName);
			}

			this.entityPersisters = entityPersisters.Cast<ILoadable>().ToArray();
			this.entityOwners = entityOwners.ToArray();
			this.entityAliases = entityAliases.ToArray();
			this.collectionPersisters = collectionPersisters.ToArray();
			this.collectionOwners = collectionOwners.ToArray();
			this.collectionAliases = collectionAliases.ToArray();
			this.lockModes = lockModes.ToArray();
			this.resultTypes = resultTypes.ToArray();
			this.transformerAliases = transformerAliases.ToArray();
			this.rowProcessor = new ResultRowProcessor(hasScalars, resultColumnProcessors.ToArray());
		}