Exemplo n.º 1
0
        public global::FormABC.Input[] Find(IEnumerable <string> uris)
        {
            var count = uris != null?uris.Count() : 0;

            var pks = new List <string>(count);

            foreach (var _u in uris ?? new string[0])
            {
                if (_u != null)
                {
                    pks.Add(_u);
                }
            }
            if (pks.Count == 0)
            {
                return(new global::FormABC.Input[0]);
            }
            var lookup = PrepareLocalFind(Locator);
            var cms    = lookup.cms;
            var result = lookup.list;
            var sw     = cms.GetWriter();

            sw.Write(@"EXECUTE ""FM-FormABC-Input""(ARRAY[");
            PostgresRecordConverter.WriteSimpleUriList(sw, pks);
            sw.Write("]::uuid[])");
            sw.Flush();
            lookup.ExecuteAll(DatabaseQuery, lookup.RichFindAll);
            var found = result.ToArray();

            return(found);
        }
Exemplo n.º 2
0
        public global::FormABC.Input Find(string uri)
        {
            if (uri == null)
            {
                return(null);
            }
            var lookup = PrepareLocalFind(Locator);
            var cms    = lookup.cms;
            var sw     = cms.GetWriter();

            sw.Write(@"EXECUTE ""FS-FormABC-Input""(");
            PostgresRecordConverter.WriteSimpleUri(sw, uri);
            sw.Write("::uuid)");
            sw.Flush();
            lookup.ExecuteOne(DatabaseQuery, lookup.RichFind);
            var result = lookup.one;

            if (!HasCustomSecurity)
            {
                return(result);
            }
            var found = new [] { result };

            return(found.Length == 1 ? result : null);
        }
        public global::UseCase1.SubmissionList[] Find(IEnumerable <string> uris)
        {
            var count = uris != null?uris.Count() : 0;

            var pks = new List <string>(count);

            foreach (var _u in uris ?? new string[0])
            {
                if (_u != null)
                {
                    pks.Add(_u);
                }
            }
            if (pks.Count == 0)
            {
                return(new global::UseCase1.SubmissionList[0]);
            }
            var lookup = PrepareLocalFind(Locator);
            var cms    = lookup.cms;
            var sw     = cms.GetWriter();

            sw.Write(@"SELECT _r FROM ""UseCase1"".""SubmissionList"" _r WHERE _r.""URI"" IN (");
            PostgresRecordConverter.WriteSimpleUriList(sw, pks);
            sw.Write(')');
            sw.Flush();
            var found = lookup.ExecuteAll(DatabaseQuery, lookup.InPkCommand).ToArray();

            return(found);
        }
Exemplo n.º 4
0
        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 = QuerySourceConverterFactory.CreateResult(s.Name, s.ItemType, s.QuerySource, parts);
                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 =>
            {
                if (value == null)
                {
                    return(default(T));
                }
                var items = PostgresRecordConverter.ParseRecord(value);
                for (int i = 0; i < items.Length; i++)
                {
                    arguments[i] = list[i](items[i]);
                }

                return((T)nex.Constructor.Invoke(arguments));
            };

            return(result);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        public Func <System.Data.IDataReader, int, global::FormABC.Input[]> Find(Revenj.DatabasePersistence.Postgres.IBulkReadQuery query, IEnumerable <string> uris)
        {
            var sw    = query.Writer;
            var cms   = query.Stream;
            var count = uris != null?uris.Count() : 0;

            var pks = new List <string>(count);

            foreach (var _u in uris ?? new string[0])
            {
                if (_u != null)
                {
                    pks.Add(_u);
                }
            }
            if (pks.Count == 0)
            {
                sw.Write("SELECT 0");
                return((dr, ind) => new global::FormABC.Input[0]);
            }

            if (query.UsePrepared)
            {
                sw.Write(@"SELECT array_agg(_r) FROM ""FormABC"".""Input_entity"" _r WHERE _r.""ID"" = ANY(");
                var nextArg = query.GetNextArgument(wr =>
                {
                    wr.Write("ARRAY[");
                    PostgresRecordConverter.WriteSimpleUriList(wr, pks);
                    wr.Write("]::uuid[]");
                },
                                                    "uuid[]");
                sw.Write(nextArg);
                sw.Write(')');
            }
            else
            {
                sw.Write(@"SELECT array_agg(_r) FROM ""FormABC"".""Input_entity"" _r WHERE _r.""ID"" IN (");
                PostgresRecordConverter.WriteSimpleUriList(sw, pks);
                sw.Write(')');
            }
            return((dr, ind) =>
            {
                var result = new List <global::FormABC.Input>(pks.Count);
                var _pg = dr.GetValue(ind);
                var _str = _pg as string;
                if (_str != null)
                {
                    result.AddRange(PostgresTypedArray.ParseCollection(cms.UseBufferedReader(_str), 0, Locator, _DatabaseCommon.FactoryFormABC_Input.InputConverter.CreateFromRecord));
                }
                else
                {
                    using (var _tr = _pg as System.IO.TextReader ?? new System.IO.StringReader(_pg.ToString()))
                        result.AddRange(PostgresTypedArray.ParseCollection(cms.UseBufferedReader(_tr), 0, Locator, _DatabaseCommon.FactoryFormABC_Input.InputConverter.CreateFromRecord));
                }
                var found = result.ToArray();

                return found;
            });
        }
