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")); }
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)); }
public void ItShouldHandleASimpleUnionAll() { var stream = AST.SqlExpression.NewSelect( AST.SelectExpression.NewPlain( AST.PlainSelectExpression.NewSet( S.UnionAll( S.PlainSelect( S.Select(S.Star <Person>()), S.From <Person>(), S.Where( S.BinExp(S.Col <Person>("Age"), AST.BinaryOperation.GreaterThan, S.Const(42)))), S.PlainSelect( S.Select(S.Star <Person>()), S.From <Person>(), S.Where( S.BinExp(S.Col <Person>("Name"), AST.BinaryOperation.Equal, S.Const("'Kalle'")))) )))); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream); var expected = "SELECT PersonRef.* FROM Person PersonRef WHERE PersonRef.Age > 42 " + "UNION ALL " + "SELECT PersonRef.* FROM Person PersonRef WHERE PersonRef.Name = 'Kalle'"; Assert.That(result, Is.EqualTo(expected)); }
public void ItShouldBePossibleToDeleteWithASubQuery() { var stream = S.DeleteNode( S.From <Car>(), S.Where( S.BinExp( S.Col <Car>("DriverId"), BinaryOperation.In, S.SubExp( S.Select(S.Col <Person>("Id")), S.From <Person>(), S.Where(S.BinExp(S.Col <Person>("Age"), BinaryOperation.LessThan, S.Const(18))))))); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream); Assert.That( result, Is.EqualTo( "DELETE FROM Car CarRef " + "WHERE CarRef.DriverId IN (" + "SELECT PersonRef.Id " + "FROM Person PersonRef " + "WHERE PersonRef.Age < 18" + ")")); }
public void ItShouldBePossibleToSelectFromACommonTableExpression() { var expr = S.SelectNode( S.With( S.TableDef <PersonLite>( 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(15))))), S.Select(S.Col <PersonLite>("Name")), S.From(new TableIdentifier <PersonLite>()), S.Where(S.BinExp(S.Col <PersonLite>("Age"), BinaryOperation.Equal, S.Const(42))) ); var result = SqlGen.SqlifyExpression(Dialects.AnsiSql.Dialect, expr); var expected = "WITH PersonLite AS (" + "SELECT PersonRef.Name, PersonRef.Age " + "FROM Person PersonRef " + "WHERE PersonRef.Age > 15" + ") " + "SELECT PersonLiteRef.Name " + "FROM PersonLite PersonLiteRef " + "WHERE PersonLiteRef.Age = 42"; Assert.That(result, Is.EqualTo(expected)); }
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"))) ))); }
public void ItShouldBePossibleToGroupByASingleColumn() { var stream = S.SelectNode( S.Select(S.Col <Person>("Name"), S.Aggregate(AggregateType.Max, S.Col <Person>("Age"))), S.From <Person>(), groupBy: S.GroupBy(S.Col <Person>("Name"))); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream); Assert.That(result, Is.EqualTo(@"SELECT PersonRef.Name, MAX(PersonRef.Age) FROM Person PersonRef GROUP BY PersonRef.Name")); }
public void ItShouldBeAbleToParameterizeAQuery() { var stream = S.SelectNode( S.Select(S.Star<Person>()), S.From<Person>(), S.Where(S.BinExp(S.Col<Person>("Age"), BinaryOperation.GreaterThan, S.Param("age")))); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream); Assert.That(result, Is.EqualTo("SELECT PersonRef.* FROM Person PersonRef WHERE PersonRef.Age > @age")); }
public void ItShouldBePossibleToSqlifyASimpleSelect() { var stream = S.SelectNode( S.Select(S.Star <Person>()), S.From <Person>()); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream); Assert.That(result, Is.EqualTo(@"SELECT PersonRef.* FROM Person PersonRef")); }
public void ItShouldBePossibleToOrderDescendingByAColumn() { var stream = S.SelectNode( S.Select(S.Star <Person>()), S.From <Person>(), orderBy: S.OrderBy(S.OrderClause(S.Col <Person>("Age"), Direction.Descending))); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream); Assert.That(result, Is.EqualTo(@"SELECT PersonRef.* FROM Person PersonRef ORDER BY PersonRef.Age DESC")); }
public void ItShouldBePossibleToSpecifyNullsFirstOrdering() { var stream = S.SelectNode( S.Select(S.Star <Person>()), S.From <Person>(), orderBy: S.OrderBy(S.OrderClause(S.Col <Person>("Age"), Direction.Ascending, NullsOrdering.NullsFirst))); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream); Assert.That(result, Is.EqualTo("SELECT PersonRef.* FROM Person PersonRef ORDER BY PersonRef.Age ASC NULLS FIRST")); }
public void ItShouldIncludeTheSchemaInTheGeneratedSql() { var stream = S.SelectNode( S.Select(S.Star <Employee>()), S.From <Employee>(), S.Where(S.BinExp(S.Col <Employee>("Salary"), BinaryOperation.GreaterThan, S.Const(25000)))); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream); Assert.That(result, Is.EqualTo(@"SELECT EmployeeRef.* FROM HumanResources.Employee EmployeeRef WHERE EmployeeRef.Salary > 25000")); }
public void ItCanSupportFlippingOrdersForInvertedComparisonWithNull() { var stream = S.SelectNode( S.Select(S.Star <Person>()), S.From <Person>(), S.Where( S.BinExp(S.Null(), BinaryOperation.NotEqual, S.Col <Person>("Age")))); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream); Assert.That(result, Is.EqualTo("SELECT PersonRef.* FROM Person PersonRef WHERE PersonRef.Age IS NOT NULL")); }
public void ItShouldBePossibleToSqlifyAWhere() { var stream = S.SelectNode( S.Select(S.Star <Person>()), S.From <Person>(), S.Where(S.BinExp(S.Col <Person>("Age"), BinaryOperation.GreaterThan, S.Const(42)))); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream); Assert.That(result, Is.EqualTo(@"SELECT PersonRef.* FROM Person PersonRef WHERE PersonRef.Age > 42")); }
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)); }
public void ItShouldProperlyQuoteIdentifiers() { var stream = S.SelectNode( S.Select( S.Col <Employee>("Name"), S.Col <Employee>("Age"), S.Col <Employee>("Id")), S.From <Employee>()); var result = SqlGen.SqlifyExpression(Dialects.SqlServer.Dialect, stream); Assert.That(result, Is.EqualTo(@"SELECT [EmployeeRef].[Name], [EmployeeRef].[Age], [EmployeeRef].[Id] FROM [HumanResources].[Employee] [EmployeeRef]")); }
public void ItShouldBePossibleToSelectWithColumnAliases() { var stream = S.SelectNode( S.Select( S.AliasedCol <Person>( AggregateType.Count, ValueNode.NullValue, "NumberOfPersons")), S.From <Person>()); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream); Assert.That(result, Is.EqualTo(@"SELECT COUNT(*) AS NumberOfPersons FROM Person PersonRef")); }
public void ItShouldProperlyQuoteIdentifiers() { var stream = S.SelectNode( S.Select( S.Col <Employee>("Name"), S.Col <Employee>("Age"), S.Col <Employee>("Id")), S.From <Employee>()); var result = SqlGen.SqlifyExpression(SqlLite.Dialect, stream); Assert.That(result, Is.EqualTo(@"SELECT ""EmployeeRef"".""Name"", ""EmployeeRef"".""Age"", ""EmployeeRef"".""Id"" FROM ""HumanResources"".""Employee"" ""EmployeeRef""")); }
public void ItShouldBePossibleToUseAnEmptyOverClause() { var expr = S.SelectNode( S.Select( S.Col <Person>("Name"), S.WinCol( AggregateType.Sum, S.Col <Person>("Age"))), S.From <Person>()); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, expr); Assert.That(result, Is.EqualTo("SELECT PersonRef.Name, SUM(PersonRef.Age) OVER () FROM Person PersonRef")); }
public void ItShouldBePossibleToSqlifyASelectWithManyColumns() { var stream = S.SelectNode( S.Select( S.Col <Person>("Name"), S.Col <Person>("Age"), S.Col <Person>("Id")), S.From <Person>()); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream); Assert.That(result, Is.EqualTo(@"SELECT PersonRef.Name, PersonRef.Age, PersonRef.Id FROM Person PersonRef")); }
public void ItShouldBePossibleToSqlifyAWhereWithAndOrsThatIsNotInBinaryExpressions() { var stream = S.SelectNode( S.Select(S.Star <Person>()), S.From <Person>(), S.Where( S.BinExp(S.Col <Person>("Age"), BinaryOperation.LessThan, S.Const(42)), S.And(S.BinExp(S.Col <Person>("Age"), BinaryOperation.GreaterThan, S.Const(12))), S.Or(S.BinExp(S.Col <Person>("Name"), BinaryOperation.Equal, S.Const("'Karl'"))))); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream); Assert.That(result, Is.EqualTo(@"SELECT PersonRef.* FROM Person PersonRef WHERE PersonRef.Age < 42 AND PersonRef.Age > 12 OR PersonRef.Name = 'Karl'")); }
public void ItShouldBePossibleToOrderByAColumn() { var expr = S.SelectNode( S.Select( S.Col <Person>("Name"), S.WinCol( AggregateType.Sum, S.Col <Person>("Age"), orderBy: new[] { S.OrderClause(S.Col <Person>("Age"), Direction.Ascending) })), S.From <Person>()); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, expr); Assert.That(result, Is.EqualTo("SELECT PersonRef.Name, SUM(PersonRef.Age) OVER (ORDER BY PersonRef.Age ASC) FROM Person PersonRef")); }
public void ItShouldBePossibleToUpdateValuesToASubExpression() { var stream = S.UpdateNode( S.UpdHead <Person>( S.Ust <Person>( "Age", S.SubExp( S.Select(S.Aggregate(AggregateType.Max, S.Col <Car>("Age"))), S.From <Car>())))); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream); Assert.That(result, Is.EqualTo("UPDATE Person SET Age = (SELECT MAX(CarRef.Age) FROM Car CarRef)")); }
public void ItShouldBePossibleToPartitionByAColumn() { var expr = S.SelectNode( S.Select( S.Col <Person>("Name"), S.WinCol( AggregateType.Max, S.Col <Person>("Age"), new[] { S.Col <Person>("Name") })), S.From <Person>()); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, expr); Assert.That(result, Is.EqualTo("SELECT PersonRef.Name, MAX(PersonRef.Age) OVER (PARTITION BY PersonRef.Name) FROM Person PersonRef")); }
public void ItShouldBePossibleToGroupByMultipleColumns() { var stream = S.SelectNode( S.Select(S.Col <Person>("Name"), S.Col <Person>("LivesAtHouseId"), S.Aggregate(AggregateType.Min, S.Col <Person>("Age"))), S.From <Person>(), groupBy: S.GroupBy(S.Col <Person>("Name"), S.Col <Person>("LivesAtHouseId"))); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream); Assert.That( result, Is.EqualTo( @"SELECT PersonRef.Name, PersonRef.LivesAtHouseId, MIN(PersonRef.Age) FROM Person PersonRef " + "GROUP BY PersonRef.Name, PersonRef.LivesAtHouseId")); }
public void ItShouldBePossibleToSqlifyAQueryWithAnInSubExpression() { var stream = S.SelectNode( S.Select(S.Star <Person>()), S.From <Person>(), S.Where( S.BinExp( S.Col <Person>("Id"), BinaryOperation.In, S.SubExp( S.Select(S.Col <Car>("DriverId")), S.From <Car>())))); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream); Assert.That(result, Is.EqualTo(@"SELECT PersonRef.* FROM Person PersonRef WHERE PersonRef.Id IN (SELECT CarRef.DriverId FROM Car CarRef)")); }
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")); }
public void ItCanGenerateSqlForToLower() { var stream = S.SelectNode( S.Select(S.Func(AST.FunctionType.Lower, new[] { S.Col <Person>("Name") })), S.From <Person>(), S.Where( S.BinExp( S.Func(AST.FunctionType.Lower, new[] { S.Col <Person>("Name") }), BinaryOperation.Equal, S.Const("'kalle'")) )); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream); string expected = "SELECT LOWER(PersonRef.Name) FROM Person PersonRef WHERE LOWER(PersonRef.Name) = 'kalle'"; Assert.That(result, Is.EqualTo(expected)); }
public void ItShouldBePossibleToSqlifyACompareAgainstASubExpressionInAWhereExpression() { var stream = S.SelectNode( S.Select(S.Star <Person>()), S.From <Person>(), S.Where( S.BinExp( S.Col <Person>("Age"), BinaryOperation.GreaterThan, S.SubExp( S.Select(S.Col <Car>("Age")), S.From <Car>(), S.Where(S.BinExp(S.Col <Car>("Id"), BinaryOperation.Equal, S.Const(42))))))); var result = SqlGen.SqlifyExpression(AnsiSql.Dialect, stream); Assert.That(result, Is.EqualTo(@"SELECT PersonRef.* FROM Person PersonRef WHERE PersonRef.Age > (SELECT CarRef.Age FROM Car CarRef WHERE CarRef.Id = 42)")); }
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")); }