예제 #1
0
		public string Simplify(QueryParts query)
		{
			var co = query.ResultOperators[0];
			var prefix = "SELECT COUNT(*)";
			query.Selects.Clear();
			query.CurrentSelectIndex = 0;

			if (co is LongCountResultOperator)
			{
				query.AddSelectPart(query.MainFrom, "COUNT(*)", "count", typeof(long), (_, __, dr) => dr.GetInt64(0));
			}
			else
			{
				query.AddSelectPart(query.MainFrom, "COUNT(*)::int", "count", typeof(int), (_, __, dr) => dr.GetInt32(0));
				prefix += "::int";
			}
			prefix += " FROM ";
			var type = query.MainFrom.FromExpression.Type.GetGenericArguments()[0];
			var isRoot = typeof(IAggregateRoot).IsAssignableFrom(type);
			if (isRoot)
				return prefix + @"""{0}"".""{1}""".With(type.Namespace, type.Name);
			var isEvent = typeof(IDomainEvent).IsAssignableFrom(type);
			if (isEvent)
				return type.IsNested
					? prefix + @"""{0}"".""{1}.{2}""".With(type.Namespace, type.DeclaringType.Name, type.Name)
					: prefix + @"""{0}"".""{1}""".With(type.Namespace, type.Name);
			var ent =
				(from i in type.GetInterfaces()
				 where i.IsGenericType
				 && i.GetGenericTypeDefinition() == typeof(ISnowflake<>)
				 select i.GetGenericArguments()[0])
				 .FirstOrDefault() ?? type;
			return prefix + @"""{0}"".""{1}""".With(ent.Namespace, ent.Name);
		}
예제 #2
0
 public bool CanSimplify(QueryParts query)
 {
     var parts = query as SubqueryParts;
     return parts != null
         && !parts.ShouldQueryInMemory
         && parts.ResultOperators.Count == 1
         && parts.ResultOperators[0] is AnyResultOperator;
 }
예제 #3
0
		public bool CanSimplify(QueryParts query)
		{
			var parts = query as SubqueryParts;
			return parts != null
				&& !parts.ShouldQueryInMemory
				&& parts.ResultOperators.Count == 1
				&& parts.Conditions.Count == 0 && parts.AdditionalJoins.Count == 0 && parts.Joins.Count == 0
				&& parts.ResultOperators[0] is AllResultOperator;
		}
예제 #4
0
		public bool CanSimplify(QueryParts query)
		{
			var type = query.MainFrom != null ? query.MainFrom.FromExpression.Type : null;
			return type != null
				&& query.ResultOperators.Count == 1
				&& query.Joins.Count == 0 && query.AdditionalJoins.Count == 0 && query.Conditions.Count == 0
				&& (query.ResultOperators[0] is CountResultOperator
					|| query.ResultOperators[0] is LongCountResultOperator)
				&& type.IsGenericType
				&& type.GetGenericTypeDefinition() == QueryableType
				&& typeof(IEntity).IsAssignableFrom(type.GetGenericArguments()[0]);
		}
예제 #5
0
        public SubqueryParts(QueryParts parentQuery, bool canQueryInMemory, Expression selector, string contextName)
            : base(parentQuery.Locator,
				contextName,
				parentQuery.ConverterFactory,
				parentQuery.Simplifications,
				parentQuery.ExpressionMatchers,
				parentQuery.MemberMatchers,
				parentQuery.ProjectionMatchers)
        {
            this.CanQueryInMemory = canQueryInMemory;
            this.ParentQuery = parentQuery;
            this.Selector = selector;
        }
예제 #6
0
		private string SimplifyConstantExpression(QueryParts query, SubqueryParts parts, string exp, ConstantExpression ce)
		{
			if (ce.Type.IsArray || ce.Value is Array)
			{
				var array = ((Array)ce.Value).Cast<object>().ToArray();
				return FormatInQuery(exp, array, parts);
			}
			else if (ce.Value is IEnumerable)
			{
				var array = ((IEnumerable)ce.Value).Cast<object>().ToArray();
				return FormatInQuery(exp, array, parts);
			}
			return exp + " = " + query.FormatObject(ce.Value);
		}
예제 #7
0
		public static SelectSubqueryData Create(QueryParts parts, SubqueryParts subquery)
		{
			Contract.Requires(parts != null);
			Contract.Requires(subquery != null);

			var selects = new List<KeyValuePair<IQuerySource, Func<object, BufferedTextReader, object>>>();
			foreach (var s in subquery.Selects)
			{
				var factory = QuerySourceConverterFactory.Create(s.QuerySource, parts);
				selects.Add(new KeyValuePair<IQuerySource, Func<object, BufferedTextReader, object>>(factory.QuerySource, factory.Instancer));
			}

			return new SelectSubqueryData(selects);
		}
예제 #8
0
		public string Simplify(QueryParts query)
		{
			var parts = query as SubqueryParts;
			var containsResult = parts.ResultOperators[0] as ContainsResultOperator;

			var exp = parts.GetSqlExpression(containsResult.Item);
			var ce = parts.MainFrom.FromExpression as ConstantExpression;
			var pe = parts.MainFrom.FromExpression as ParameterExpression;
			var ma = parts.MainFrom.FromExpression as MemberExpression;
			return ce != null
				? SimplifyConstantExpression(query, parts, exp, ce)
				: ma != null
				? SimplifyMemberExpression(query, parts, exp, ma)
				: exp + " = ANY(" + (pe != null ? query.ContextName + "\"" + pe.Name + "\"" : parts.GetSqlExpression(parts.MainFrom.FromExpression)) + ")";
		}