Exemplo n.º 7
0
        private void CreateSelector(IQuerySource qs)
        {
            var index = Query.CurrentSelectIndex;

            if (qs.ItemType.IsGrouping())
            {
                var factoryKey   = QuerySourceConverterFactory.CreateResult("Key", qs.ItemType.GetGenericArguments()[0], qs, Query);
                var factoryValue = QuerySourceConverterFactory.Create(qs, Query);
                var genericType  = qs.ItemType.CreateGrouping();
                if (Query.AddSelectPart(
                        qs,
                        "\"{0}\".\"Key\" AS \"{0}.Key\", \"{0}\".\"Values\" AS \"{0}\"".With(factoryValue.Name),
                        factoryValue.Name,
                        qs.ItemType,
                        (_, reader, dr) =>
                {
                    var keyInstance = factoryKey.Instancer(dr.GetValue(index), reader);
                    object[] array;
                    if (dr.IsDBNull(index + 1))
                    {
                        array = new string[0];
                    }
                    else
                    {
                        var obj = dr.GetValue(index + 1);
                        var tr = obj as TextReader;
                        if (tr != null)
                        {
                            array = PostgresRecordConverter.ParseArray(tr.ReadToEnd());
                        }
                        else
                        {
                            array = PostgresRecordConverter.ParseArray(obj as string);
                        }
                    };
                    var valueInstances = array.Select(it => factoryValue.Instancer(it, reader));
                    return(Activator.CreateInstance(genericType, keyInstance, valueInstances));
                }))
                {
                    Query.CurrentSelectIndex++;
                }
            }
            else
            {
                var factory = QuerySourceConverterFactory.Create(qs, Query);
                Query.AddSelectPart(
                    factory.QuerySource,
                    "\"{0}\"".With(factory.Name),
                    factory.Name,
                    factory.Type,
                    (_, reader, dr) => dr.IsDBNull(index) ? null : factory.Instancer(dr.GetValue(index), reader));
            }
        }
Exemplo n.º 8
0
        public Func <System.Data.IDataReader, int, global::FormABC.Input> Find(Revenj.DatabasePersistence.Postgres.IBulkReadQuery query, string uri)
        {
            var sw  = query.Writer;
            var cms = query.Stream;

            if (uri == null)
            {
                sw.Write("SELECT 0");
                return((dr, ind) => null);
            }
            sw.Write(@"SELECT _r FROM ""FormABC"".""Input_entity"" _r WHERE _r.""ID"" = ");
            if (query.UsePrepared)
            {
                var nextArg = query.GetNextArgument(wr =>
                {
                    PostgresRecordConverter.WriteSimpleUri(wr, uri);
                    wr.Write("::uuid");
                },
                                                    "uuid");
                sw.Write(nextArg);
            }
            else
            {
                PostgresRecordConverter.WriteSimpleUri(sw, uri);
            }
            return((dr, ind) =>
            {
                global::FormABC.Input result = null;
                var _pg = dr.GetValue(ind);
                var _str = _pg as string;
                if (_str != null)
                {
                    result = _DatabaseCommon.FactoryFormABC_Input.InputConverter.CreateFromRecord(cms.UseBufferedReader(_str), 0, Locator);
                }
                else
                {
                    using (var _tr = _pg as System.IO.TextReader ?? new System.IO.StringReader(_pg.ToString()))
                        result = _DatabaseCommon.FactoryFormABC_Input.InputConverter.CreateFromRecord(cms.UseBufferedReader(_tr), 0, Locator);
                }
                if (!HasCustomSecurity)
                {
                    return result;
                }
                var found = new [] { result };

                return found.Length == 1 ? result : null;
            });
        }
