Add scoped typed multi-table select builder

This commit is contained in:
root
2026-02-27 09:29:23 +01:00
parent 8210fbfdb5
commit 8555e1958c
8 changed files with 1706 additions and 4 deletions

View File

@@ -0,0 +1,126 @@
/*
* DynamORM - Dynamic Object-Relational Mapping library.
* Copyright (c) 2012-2026, Grzegorz Russek (grzegorz.russek@gmail.com)
* All rights reserved.
*/
using System.Text.RegularExpressions;
using DynamORM.Tests.Helpers;
using DynamORM.TypedSql;
using NUnit.Framework;
namespace DynamORM.Tests.TypedSql
{
[TestFixture]
public class TypedSqlScopeDslTests : TestsBase
{
private static string NormalizeSql(string sql)
{
int index = 0;
return Regex.Replace(sql, @"\[\$[^\]]+\]", m => string.Format("[${0}]", index++));
}
[SetUp]
public void SetUp()
{
CreateTestDatabase();
CreateDynamicDatabase(
DynamicDatabaseOptions.SingleConnection |
DynamicDatabaseOptions.SingleTransaction |
DynamicDatabaseOptions.SupportLimitOffset |
DynamicDatabaseOptions.SupportNoLock);
}
[TearDown]
public void TearDown()
{
DestroyDynamicDatabase();
DestroyTestDatabase();
}
[Test]
public void TestScopeBuilderSupportsTwoTableSelectAndWhere()
{
var cmd = Database.FromTypedScope<TypedJoinUser>("u")
.Join<TypedJoinUserClient>(j => j.Left().As("c").OnSql((u, c) => u.Col(x => x.IdUser).Eq(c.Col(x => x.UserId))))
.SelectSql(
(u, c) => u.Col(x => x.IdUser).As("user_id"),
(u, c) => c.Col(x => x.Users).As("users"))
.WhereSql((u, c) => u.Col(x => x.Active).Eq(1).And(c.Col(x => x.Deleted).Eq(0)));
Assert.AreEqual(
"SELECT u.\"Id_User\" AS \"user_id\", c.\"Users\" AS \"users\" FROM \"dbo\".\"Users\" AS u LEFT JOIN \"dbo\".\"UserClients\" AS c ON (u.\"Id_User\" = c.\"User_Id\") WHERE ((u.\"Active\" = [$0]) AND (c.\"Deleted\" = [$1]))",
NormalizeSql(cmd.CommandText()));
}
[Test]
public void TestScopeBuilderSupportsJoinOnPreviousJoinedAlias()
{
var cmd = Database.FromTypedScope<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, c, r) => c.Col(x => x.UserId).Eq(r.Col(x => x.UserId))))
.SelectSql(
(u, c, r) => u.Col(x => x.IdUser),
(u, c, r) => c.Col(x => x.Users).As("users"),
(u, c, r) => r.Col(x => x.RoleName).As("role_name"))
.OrderBySql(
(u, c, r) => c.Col(x => x.Users).Asc(),
(u, c, r) => r.Col(x => x.RoleName).Desc());
Assert.AreEqual(
"SELECT u.\"Id_User\", c.\"Users\" AS \"users\", r.\"Role_Name\" AS \"role_name\" 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 (c.\"User_Id\" = r.\"User_Id\") ORDER BY c.\"Users\" ASC, r.\"Role_Name\" DESC",
NormalizeSql(cmd.CommandText()));
}
[Test]
public void TestScopeBuilderSupportsCustomJoinTypeAndNoLock()
{
var cmd = Database.FromTypedScope<TypedJoinUser>("u")
.Join<TypedJoinUserClient>(j => j.Type("CROSS APPLY").As("c").NoLock())
.SelectSql(
(u, c) => u.Col(x => x.IdUser),
(u, c) => c.All());
Assert.AreEqual(
"SELECT u.\"Id_User\", c.* FROM \"dbo\".\"Users\" AS u CROSS APPLY \"dbo\".\"UserClients\" AS c WITH(NOLOCK)",
NormalizeSql(cmd.CommandText()));
}
[Test]
public void TestScopeBuilderSupportsHavingAcrossFourJoinedTables()
{
var cmd = Database.FromTypedScope<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, c, r) => u.Col(x => x.IdUser).Eq(r.Col(x => x.UserId))))
.Join<TypedJoinUserRegion>(j => j.Left().As("g").OnSql((u, c, r, g) => r.Col(x => x.UserId).Eq(g.Col(x => x.UserId))))
.SelectSql(
(u, c, r, g) => u.Col(x => x.IdUser),
(u, c, r, g) => Sql.Count(g.Col(x => x.RegionId)).As("region_count"))
.GroupBySql((u, c, r, g) => u.Col(x => x.IdUser))
.HavingSql((u, c, r, g) => Sql.Count(g.Col(x => x.RegionId)).Gt(0));
Assert.AreEqual(
"SELECT u.\"Id_User\", COUNT(g.\"Region_Id\") AS \"region_count\" 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 (r.\"User_Id\" = g.\"User_Id\") GROUP BY u.\"Id_User\" HAVING (COUNT(g.\"Region_Id\") > [$0])",
NormalizeSql(cmd.CommandText()));
}
[Test]
public void TestScopeBuilderSupportsFiveTypedTables()
{
var cmd = Database.FromTypedScope<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, c, r) => c.Col(x => x.UserId).Eq(r.Col(x => x.UserId))))
.Join<TypedJoinUserRegion>(j => j.Left().As("g").OnSql((u, c, r, g) => r.Col(x => x.UserId).Eq(g.Col(x => x.UserId))))
.Join<TypedJoinUserDepartment>(j => j.Left().As("d").OnSql((u, c, r, g, d) => g.Col(x => x.RegionId).Eq(d.Col(x => x.RegionId))))
.SelectSql(
(u, c, r, g, d) => u.Col(x => x.IdUser).As("user_id"),
(u, c, r, g, d) => d.Col(x => x.DepartmentName).As("department_name"))
.WhereSql((u, c, r, g, d) => d.Col(x => x.DepartmentId).Gt(0))
.OrderBySql((u, c, r, g, d) => d.Col(x => x.DepartmentName).Asc());
Assert.AreEqual(
"SELECT u.\"Id_User\" AS \"user_id\", d.\"Department_Name\" AS \"department_name\" 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 (c.\"User_Id\" = r.\"User_Id\") LEFT JOIN \"dbo\".\"UserRegions\" AS g ON (r.\"User_Id\" = g.\"User_Id\") LEFT JOIN \"dbo\".\"UserDepartments\" AS d ON (g.\"Region_Id\" = d.\"Region_Id\") WHERE (d.\"Department_Id\" > [$0]) ORDER BY d.\"Department_Name\" ASC",
NormalizeSql(cmd.CommandText()));
}
}
}