public QueryParameters GetQueryParameters()
        {
            RowSelection selection = new RowSelection();

            selection.FirstRow  = rootCriteria.FirstResult;
            selection.MaxRows   = rootCriteria.MaxResults;
            selection.Timeout   = rootCriteria.Timeout;
            selection.FetchSize = rootCriteria.FetchSize;

            var lockModes = new Dictionary <string, LockMode>();

            foreach (KeyValuePair <string, LockMode> me in rootCriteria.LockModes)
            {
                ICriteria subcriteria = GetAliasedCriteria(me.Key);
                lockModes[GetSQLAlias(subcriteria)] = me.Value;
            }

            foreach (CriteriaImpl.Subcriteria subcriteria in rootCriteria.IterateSubcriteria())
            {
                LockMode lm = subcriteria.LockMode;
                if (lm != null)
                {
                    lockModes[GetSQLAlias(subcriteria)] = lm;
                }
            }

            IDictionary <string, TypedValue> queryNamedParameters = CollectedParameters.ToDictionary(np => np.Name, np => new TypedValue(np.Type, np.Value));

            return
                (new QueryParameters(
                     queryNamedParameters,
                     lockModes,
                     selection,
                     rootCriteria.IsReadOnlyInitialized,
                     rootCriteria.IsReadOnlyInitialized ? rootCriteria.IsReadOnly : false,
                     rootCriteria.Cacheable,
                     rootCriteria.CacheRegion,
                     rootCriteria.Comment,
                     rootCriteria.LookupByNaturalKey,
                     rootCriteria.ResultTransformer)
            {
                CacheMode = rootCriteria.CacheMode
            });
        }
Ejemplo n.º 2
0
        public QueryParameters GetQueryParameters()
        {
            ArrayList    values = new ArrayList(usedTypedValues.Count);
            List <IType> types  = new List <IType>(usedTypedValues.Count);

            foreach (TypedValue value in usedTypedValues)
            {
                values.Add(value.Value);
                types.Add(value.Type);
            }

            object[] valueArray = values.ToArray();
            IType[]  typeArray  = types.ToArray();

            RowSelection selection = new RowSelection();

            selection.FirstRow  = rootCriteria.FirstResult;
            selection.MaxRows   = rootCriteria.MaxResults;
            selection.Timeout   = rootCriteria.Timeout;
            selection.FetchSize = rootCriteria.FetchSize;

            Dictionary <string, LockMode> lockModes = new Dictionary <string, LockMode>();

            foreach (KeyValuePair <string, LockMode> me in rootCriteria.LockModes)
            {
                ICriteria subcriteria = GetAliasedCriteria(me.Key);
                lockModes[GetSQLAlias(subcriteria)] = me.Value;
            }

            foreach (CriteriaImpl.Subcriteria subcriteria in rootCriteria.IterateSubcriteria())
            {
                LockMode lm = subcriteria.LockMode;
                if (lm != null)
                {
                    lockModes[GetSQLAlias(subcriteria)] = lm;
                }
            }

            return
                (new QueryParameters(typeArray, valueArray, lockModes, selection, rootCriteria.Cacheable, rootCriteria.CacheRegion,
                                     rootCriteria.Comment, rootCriteria.LookupByNaturalKey, rootCriteria.ResultTransformer, _tempPagingParameterIndexes));
        }
Ejemplo n.º 3
0
        private static void CloneSubcriteriaAndOrders(CriteriaImpl clone, CriteriaImpl original)
        {
            //we need to preserve the parent criteria, we rely on the orderring when creating the
            //subcriterias initially here, so we don't need to make more than a single pass
            Hashtable newParents = new Hashtable();
            newParents[original] = clone;

            foreach (CriteriaImpl.Subcriteria subcriteria in original.IterateSubcriteria())
            {
                ICriteria currentParent = (ICriteria)newParents[subcriteria.Parent];
                if (currentParent == null)
                {
                    throw new AssertionFailure(
                        "Could not find parent for subcriteria in the previous subcriteria. If you see this error, it is a bug");
                }
                CriteriaImpl.Subcriteria clonedSubCriteria =
                    new CriteriaImpl.Subcriteria(clone, currentParent, subcriteria.Path, subcriteria.Alias, subcriteria.JoinType);
                clonedSubCriteria.SetLockMode(subcriteria.LockMode);
                newParents[subcriteria] = clonedSubCriteria;
            }

            // remap the orders
            foreach (CriteriaImpl.OrderEntry orderEntry in original.IterateOrderings())
            {
                ICriteria currentParent = (ICriteria)newParents[orderEntry.Criteria];
                if (currentParent == null)
                {
                    throw new AssertionFailure(
                        "Could not find parent for order in the previous criteria. If you see this error, it is a bug");
                }
                currentParent.AddOrder(orderEntry.Order);
            }

            // remap the restrictions to appropriate criterias
            foreach (CriteriaImpl.CriterionEntry criterionEntry in original.Restrictions)
            {
                ICriteria currentParent = (ICriteria) newParents[criterionEntry.Criteria];
                if (currentParent == null)
                {
                    throw new AssertionFailure(
                        "Could not find parent for restriction in the previous criteria. If you see this error, it is a bug.");
                }

                currentParent.Add(criterionEntry.Criterion);
            }
        }