Exemplo n.º 9
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);
        }
        public global::UseCase1.SubmissionList Find(string uri)
        {
            if (uri == null)
            {
                return(null);
            }
            var lookup = PrepareLocalFind(Locator);
            var cms    = lookup.cms;
            var sw     = cms.GetWriter();

            sw.Write(@"SELECT _r FROM ""UseCase1"".""SubmissionList"" _r WHERE _r.""URI"" = ");
            PostgresRecordConverter.WriteSimpleUri(sw, uri);
            sw.Flush();
            var result = lookup.ExecuteOne(DatabaseQuery, lookup.OnePkCommand);

            if (!HasCustomSecurity)
            {
                return(result);
            }
            var found = new [] { result };

            return(found.Length == 1 ? result : null);
        }
Exemplo n.º 11
0
        protected override Expression VisitSubQueryExpression(SubQueryExpression expression)
        {
            if (ProcessedExpressions.Contains(expression))
            {
                return(expression);
            }

            if (DataSources.Count == 0)
            {
                DataSources.Add(Query.MainFrom.FromExpression);
                DataSources.AddRange(Query.Joins.Select(it => it.InnerSequence));
                DataSources.AddRange(Query.AdditionalJoins.Select(it => it.FromExpression));
            }

            var model = expression.QueryModel;

            if (model.BodyClauses.Count == 0 &&
                DataSources.Contains(model.MainFromClause.FromExpression))
            {
                return(expression);
            }

            ProcessedExpressions.Add(expression);

            foreach (var candidate in Query.ProjectionMatchers)
            {
                if (candidate.TryMatch(expression, Query, exp => VisitExpression(exp), Query.Context))
                {
                    return(expression);
                }
            }

            var subquery = SubqueryGeneratorQueryModelVisitor.ParseSubquery(expression.QueryModel, Query, true);

            var cnt = Query.CurrentSelectIndex;
            //TODO true or false
            var sql = subquery.BuildSqlString(true);

            var selector  = model.SelectClause.Selector;
            var projector = (IExecuteFunc)Activator.CreateInstance(typeof(GenericProjection <>).MakeGenericType(selector.Type), model);
            var ssd       = SelectSubqueryData.Create(Query, subquery);

            if (subquery.ResultOperators.Any(it => it is FirstResultOperator))
            {
                Query.AddSelectPart(
                    expression.QueryModel.MainFromClause,
                    @"(SELECT ""{1}"" FROM ({2}) ""{1}"" LIMIT 1) AS ""{0}""".With(
                        "_first_or_default_" + cnt,
                        expression.QueryModel.MainFromClause.ItemName,
                        sql),
                    "_first_or_default_" + cnt,
                    expression.QueryModel.ResultTypeOverride,
                    (rom, reader, dr) =>
                {
                    if (dr.IsDBNull(cnt))
                    {
                        return(null);
                    }
                    var tuple  = new[] { dr.GetValue(cnt).ToString() };
                    var result = ssd.ProcessRow(rom, reader, tuple);
                    return(projector.Eval(result));
                });
            }
            else
            {
                Query.AddSelectPart(
                    expression.QueryModel.MainFromClause,
                    @"(SELECT ARRAY_AGG(""{1}"") FROM ({2}) ""{1}"") AS ""{0}""".With(
                        "_subquery_" + cnt,
                        expression.QueryModel.MainFromClause.ItemName,
                        sql),
                    "_subquery_" + cnt,
                    expression.QueryModel.ResultTypeOverride,
                    (rom, reader, dr) =>
                {
                    object[] array;
                    if (dr.IsDBNull(cnt))
                    {
                        array = new string[0];
                    }
                    else
                    {
                        var value = dr.GetValue(cnt);
                        if (value is string[])
                        {
                            array = value as string[];
                        }
                        else
                        {
                            var obj = dr.GetValue(cnt);
                            //TODO fix dead code usage
                            var tr = obj as TextReader;
                            if (tr != null)
                            {
                                array = PostgresRecordConverter.ParseArray(tr.ReadToEnd());
                            }
                            else
                            {
                                array = PostgresRecordConverter.ParseArray(obj as string);
                            }
                        }
                    }
                    var resultItems = new ResultObjectMapping[array.Length];
                    for (int i = 0; i < array.Length; i++)
                    {
                        resultItems[i] = ssd.ProcessRow(rom, reader, array[i]);
                    }
                    return(projector.Process(resultItems));
                });
            }
            return(expression);
        }