예제 #9
0
		public bool CanSimplify(QueryParts query)
		{
			var parts = query as SubqueryParts;
			var ce = parts != null ? parts.MainFrom.FromExpression as ConstantExpression : null;
			var me = parts != null ? parts.MainFrom.FromExpression as MemberExpression : null;
			var pe = parts != null ? parts.MainFrom.FromExpression as ParameterExpression : null;
			return parts != null
				&& (ce != null && ce.Type.IsSupportedCollectionType()
					|| me != null
					|| pe != null)
				&& !parts.ShouldQueryInMemory
				&& parts.ResultOperators.Count == 1
				&& parts.Joins.Count == 0 && parts.AdditionalJoins.Count == 0 && parts.Conditions.Count == 0
				&& parts.ResultOperators[0] is ContainsResultOperator;
		}
예제 #10
0
 public bool CanSimplify(QueryParts query)
 {
     var parts = query as SubqueryParts;
     var me = parts != null ? parts.MainFrom.FromExpression as MemberExpression : null;
     return parts != null
         && me != null
         && !parts.ShouldQueryInMemory
         && parts.ResultOperators.Count == 1
         && parts.Joins.Count == 0 && parts.AdditionalJoins.Count == 0 && parts.Conditions.Count == 0
         && (me.Expression is QuerySourceReferenceExpression
             || me.Expression is ParameterExpression)
         && me.Type.IsSupportedCollectionType()
         && (parts.ResultOperators[0] is CountResultOperator
             || parts.ResultOperators[0] is LongCountResultOperator);
 }
예제 #11
0
        public string Simplify(QueryParts query)
        {
            var parts = query as SubqueryParts;

            var me = parts.MainFrom.FromExpression as MemberExpression;
            if (me != null && parts.Conditions.Count == 0)
            {
                var qse = me.Expression as QuerySourceReferenceExpression;
                if (qse != null)
                    return
                        "EXISTS (SELECT * FROM UNNEST((\"{0}\").\"{1}\"))".With(
                            qse.ReferencedQuerySource.ItemName,
                            me.Member.Name);
            }

            return "EXISTS (SELECT * {0} {1})".With(parts.GetFromPart(), parts.GetWherePart());
        }
예제 #12
0
        public string Simplify(QueryParts query)
        {
            var parts = query as SubqueryParts;

            var me = parts.MainFrom.FromExpression as MemberExpression;
            var qsre = me.Expression as QuerySourceReferenceExpression;
            var pe = me.Expression as ParameterExpression;
            if (me.Type == typeof(byte[]))
            {
                return @"COALESCE(octet_length(({2}""{0}"").""{1}""), 0)".With(
                    qsre != null ? qsre.ReferencedQuerySource.ItemName : pe.Name,
                    me.Member.Name,
                    pe != null ? query.Context.Name : string.Empty);
            }
            return @"COALESCE(array_upper(({2}""{0}"").""{1}"", 1), 0)".With(
                qsre != null ? qsre.ReferencedQuerySource.ItemName : pe.Name,
                me.Member.Name,
                pe != null ? query.Context.Name : string.Empty);
        }
 public static SubqueryParts ParseSubquery(QueryModel queryModel, QueryParts parentQuery, bool canQueryInMemory, QueryContext context)
 {
     var visitor = new SubqueryGeneratorQueryModelVisitor(parentQuery, canQueryInMemory, queryModel.SelectClause.Selector, context);
     visitor.VisitQueryModel(queryModel);
     return visitor.QueryParts;
 }
 public static SubqueryParts ParseSubquery(QueryModel queryModel, QueryParts parentQuery, bool canQueryInMemory)
 {
     return ParseSubquery(queryModel, parentQuery, canQueryInMemory, parentQuery.Context);
 }
 public static SubqueryParts ParseSubquery(QueryModel queryModel, QueryParts parentQuery)
 {
     return ParseSubquery(queryModel, parentQuery, false, parentQuery.Context);
 }
 private SubqueryGeneratorQueryModelVisitor(QueryParts parentQuery, bool canQueryInMemory, Expression selector, QueryContext context)
 {
     QueryParts = new SubqueryParts(parentQuery, canQueryInMemory, selector, context);
 }
예제 #17
0
 public SubqueryParts(QueryParts parentQuery, Expression selector, QueryContext context)
     : this(parentQuery, false, selector, context)
 {
 }
예제 #18
0
 public SubqueryParts(QueryParts parentQuery, Expression selector, string contextName)
     : this(parentQuery, false, selector, contextName)
 {
 }
예제 #19
0
 public SubqueryParts(QueryParts parentQuery, Expression selector, QueryContext context)
     : this(parentQuery, false, selector, context)
 {
 }
예제 #20
0
		public string Simplify(QueryParts query)
		{
			var parts = query as SubqueryParts;
			var aro = parts.ResultOperators[0] as AllResultOperator;
			return " NOT EXISTS (SELECT * {0} WHERE NOT ({1}))".With(parts.GetFromPart(), parts.GetSqlExpression(aro.Predicate));
		}
예제 #21
0
		private string SimplifyMemberExpression(QueryParts query, SubqueryParts parts, string exp, MemberExpression ma)
		{
			return exp + " IN (SELECT " + query.GetSqlExpression(parts.Selector)
					+ " FROM unnest(" + query.GetSqlExpression(ma) + ") \"" + query.MainFrom.ItemName + "\")";
		}
예제 #22
0
 public SubqueryParts(QueryParts parentQuery, Expression selector, string contextName)
     : this(parentQuery, false, selector, contextName)
 {
 }