Ejemplo n.º 4
0
        private static ICriteria ToShardedCriteria(CriteriaImpl other, IShardedSessionImplementor shardedSession)
        {
            Preconditions.CheckNotNull(other);
            Preconditions.CheckNotNull(shardedSession);

            var entityName = other.EntityOrClassName;
            var alias      = other.Alias;

            ICriteria CriteriaFactory(ISession s) =>
            alias != null
                                        ? s.CreateCriteria(entityName, alias)
                                        : s.CreateCriteria(entityName);

            var result = new ShardedCriteriaImpl(shardedSession, other.EntityOrClassName, CriteriaFactory);

            foreach (var entry in other.IterateSubcriteria())
            {
                result.CreateCriteria(entry.Path, entry.Alias, entry.JoinType, entry.WithClause);
            }
            foreach (var entry in other.IterateExpressionEntries())
            {
                result.Add(entry.Criterion);
            }
            foreach (var entry in other.LockModes)
            {
                result.SetLockMode(entry.Key, entry.Value);
            }
            foreach (var entry in other.IterateOrderings())
            {
                result.AddOrder(entry.Order);
            }

            if (other.Cacheable)
            {
                result.SetCacheable(true);
                if (other.CacheMode != null)
                {
                    result.SetCacheMode(other.CacheMode.Value);
                }
                if (other.CacheRegion != null)
                {
                    result.SetCacheRegion(other.CacheRegion);
                }
            }

            if (other.Comment != null)
            {
                result.SetComment(other.Comment);
            }
            if (other.FetchSize > 0)
            {
                result.SetFetchSize(other.FetchSize);
            }
            if (other.FirstResult > 0)
            {
                result.SetFirstResult(other.FirstResult);
            }
            if (other.MaxResults > 0)
            {
                result.SetMaxResults(other.MaxResults);
            }
            if (other.Projection != null)
            {
                result.SetProjection(other.Projection);
            }
            if (other.ResultTransformer != null)
            {
                result.SetResultTransformer(other.ResultTransformer);
            }
            if (other.Timeout > 0)
            {
                result.SetTimeout(other.Timeout);
            }
            if (other.IsReadOnlyInitialized)
            {
                result.SetReadOnly(other.IsReadOnly);
            }

            return(result);
        }
Ejemplo n.º 5
0
        public QueryParameters GetQueryParameters()
        {
            RowSelection selection = new RowSelection();

            selection.FirstRow  = rootCriteria.FirstResult;
            selection.MaxRows   = rootCriteria.MaxResults;
            selection.Timeout   = rootCriteria.Timeout;
            selection.FetchSize = rootCriteria.FetchSize;

            Dictionary <string, LockMode> lockModes = new Dictionary <string, LockMode>();

            foreach (KeyValuePair <string, LockMode> me in rootCriteria.LockModes)
            {
                ICriteria subcriteria = GetAliasedCriteria(me.Key);
                lockModes[GetSQLAlias(subcriteria)] = me.Value;
            }

            List <TypedValue> typedValues = new List <TypedValue>();

            // NH-specific: Get parameters for projections first
            if (this.HasProjection)
            {
                typedValues.AddRange(rootCriteria.Projection.GetTypedValues(rootCriteria, this));
            }

            foreach (CriteriaImpl.Subcriteria subcriteria in rootCriteria.IterateSubcriteria())
            {
                LockMode lm = subcriteria.LockMode;
                if (lm != null)
                {
                    lockModes[GetSQLAlias(subcriteria)] = lm;
                }
                // Get parameters that may be used in JOINs
                if (subcriteria.WithClause != null)
                {
                    typedValues.AddRange(subcriteria.WithClause.GetTypedValues(subcriteria, this));
                }
            }

            List <TypedValue> groupedTypedValues = new List <TypedValue>();

            // Type and value gathering for the WHERE clause needs to come AFTER lock mode gathering,
            // because the lock mode gathering loop now contains join clauses which can contain
            // parameter bindings (as in the HQL WITH clause).
            foreach (CriteriaImpl.CriterionEntry ce in rootCriteria.IterateExpressionEntries())
            {
                bool          criteriaContainsGroupedProjections = false;
                IProjection[] projections = ce.Criterion.GetProjections();

                if (projections != null)
                {
                    foreach (IProjection projection in projections)
                    {
                        if (projection.IsGrouped)
                        {
                            criteriaContainsGroupedProjections = true;
                            break;
                        }
                    }
                }

                if (criteriaContainsGroupedProjections)
                {
                    // GROUP BY/HAVING parameters need to be added after WHERE parameters - so don't add them
                    // to typedValues yet
                    groupedTypedValues.AddRange(ce.Criterion.GetTypedValues(ce.Criteria, this));
                }
                else
                {
                    typedValues.AddRange(ce.Criterion.GetTypedValues(ce.Criteria, this));
                }
            }

            // NH-specific: GROUP BY/HAVING parameters need to appear after WHERE parameters
            if (groupedTypedValues.Count > 0)
            {
                typedValues.AddRange(groupedTypedValues);
            }

            // NH-specific: To support expressions/projections used in ORDER BY
            foreach (CriteriaImpl.OrderEntry oe in rootCriteria.IterateOrderings())
            {
                typedValues.AddRange(oe.Order.GetTypedValues(oe.Criteria, this));
            }

            return
                (new QueryParameters(
                     typedValues.Select(tv => tv.Type).ToArray(),
                     typedValues.Select(tv => tv.Value).ToArray(),
                     lockModes,
                     selection,
                     rootCriteria.IsReadOnlyInitialized,
                     rootCriteria.IsReadOnlyInitialized ? rootCriteria.IsReadOnly : false,
                     rootCriteria.Cacheable,
                     rootCriteria.CacheRegion,
                     rootCriteria.Comment,
                     rootCriteria.LookupByNaturalKey,
                     rootCriteria.ResultTransformer,
                     _tempPagingParameterIndexes));
        }