public void NestedSelectMappingTest()
        {
            QueryDefinition nestedSelectMapping = Define.QueryResult("select id as cid, sname,'2017-03-01' as LastUpdate, fname, BDAY, ADR_STREET, ADR_CITY from customer",
                                                                     new
            {
                Id         = Define.Column("cid"),
                SName      = Define.Column(),
                Age        = 22,
                LastUpdate = Define.Column(),
                Birthday   = Define.Column("BDAY"),
                Address    = new
                {
                    Street = Define.Column("ADR_STREET"),
                    City   = Define.Column("ADR_CITY"),
                },
                Invoices = Define.QueryResult("select id as iid, inv_date, amount from invoices where customer_id=${column:cid} and amount > ${context:min_amount}",
                                              new
                {
                    InvoiceId   = Define.Column("iid"),
                    Inv_Date    = Define.Column(),
                    TotalAmount = Define.Column("amount"),
                }
                                              ),
                Orders = Define.QueryResult("select id as oid, article from orders where customer_id=${column:cid}",
                                            new
                {
                    OrderId     = Define.Column("oid"),
                    ArticleName = Define.Column("article"),
                }
                                            )
            });

            var compiledMapping = nestedSelectMapping.Compile();

            // Execute Mapping
            DoInConnection((connection) =>
            {
                using (var engine = new MappingEngine(connection, compiledMapping))
                {
                    using (var ms = new MemoryStream())
                    {
                        var context = new Dictionary <string, object>();
                        context.Add("min_amount", 100);
                        engine.ExecuteMapping(ms, context, false);
                        var json = System.Text.Encoding.UTF8.GetString(ms.ToArray());
                        Assert.Equal(nestedSelectGeneratedJson, json);
                    }
                }
            }
                           );
        }
Beispiel #2
0
        public void TestNestedResultsMappingAnonymousType()
        {
            TemplateDefinition nestedResultsMapping = Define.Template(
                new
            {
                Version = "2.0",
                Results =
                    Define.QueryWithNestedResults("cid",
                                                  @"select c.id as cid, sname,'2017-03-01' as LastUpdate, fname, BDAY, ADR_STREET, ADR_CITY, i.id as iid, i.inv_date, i.amount, o.article, o.id as oid
                                                    from customer c
                                                    left join orders o on c.id = o.customer_id
                                                    left join invoices i on c.id = i.customer_id
                                                    where i.amount > ${context:min_amount}"
                                                  ,
                                                  new
                {
                    Id         = Define.Column("cid"),
                    SName      = Define.Column(),
                    LastUpdate = Define.Column(),
                    Birthday   = Define.Column("BDAY"),
                    Address    = new
                    {
                        Street = Define.Column("ADR_STREET"),
                        City   = Define.Column("ADR_CITY"),
                    },
                    Invoices = Define.NestedResults("iid",
                                                    new
                    {
                        InvoiceId   = Define.Column("iid"),
                        Inv_Date    = Define.Column(),
                        TotalAmount = Define.Column("amount"),
                    }
                                                    ),
                    Orders = Define.NestedResults("oid",
                                                  new
                    {
                        OrderId     = Define.Column("oid"),
                        ArticleName = Define.Column("article"),
                    }
                                                  )
                }
                                                  )
            });

            // Compile Mapping

            var compiledMapping = nestedResultsMapping.Compile();

            // Execute Mapping

            using (var engine = new MappingEngine(() => new SqliteConnection("Data Source=Test.db"), compiledMapping))

                using (var ms = new MemoryStream())
                {
                    var context = new Dictionary <string, object>();
                    context.Add("min_amount", 100);
                    engine.ExecuteMapping(ms, context, false);
                    var json = System.Text.Encoding.UTF8.GetString(ms.ToArray());
                    Assert.Equal(nestedResultsGeneratedJson, json);
                }
        }