Esempio n. 1
0
        private string FormatInQuery(string exp, object[] array, SubqueryParts query)
        {
            if (array.Length == 0)
            {
                return("false");
            }
            if (array.Length == 1 && array[0] == null)
            {
                return(exp + " IS NULL ");
            }

            var qsre = query.Selector as QuerySourceReferenceExpression;

            if (qsre != null && qsre.ReferencedQuerySource.Equals(query.MainFrom) &&
                NpgsqlTypes.TypeConverter.CanConvert(query.MainFrom.ItemType))
            {
                //TODO: values
                return(array.Length == 1
                                        ? exp + " = " + NpgsqlTypes.TypeConverter.Convert(query.MainFrom.ItemType, array[0])
                                        : exp + " IN ("
                       + string.Join(
                           ",",
                           array.Select(it => NpgsqlTypes.TypeConverter.Convert(query.MainFrom.ItemType, it)))
                       + ")");
            }

            return(exp + " IN (SELECT " + query.GetSqlExpression(query.Selector)
                   + " FROM unnest(" + query.FormatArray(array) + ") \"" + query.MainFrom.ItemName + "\")");
        }
        private string SimplifyExpression(QueryParts query, SubqueryParts parts, string exp, Expression e)
        {
            var source = query.GetSqlExpression(e);
            var qsre   = parts.Selector as QuerySourceReferenceExpression;

            if (qsre != null && qsre.ReferencedQuerySource.Equals(query.MainFrom))
            {
                return(exp + " = ANY(" + source + ")");
            }
            return(exp + " IN (SELECT " + query.GetSqlExpression(parts.Selector) + " FROM unnest(" + source + ") \"" + query.MainFrom.ItemName + "\")");
        }
Esempio n. 3
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));
 }
Esempio n. 4
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));
        }
        protected override Expression VisitSubQueryExpression(SubQueryExpression expression)
        {
            var sqParts = new SubqueryParts(Query, expression.QueryModel.SelectClause.Selector, Query.Context);
            var sq      = SubqueryGeneratorQueryModelVisitor.ParseSubquery(expression.QueryModel, sqParts);

            if (!Query.CanQueryInMemory)
            {
                Query.AddSelectPart(
                    null,
                    "(" + sq.BuildSqlString(true) + ")",
                    null,
                    expression.QueryModel.ResultTypeOverride ?? expression.QueryModel.SelectClause.Selector.Type,
                    null);
            }
            return(expression);
        }
Esempio n. 6
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 + "\")");
 }
 private SubquerySelectExpressionTreeVisitor(SubqueryParts query)
 {
     this.Query = query;
 }
        public static void ProcessExpression(Expression linqExpression, SubqueryParts query)
        {
            var visitor = new SubquerySelectExpressionTreeVisitor(query);

            visitor.VisitExpression(linqExpression);
        }
Esempio n. 9
0
 private SubqueryGeneratorQueryModelVisitor(QueryParts parentQuery, bool canQueryInMemory, Expression selector, string contextName)
 {
     QueryParts = new SubqueryParts(parentQuery, canQueryInMemory, selector, contextName);
 }
        private static Func <string, T> ProjectNew <T>(NewExpression nex, QueryParts parts, SubqueryParts subquery)
        {
            var results = new Dictionary <Expression, Func <object, object> >();
            var list    = new List <Func <object, object> >();

            foreach (var s in subquery.Selects)
            {
                var factory = CreateResult(s.Name, s.ItemType, s.QuerySource, parts, true);
                if (s.Expression == null)
                {
                    throw new FrameworkException("Null expression!?");
                }
                results[s.Expression] = factory.Instancer;
                list.Add(factory.Instancer);
            }

            var arguments = new object[nex.Arguments.Count];

            Func <string, T> result = value =>
            {
                //TODO fix later
                //if (value == null)
                return(default(T));

                /*var items = OracleRecordConverter.ParseRecord(value);
                 * for (int i = 0; i < items.Length; i++)
                 *      arguments[i] = list[i](items[i]);
                 *
                 * return (T)nex.Constructor.Invoke(arguments);*/
            };

            return(result);
        }
        private static Func <string, T> ProjectMapping <T>(SubQueryExpression sqe, QueryParts parts, SubqueryParts subquery)
        {
            Func <string, T> result;
            var projector = ProjectorBuildingExpressionTreeVisitor <T> .BuildProjector(sqe.QueryModel);

            if (subquery.Selects.Count == 1)
            {
                var sel     = subquery.Selects[0];
                var factory = CreateResult(sel.Name, sel.ItemType, sel.QuerySource, parts, true);
                result = value => (T)factory.Instancer(value);
            }
            else
            {
                var ssd = SelectSubqueryData.Create(parts, subquery);
                result = value =>
                {
                    //TODO fix later
                    return(default(T));

                    /*
                     * var items = OracleRecordConverter.ParseRecord(value);
                     * var rom = ssd.ProcessRow(null, items);
                     * var res = projector(rom);
                     * return res;*/
                };
            }
            return(result);
        }
Esempio n. 12
0
        private static Func <string, T> ProjectMapping <T>(SubQueryExpression sqe, QueryParts parts, SubqueryParts subquery)
        {
            var ssd = SelectSubqueryData.Create(parts, subquery);

            var projector = ProjectorBuildingExpressionTreeVisitor <T> .BuildProjector(sqe.QueryModel);

            //TODO TextReader/string !?
            Func <string, T> result = value =>
            {
                var items = PostgresRecordConverter.ParseRecord(value);
                var rom   = ssd.ProcessRow(null, items);
                var res   = projector(rom);
                return(res);
            };

            return(result);
        }
        private static Func <string, BufferedTextReader, T> ProjectMapping <T>(SubQueryExpression sqe, QueryParts parts, SubqueryParts subquery)
        {
            Func <string, BufferedTextReader, T> result;
            var projector = ProjectorBuildingExpressionTreeVisitor <T> .BuildProjector(sqe.QueryModel);

            if (subquery.Selects.Count == 1)
            {
                var sel     = subquery.Selects[0];
                var factory = CreateResult(sel.Name, sel.ItemType, sel.QuerySource, parts);
                result = (value, reader) => (T)factory.Instancer(value, reader);
            }
            else
            {
                var ssd = SelectSubqueryData.Create(parts, subquery);
                //TODO TextReader/string !?
                result = (value, reader) =>
                {
                    var items = PostgresRecordConverter.ParseRecord(value);
                    var rom   = ssd.ProcessRow(null, reader, items);
                    var res   = projector(rom);
                    return(res);
                };
            }
            return(result);
        }
Esempio n. 14
0
        private static Func <string, T> ProjectMapping <T>(SubQueryExpression sqe, QueryParts parts, SubqueryParts subquery)
        {
            var ssd = SelectSubqueryData.Create(parts, subquery);

            var projector = ProjectorBuildingExpressionTreeVisitor <T> .BuildProjector(sqe.QueryModel);

            Func <string, T> result = value =>
            {
                //TODO fix later
                return(default(T));

                /*
                 * var items = OracleRecordConverter.ParseRecord(value);
                 * var rom = ssd.ProcessRow(null, items);
                 * var res = projector(rom);
                 * return res;*/
            };

            return(result);
        }