diff --git a/Src/Asp.NetCore2/MySqlTest/Program.cs b/Src/Asp.NetCore2/MySqlTest/Program.cs index 51699b23e..4cd80127e 100644 --- a/Src/Asp.NetCore2/MySqlTest/Program.cs +++ b/Src/Asp.NetCore2/MySqlTest/Program.cs @@ -22,7 +22,12 @@ namespace OrmTest _9_Update.Init(); _a1_Delete.Init(); _a2_Sql.Init(); - _a3_Merge.Init(); + _a3_Merge.Init(); + _a4_SplitTable.Init(); + _a5_GridSave.Init(); + _a6_SqlPage.Init(); + _a7_JsonType.Init(); + _a8_SelectReturnType.Init(); } } diff --git a/Src/Asp.NetCore2/MySqlTest/a4_SplitTable.cs b/Src/Asp.NetCore2/MySqlTest/a4_SplitTable.cs new file mode 100644 index 000000000..dca9e4542 --- /dev/null +++ b/Src/Asp.NetCore2/MySqlTest/a4_SplitTable.cs @@ -0,0 +1,66 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + internal class _a4_SplitTable + { + // Method for initialization and testing of split tables + // 用于初始化和测试分表的方法 + public static void Init() + { + // Obtain a new instance of SqlSugarClient + // 获取 SqlSugarClient 的新实例 + SqlSugarClient db = DbHelper.GetNewDb(); + + + // Entities change the synchronization table structure + // 实体变化同步表结构 + db.CodeFirst.SplitTables().InitTables(); + + // Insert records into the split table and create table + // 向分表插入记录并创建表 + db.Insertable(new SplitTableDemo() { Name = "jack", Time = DateTime.Now }).SplitTable().ExecuteCommand(); + db.Insertable(new SplitTableDemo() { Name = "jack2", Time = DateTime.Now.AddDays(-11) }).SplitTable().ExecuteCommand(); + + // Query records from the split table within a specified date range + // 在指定日期范围内从分表查询记录 + var list = db.Queryable() + .Where(it=>it.Name!=null) + .SplitTable(DateTime.Now.Date.AddYears(-1), DateTime.Now) + .ToList(); + + // Update records from the split table + // 从分表更新记录 + var updateList = list.Take(2).ToList(); + db.Updateable(updateList).SplitTable().ExecuteCommand(); + + // Delete records from the split table + // 从分表删除记录 + db.Deleteable(updateList).SplitTable().ExecuteCommand(); + } + + // Entity class representing the split table + // 代表分表的实体类 + [SplitTable(SplitType.Day)] // Specify the split type as "Day" + // 指定分表类型为“Day” + [SqlSugar.SugarTable("SplitTableDemo_{year}{month}{day}")] // Specify the table name pattern + // 指定表名模式 + public class SplitTableDemo + { + [SugarColumn(IsPrimaryKey = true)] // Specify primary key + // 指定主键 + public Guid Pk { get; set; } + public string Name { get; set; } + + [SugarColumn(IsNullable = true)] + [SplitField] // Mark the field as a split field + // 将字段标记为分表字段 + public DateTime Time { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/MySqlTest/a5_GridSave.cs b/Src/Asp.NetCore2/MySqlTest/a5_GridSave.cs new file mode 100644 index 000000000..4ee26ac92 --- /dev/null +++ b/Src/Asp.NetCore2/MySqlTest/a5_GridSave.cs @@ -0,0 +1,81 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + internal class _a5_GridSave + { + public static void Init() + { + // Get a new database connection + // 获取一个新的数据库连接 + SqlSugarClient db = DbHelper.GetNewDb(); + + // Initialize tables using CodeFirst + // 使用 CodeFirst 初始化表 + db.CodeFirst.InitTables(); + + // Clear table data + // 清空表数据 + db.DbMaintenance.TruncateTable(); + + // Insert two student records + // 插入两条学生记录 + db.Insertable(new List() { + new Student() {Name= "jack",CreateTime=DateTime.Now}, + new Student() {Name= "tom",CreateTime=DateTime.Now} + }).ExecuteReturnIdentity(); + + // Query all student records + // 查询所有学生记录 + List getAll = db.Queryable().ToList(); + + + + // Enable entity tracking for the list 'getAll' + // 启用对列表 'getAll' 的实体跟踪 + db.Tracking(getAll); + + + + + // Remove the first record + // 移除第一条记录 + getAll.RemoveAt(0); + + // Modify the name of the last record + // 修改最后一条记录的姓名 + getAll[getAll.Count - 1].Name += "_Update"; + + // Add a new record + // 添加新记录 + getAll.Add(new Student { Name = "NewRecord" }); + + + + + + // Execute GridSave operation + // 执行 GridSave 操作 + db.GridSave(getAll).ExecuteCommand(); + + // Query all students again + // 再次查询所有学生 + var list = db.Queryable().ToList(); + } + + // Define the entity class 定义实体类 + [SugarTable("SaveTable_a5")] + public class Student + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + public string Name { get; set; } + public DateTime CreateTime { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/MySqlTest/a6_SqlPage.cs b/Src/Asp.NetCore2/MySqlTest/a6_SqlPage.cs new file mode 100644 index 000000000..47a67aaef --- /dev/null +++ b/Src/Asp.NetCore2/MySqlTest/a6_SqlPage.cs @@ -0,0 +1,59 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices.ComTypes; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + internal class _a6_SqlPage + { + /// + /// + /// Initializes an example method for SQL paging operations. + /// 初始化 SQL 分页操作的示例方法。 + /// + internal static void Init() + { + // Get a new database connection object + // 获取新的数据库连接对象 + var db = DbHelper.GetNewDb(); + + // CodeFirst initializes the ClassA table + // CodeFirst 初始化 ClassA 表 + db.CodeFirst.InitTables(); + for (int i = 0; i < 16; i++) + { + db.Insertable(new ClassA() { Name = Guid.NewGuid().ToString("N") }).ExecuteCommand(); + } + + + // Query data using paging and get the total count + // 使用分页查询数据,并获取总记录数 + int count = 0; + var list = db.SqlQueryable("select * from Table_a6").ToPageList(1, 5, ref count); + + + + // Asynchronously query data using paging and get the total count + // 使用异步方式分页查询数据,并获取总记录数 + RefAsync countAsync = 0; + var listAsync = db.SqlQueryable("select * from Table_a6").ToPageListAsync(1, 5, countAsync).GetAwaiter().GetResult(); + } + + /// + /// Example entity class. + /// 示例实体类。 + /// + [SugarTable("Table_a6")] + public class ClassA + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + + public string Name { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/MySqlTest/a7_JsonType.cs b/Src/Asp.NetCore2/MySqlTest/a7_JsonType.cs new file mode 100644 index 000000000..86557e6e9 --- /dev/null +++ b/Src/Asp.NetCore2/MySqlTest/a7_JsonType.cs @@ -0,0 +1,71 @@ +using SqlSugar; +using System; +using System.Collections.Generic; + +namespace OrmTest +{ + internal class _a7_JsonType + { + /// + /// Demonstrates JSON operations with SqlSugar. + /// 展示了在 SqlSugar 中进行 JSON 操作的示例。 + /// + internal static void Init() + { + // Get a new database connection object + // 获取一个新的数据库连接对象 + var db = DbHelper.GetNewDb(); + + // Create table + // 创建表 + db.CodeFirst.InitTables(); + + // Insert a record with a JSON property + // 插入一条包含 JSON 属性的记录 + db.Insertable(new UnitJsonTest() + { + Name = "json1", + Order = new Order { Id = 1, Name = "order1" } + }).ExecuteCommand(); + + // Query all records from the table + // 查询表中的所有记录 + var list = db.Queryable().ToList(); + + //Sqlfunc.JsonXXX + var list2=db.Queryable() + .Select(it => new + { + id=it.Id, + jsonname=SqlFunc.JsonField(it.Order,"Name") + }) + .ToList(); + } + + /// + /// Represents a class with a JSON property. + /// 表示一个包含 JSON 属性的类。 + /// + [SugarTable("UnitJsonTest_a7")] + public class UnitJsonTest + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + + [SugarColumn(IsJson = true)] + public Order Order { get; set; } + + public string Name { get; set; } + } + + /// + /// Represents an order entity. + /// 表示订单实体。 + /// + public class Order + { + public int Id { get; set; } + public string Name { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/MySqlTest/a8_SelectReturnType.cs b/Src/Asp.NetCore2/MySqlTest/a8_SelectReturnType.cs new file mode 100644 index 000000000..4e7e67af1 --- /dev/null +++ b/Src/Asp.NetCore2/MySqlTest/a8_SelectReturnType.cs @@ -0,0 +1,68 @@ +using SqlSugar; +using System; +using System.Collections.Generic; + +namespace OrmTest +{ + internal class _a8_SelectReturnType + { + internal static void Init() + { + var db = DbHelper.GetNewDb(); + + //Create table + //建表 + db.CodeFirst.InitTables(); + //init data + db.Insertable(new Student() { CreateTime = DateTime.Now, Name = "aa" }).ExecuteCommand(); + + + // 返回匿名对象 (Return anonymous objects) + var dynamicList = db.Queryable().Select().ToList(); + // SELECT * FROM Student_a8 + + // 手动:返回匿名集合,支持跨程序集 (Manually: Return an anonymous collection, supporting cross-assembly) + List dynamicListCrossAssembly = db.Queryable().Select(it => (dynamic)new { id = it.Id }).ToList(); + // SELECT id AS id FROM Student_a8 + + // 手动:返回匿名集合,不能跨程序集 (Manually: Return an anonymous collection, within the same assembly) + var dynamicListWithinAssembly = db.Queryable().Select(it => new { id = it.Id }).ToList(); + // SELECT id AS id FROM Student_a8 + + // 手动:返回类集合-手动 (Manually: Return a class collection manually) + List classList = db.Queryable().Select(it => new Student { Id = it.Id }).ToList(); + // SELECT id AS Id FROM Student_a8 + + // 自动返回DTO集合: 请升级至 5.1.3.2 版本 (Automatically return DTO collections: Upgrade to version 5.1.3.2) + var listDto = db.Queryable().Select().ToList(); + + // 自动返回DTO: 请升级至 5.1.3.35 版本 (Automatically return DTO: Upgrade to version 5.1.3.35) + var listDtoAutoMap = db.Queryable() + .Select(it => new StudentDto + { + AppendColumn = 100 // 手动指定一列在自动映射 (Manually specify a column in automatic mapping) + }, + true) // true 表示开启自动映射 (true indicates enabling automatic mapping) + .ToList(); + } + + // 学生表的实体类 (Entity class for the Student table) + [SugarTable("Student_a8")] + public class Student + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + public string Name { get; set; } + public DateTime CreateTime { get; set; } + } + + // DTO 数据传输对象 (DTO - Data Transfer Object) + public class StudentDto + { + public int Id { get; set; } + public string Name { get; set; } + public DateTime CreateTime { get; set; } + public int AppendColumn { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/PgSqlTest/Program.cs b/Src/Asp.NetCore2/PgSqlTest/Program.cs index 3349efb2a..49a240df3 100644 --- a/Src/Asp.NetCore2/PgSqlTest/Program.cs +++ b/Src/Asp.NetCore2/PgSqlTest/Program.cs @@ -22,7 +22,12 @@ namespace OrmTest _9_Update.Init(); _a1_Delete.Init(); _a2_Sql.Init(); - _a3_Merge.Init(); + _a3_Merge.Init(); + _a4_SplitTable.Init(); + _a5_GridSave.Init(); + _a6_SqlPage.Init(); + _a7_JsonType.Init(); + _a8_SelectReturnType.Init(); } } diff --git a/Src/Asp.NetCore2/PgSqlTest/a4_SplitTable.cs b/Src/Asp.NetCore2/PgSqlTest/a4_SplitTable.cs new file mode 100644 index 000000000..dca9e4542 --- /dev/null +++ b/Src/Asp.NetCore2/PgSqlTest/a4_SplitTable.cs @@ -0,0 +1,66 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + internal class _a4_SplitTable + { + // Method for initialization and testing of split tables + // 用于初始化和测试分表的方法 + public static void Init() + { + // Obtain a new instance of SqlSugarClient + // 获取 SqlSugarClient 的新实例 + SqlSugarClient db = DbHelper.GetNewDb(); + + + // Entities change the synchronization table structure + // 实体变化同步表结构 + db.CodeFirst.SplitTables().InitTables(); + + // Insert records into the split table and create table + // 向分表插入记录并创建表 + db.Insertable(new SplitTableDemo() { Name = "jack", Time = DateTime.Now }).SplitTable().ExecuteCommand(); + db.Insertable(new SplitTableDemo() { Name = "jack2", Time = DateTime.Now.AddDays(-11) }).SplitTable().ExecuteCommand(); + + // Query records from the split table within a specified date range + // 在指定日期范围内从分表查询记录 + var list = db.Queryable() + .Where(it=>it.Name!=null) + .SplitTable(DateTime.Now.Date.AddYears(-1), DateTime.Now) + .ToList(); + + // Update records from the split table + // 从分表更新记录 + var updateList = list.Take(2).ToList(); + db.Updateable(updateList).SplitTable().ExecuteCommand(); + + // Delete records from the split table + // 从分表删除记录 + db.Deleteable(updateList).SplitTable().ExecuteCommand(); + } + + // Entity class representing the split table + // 代表分表的实体类 + [SplitTable(SplitType.Day)] // Specify the split type as "Day" + // 指定分表类型为“Day” + [SqlSugar.SugarTable("SplitTableDemo_{year}{month}{day}")] // Specify the table name pattern + // 指定表名模式 + public class SplitTableDemo + { + [SugarColumn(IsPrimaryKey = true)] // Specify primary key + // 指定主键 + public Guid Pk { get; set; } + public string Name { get; set; } + + [SugarColumn(IsNullable = true)] + [SplitField] // Mark the field as a split field + // 将字段标记为分表字段 + public DateTime Time { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/PgSqlTest/a5_GridSave.cs b/Src/Asp.NetCore2/PgSqlTest/a5_GridSave.cs new file mode 100644 index 000000000..4ee26ac92 --- /dev/null +++ b/Src/Asp.NetCore2/PgSqlTest/a5_GridSave.cs @@ -0,0 +1,81 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + internal class _a5_GridSave + { + public static void Init() + { + // Get a new database connection + // 获取一个新的数据库连接 + SqlSugarClient db = DbHelper.GetNewDb(); + + // Initialize tables using CodeFirst + // 使用 CodeFirst 初始化表 + db.CodeFirst.InitTables(); + + // Clear table data + // 清空表数据 + db.DbMaintenance.TruncateTable(); + + // Insert two student records + // 插入两条学生记录 + db.Insertable(new List() { + new Student() {Name= "jack",CreateTime=DateTime.Now}, + new Student() {Name= "tom",CreateTime=DateTime.Now} + }).ExecuteReturnIdentity(); + + // Query all student records + // 查询所有学生记录 + List getAll = db.Queryable().ToList(); + + + + // Enable entity tracking for the list 'getAll' + // 启用对列表 'getAll' 的实体跟踪 + db.Tracking(getAll); + + + + + // Remove the first record + // 移除第一条记录 + getAll.RemoveAt(0); + + // Modify the name of the last record + // 修改最后一条记录的姓名 + getAll[getAll.Count - 1].Name += "_Update"; + + // Add a new record + // 添加新记录 + getAll.Add(new Student { Name = "NewRecord" }); + + + + + + // Execute GridSave operation + // 执行 GridSave 操作 + db.GridSave(getAll).ExecuteCommand(); + + // Query all students again + // 再次查询所有学生 + var list = db.Queryable().ToList(); + } + + // Define the entity class 定义实体类 + [SugarTable("SaveTable_a5")] + public class Student + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + public string Name { get; set; } + public DateTime CreateTime { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/PgSqlTest/a6_SqlPage.cs b/Src/Asp.NetCore2/PgSqlTest/a6_SqlPage.cs new file mode 100644 index 000000000..47a67aaef --- /dev/null +++ b/Src/Asp.NetCore2/PgSqlTest/a6_SqlPage.cs @@ -0,0 +1,59 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices.ComTypes; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + internal class _a6_SqlPage + { + /// + /// + /// Initializes an example method for SQL paging operations. + /// 初始化 SQL 分页操作的示例方法。 + /// + internal static void Init() + { + // Get a new database connection object + // 获取新的数据库连接对象 + var db = DbHelper.GetNewDb(); + + // CodeFirst initializes the ClassA table + // CodeFirst 初始化 ClassA 表 + db.CodeFirst.InitTables(); + for (int i = 0; i < 16; i++) + { + db.Insertable(new ClassA() { Name = Guid.NewGuid().ToString("N") }).ExecuteCommand(); + } + + + // Query data using paging and get the total count + // 使用分页查询数据,并获取总记录数 + int count = 0; + var list = db.SqlQueryable("select * from Table_a6").ToPageList(1, 5, ref count); + + + + // Asynchronously query data using paging and get the total count + // 使用异步方式分页查询数据,并获取总记录数 + RefAsync countAsync = 0; + var listAsync = db.SqlQueryable("select * from Table_a6").ToPageListAsync(1, 5, countAsync).GetAwaiter().GetResult(); + } + + /// + /// Example entity class. + /// 示例实体类。 + /// + [SugarTable("Table_a6")] + public class ClassA + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + + public string Name { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/PgSqlTest/a7_JsonType.cs b/Src/Asp.NetCore2/PgSqlTest/a7_JsonType.cs new file mode 100644 index 000000000..86557e6e9 --- /dev/null +++ b/Src/Asp.NetCore2/PgSqlTest/a7_JsonType.cs @@ -0,0 +1,71 @@ +using SqlSugar; +using System; +using System.Collections.Generic; + +namespace OrmTest +{ + internal class _a7_JsonType + { + /// + /// Demonstrates JSON operations with SqlSugar. + /// 展示了在 SqlSugar 中进行 JSON 操作的示例。 + /// + internal static void Init() + { + // Get a new database connection object + // 获取一个新的数据库连接对象 + var db = DbHelper.GetNewDb(); + + // Create table + // 创建表 + db.CodeFirst.InitTables(); + + // Insert a record with a JSON property + // 插入一条包含 JSON 属性的记录 + db.Insertable(new UnitJsonTest() + { + Name = "json1", + Order = new Order { Id = 1, Name = "order1" } + }).ExecuteCommand(); + + // Query all records from the table + // 查询表中的所有记录 + var list = db.Queryable().ToList(); + + //Sqlfunc.JsonXXX + var list2=db.Queryable() + .Select(it => new + { + id=it.Id, + jsonname=SqlFunc.JsonField(it.Order,"Name") + }) + .ToList(); + } + + /// + /// Represents a class with a JSON property. + /// 表示一个包含 JSON 属性的类。 + /// + [SugarTable("UnitJsonTest_a7")] + public class UnitJsonTest + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + + [SugarColumn(IsJson = true)] + public Order Order { get; set; } + + public string Name { get; set; } + } + + /// + /// Represents an order entity. + /// 表示订单实体。 + /// + public class Order + { + public int Id { get; set; } + public string Name { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/PgSqlTest/a8_SelectReturnType.cs b/Src/Asp.NetCore2/PgSqlTest/a8_SelectReturnType.cs new file mode 100644 index 000000000..4e7e67af1 --- /dev/null +++ b/Src/Asp.NetCore2/PgSqlTest/a8_SelectReturnType.cs @@ -0,0 +1,68 @@ +using SqlSugar; +using System; +using System.Collections.Generic; + +namespace OrmTest +{ + internal class _a8_SelectReturnType + { + internal static void Init() + { + var db = DbHelper.GetNewDb(); + + //Create table + //建表 + db.CodeFirst.InitTables(); + //init data + db.Insertable(new Student() { CreateTime = DateTime.Now, Name = "aa" }).ExecuteCommand(); + + + // 返回匿名对象 (Return anonymous objects) + var dynamicList = db.Queryable().Select().ToList(); + // SELECT * FROM Student_a8 + + // 手动:返回匿名集合,支持跨程序集 (Manually: Return an anonymous collection, supporting cross-assembly) + List dynamicListCrossAssembly = db.Queryable().Select(it => (dynamic)new { id = it.Id }).ToList(); + // SELECT id AS id FROM Student_a8 + + // 手动:返回匿名集合,不能跨程序集 (Manually: Return an anonymous collection, within the same assembly) + var dynamicListWithinAssembly = db.Queryable().Select(it => new { id = it.Id }).ToList(); + // SELECT id AS id FROM Student_a8 + + // 手动:返回类集合-手动 (Manually: Return a class collection manually) + List classList = db.Queryable().Select(it => new Student { Id = it.Id }).ToList(); + // SELECT id AS Id FROM Student_a8 + + // 自动返回DTO集合: 请升级至 5.1.3.2 版本 (Automatically return DTO collections: Upgrade to version 5.1.3.2) + var listDto = db.Queryable().Select().ToList(); + + // 自动返回DTO: 请升级至 5.1.3.35 版本 (Automatically return DTO: Upgrade to version 5.1.3.35) + var listDtoAutoMap = db.Queryable() + .Select(it => new StudentDto + { + AppendColumn = 100 // 手动指定一列在自动映射 (Manually specify a column in automatic mapping) + }, + true) // true 表示开启自动映射 (true indicates enabling automatic mapping) + .ToList(); + } + + // 学生表的实体类 (Entity class for the Student table) + [SugarTable("Student_a8")] + public class Student + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + public string Name { get; set; } + public DateTime CreateTime { get; set; } + } + + // DTO 数据传输对象 (DTO - Data Transfer Object) + public class StudentDto + { + public int Id { get; set; } + public string Name { get; set; } + public DateTime CreateTime { get; set; } + public int AppendColumn { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/SqliteTest/Program.cs b/Src/Asp.NetCore2/SqliteTest/Program.cs index 5122eea2e..34487e74d 100644 --- a/Src/Asp.NetCore2/SqliteTest/Program.cs +++ b/Src/Asp.NetCore2/SqliteTest/Program.cs @@ -22,7 +22,12 @@ namespace OrmTest _9_Update.Init(); _a1_Delete.Init(); _a2_Sql.Init(); - _a3_Merge.Init(); + _a3_Merge.Init(); + _a4_SplitTable.Init(); + _a5_GridSave.Init(); + _a6_SqlPage.Init(); + _a7_JsonType.Init(); + _a8_SelectReturnType.Init(); } } diff --git a/Src/Asp.NetCore2/SqliteTest/a4_SplitTable.cs b/Src/Asp.NetCore2/SqliteTest/a4_SplitTable.cs new file mode 100644 index 000000000..dca9e4542 --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/a4_SplitTable.cs @@ -0,0 +1,66 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + internal class _a4_SplitTable + { + // Method for initialization and testing of split tables + // 用于初始化和测试分表的方法 + public static void Init() + { + // Obtain a new instance of SqlSugarClient + // 获取 SqlSugarClient 的新实例 + SqlSugarClient db = DbHelper.GetNewDb(); + + + // Entities change the synchronization table structure + // 实体变化同步表结构 + db.CodeFirst.SplitTables().InitTables(); + + // Insert records into the split table and create table + // 向分表插入记录并创建表 + db.Insertable(new SplitTableDemo() { Name = "jack", Time = DateTime.Now }).SplitTable().ExecuteCommand(); + db.Insertable(new SplitTableDemo() { Name = "jack2", Time = DateTime.Now.AddDays(-11) }).SplitTable().ExecuteCommand(); + + // Query records from the split table within a specified date range + // 在指定日期范围内从分表查询记录 + var list = db.Queryable() + .Where(it=>it.Name!=null) + .SplitTable(DateTime.Now.Date.AddYears(-1), DateTime.Now) + .ToList(); + + // Update records from the split table + // 从分表更新记录 + var updateList = list.Take(2).ToList(); + db.Updateable(updateList).SplitTable().ExecuteCommand(); + + // Delete records from the split table + // 从分表删除记录 + db.Deleteable(updateList).SplitTable().ExecuteCommand(); + } + + // Entity class representing the split table + // 代表分表的实体类 + [SplitTable(SplitType.Day)] // Specify the split type as "Day" + // 指定分表类型为“Day” + [SqlSugar.SugarTable("SplitTableDemo_{year}{month}{day}")] // Specify the table name pattern + // 指定表名模式 + public class SplitTableDemo + { + [SugarColumn(IsPrimaryKey = true)] // Specify primary key + // 指定主键 + public Guid Pk { get; set; } + public string Name { get; set; } + + [SugarColumn(IsNullable = true)] + [SplitField] // Mark the field as a split field + // 将字段标记为分表字段 + public DateTime Time { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/SqliteTest/a5_GridSave.cs b/Src/Asp.NetCore2/SqliteTest/a5_GridSave.cs new file mode 100644 index 000000000..4ee26ac92 --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/a5_GridSave.cs @@ -0,0 +1,81 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + internal class _a5_GridSave + { + public static void Init() + { + // Get a new database connection + // 获取一个新的数据库连接 + SqlSugarClient db = DbHelper.GetNewDb(); + + // Initialize tables using CodeFirst + // 使用 CodeFirst 初始化表 + db.CodeFirst.InitTables(); + + // Clear table data + // 清空表数据 + db.DbMaintenance.TruncateTable(); + + // Insert two student records + // 插入两条学生记录 + db.Insertable(new List() { + new Student() {Name= "jack",CreateTime=DateTime.Now}, + new Student() {Name= "tom",CreateTime=DateTime.Now} + }).ExecuteReturnIdentity(); + + // Query all student records + // 查询所有学生记录 + List getAll = db.Queryable().ToList(); + + + + // Enable entity tracking for the list 'getAll' + // 启用对列表 'getAll' 的实体跟踪 + db.Tracking(getAll); + + + + + // Remove the first record + // 移除第一条记录 + getAll.RemoveAt(0); + + // Modify the name of the last record + // 修改最后一条记录的姓名 + getAll[getAll.Count - 1].Name += "_Update"; + + // Add a new record + // 添加新记录 + getAll.Add(new Student { Name = "NewRecord" }); + + + + + + // Execute GridSave operation + // 执行 GridSave 操作 + db.GridSave(getAll).ExecuteCommand(); + + // Query all students again + // 再次查询所有学生 + var list = db.Queryable().ToList(); + } + + // Define the entity class 定义实体类 + [SugarTable("SaveTable_a5")] + public class Student + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + public string Name { get; set; } + public DateTime CreateTime { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/SqliteTest/a6_SqlPage.cs b/Src/Asp.NetCore2/SqliteTest/a6_SqlPage.cs new file mode 100644 index 000000000..47a67aaef --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/a6_SqlPage.cs @@ -0,0 +1,59 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices.ComTypes; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + internal class _a6_SqlPage + { + /// + /// + /// Initializes an example method for SQL paging operations. + /// 初始化 SQL 分页操作的示例方法。 + /// + internal static void Init() + { + // Get a new database connection object + // 获取新的数据库连接对象 + var db = DbHelper.GetNewDb(); + + // CodeFirst initializes the ClassA table + // CodeFirst 初始化 ClassA 表 + db.CodeFirst.InitTables(); + for (int i = 0; i < 16; i++) + { + db.Insertable(new ClassA() { Name = Guid.NewGuid().ToString("N") }).ExecuteCommand(); + } + + + // Query data using paging and get the total count + // 使用分页查询数据,并获取总记录数 + int count = 0; + var list = db.SqlQueryable("select * from Table_a6").ToPageList(1, 5, ref count); + + + + // Asynchronously query data using paging and get the total count + // 使用异步方式分页查询数据,并获取总记录数 + RefAsync countAsync = 0; + var listAsync = db.SqlQueryable("select * from Table_a6").ToPageListAsync(1, 5, countAsync).GetAwaiter().GetResult(); + } + + /// + /// Example entity class. + /// 示例实体类。 + /// + [SugarTable("Table_a6")] + public class ClassA + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + + public string Name { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/SqliteTest/a7_JsonType.cs b/Src/Asp.NetCore2/SqliteTest/a7_JsonType.cs new file mode 100644 index 000000000..fb6444afa --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/a7_JsonType.cs @@ -0,0 +1,62 @@ +using SqlSugar; +using System; +using System.Collections.Generic; + +namespace OrmTest +{ + internal class _a7_JsonType + { + /// + /// Demonstrates JSON operations with SqlSugar. + /// 展示了在 SqlSugar 中进行 JSON 操作的示例。 + /// + internal static void Init() + { + // Get a new database connection object + // 获取一个新的数据库连接对象 + var db = DbHelper.GetNewDb(); + + // Create table + // 创建表 + db.CodeFirst.InitTables(); + + // Insert a record with a JSON property + // 插入一条包含 JSON 属性的记录 + db.Insertable(new UnitJsonTest() + { + Name = "json1", + Order = new Order { Id = 1, Name = "order1" } + }).ExecuteCommand(); + + // Query all records from the table + // 查询表中的所有记录 + var list = db.Queryable().ToList(); + } + + /// + /// Represents a class with a JSON property. + /// 表示一个包含 JSON 属性的类。 + /// + [SugarTable("UnitJsonTest_a7")] + public class UnitJsonTest + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + + [SugarColumn(IsJson = true)] + public Order Order { get; set; } + + public string Name { get; set; } + } + + /// + /// Represents an order entity. + /// 表示订单实体。 + /// + public class Order + { + public int Id { get; set; } + public string Name { get; set; } + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/SqliteTest/a8_SelectReturnType.cs b/Src/Asp.NetCore2/SqliteTest/a8_SelectReturnType.cs new file mode 100644 index 000000000..4e7e67af1 --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/a8_SelectReturnType.cs @@ -0,0 +1,68 @@ +using SqlSugar; +using System; +using System.Collections.Generic; + +namespace OrmTest +{ + internal class _a8_SelectReturnType + { + internal static void Init() + { + var db = DbHelper.GetNewDb(); + + //Create table + //建表 + db.CodeFirst.InitTables(); + //init data + db.Insertable(new Student() { CreateTime = DateTime.Now, Name = "aa" }).ExecuteCommand(); + + + // 返回匿名对象 (Return anonymous objects) + var dynamicList = db.Queryable().Select().ToList(); + // SELECT * FROM Student_a8 + + // 手动:返回匿名集合,支持跨程序集 (Manually: Return an anonymous collection, supporting cross-assembly) + List dynamicListCrossAssembly = db.Queryable().Select(it => (dynamic)new { id = it.Id }).ToList(); + // SELECT id AS id FROM Student_a8 + + // 手动:返回匿名集合,不能跨程序集 (Manually: Return an anonymous collection, within the same assembly) + var dynamicListWithinAssembly = db.Queryable().Select(it => new { id = it.Id }).ToList(); + // SELECT id AS id FROM Student_a8 + + // 手动:返回类集合-手动 (Manually: Return a class collection manually) + List classList = db.Queryable().Select(it => new Student { Id = it.Id }).ToList(); + // SELECT id AS Id FROM Student_a8 + + // 自动返回DTO集合: 请升级至 5.1.3.2 版本 (Automatically return DTO collections: Upgrade to version 5.1.3.2) + var listDto = db.Queryable().Select().ToList(); + + // 自动返回DTO: 请升级至 5.1.3.35 版本 (Automatically return DTO: Upgrade to version 5.1.3.35) + var listDtoAutoMap = db.Queryable() + .Select(it => new StudentDto + { + AppendColumn = 100 // 手动指定一列在自动映射 (Manually specify a column in automatic mapping) + }, + true) // true 表示开启自动映射 (true indicates enabling automatic mapping) + .ToList(); + } + + // 学生表的实体类 (Entity class for the Student table) + [SugarTable("Student_a8")] + public class Student + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + public string Name { get; set; } + public DateTime CreateTime { get; set; } + } + + // DTO 数据传输对象 (DTO - Data Transfer Object) + public class StudentDto + { + public int Id { get; set; } + public string Name { get; set; } + public DateTime CreateTime { get; set; } + public int AppendColumn { get; set; } + } + } +} \ No newline at end of file