Add positional typed multi-table SQL contexts
This commit is contained in:
36
DynamORM.Tests/Helpers/TypedMultiJoinModels.cs
Normal file
36
DynamORM.Tests/Helpers/TypedMultiJoinModels.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* DynamORM - Dynamic Object-Relational Mapping library.
|
||||
* Copyright (c) 2012-2026, Grzegorz Russek (grzegorz.russek@gmail.com)
|
||||
* All rights reserved.
|
||||
*/
|
||||
|
||||
using DynamORM.Mapper;
|
||||
|
||||
namespace DynamORM.Tests.Helpers
|
||||
{
|
||||
[Table(Name = "UserRoles", Owner = "dbo")]
|
||||
public class TypedJoinUserRole
|
||||
{
|
||||
[Column("Role_Id", true)]
|
||||
public long RoleId { get; set; }
|
||||
|
||||
[Column("User_Id")]
|
||||
public long UserId { get; set; }
|
||||
|
||||
[Column("Role_Name")]
|
||||
public string RoleName { get; set; }
|
||||
}
|
||||
|
||||
[Table(Name = "UserRegions", Owner = "dbo")]
|
||||
public class TypedJoinUserRegion
|
||||
{
|
||||
[Column("Region_Id", true)]
|
||||
public long RegionId { get; set; }
|
||||
|
||||
[Column("User_Id")]
|
||||
public long UserId { get; set; }
|
||||
|
||||
[Column("Region_Code")]
|
||||
public string RegionCode { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -386,5 +386,117 @@ namespace DynamORM.Tests.TypedSql
|
||||
"UPDATE \"sample_users\" SET \"code\" = (SELECT x.\"user_code\" FROM \"sample_users\" AS x WHERE (x.\"id_user\" = [$0])), \"first\" = LOWER([$1]) WHERE (\"id\" = [$2])",
|
||||
NormalizeSql(cmd.CommandText()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestSelectSqlSupportsPositionalJoinedContexts()
|
||||
{
|
||||
var cmd = Database.FromTyped<TypedJoinUser>("u")
|
||||
.Join<TypedJoinUserClient>(j => j.Left().As("c").OnSql((u, c) => u.Col(x => x.IdUser).Eq(c.Col(x => x.UserId))))
|
||||
.SelectSql<TypedJoinUserClient>(
|
||||
(u, c) => u.Col(x => x.IdUser).As("user_id"),
|
||||
(u, c) => c.Col(x => x.Users).As("client_users"));
|
||||
|
||||
Assert.AreEqual(
|
||||
"SELECT u.\"Id_User\" AS \"user_id\", c.\"Users\" AS \"client_users\" FROM \"dbo\".\"Users\" AS u LEFT JOIN \"dbo\".\"UserClients\" AS c ON (u.\"Id_User\" = c.\"User_Id\")",
|
||||
NormalizeSql(cmd.CommandText()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestWhereGroupHavingAndOrderBySupportPositionalJoinedContexts()
|
||||
{
|
||||
var cmd = Database.FromTyped<TypedJoinUser>("u")
|
||||
.Join<TypedJoinUserClient>(j => j.Inner().As("c").OnSql((u, c) => u.Col(x => x.IdUser).Eq(c.Col(x => x.UserId))))
|
||||
.SelectSql<TypedJoinUserClient>(
|
||||
(u, c) => u.Col(x => x.IdUser),
|
||||
(u, c) => Sql.Count(c.Col(x => x.UserId)).As("cnt"))
|
||||
.WhereSql<TypedJoinUserClient>((u, c) => u.Col(x => x.Active).Eq(1).And(c.Col(x => x.Deleted).Eq(0)))
|
||||
.GroupBySql<TypedJoinUserClient>(
|
||||
(u, c) => u.Col(x => x.IdUser),
|
||||
(u, c) => c.Col(x => x.Users))
|
||||
.HavingSql<TypedJoinUserClient>((u, c) => Sql.Count(c.Col(x => x.UserId)).Gt(0))
|
||||
.OrderBySql<TypedJoinUserClient>(
|
||||
(u, c) => c.Col(x => x.Users).Asc(),
|
||||
(u, c) => Sql.Count(c.Col(x => x.UserId)).Desc());
|
||||
|
||||
Assert.AreEqual(
|
||||
"SELECT u.\"Id_User\", COUNT(c.\"User_Id\") AS \"cnt\" FROM \"dbo\".\"Users\" AS u INNER JOIN \"dbo\".\"UserClients\" AS c ON (u.\"Id_User\" = c.\"User_Id\") WHERE ((u.\"Active\" = [$0]) AND (c.\"Deleted\" = [$1])) GROUP BY u.\"Id_User\", c.\"Users\" HAVING (COUNT(c.\"User_Id\") > [$2]) ORDER BY c.\"Users\" ASC, COUNT(c.\"User_Id\") DESC",
|
||||
NormalizeSql(cmd.CommandText()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestPositionalJoinedContextsSupportThreeJoins()
|
||||
{
|
||||
var cmd = Database.FromTyped<TypedJoinUser>("u")
|
||||
.Join<TypedJoinUserClient>(j => j.Left().As("c").OnSql((u, c) => u.Col(x => x.IdUser).Eq(c.Col(x => x.UserId))))
|
||||
.Join<TypedJoinUserRole>(j => j.Left().As("r").OnSql((u, r) => u.Col(x => x.IdUser).Eq(r.Col(x => x.UserId))))
|
||||
.Join<TypedJoinUserRegion>(j => j.Left().As("g").OnSql((u, g) => u.Col(x => x.IdUser).Eq(g.Col(x => x.UserId))))
|
||||
.SelectSql<TypedJoinUserClient, TypedJoinUserRole, TypedJoinUserRegion>(
|
||||
(u, c, r, g) => u.Col(x => x.IdUser),
|
||||
(u, c, r, g) => c.Col(x => x.Users).As("client_users"),
|
||||
(u, c, r, g) => r.Col(x => x.RoleName).As("role_name"),
|
||||
(u, c, r, g) => g.Col(x => x.RegionCode).As("region_code"))
|
||||
.WhereSql<TypedJoinUserClient, TypedJoinUserRole, TypedJoinUserRegion>(
|
||||
(u, c, r, g) => c.Col(x => x.Deleted).Eq(0).And(r.Col(x => x.RoleId).Gt(0)).And(g.Col(x => x.RegionId).Gt(0)))
|
||||
.OrderBySql<TypedJoinUserClient, TypedJoinUserRole, TypedJoinUserRegion>(
|
||||
(u, c, r, g) => r.Col(x => x.RoleName).Asc(),
|
||||
(u, c, r, g) => g.Col(x => x.RegionCode).Desc());
|
||||
|
||||
Assert.AreEqual(
|
||||
"SELECT u.\"Id_User\", c.\"Users\" AS \"client_users\", r.\"Role_Name\" AS \"role_name\", g.\"Region_Code\" AS \"region_code\" FROM \"dbo\".\"Users\" AS u LEFT JOIN \"dbo\".\"UserClients\" AS c ON (u.\"Id_User\" = c.\"User_Id\") LEFT JOIN \"dbo\".\"UserRoles\" AS r ON (u.\"Id_User\" = r.\"User_Id\") LEFT JOIN \"dbo\".\"UserRegions\" AS g ON (u.\"Id_User\" = g.\"User_Id\") WHERE (((c.\"Deleted\" = [$0]) AND (r.\"Role_Id\" > [$1])) AND (g.\"Region_Id\" > [$2])) ORDER BY r.\"Role_Name\" ASC, g.\"Region_Code\" DESC",
|
||||
NormalizeSql(cmd.CommandText()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestPositionalJoinedContextsSupportFourJoinedTables()
|
||||
{
|
||||
var cmd = Database.FromTyped<TypedFluentUser>("u")
|
||||
.Join<TypedFluentUser>(j => j.Left().As("b").OnSql((u, b) => u.Col(x => x.Id).Eq(b.Col(x => x.Id))))
|
||||
.Join<TypedFluentUser>(j => j.Left().As("c").OnSql((u, c) => u.Col(x => x.Id).Eq(c.Col(x => x.Id))))
|
||||
.Join<TypedFluentUser>(j => j.Left().As("d").OnSql((u, d) => u.Col(x => x.Id).Eq(d.Col(x => x.Id))))
|
||||
.Join<TypedFluentUser>(j => j.Left().As("e").OnSql((u, e) => u.Col(x => x.Id).Eq(e.Col(x => x.Id))))
|
||||
.SelectSql<TypedFluentUser, TypedFluentUser, TypedFluentUser, TypedFluentUser>(
|
||||
(u, b, c, d, e) => u.Col(x => x.Id).As("root_id"),
|
||||
(u, b, c, d, e) => b.Col(x => x.Code).As("b_code"),
|
||||
(u, b, c, d, e) => c.Col(x => x.Code).As("c_code"),
|
||||
(u, b, c, d, e) => d.Col(x => x.Code).As("d_code"),
|
||||
(u, b, c, d, e) => e.Col(x => x.Code).As("e_code"))
|
||||
.WhereSql<TypedFluentUser, TypedFluentUser, TypedFluentUser, TypedFluentUser>(
|
||||
(u, b, c, d, e) => b.Col(x => x.Code).IsNotNull()
|
||||
.And(c.Col(x => x.Code).IsNotNull())
|
||||
.And(d.Col(x => x.Code).IsNotNull())
|
||||
.And(e.Col(x => x.Code).IsNotNull()));
|
||||
|
||||
Assert.AreEqual(
|
||||
"SELECT u.\"id_user\" AS \"root_id\", b.\"user_code\" AS \"b_code\", c.\"user_code\" AS \"c_code\", d.\"user_code\" AS \"d_code\", e.\"user_code\" AS \"e_code\" FROM \"sample_users\" AS u LEFT JOIN \"sample_users\" AS b ON (u.\"id_user\" = b.\"id_user\") LEFT JOIN \"sample_users\" AS c ON (u.\"id_user\" = c.\"id_user\") LEFT JOIN \"sample_users\" AS d ON (u.\"id_user\" = d.\"id_user\") LEFT JOIN \"sample_users\" AS e ON (u.\"id_user\" = e.\"id_user\") WHERE ((((b.\"user_code\" IS NOT NULL) AND (c.\"user_code\" IS NOT NULL)) AND (d.\"user_code\" IS NOT NULL)) AND (e.\"user_code\" IS NOT NULL))",
|
||||
NormalizeSql(cmd.CommandText()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestPositionalJoinedContextsValidateRequestedType()
|
||||
{
|
||||
var builder = Database.FromTyped<TypedJoinUser>("u")
|
||||
.Join<TypedJoinUserClient>(j => j.Left().As("c").OnSql((u, c) => u.Col(x => x.IdUser).Eq(c.Col(x => x.UserId))));
|
||||
|
||||
var ex = Assert.Throws<System.InvalidOperationException>(() =>
|
||||
builder.SelectSql<TypedJoinUserRole>((u, r) => r.Col(x => x.RoleId).As("role_id")));
|
||||
|
||||
Assert.AreEqual(
|
||||
"Typed join context at position 1 is DynamORM.Tests.Helpers.TypedJoinUserClient, not DynamORM.Tests.Helpers.TypedJoinUserRole.",
|
||||
ex.Message);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestPositionalJoinedContextsValidateRequestedPosition()
|
||||
{
|
||||
var builder = Database.FromTyped<TypedJoinUser>("u")
|
||||
.Join<TypedJoinUserClient>(j => j.Left().As("c").OnSql((u, c) => u.Col(x => x.IdUser).Eq(c.Col(x => x.UserId))));
|
||||
|
||||
var ex = Assert.Throws<System.InvalidOperationException>(() =>
|
||||
builder.SelectSql<TypedJoinUserClient, TypedJoinUserRole>((u, c, r) => r.Col(x => x.RoleId).As("role_id")));
|
||||
|
||||
Assert.AreEqual(
|
||||
"Typed join context at position 2 is not available.",
|
||||
ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user