示例#1
0
        public void ItShouldBePossibleToInnerJoinATableOnItselfSeveralTimes()
        {
            var child = Table <Person> .WithReferenceName("child");

            var parent = Table <Person> .WithReferenceName("parent");

            var grandparent = Table <Person> .WithReferenceName("grandparent");

            var expr = Select.Column(grandparent, gp => new { gp.Name, gp.Age })
                       .From(child)
                       .InnerJoin(child, parent, (c, p) => c.ParentId == p.Id)
                       .InnerJoin(child, parent, grandparent, (c, p, gp) => p.ParentId == gp.Id && c.LivesAtHouseId == gp.LivesAtHouseId);
            var result = expr.ToSqlExpression();

            var expected =
                S.SelectNode(
                    S.Select(
                        S.Col(grandparent, "Name"),
                        S.Col(grandparent, "Age")),
                    S.From(
                        child,
                        S.Join(
                            child,
                            parent,
                            grandparent,
                            JoinType.InnerJoin,
                            S.BinExp(
                                S.BinExp(S.Col(parent, "ParentId"), BinaryOperation.Equal, S.Col(grandparent, "Id")),
                                BinaryOperation.And,
                                S.BinExp(S.Col(child, "LivesAtHouseId"), BinaryOperation.Equal, S.Col(grandparent, "LivesAtHouseId")))),
                        S.Join(child, parent, JoinType.InnerJoin, S.BinExp(S.Col(child, "ParentId"), BinaryOperation.Equal, S.Col(parent, "Id")))));

            Assert.That(result, Is.EqualTo(expected));
        }
示例#2
0
        public void ItShouldBePossibleToUseMultipleHavingExpressions()
        {
            var stream =
                S.SelectNode(
                    S.Select(
                        S.Col <Person>("Name"),
                        S.Aggregate(AggregateType.Avg, S.Col <Person>("Age")),
                        S.Col <Car>("Brand"),
                        S.Aggregate(AggregateType.Min, S.Col <Car>("Age"))),
                    S.From <Person>(
                        S.Join <Person, Car>(JoinType.InnerJoin, S.BinExp(S.Col <Person>("Id"), BinaryOperation.Equal, S.Col <Car>("DriverId")))),
                    groupBy:
                    S.GroupBy(
                        new[] { S.Col <Person>("Name"), S.Col <Car>("Brand"), },
                        S.And(S.BinExp(S.Aggregate(AggregateType.Avg, S.Col <Person>("Age")), BinaryOperation.GreaterThan, S.Const(42))),
                        S.And(S.BinExp(S.Aggregate(AggregateType.Min, S.Col <Car>("Age")), BinaryOperation.GreaterThan, S.Const(2)))));
            var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream);

            Assert.That(
                result,
                Is.EqualTo(
                    "SELECT PersonRef.Name, AVG(PersonRef.Age), CarRef.Brand, MIN(CarRef.Age) " +
                    "FROM Person PersonRef " +
                    "INNER JOIN Car CarRef ON PersonRef.Id = CarRef.DriverId " +
                    "GROUP BY PersonRef.Name, CarRef.Brand " +
                    "HAVING AVG(PersonRef.Age) > 42 " +
                    "AND MIN(CarRef.Age) > 2"));
        }
示例#3
0
 private SqlExpression GetExpectedJoinStream(JoinType joinKeyword)
 {
     return
         (S.SelectNode(
              S.Select(S.Star <Person>(), S.Star <Car>()),
              S.From <Person>(
                  S.Join <Person, Car>(joinKeyword, S.BinExp(S.Col <Person>("Id"), BinaryOperation.Equal, S.Col <Car>("DriverId")))
                  )));
 }
示例#4
0
        public void ItShouldBePossibleToSelectFromSeveralCommonTableExpressions()
        {
            var oldies = Table <PersonLite> .WithTableAlias("Oldies");

            var younglings = Table <PersonLite> .WithTableAlias("YoungOnes");

            var expr =
                S.SelectNode(
                    S.With(
                        S.TableDef(
                            younglings,
                            S.Select(S.Col <Person>("Name"), S.Col <Person>("Age")),
                            S.From <Person>(),
                            S.Where(S.BinExp(S.Col <Person>("Age"), BinaryOperation.LessThanOrEqual, S.Const(15)))),
                        S.TableDef(
                            oldies,
                            S.Select(S.Col <Person>("Name"), S.Col <Person>("Age")),
                            S.From <Person>(),
                            S.Where(S.BinExp(S.Col <Person>("Age"), BinaryOperation.GreaterThan, S.Const(40))))
                        ),
                    S.Select(S.Col(oldies, "Name"), S.Col(younglings, "Name")),
                    S.From(
                        oldies,
                        S.Join(
                            oldies,
                            younglings,
                            Domain.AST.JoinType.InnerJoin,
                            S.BinExp(
                                S.BinExp(S.Col(oldies, "Age"), BinaryOperation.Subtract, S.Const(30)),
                                BinaryOperation.Equal,
                                S.Col(younglings, "Age"))))
                    );

            var result = SqlGen.SqlifyExpression(Dialects.AnsiSql.Dialect, expr);

            var expected =
                "WITH Oldies AS (" +
                "SELECT PersonRef.Name, PersonRef.Age " +
                "FROM Person PersonRef " +
                "WHERE PersonRef.Age > 40" +
                "), YoungOnes AS (" +
                "SELECT PersonRef.Name, PersonRef.Age " +
                "FROM Person PersonRef " +
                "WHERE PersonRef.Age <= 15" +
                ") " +
                "SELECT OldiesRef.Name, YoungOnesRef.Name " +
                "FROM Oldies OldiesRef " +
                "INNER JOIN YoungOnes YoungOnesRef ON OldiesRef.Age - 30 = YoungOnesRef.Age";

            Assert.That(result, Is.EqualTo(expected));
        }
