Exemplo n.º 1
0
        public void WithColumnCombined()
        {
            var t1 = ModelsRegistry.GetTable <MyModel>();
            var t2 = ModelsRegistry.GetTable <MyModel2>();

            var rq1 = new RawQuery();

            rq1.AppendColumn(NodeVisitor.EvaluateFuncExpressionToColumn <MyModel>(x => x.Name), new RawQuery.TableNamePlaceholder(t1, "M1"));
            rq1.Append(" = 123");
            rq1.ApplyAlias("M1", "t1");

            var rq2 = new RawQuery();

            rq2.AppendColumn(NodeVisitor.EvaluateFuncExpressionToColumn <MyModel2>(x => x.Name1), new RawQuery.TableNamePlaceholder(t2, "M2"));
            rq2.Append(" = 456");
            rq2.ApplyAlias("M2", "t2");

            var rq = new RawQuery();

            rq.Append(rq1);
            rq.Append(", ");
            rq.Append(rq2);

            Utils.AssertRawQueryWithAliases(rq, @"t1.""name"" = 123, t2.name1 = 456");
        }
Exemplo n.º 2
0
        public static SelectFromBuilder FromCombinedExpression <TCombinedModel, TNewModel>(TablesList tablesList, Expression <Func <TCombinedModel, TNewModel> > prBody)
        {
            var builder = new SelectFromBuilder();

            builder.LeftJoinsExpressions = tablesList.JoinExpressions;

            var options = new EvaluationOptions();

            foreach (var tablePlaceholder in tablesList.Tables)
            {
                builder.AddTable(tablePlaceholder);
                // options.ParameterToTableAlias.Add(tablePlaceholder.Name, tablePlaceholder);
            }

            var tableToPlaceholder = tablesList.Tables.ToDictionary(x => x.Name);

            TypedExpression exp;

            switch (prBody.Body)
            {
            /* For:
             * .Select(x => new {
             *                  M1 = x.M1,
             *                  M2_name = x.M2.Name1,
             *                  M3_calc = x.M2.Id * 2,
             *              })
             */
            case NewExpression newExpression:
            {
                IEnumerable <Expression> args = newExpression.Arguments;

                var resultProcessor = new AnonymousTypeSelectResultProcessor <TNewModel>();
                builder.SelectResultProcessor = resultProcessor;

                foreach (var argExpression in args)
                {
                    // Member is Table (like M1 = x.M1)
                    if (argExpression is MemberExpression memberExpression && ModelsRegistry.IsTable(memberExpression.Type))
                    {
                        var tablePlaceholder = tableToPlaceholder[memberExpression.Member.Name];
                        var table            = tablePlaceholder.Table;

                        var tableTestRawQuery = new RawQuery();
                        tableTestRawQuery.AppendTable(tablePlaceholder);
                        tableTestRawQuery.Append(" IS NULL");
                        builder.AddSelectPart(tableTestRawQuery, KDPgValueTypeInstances.Boolean);

                        foreach (var column in table.Columns)
                        {
                            var rq = new RawQuery();
                            rq.AppendColumn(column, tablePlaceholder);
                            builder.AddSelectPart(rq, column.Type);
                        }

                        resultProcessor.AddModelEntry(table);
                    }