Ejemplo n.º 1
0
        public IQueryOver <NodeVersion, NodeVersionStatusHistory> GenerateVersionedQueryPlusAttributes(Guid[] nodeIds = null, RevisionStatusType revisionStatus = null, bool limitToLatestRevision = true, IEnumerable <SortClause> sortClauses = null)
        {
            if (sortClauses == null)
            {
                sortClauses = Enumerable.Empty <SortClause>();
            }
            NodeVersion outerVersionSelect = null;
            var         outerQuery         = GenerateVersionedQuery(out outerVersionSelect, nodeIds, revisionStatus, limitToLatestRevision);

            Attribute                attribAlias              = null;
            AttributeDateValue       attributeDateValue       = null;
            AttributeDecimalValue    attributeDecimalValue    = null;
            AttributeIntegerValue    attributeIntegerValue    = null;
            AttributeStringValue     attributeStringValue     = null;
            AttributeLongStringValue attributeLongStringValue = null;

            outerQuery = outerQuery
                         .Left.JoinAlias(() => outerVersionSelect.Attributes, () => attribAlias) // Using a left join identifies to Nh that it can reuse the loaded Attributes because Nh considers them to be unaffected by the query, otherwise it issues another select from accessing NodeVersion.Attributes
                         .Left.JoinAlias(() => attribAlias.AttributeStringValues, () => attributeStringValue)
                         .Left.JoinAlias(() => attribAlias.AttributeLongStringValues, () => attributeLongStringValue)
                         .Left.JoinAlias(() => attribAlias.AttributeIntegerValues, () => attributeIntegerValue)
                         .Left.JoinAlias(() => attribAlias.AttributeDecimalValues, () => attributeDecimalValue)
                         .Left.JoinAlias(() => attribAlias.AttributeDateValues, () => attributeDateValue);

            //if (sortClauses.Any())
            //{
            //    AttributeDefinition attribDef = null;
            //    outerQuery = outerQuery.JoinAlias(() => attribAlias.AttributeDefinition, () => attribDef);
            //    foreach (var sortClause in sortClauses)
            //    {
            //        var coalesce = Projections.SqlFunction("coalesce", NHibernateUtil.String,
            //            Projections.Property(() => attributeStringValue.Value),
            //            Projections.Property(() => attributeLongStringValue.Value));
            //        if (sortClause.Direction == SortDirection.Ascending)
            //            outerQuery = outerQuery.OrderBy(coalesce).Asc;
            //        else
            //            outerQuery = outerQuery.OrderBy(coalesce).Desc;
            //    }
            //}

            return(outerQuery);
        }
Ejemplo n.º 2
0
        public void AddAttributeValueFuturesToSession(Guid[] versionIds, NodeVersion outerVersionSelect)
        {
            Attribute            attribAlias    = null;
            Attribute            aliasForString = null;
            AttributeStringValue stringsLoader  = null;
            var strings = NhSession.QueryOver <Attribute>(() => attribAlias)
                          .Left.JoinAlias(() => attribAlias.AttributeStringValues, () => stringsLoader)
                          .Where(() => attribAlias.NodeVersion.Id.IsIn(versionIds))
                          .Future <Attribute>();

            Attribute            aliasForLongString = null;
            AttributeStringValue longStringsLoader  = null;
            var longStrings = NhSession.QueryOver <Attribute>(() => attribAlias)
                              .Left.JoinAlias(() => attribAlias.AttributeLongStringValues, () => longStringsLoader)
                              .Where(() => attribAlias.NodeVersion.Id.IsIn(versionIds))
                              .Future <Attribute>();

            Attribute             aliasForInteger = null;
            AttributeIntegerValue integerLoader   = null;
            var integers = NhSession.QueryOver <Attribute>(() => attribAlias)
                           .Left.JoinAlias(() => attribAlias.AttributeIntegerValues, () => integerLoader)
                           .Where(() => attribAlias.NodeVersion.Id.IsIn(versionIds))
                           .Future <Attribute>();

            Attribute             aliasForDecimal = null;
            AttributeDecimalValue decimalLoader   = null;
            var decimals = NhSession.QueryOver <Attribute>(() => attribAlias)
                           .Left.JoinAlias(() => attribAlias.AttributeDecimalValues, () => decimalLoader)
                           .Where(() => attribAlias.NodeVersion.Id.IsIn(versionIds))
                           .Future <Attribute>();

            Attribute          aliasForDate = null;
            AttributeDateValue dateLoader   = null;
            var dates = NhSession.QueryOver <Attribute>(() => attribAlias)
                        .Left.JoinAlias(() => attribAlias.AttributeDateValues, () => dateLoader)
                        .Where(() => attribAlias.NodeVersion.Id.IsIn(versionIds))
                        .Future <Attribute>();
        }
Ejemplo n.º 3
0
 private static AttributeStringValue GetFirstString(Attribute leftAttrib)
 {
     return(leftAttrib.AttributeStringValues.FirstOrDefault());
 }