示例#5
0
        public void ItShouldBeAbleToSqlifyMultipleTableJoins()
        {
            var stream =
                S.SelectNode(
                    S.Select(S.Star <Person>(), S.Star <Car>(), S.Star <House>()),
                    S.From <House>(
                        S.Join <House, Person>(JoinType.InnerJoin, S.BinExp(S.Col <House>("Id"), BinaryOperation.Equal, S.Col <Person>("LivesAtHouseId"))),
                        S.Join <Person, Car>(JoinType.LeftJoin, S.BinExp(S.Col <Person>("Id"), BinaryOperation.Equal, S.Col <Car>("DriverId")))));
            var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream);

            Assert.That(
                result,
                Is.EqualTo(
                    @"SELECT PersonRef.*, CarRef.*, HouseRef.* " +
                    "FROM House HouseRef " +
                    "INNER JOIN Person PersonRef ON HouseRef.Id = PersonRef.LivesAtHouseId " +
                    "LEFT JOIN Car CarRef ON PersonRef.Id = CarRef.DriverId"));
        }
示例#6
0
        public void ItShouldBePossibleToJoinATableToItself()
        {
            var child = Table <Person> .WithReferenceName("child");

            var parent = Table <Person> .WithReferenceName("parent");

            var stream =
                S.SelectNode(
                    S.Select(S.Col(child, "Name"), S.Col(parent, "Name")),
                    S.From(
                        child,
                        S.Join(child, parent, JoinType.InnerJoin, S.BinExp(S.Col(child, "ParentId"), BinaryOperation.Equal, S.Col(parent, "Id")))));
            var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream);

            Assert.That(result,
                        Is.EqualTo("SELECT child.Name, parent.Name FROM Person child " +
                                   "INNER JOIN Person parent ON child.ParentId = parent.Id"));
        }
示例#7
0
        public void ItShouldBePossibleToDoARecursiveCommonTableExpression()
        {
            var stream =
                S.SelectNode(
                    S.With(
                        S.TableDef <RecursivePerson>(
                            S.UnionAll(
                                S.PlainSelect(
                                    S.Select(
                                        S.AliasedCol(S.Const(0), "Level"),
                                        S.Col <Person>("Name"),
                                        S.Col <Person>("ParentId")),
                                    S.From <Person>(),
                                    S.Where(
                                        S.BinExp(S.Col <Person>("Name"), BinaryOperation.Equal, S.Const("'Kalle'")))),
                                S.PlainSelect(
                                    S.Select(
                                        S.AliasedCol(
                                            S.BinExp(S.Col <RecursivePerson>("Level"), BinaryOperation.Add, S.Const(1)), "Level"),
                                        S.Col <Person>("Name"),
                                        S.Col <Person>("ParentId")),
                                    S.From <Person>(
                                        S.Join <Person, RecursivePerson>(
                                            AST.JoinType.InnerJoin,
                                            S.BinExp(S.Col <Person>("Id"), BinaryOperation.Equal, S.Col <RecursivePerson>("ParentId")))))))),
                    S.PlainSelect(
                        S.Select(S.Star <RecursivePerson>()),
                        S.From <RecursivePerson>()));
            var result = SqlGen.SqlifyExpression(Dialects.AnsiSql.Dialect, stream);

            var expected =
                "WITH RecursivePerson AS (" +
                "SELECT 0 AS Level, PersonRef.Name, PersonRef.ParentId FROM Person PersonRef WHERE PersonRef.Name = 'Kalle' " +
                "UNION ALL " +
                "SELECT RecursivePersonRef.Level + 1 AS Level, PersonRef.Name, PersonRef.ParentId " +
                "FROM Person PersonRef " +
                "INNER JOIN RecursivePerson RecursivePersonRef ON PersonRef.Id = RecursivePersonRef.ParentId" +
                ") " +
                "SELECT RecursivePersonRef.* FROM RecursivePerson RecursivePersonRef";

            Assert.That(result, Is.EqualTo(expected));
        }
示例#8
0
        public void ItShouldBePossibleToOrderBySeveralColumns()
        {
            var stream =
                S.SelectNode(
                    S.Select(S.Star <Person>(), S.Star <Car>()),
                    S.From <Person>(
                        S.Join <Person, Car>(JoinType.InnerJoin, S.BinExp(S.Col <Person>("Id"), BinaryOperation.Equal, S.Col <Car>("DriverId")))),
                    orderBy:
                    S.OrderBy(
                        S.OrderClause(S.Col <Person>("Age"), Direction.Ascending),
                        S.OrderClause(S.Col <Car>("Brand"), Direction.Descending)));
            var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream);

            Assert.That(
                result,
                Is.EqualTo(
                    @"SELECT PersonRef.*, CarRef.* FROM Person PersonRef " +
                    "INNER JOIN Car CarRef ON PersonRef.Id = CarRef.DriverId " +
                    "ORDER BY PersonRef.Age ASC, CarRef.Brand DESC"));
        }
示例#9
0
        public void ItShouldBePossibleToGroupByColumnsFromSeveralTables()
        {
            var stream =
                S.SelectNode(
                    S.Select(S.Col <Person>("Age"), S.Col <Car>("Brand"), S.Aggregate(AggregateType.Count, S.Null())),
                    S.From <Person>(
                        S.Join <Person, Car>(
                            JoinType.InnerJoin,
                            S.BinExp(S.Col <Person>("Id"), BinaryOperation.Equal, S.Col <Car>("DriverId")))),
                    groupBy: S.GroupBy(S.Col <Person>("Age"), S.Col <Car>("Brand")));
            var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream);

            Assert.That(
                result,
                Is.EqualTo(
                    "SELECT PersonRef.Age, CarRef.Brand, COUNT(*) " +
                    "FROM Person PersonRef " +
                    "INNER JOIN Car CarRef ON PersonRef.Id = CarRef.DriverId " +
                    "GROUP BY PersonRef.Age, CarRef.Brand"));
        }
示例#10
0
        public void ItShouldBePossibleToJoinATableToItself()
        {
            var child = Table <Person> .WithReferenceName("child");

            var parent = Table <Person> .WithReferenceName("parent");

            var expr = Select.Column(child, p => new { p.Name })
                       .Column(parent, p => new { p.Name })
                       .From(child)
                       .InnerJoin(child, parent, (p1, p2) => p1.ParentId == p2.Id);
            var result = expr.ToSqlExpression();

            var expected =
                S.SelectNode(
                    S.Select(S.Col(parent, "Name"), S.Col(child, "Name")),
                    S.From(
                        child,
                        S.Join(child, parent, JoinType.InnerJoin, S.BinExp(S.Col(child, "ParentId"), BinaryOperation.Equal, S.Col(parent, "Id")))));

            Assert.That(result, Is.EqualTo(expected));
        }
示例#11
0
        public void ItShouldBePossibleToInnerJoinATableOnSeveralOtherTables()
        {
            var expr = Select.Star <Car>()
                       .From <House>()
                       .InnerJoin <House, Person>((h, p) => h.Id == p.LivesAtHouseId)
                       .InnerJoin <House, Person, Car>((h, p, c) => c.Brand == h.Address && p.LivesAtHouseId == h.Id);
            var result = expr.ToSqlExpression();

            var expected =
                S.SelectNode(
                    S.Select(S.Star <Car>()),
                    S.From <House>(
                        S.Join <House, Person, Car>(
                            JoinType.InnerJoin,
                            S.BinExp(
                                S.BinExp(S.Col <Car>("Brand"), BinaryOperation.Equal, S.Col <House>("Address")),
                                BinaryOperation.And,
                                S.BinExp(S.Col <Person>("LivesAtHouseId"), BinaryOperation.Equal, S.Col <House>("Id")))),
                        S.Join <House, Person>(
                            JoinType.InnerJoin,
                            S.BinExp(S.Col <House>("Id"), BinaryOperation.Equal, S.Col <Person>("LivesAtHouseId")))));

            Assert.That(result, Is.EqualTo(expected));
        }
示例#12
0
        public void ItCanGenerateSqlForAConcatFunctionCall()
        {
            var stream =
                S.SelectNode(
                    S.Select(S.Func(AST.FunctionType.Concat, new[] { S.Col <Person>("Name"), S.Col <Car>("Brand"), S.Col <Person>("Name") })),
                    S.From <Person>(
                        S.Join <Person, Car>(
                            AST.JoinType.InnerJoin,
                            S.BinExp(S.Col <Person>("Id"), BinaryOperation.Equal, S.Col <Car>("DriverId")))),
                    S.Where(
                        S.BinExp(
                            S.Func(AST.FunctionType.Concat, new[] { S.Col <Person>("Name"), S.Col <Car>("Brand"), }),
                            BinaryOperation.Equal,
                            S.Const("'kallesaab'"))));
            var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream);

            string expected =
                "SELECT CONCAT(PersonRef.Name, CarRef.Brand, PersonRef.Name) " +
                "FROM Person PersonRef " +
                "INNER JOIN Car CarRef ON PersonRef.Id = CarRef.DriverId " +
                "WHERE CONCAT(PersonRef.Name, CarRef.Brand) = 'kallesaab'";

            Assert.That(result, Is.EqualTo(expected));
        }