Ejemplo n.º 4
0
 private static AttributeDateValue GetFirstDate(Attribute leftAttrib)
 {
     return(leftAttrib.AttributeDateValues.FirstOrDefault());
 }
        public override QueryOver <NodeVersion> VisitFieldPredicate(FieldPredicateExpression node)
        {
            var fieldName  = node.SelectorExpression.FieldName;
            var valueKey   = node.SelectorExpression.ValueKey;
            var fieldValue = node.ValueExpression.Value;

            switch (fieldName.ToLowerInvariant())
            {
            case "id":
                Guid idValue = GetIdValue(node);

                switch (node.ValueExpression.ClauseType)
                {
                case ValuePredicateType.Equal:
                    return(QueryOver.Of <NodeVersion>().Where(x => x.Node.Id == idValue).Select(x => x.Id));

                case ValuePredicateType.NotEqual:
                    return(QueryOver.Of <NodeVersion>().Where(x => x.Node.Id != idValue).Select(x => x.Id));;

                default:
                    throw new InvalidOperationException(
                              "Cannot query an item by id by any other operator than == or !=");
                }
            }

            NodeVersion              aliasNodeVersion         = null;
            Attribute                aliasAttribute           = null;
            AttributeDefinition      aliasAttributeDefinition = null;
            AttributeStringValue     aliasStringValue         = null;
            AttributeLongStringValue aliasLongStringValue     = null;
            NodeRelation             aliasNodeRelation        = null;
            AttributeDateValue       aliasDateValue           = null;

            //TODO: This is going to need to lookup more than string values

            var queryString = QueryOver.Of <NodeVersion>(() => aliasNodeVersion)
                              .JoinQueryOver <Attribute>(() => aliasNodeVersion.Attributes, () => aliasAttribute)
                              .JoinQueryOver <AttributeDefinition>(() => aliasAttribute.AttributeDefinition, () => aliasAttributeDefinition)
                              .Left.JoinQueryOver <AttributeStringValue>(() => aliasAttribute.AttributeStringValues, () => aliasStringValue)
                              .Left.JoinQueryOver <AttributeLongStringValue>(() => aliasAttribute.AttributeLongStringValues, () => aliasLongStringValue)
                              .Left.JoinQueryOver(() => aliasAttribute.AttributeDateValues, () => aliasDateValue)
                              //select the field name...
                              .Where(x => aliasAttributeDefinition.Alias == fieldName);

            if (!valueKey.IsNullOrWhiteSpace())
            {
                //if the value key is specified, then add that to the query
                queryString = queryString.And(() => aliasStringValue.ValueKey == valueKey || aliasLongStringValue.ValueKey == valueKey).Select(x => x.Id);
            }


            //now, select the field value....
            switch (node.ValueExpression.ClauseType)
            {
            case ValuePredicateType.Equal:
                var sqlCeCompatibleQuery =
                    Restrictions.Or(
                        Restrictions.Eq(
                            Projections.Property <AttributeStringValue>(x => aliasStringValue.Value), fieldValue),
                        Restrictions.Like(
                            Projections.Property <AttributeLongStringValue>(x => aliasLongStringValue.Value),
                            fieldValue as string,
                            MatchMode.Exact));

                return(queryString.And(sqlCeCompatibleQuery).Select(x => x.Id));

            case ValuePredicateType.NotEqual:
                return(queryString.And(x => aliasStringValue.Value != fieldValue).Select(x => x.Id));

            case ValuePredicateType.MatchesWildcard:
            case ValuePredicateType.Contains:
                return(queryString.And(Restrictions.Like(Projections.Property <AttributeStringValue>(x => aliasStringValue.Value), fieldValue as string, MatchMode.Anywhere)).Select(x => x.Id));

            case ValuePredicateType.StartsWith:
                return(queryString.And(Restrictions.Like(Projections.Property <AttributeStringValue>(x => aliasStringValue.Value), fieldValue as string, MatchMode.Start)).Select(x => x.Id));

            case ValuePredicateType.EndsWith:
                return(queryString.And(Restrictions.Like(Projections.Property <AttributeStringValue>(x => aliasStringValue.Value), fieldValue as string, MatchMode.End)).Select(x => x.Id));

            case ValuePredicateType.LessThanOrEqual:
                return
                    (queryString.And(
                         Restrictions.Le(Projections.Property <AttributeDateValue>(x => aliasDateValue.Value),
                                         (DateTimeOffset)fieldValue)));

            case ValuePredicateType.GreaterThanOrEqual:
                return
                    (queryString.And(
                         Restrictions.Ge(Projections.Property <AttributeDateValue>(x => aliasDateValue.Value),
                                         (DateTimeOffset)fieldValue)));

            default:
                throw new InvalidOperationException(
                          "This linq provider doesn't support a ClauseType of {0} for field {1}".InvariantFormat(
                              node.ValueExpression.ClauseType.ToString(), fieldName));
            }
        }