diff --git a/FreeSql.Tests.Provider.MySqlConnector/FreeSql.Tests.Provider.MySqlConnector.csproj b/FreeSql.Tests.Provider.MySqlConnector/FreeSql.Tests.Provider.MySqlConnector.csproj
new file mode 100644
index 00000000..7ba5e744
--- /dev/null
+++ b/FreeSql.Tests.Provider.MySqlConnector/FreeSql.Tests.Provider.MySqlConnector.csproj
@@ -0,0 +1,21 @@
+
+
+
+ netcoreapp2.2
+
+ false
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/Curd/MySqlDeleteTest.cs b/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/Curd/MySqlDeleteTest.cs
new file mode 100644
index 00000000..fe402ce3
--- /dev/null
+++ b/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/Curd/MySqlDeleteTest.cs
@@ -0,0 +1,87 @@
+using FreeSql.DataAnnotations;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Xunit;
+
+namespace FreeSql.Tests.MySqlConnector {
+ public class MySqlDeleteTest {
+
+ IDelete delete => g.mysql.Delete(); //��������
+
+ [Table(Name = "tb_topic")]
+ class Topic {
+ [Column(IsIdentity = true, IsPrimary = true)]
+ public int Id { get; set; }
+ public int Clicks { get; set; }
+ public string Title { get; set; }
+ public DateTime CreateTime { get; set; }
+ }
+
+ [Fact]
+ public void Dywhere() {
+ Assert.Null(g.mysql.Delete().ToSql());
+ var sql = g.mysql.Delete(new[] { 1, 2 }).ToSql();
+ Assert.Equal("DELETE FROM `tb_topic` WHERE (`Id` = 1 OR `Id` = 2)", sql);
+
+ sql = g.mysql.Delete(new Topic { Id = 1, Title = "test" }).ToSql();
+ Assert.Equal("DELETE FROM `tb_topic` WHERE (`Id` = 1)", sql);
+
+ sql = g.mysql.Delete(new[] { new Topic { Id = 1, Title = "test" }, new Topic { Id = 2, Title = "test" } }).ToSql();
+ Assert.Equal("DELETE FROM `tb_topic` WHERE (`Id` = 1 OR `Id` = 2)", sql);
+
+ sql = g.mysql.Delete(new { id = 1 }).ToSql();
+ Assert.Equal("DELETE FROM `tb_topic` WHERE (`Id` = 1)", sql);
+ }
+
+ [Fact]
+ public void Where() {
+ var sql = delete.Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
+ Assert.Equal("DELETE FROM `tb_topic` WHERE (`Id` = 1)", sql);
+
+ sql = delete.Where("id = @id", new { id = 1 }).ToSql().Replace("\r\n", "");
+ Assert.Equal("DELETE FROM `tb_topic` WHERE (id = @id)", sql);
+
+ var item = new Topic { Id = 1, Title = "newtitle" };
+ sql = delete.Where(item).ToSql().Replace("\r\n", "");
+ Assert.Equal("DELETE FROM `tb_topic` WHERE (`Id` = 1)", sql);
+
+ var items = new List();
+ for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
+
+ sql = delete.Where(items).ToSql().Replace("\r\n", "");
+ Assert.Equal("DELETE FROM `tb_topic` WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))", sql);
+ }
+ [Fact]
+ public void WhereExists() {
+
+ }
+ [Fact]
+ public void ExecuteAffrows() {
+
+ var id = g.mysql.Insert(new Topic { Title = "xxxx" }).ExecuteIdentity();
+ Assert.Equal(1, delete.Where(a => a.Id == id).ExecuteAffrows());
+ }
+ [Fact]
+ public void ExecuteDeleted() {
+
+ //delete.Where(a => a.Id > 0).ExecuteDeleted();
+ }
+
+ [Fact]
+ public void AsTable() {
+ Assert.Null(g.mysql.Delete().ToSql());
+ var sql = g.mysql.Delete(new[] { 1, 2 }).AsTable(a => "TopicAsTable").ToSql();
+ Assert.Equal("DELETE FROM `TopicAsTable` WHERE (`Id` = 1 OR `Id` = 2)", sql);
+
+ sql = g.mysql.Delete(new Topic { Id = 1, Title = "test" }).AsTable(a => "TopicAsTable").ToSql();
+ Assert.Equal("DELETE FROM `TopicAsTable` WHERE (`Id` = 1)", sql);
+
+ sql = g.mysql.Delete(new[] { new Topic { Id = 1, Title = "test" }, new Topic { Id = 2, Title = "test" } }).AsTable(a => "TopicAsTable").ToSql();
+ Assert.Equal("DELETE FROM `TopicAsTable` WHERE (`Id` = 1 OR `Id` = 2)", sql);
+
+ sql = g.mysql.Delete(new { id = 1 }).AsTable(a => "TopicAsTable").ToSql();
+ Assert.Equal("DELETE FROM `TopicAsTable` WHERE (`Id` = 1)", sql);
+ }
+ }
+}
diff --git a/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/Curd/MySqlInsertTest.cs b/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/Curd/MySqlInsertTest.cs
new file mode 100644
index 00000000..ff222517
--- /dev/null
+++ b/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/Curd/MySqlInsertTest.cs
@@ -0,0 +1,135 @@
+using FreeSql.DataAnnotations;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Xunit;
+
+namespace FreeSql.Tests.MySqlConnector {
+ public class MySqlInsertTest {
+
+ IInsert insert => g.mysql.Insert(); //��������
+
+ [Table(Name = "tb_topic")]
+ class Topic {
+ [Column(IsIdentity = true, IsPrimary = true)]
+ public int Id { get; set; }
+ public int Clicks { get; set; }
+ public string Title { get; set; }
+ public DateTime CreateTime { get; set; }
+ }
+ class TestEnumInsertTb {
+ [Column(IsIdentity = true)]
+ public int id { get; set; }
+ public TestEnumInserTbType type { get; set; }
+ public DateTime time { get; set; } = new DateTime();
+ }
+ enum TestEnumInserTbType { str1, biggit, sum211 }
+
+ [Fact]
+ public void AppendData() {
+ var items = new List();
+ for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
+
+ var sql = insert.AppendData(items.First()).ToSql();
+ Assert.Equal("INSERT INTO `tb_topic`(`Clicks`, `Title`, `CreateTime`) VALUES(@Clicks_0, @Title_0, @CreateTime_0)", sql);
+
+ sql = insert.AppendData(items).ToSql();
+ Assert.Equal("INSERT INTO `tb_topic`(`Clicks`, `Title`, `CreateTime`) VALUES(@Clicks_0, @Title_0, @CreateTime_0), (@Clicks_1, @Title_1, @CreateTime_1), (@Clicks_2, @Title_2, @CreateTime_2), (@Clicks_3, @Title_3, @CreateTime_3), (@Clicks_4, @Title_4, @CreateTime_4), (@Clicks_5, @Title_5, @CreateTime_5), (@Clicks_6, @Title_6, @CreateTime_6), (@Clicks_7, @Title_7, @CreateTime_7), (@Clicks_8, @Title_8, @CreateTime_8), (@Clicks_9, @Title_9, @CreateTime_9)", sql);
+
+ sql = insert.AppendData(items).InsertColumns(a => a.Title).ToSql();
+ Assert.Equal("INSERT INTO `tb_topic`(`Title`) VALUES(@Title_0), (@Title_1), (@Title_2), (@Title_3), (@Title_4), (@Title_5), (@Title_6), (@Title_7), (@Title_8), (@Title_9)", sql);
+
+ sql = insert.AppendData(items).IgnoreColumns(a => a.CreateTime).ToSql();
+ Assert.Equal("INSERT INTO `tb_topic`(`Clicks`, `Title`) VALUES(@Clicks_0, @Title_0), (@Clicks_1, @Title_1), (@Clicks_2, @Title_2), (@Clicks_3, @Title_3), (@Clicks_4, @Title_4), (@Clicks_5, @Title_5), (@Clicks_6, @Title_6), (@Clicks_7, @Title_7), (@Clicks_8, @Title_8), (@Clicks_9, @Title_9)", sql);
+
+ sql = g.mysql.Insert().AppendData(new TestEnumInsertTb { type = TestEnumInserTbType.sum211, time = DateTime.Now }).ToSql();
+ Assert.Equal("INSERT INTO `TestEnumInsertTb`(`type`, `time`) VALUES(@type_0, @time_0)", sql);
+
+ sql = g.mysql.Insert().AppendData(new TestEnumInsertTb { type = TestEnumInserTbType.sum211 }).NoneParameter().ToSql();
+ Assert.Equal("INSERT INTO `TestEnumInsertTb`(`type`, `time`) VALUES('sum211', '0001-01-01 00:00:00.000')", sql);
+ }
+
+ [Fact]
+ public void InsertColumns() {
+ var items = new List();
+ for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
+
+ var sql = insert.AppendData(items).InsertColumns(a => a.Title).ToSql();
+ Assert.Equal("INSERT INTO `tb_topic`(`Title`) VALUES(@Title_0), (@Title_1), (@Title_2), (@Title_3), (@Title_4), (@Title_5), (@Title_6), (@Title_7), (@Title_8), (@Title_9)", sql);
+
+ sql = insert.AppendData(items).InsertColumns(a =>new { a.Title, a.Clicks }).ToSql();
+ Assert.Equal("INSERT INTO `tb_topic`(`Clicks`, `Title`) VALUES(@Clicks_0, @Title_0), (@Clicks_1, @Title_1), (@Clicks_2, @Title_2), (@Clicks_3, @Title_3), (@Clicks_4, @Title_4), (@Clicks_5, @Title_5), (@Clicks_6, @Title_6), (@Clicks_7, @Title_7), (@Clicks_8, @Title_8), (@Clicks_9, @Title_9)", sql);
+ }
+ [Fact]
+ public void IgnoreColumns() {
+ var items = new List();
+ for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
+
+ var sql = insert.AppendData(items).IgnoreColumns(a => a.CreateTime).ToSql();
+ Assert.Equal("INSERT INTO `tb_topic`(`Clicks`, `Title`) VALUES(@Clicks_0, @Title_0), (@Clicks_1, @Title_1), (@Clicks_2, @Title_2), (@Clicks_3, @Title_3), (@Clicks_4, @Title_4), (@Clicks_5, @Title_5), (@Clicks_6, @Title_6), (@Clicks_7, @Title_7), (@Clicks_8, @Title_8), (@Clicks_9, @Title_9)", sql);
+
+ sql = insert.AppendData(items).IgnoreColumns(a => new { a.Title, a.CreateTime }).ToSql();
+ Assert.Equal("INSERT INTO `tb_topic`(`Clicks`) VALUES(@Clicks_0), (@Clicks_1), (@Clicks_2), (@Clicks_3), (@Clicks_4), (@Clicks_5), (@Clicks_6), (@Clicks_7), (@Clicks_8), (@Clicks_9)", sql);
+ }
+ [Fact]
+ public void ExecuteAffrows() {
+ var items = new List();
+ for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100, CreateTime = DateTime.Now });
+
+ Assert.Equal(1, insert.AppendData(items.First()).ExecuteAffrows());
+ Assert.Equal(10, insert.AppendData(items).ExecuteAffrows());
+
+ Assert.Equal(1, g.mysql.Insert().AppendData(new TestEnumInsertTb { type = TestEnumInserTbType.sum211 }).ExecuteAffrows());
+ Assert.Equal(1, g.mysql.Insert().AppendData(new TestEnumInsertTb { type = TestEnumInserTbType.sum211 }).NoneParameter().ExecuteAffrows());
+ }
+ [Fact]
+ public void ExecuteIdentity() {
+ var items = new List();
+ for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
+
+ Assert.NotEqual(0, insert.AppendData(items.First()).ExecuteIdentity());
+
+ var id = g.mysql.Insert().AppendData(new TestEnumInsertTb { type = TestEnumInserTbType.sum211 }).ExecuteIdentity();
+ Assert.Equal(TestEnumInserTbType.sum211, g.mysql.Select< TestEnumInsertTb>().Where(a => a.id == id).First()?.type);
+ id = g.mysql.Insert().AppendData(new TestEnumInsertTb { type = TestEnumInserTbType.sum211 }).NoneParameter().ExecuteIdentity();
+ Assert.Equal(TestEnumInserTbType.sum211, g.mysql.Select().Where(a => a.id == id).First()?.type);
+ }
+ [Fact]
+ public void ExecuteInserted() {
+ var items = new List();
+ for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
+
+ //insert.AppendData(items.First()).ExecuteInserted();
+ }
+
+ [Fact]
+ public void AsTable() {
+ var items = new List();
+ for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newTitle{a}", Clicks = a * 100 });
+
+ var sql = insert.AppendData(items.First()).AsTable(a => "Topic_InsertAsTable").ToSql();
+ Assert.Equal("INSERT INTO `Topic_InsertAsTable`(`Clicks`, `Title`, `CreateTime`) VALUES(@Clicks_0, @Title_0, @CreateTime_0)", sql);
+
+ sql = insert.AppendData(items).AsTable(a => "Topic_InsertAsTable").ToSql();
+ Assert.Equal("INSERT INTO `Topic_InsertAsTable`(`Clicks`, `Title`, `CreateTime`) VALUES(@Clicks_0, @Title_0, @CreateTime_0), (@Clicks_1, @Title_1, @CreateTime_1), (@Clicks_2, @Title_2, @CreateTime_2), (@Clicks_3, @Title_3, @CreateTime_3), (@Clicks_4, @Title_4, @CreateTime_4), (@Clicks_5, @Title_5, @CreateTime_5), (@Clicks_6, @Title_6, @CreateTime_6), (@Clicks_7, @Title_7, @CreateTime_7), (@Clicks_8, @Title_8, @CreateTime_8), (@Clicks_9, @Title_9, @CreateTime_9)", sql);
+
+ sql = insert.AppendData(items).InsertColumns(a => a.Title).AsTable(a => "Topic_InsertAsTable").ToSql();
+ Assert.Equal("INSERT INTO `Topic_InsertAsTable`(`Title`) VALUES(@Title_0), (@Title_1), (@Title_2), (@Title_3), (@Title_4), (@Title_5), (@Title_6), (@Title_7), (@Title_8), (@Title_9)", sql);
+
+ sql = insert.AppendData(items).IgnoreColumns(a => a.CreateTime).AsTable(a => "Topic_InsertAsTable").ToSql();
+ Assert.Equal("INSERT INTO `Topic_InsertAsTable`(`Clicks`, `Title`) VALUES(@Clicks_0, @Title_0), (@Clicks_1, @Title_1), (@Clicks_2, @Title_2), (@Clicks_3, @Title_3), (@Clicks_4, @Title_4), (@Clicks_5, @Title_5), (@Clicks_6, @Title_6), (@Clicks_7, @Title_7), (@Clicks_8, @Title_8), (@Clicks_9, @Title_9)", sql);
+
+ sql = insert.AppendData(items).InsertColumns(a => a.Title).AsTable(a => "Topic_InsertAsTable").ToSql();
+ Assert.Equal("INSERT INTO `Topic_InsertAsTable`(`Title`) VALUES(@Title_0), (@Title_1), (@Title_2), (@Title_3), (@Title_4), (@Title_5), (@Title_6), (@Title_7), (@Title_8), (@Title_9)", sql);
+
+ sql = insert.AppendData(items).InsertColumns(a => new { a.Title, a.Clicks }).AsTable(a => "Topic_InsertAsTable").ToSql();
+ Assert.Equal("INSERT INTO `Topic_InsertAsTable`(`Clicks`, `Title`) VALUES(@Clicks_0, @Title_0), (@Clicks_1, @Title_1), (@Clicks_2, @Title_2), (@Clicks_3, @Title_3), (@Clicks_4, @Title_4), (@Clicks_5, @Title_5), (@Clicks_6, @Title_6), (@Clicks_7, @Title_7), (@Clicks_8, @Title_8), (@Clicks_9, @Title_9)", sql);
+
+ sql = insert.AppendData(items).IgnoreColumns(a => a.CreateTime).AsTable(a => "Topic_InsertAsTable").ToSql();
+ Assert.Equal("INSERT INTO `Topic_InsertAsTable`(`Clicks`, `Title`) VALUES(@Clicks_0, @Title_0), (@Clicks_1, @Title_1), (@Clicks_2, @Title_2), (@Clicks_3, @Title_3), (@Clicks_4, @Title_4), (@Clicks_5, @Title_5), (@Clicks_6, @Title_6), (@Clicks_7, @Title_7), (@Clicks_8, @Title_8), (@Clicks_9, @Title_9)", sql);
+
+ sql = insert.AppendData(items).IgnoreColumns(a => new { a.Title, a.CreateTime }).AsTable(a => "Topic_InsertAsTable").ToSql();
+ Assert.Equal("INSERT INTO `Topic_InsertAsTable`(`Clicks`) VALUES(@Clicks_0), (@Clicks_1), (@Clicks_2), (@Clicks_3), (@Clicks_4), (@Clicks_5), (@Clicks_6), (@Clicks_7), (@Clicks_8), (@Clicks_9)", sql);
+ }
+ }
+}
diff --git a/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/Curd/MySqlSelectTest.cs b/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/Curd/MySqlSelectTest.cs
new file mode 100644
index 00000000..19b22f96
--- /dev/null
+++ b/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/Curd/MySqlSelectTest.cs
@@ -0,0 +1,1231 @@
+using FreeSql.DataAnnotations;
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Xunit;
+
+namespace FreeSql.Tests.MySqlConnector {
+
+ public class MySqlSelectTest {
+
+ ISelect select => g.mysql.Select();
+
+ [Table(Name = "tb_topic")]
+ public class Topic {
+ [Column(IsIdentity = true, IsPrimary = true)]
+ public int Id { get; set; }
+ public int Clicks { get; set; }
+ [Column(OldName = "TypeGuid")]
+ public int TypeGuid { get; set; }
+ public TestTypeInfo Type { get; set; }
+ public string Title { get; set; }
+ public DateTime CreateTime { get; set; }
+
+ public virtual TopicFields Fields { get; set; }
+ }
+ public class TopicFields {
+ [Column(IsPrimary = true)]
+ public int TopicId { get; set; }
+ public virtual Topic Topic { get; set; }
+ }
+ public class TestTypeInfo {
+ [Column(IsIdentity = true)]
+ public int Guid { get; set; }
+
+ public int ParentId { get; set; }
+ public virtual TestTypeParentInfo Parent { get; set; }
+
+ public string Name { get; set; }
+
+ public virtual ICollection Topics { get; set; }
+ }
+ public class TestTypeParentInfo {
+ [Column(IsIdentity = true)]
+ public int Id { get; set; }
+ public string Name { get; set; }
+
+ public List Types { get; set; }
+ }
+
+ public partial class Song {
+ [Column(IsIdentity = true)]
+ public int Id { get; set; }
+ public DateTime? Create_time { get; set; }
+ public bool? Is_deleted { get; set; }
+ public string Title { get; set; }
+ public string Url { get; set; }
+
+ public virtual ICollection Tags { get; set; }
+ }
+ public partial class Song_tag {
+ public int Song_id { get; set; }
+ public virtual Song Song { get; set; }
+
+ public int Tag_id { get; set; }
+ public virtual Tag Tag { get; set; }
+ }
+ public partial class Tag {
+ [Column(IsIdentity = true)]
+ public int Id { get; set; }
+ public int? Parent_id { get; set; }
+ public virtual Tag Parent { get; set; }
+
+ public decimal? Ddd { get; set; }
+ public string Name { get; set; }
+
+ public virtual ICollection Songs { get; set; }
+ public virtual ICollection Tags { get; set; }
+ }
+
+ [Table(Name = "TestInfoT1", SelectFilter = " a.id > 0")]
+ class TestInfo {
+ [Column(IsIdentity = true, IsPrimary = true)]
+ public int Id { get; set; }
+ public int TypeGuid { get; set; }
+ public TestTypeInfo Type { get; set; }
+ public string Title { get; set; }
+ public DateTime CreateTime { get; set; }
+ }
+
+ [Fact]
+ public void AsSelect() {
+ //OneToOne、ManyToOne
+ var t0 = g.mysql.Select().Where(a => a.Parent.Parent.Name == "粤语").ToSql();
+ //SELECT a.`Id`, a.`Parent_id`, a__Parent.`Id` as3, a__Parent.`Parent_id` as4, a__Parent.`Ddd`, a__Parent.`Name`, a.`Ddd` as7, a.`Name` as8
+ //FROM `Tag` a
+ //LEFT JOIN `Tag` a__Parent ON a__Parent.`Id` = a.`Parent_id`
+ //LEFT JOIN `Tag` a__Parent__Parent ON a__Parent__Parent.`Id` = a__Parent.`Parent_id`
+ //WHERE (a__Parent__Parent.`Name` = '粤语')
+
+ //OneToMany
+ var t1 = g.mysql.Select().Where(a => a.Tags.AsSelect().Any(t => t.Parent.Id == 10)).ToSql();
+ //SELECT a.`Id`, a.`Parent_id`, a.`Ddd`, a.`Name`
+ //FROM `Tag` a
+ //WHERE (exists(SELECT 1
+ // FROM `Tag` t
+ // LEFT JOIN `Tag` t__Parent ON t__Parent.`Id` = t.`Parent_id`
+ // WHERE (t__Parent.`Id` = 10) AND (t.`Parent_id` = a.`Id`)
+ // limit 0,1))
+
+ //ManyToMany
+ var t2 = g.mysql.Select().Where(s => s.Tags.AsSelect().Any(t => t.Name == "国语")).ToSql();
+ //SELECT a.`Id`, a.`Create_time`, a.`Is_deleted`, a.`Title`, a.`Url`
+ //FROM `Song` a
+ //WHERE(exists(SELECT 1
+ // FROM `Song_tag` Mt_Ms
+ // WHERE(Mt_Ms.`Song_id` = a.`Id`) AND(exists(SELECT 1
+ // FROM `Tag` t
+ // WHERE(t.`Name` = '国语') AND(t.`Id` = Mt_Ms.`Tag_id`)
+ // limit 0, 1))
+ // limit 0, 1))
+ }
+
+ [Fact]
+ public void Lazy() {
+ var tags = g.mysql.Select().Where(a => a.Parent.Name == "xxx")
+ .LeftJoin(a => a.Parent_id == a.Parent.Id)
+ .ToSql();
+
+ var songs = g.mysql.Select().Limit(10).ToList();
+ }
+
+ [Fact]
+ public void ToDataTable() {
+ var items = new List();
+ for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100, CreateTime = DateTime.Now });
+
+ Assert.Equal(1, g.mysql.Insert().AppendData(items.First()).ExecuteAffrows());
+ Assert.Equal(10, g.mysql.Insert().AppendData(items).ExecuteAffrows());
+
+ //items = Enumerable.Range(0, 9989).Select(a => new Topic { Title = "newtitle" + a, CreateTime = DateTime.Now }).ToList();
+ //Assert.Equal(9989, g.mysql.Insert(items).ExecuteAffrows());
+
+ var dt1 = select.Limit(10).ToDataTable();
+ var dt2 = select.Limit(10).ToDataTable("id, 111222");
+ var dt3 = select.Limit(10).ToDataTable(a => new { a.Id, a.Type.Name, now = DateTime.Now });
+ }
+
+ class TestDto {
+ public int id { get; set; }
+ public string name { get; set; } //这是join表的属性
+ public int ParentId { get; set; } //这是join表的属性
+ }
+ [Fact]
+ public void ToList() {
+
+ var testDto1 = select.Limit(10).ToList(a => new TestDto { id = a.Id, name = a.Title });
+ var testDto2 = select.Limit(10).ToList(a => new TestDto());
+ var testDto3 = select.Limit(10).ToList(a => new TestDto { });
+ var testDto4 = select.Limit(10).ToList(a => new TestDto() { });
+ var testDto5 = select.Limit(10).ToList();
+
+ var testDto11 = select.LeftJoin(a => a.Type.Guid == a.TypeGuid).Limit(10).ToList(a => new TestDto { id = a.Id, name = a.Title });
+ var testDto22 = select.LeftJoin(a => a.Type.Guid == a.TypeGuid).Limit(10).ToList(a => new TestDto());
+ var testDto33 = select.LeftJoin(a => a.Type.Guid == a.TypeGuid).Limit(10).ToList(a => new TestDto { });
+ var testDto44 = select.LeftJoin(a => a.Type.Guid == a.TypeGuid).Limit(10).ToList(a => new TestDto() { });
+ var testDto55 = select.LeftJoin(a => a.Type.Guid == a.TypeGuid).Limit(10).ToList();
+
+ var t0 = select.Limit(50).ToList();
+
+
+ var t1 = g.mysql.Select().Where("").Where(a => a.Id > 0).Skip(100).Limit(200).ToSql();
+ var t2 = g.mysql.Select().As("b").Where("").Where(a => a.Id > 0).Skip(100).Limit(200).ToSql();
+
+
+ var sql1 = select.LeftJoin(a => a.Type.Guid == a.TypeGuid).ToSql();
+ var sql2 = select.LeftJoin((a, b) => a.TypeGuid == b.Guid && b.Name == "111").ToSql();
+ var sql3 = select.LeftJoin("TestTypeInfo b on b.Guid = a.TypeGuid").ToSql();
+
+ //g.mysql.Select().Join((a, b, c) => new Model.JoinResult3(
+ // Model.JoinType.LeftJoin, a.TypeGuid == b.Guid,
+ // Model.JoinType.InnerJoin, c.Id == b.ParentId && c.Name == "xxx")
+ //);
+
+ //var sql4 = select.From((a, b, c) => new SelectFrom()
+ // .InnerJoin(a.TypeGuid == b.Guid)
+ // .LeftJoin(c.Id == b.ParentId)
+ // .Where(b.Name == "xxx"))
+ //.Where(a => a.Id == 1).ToSql();
+
+ var sql4 = select.From((s, b, c) => s
+ .InnerJoin(a => a.TypeGuid == b.Guid)
+ .LeftJoin(a => c.Id == b.ParentId)
+ .Where(a => b.Name == "xxx")).ToSql();
+ //.Where(a => a.Id == 1).ToSql();
+
+
+ var list111 = select.From((s, b, c) => s
+ .InnerJoin(a => a.TypeGuid == b.Guid)
+ .LeftJoin(a => c.Id == b.ParentId)
+ .Where(a => b.Name != "xxx"));
+ var list111sql = list111.ToSql();
+ var list111data = list111.ToList((a, b, c) => new {
+ a.Id,
+ title_substring = a.Title.Substring(0, 1),
+ a.Type,
+ ccc = new { a.Id, a.Title },
+ tp = a.Type,
+ tp2 = new {
+ a.Id,
+ tp2 = a.Type.Name
+ },
+ tp3 = new {
+ a.Id,
+ tp33 = new {
+ a.Id
+ }
+ }
+ });
+
+ var ttt122 = g.mysql.Select().Where(a => a.Id > 0).ToSql();
+ var sql5 = g.mysql.Select().From((s, b, c) => s).Where((a, b, c) => a.Id == b.ParentId).ToSql();
+ var t11112 = g.mysql.Select().ToList(a => new {
+ a.Id,
+ a.Title,
+ a.Type,
+ ccc = new { a.Id, a.Title },
+ tp = a.Type,
+ tp2 = new {
+ a.Id,
+ tp2 = a.Type.Name
+ },
+ tp3 = new {
+ a.Id,
+ tp33 = new {
+ a.Id
+ }
+ }
+
+ });
+
+ var t100 = g.mysql.Select().Where("").Where(a => a.Id > 0).Skip(100).Limit(200).ToList();
+ var t101 = g.mysql.Select().As("b").Where("").Where(a => a.Id > 0).Skip(100).Limit(200).ToList();
+
+
+ var t1111 = g.mysql.Select().ToList(a => new { a.Id, a.Title, a.Type });
+
+ var t2222 = g.mysql.Select().ToList(a => new { a.Id, a.Title, a.Type.Name });
+
+ g.mysql.Insert().AppendData(new TestGuidIdToList()).ExecuteAffrows();
+ var testGuidId5 = g.mysql.Select().ToList();
+ var testGuidId6 = g.mysql.Select().ToList(a => a.id);
+
+ var t11 = select.Where(a => a.Type.Name.Length > 0).ToList(true);
+ var t21 = select.Where(a => a.Type.Parent.Name.Length > 0).ToList(true);
+ }
+ class TestGuidIdToList {
+ public Guid id { get; set; }
+ public string title { get; set; } = Guid.NewGuid().ToString();
+ }
+ [Fact]
+ public void ToOne() {
+ var testnotfind = select.Where("1=2").First(a => a.CreateTime);
+ Assert.Equal(default(DateTime), testnotfind);
+ }
+ [Fact]
+ public void ToSql() {
+ g.mysql.Insert().AppendData(new testenumWhere { type = testenumWhereType.Blaaa }).ExecuteAffrows();
+
+ var sql1 = g.mysql.Select().Where(a => a.type == testenumWhereType.Blaaa).ToSql();
+ var sql2 = g.mysql.Select().Where(a => testenumWhereType.Blaaa == a.type).ToSql();
+
+ var sql3 = g.mysql.Select().Where(a => a.type.Equals(testenumWhereType.Blaaa)).ToSql();
+ var tolist = g.mysql.Select().Where(a => a.type == testenumWhereType.Blaaa).ToList();
+ }
+ class testenumWhere {
+ public Guid id { get; set; }
+ public testenumWhereType type { get; set; }
+ }
+ public enum testenumWhereType { Menu, Class, Blaaa }
+
+ [Fact]
+ public void Any() {
+ var count = select.Where(a => 1 == 1).Count();
+ Assert.False(select.Where(a => 1 == 2).Any());
+ Assert.Equal(count > 0, select.Where(a => 1 == 1).Any());
+
+ var sql2222 = select.Where(a =>
+ select.Where(b => b.Id == a.Id &&
+ select.Where(c => c.Id == b.Id).Where(d => d.Id == a.Id).Where(e => e.Id == b.Id)
+ //.Offset(a.Id)
+ .Any()
+ ).Any(c => c.Id == a.Id + 10)
+ );
+ var sql2222Tolist = sql2222.ToList();
+
+ var collectionSelect = select.Where(a =>
+ a.Type.Guid == a.TypeGuid &&
+ a.Type.Parent.Id == a.Type.ParentId &&
+ a.Type.Parent.Types.AsSelect().Where(b => b.Name == a.Title).Any(b => b.ParentId == a.Type.Parent.Id)
+ );
+ collectionSelect.ToList();
+ }
+ [Fact]
+ public void Count() {
+ var count = select.Where(a => 1 == 1).Count();
+ select.Where(a => 1 == 1).Count(out var count2);
+ Assert.Equal(count, count2);
+ Assert.Equal(0, select.Where(a => 1 == 2).Count());
+ }
+ [Fact]
+ public void Master() {
+ Assert.StartsWith(" SELECT", select.Master().Where(a => 1 == 1).ToSql());
+ }
+ [Fact]
+ public void From() {
+ var query2 = select.From((s, b) => s
+ .LeftJoin(a => a.TypeGuid == b.Guid)
+ );
+ var sql2 = query2.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a LEFT JOIN `TestTypeInfo` b ON a.`TypeGuid` = b.`Guid`", sql2);
+ query2.ToList();
+
+ var query3 = select.From((s, b, c) => s
+ .LeftJoin(a => a.TypeGuid == b.Guid)
+ .LeftJoin(a => b.ParentId == c.Id)
+ );
+ var sql3 = query3.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a LEFT JOIN `TestTypeInfo` b ON a.`TypeGuid` = b.`Guid` LEFT JOIN `TestTypeParentInfo` c ON b.`ParentId` = c.`Id`", sql3);
+ query3.ToList();
+ }
+ [Fact]
+ public void LeftJoin() {
+ //����е�������a.Type��a.Type.Parent ���ǵ�������
+ var query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid);
+ var sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid`", sql);
+ query.ToList();
+
+ query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx");
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` AND a__Type.`Name` = 'xxx'", sql);
+ query.ToList();
+
+ query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` AND a__Type.`Name` = 'xxx' LEFT JOIN `TestTypeParentInfo` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId` WHERE (a__Type__Parent.`Id` = 10)", sql);
+ query.ToList();
+
+ //���û�е�������
+ query = select.LeftJoin((a, b) => b.Guid == a.TypeGuid);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a LEFT JOIN `TestTypeInfo` b ON b.`Guid` = a.`TypeGuid`", sql);
+ query.ToList();
+
+ query = select.LeftJoin((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx");
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a LEFT JOIN `TestTypeInfo` b ON b.`Guid` = a.`TypeGuid` AND b.`Name` = 'xxx'", sql);
+ query.ToList();
+
+ query = select.LeftJoin((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a LEFT JOIN `TestTypeInfo` b ON b.`Guid` = a.`TypeGuid` AND b.`Name` = 'xxx' LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` LEFT JOIN `TestTypeParentInfo` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId` WHERE (a__Type__Parent.`Id` = 10)", sql);
+ query.ToList();
+
+ //�������
+ query = select
+ .LeftJoin(a => a.Type.Guid == a.TypeGuid)
+ .LeftJoin(a => a.Type.Parent.Id == a.Type.ParentId);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` LEFT JOIN `TestTypeParentInfo` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId`", sql);
+ query.ToList();
+
+ query = select
+ .LeftJoin((a, a__Type) => a__Type.Guid == a.TypeGuid)
+ .LeftJoin((a, c) => c.Id == a.Type.ParentId);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` LEFT JOIN `TestTypeParentInfo` c ON c.`Id` = a__Type.`ParentId`", sql);
+ query.ToList();
+
+ //���û�е�������b��c������ϵ
+ var query2 = select.From((s, b, c) => s
+ .LeftJoin(a => a.TypeGuid == b.Guid)
+ .LeftJoin(a => b.ParentId == c.Id));
+ sql = query2.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a LEFT JOIN `TestTypeInfo` b ON a.`TypeGuid` = b.`Guid` LEFT JOIN `TestTypeParentInfo` c ON b.`ParentId` = c.`Id`", sql);
+ query2.ToList();
+
+ //������϶����㲻��
+ query = select.LeftJoin("TestTypeInfo b on b.Guid = a.TypeGuid");
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a LEFT JOIN TestTypeInfo b on b.Guid = a.TypeGuid", sql);
+ query.ToList();
+
+ query = select.LeftJoin("TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = @bname", new { bname = "xxx" });
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a LEFT JOIN TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = @bname", sql);
+ query.ToList();
+ }
+ [Fact]
+ public void InnerJoin() {
+ //����е�������a.Type��a.Type.Parent ���ǵ�������
+ var query = select.InnerJoin(a => a.Type.Guid == a.TypeGuid);
+ var sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a INNER JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid`", sql);
+ query.ToList();
+
+ query = select.InnerJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx");
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a INNER JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` AND a__Type.`Name` = 'xxx'", sql);
+ query.ToList();
+
+ query = select.InnerJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a INNER JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` AND a__Type.`Name` = 'xxx' LEFT JOIN `TestTypeParentInfo` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId` WHERE (a__Type__Parent.`Id` = 10)", sql);
+ query.ToList();
+
+ //���û�е�������
+ query = select.InnerJoin((a, b) => b.Guid == a.TypeGuid);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a INNER JOIN `TestTypeInfo` b ON b.`Guid` = a.`TypeGuid`", sql);
+ query.ToList();
+
+ query = select.InnerJoin((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx");
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a INNER JOIN `TestTypeInfo` b ON b.`Guid` = a.`TypeGuid` AND b.`Name` = 'xxx'", sql);
+ query.ToList();
+
+ query = select.InnerJoin((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a INNER JOIN `TestTypeInfo` b ON b.`Guid` = a.`TypeGuid` AND b.`Name` = 'xxx' LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` LEFT JOIN `TestTypeParentInfo` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId` WHERE (a__Type__Parent.`Id` = 10)", sql);
+ query.ToList();
+
+ //�������
+ query = select
+ .InnerJoin(a => a.Type.Guid == a.TypeGuid)
+ .InnerJoin(a => a.Type.Parent.Id == a.Type.ParentId);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a INNER JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` INNER JOIN `TestTypeParentInfo` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId`", sql);
+ query.ToList();
+
+ query = select
+ .InnerJoin((a, a__Type) => a__Type.Guid == a.TypeGuid)
+ .InnerJoin((a, c) => c.Id == a.Type.ParentId);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a INNER JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` INNER JOIN `TestTypeParentInfo` c ON c.`Id` = a__Type.`ParentId`", sql);
+ query.ToList();
+
+ //���û�е�������b��c������ϵ
+ var query2 = select.From((s, b, c) => s
+ .InnerJoin(a => a.TypeGuid == b.Guid)
+ .InnerJoin(a => b.ParentId == c.Id));
+ sql = query2.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a INNER JOIN `TestTypeInfo` b ON a.`TypeGuid` = b.`Guid` INNER JOIN `TestTypeParentInfo` c ON b.`ParentId` = c.`Id`", sql);
+ query2.ToList();
+
+ //������϶����㲻��
+ query = select.InnerJoin("TestTypeInfo b on b.Guid = a.TypeGuid");
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a INNER JOIN TestTypeInfo b on b.Guid = a.TypeGuid", sql);
+ query.ToList();
+
+ query = select.InnerJoin("TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = @bname", new { bname = "xxx" });
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a INNER JOIN TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = @bname", sql);
+ query.ToList();
+
+ }
+ [Fact]
+ public void RightJoin() {
+ //����е�������a.Type��a.Type.Parent ���ǵ�������
+ var query = select.RightJoin(a => a.Type.Guid == a.TypeGuid);
+ var sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a RIGHT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid`", sql);
+ query.ToList();
+
+ query = select.RightJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx");
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a RIGHT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` AND a__Type.`Name` = 'xxx'", sql);
+ query.ToList();
+
+ query = select.RightJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a RIGHT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` AND a__Type.`Name` = 'xxx' LEFT JOIN `TestTypeParentInfo` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId` WHERE (a__Type__Parent.`Id` = 10)", sql);
+ query.ToList();
+
+ //���û�е�������
+ query = select.RightJoin((a, b) => b.Guid == a.TypeGuid);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a RIGHT JOIN `TestTypeInfo` b ON b.`Guid` = a.`TypeGuid`", sql);
+ query.ToList();
+
+ query = select.RightJoin((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx");
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a RIGHT JOIN `TestTypeInfo` b ON b.`Guid` = a.`TypeGuid` AND b.`Name` = 'xxx'", sql);
+ query.ToList();
+
+ query = select.RightJoin((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a RIGHT JOIN `TestTypeInfo` b ON b.`Guid` = a.`TypeGuid` AND b.`Name` = 'xxx' LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` LEFT JOIN `TestTypeParentInfo` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId` WHERE (a__Type__Parent.`Id` = 10)", sql);
+ query.ToList();
+
+ //�������
+ query = select
+ .RightJoin(a => a.Type.Guid == a.TypeGuid)
+ .RightJoin(a => a.Type.Parent.Id == a.Type.ParentId);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a RIGHT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` RIGHT JOIN `TestTypeParentInfo` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId`", sql);
+ query.ToList();
+
+ query = select
+ .RightJoin((a, a__Type) => a__Type.Guid == a.TypeGuid)
+ .RightJoin((a, c) => c.Id == a.Type.ParentId);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a RIGHT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` RIGHT JOIN `TestTypeParentInfo` c ON c.`Id` = a__Type.`ParentId`", sql);
+ query.ToList();
+
+ //���û�е�������b��c������ϵ
+ var query2 = select.From((s, b, c) => s
+ .RightJoin(a => a.TypeGuid == b.Guid)
+ .RightJoin(a => b.ParentId == c.Id));
+ sql = query2.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a RIGHT JOIN `TestTypeInfo` b ON a.`TypeGuid` = b.`Guid` RIGHT JOIN `TestTypeParentInfo` c ON b.`ParentId` = c.`Id`", sql);
+ query2.ToList();
+
+ //������϶����㲻��
+ query = select.RightJoin("TestTypeInfo b on b.Guid = a.TypeGuid");
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a RIGHT JOIN TestTypeInfo b on b.Guid = a.TypeGuid", sql);
+ query.ToList();
+
+ query = select.RightJoin("TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = @bname", new { bname = "xxx" });
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a RIGHT JOIN TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = @bname", sql);
+ query.ToList();
+
+ }
+ [Fact]
+ public void Where() {
+
+ var sqltmp1 = select.Where(a => a.Id == 0 && (a.Title == "x" || a.Title == "y") && a.Clicks == 1).ToSql();
+ var sqltmp2 = select.Where(a => a.Id.Equals(true) && (a.Title.Equals("x") || a.Title.Equals("y")) && a.Clicks.Equals(1)).ToSql();
+ var sqltmp3 = select.Where(a => a.Id == 0).Where(a => ((a.Title == "x" && a.Title == "z") || a.Title == "y")).ToSql();
+
+ var sqltmp4 = select.Where(a => (a.Id - 10) / 2 > 0).ToSql();
+
+ //����е�������a.Type��a.Type.Parent ���ǵ�������
+ var query = select.Where(a => a.Id == 10);
+ var sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a WHERE (a.`Id` = 10)", sql);
+ query.ToList();
+
+ query = select.Where(a => a.Id == 10 && a.Id > 10 || a.Clicks > 100);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a WHERE ((a.`Id` = 10 AND a.`Id` > 10 OR a.`Clicks` > 100))", sql);
+ query.ToList();
+
+ query = select.Where(a => a.Id == 10).Where(a => a.Clicks > 100);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a WHERE (a.`Id` = 10) AND (a.`Clicks` > 100)", sql);
+ query.ToList();
+
+ query = select.Where(a => a.Type.Name == "typeTitle");
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` WHERE (a__Type.`Name` = 'typeTitle')", sql);
+ query.ToList();
+
+ query = select.Where(a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` WHERE (a__Type.`Name` = 'typeTitle' AND a__Type.`Guid` = a.`TypeGuid`)", sql);
+ query.ToList();
+
+ query = select.Where(a => a.Type.Parent.Name == "tparent");
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` LEFT JOIN `TestTypeParentInfo` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId` WHERE (a__Type__Parent.`Name` = 'tparent')", sql);
+ query.ToList();
+
+ //���û�е������ԣ��������
+ query = select.Where((a, b) => b.Guid == a.TypeGuid && b.Name == "typeTitle");
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a, `TestTypeInfo` b WHERE (b.`Guid` = a.`TypeGuid` AND b.`Name` = 'typeTitle')", sql);
+ query.ToList();
+
+ query = select.Where((a, b) => b.Name == "typeTitle" && b.Guid == a.TypeGuid);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a, `TestTypeInfo` b WHERE (b.`Name` = 'typeTitle' AND b.`Guid` = a.`TypeGuid`)", sql);
+ query.ToList();
+
+ query = select.Where((a, b, c) => c.Name == "tparent");
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a, `TestTypeParentInfo` c WHERE (c.`Name` = 'tparent')", sql);
+ query.ToList();
+
+ //����һ�� From ��Ķ������
+ var query2 = select.From((s, b, c) => s
+ .Where(a => a.Id == 10 && c.Name == "xxx")
+ .Where(a => b.ParentId == 20));
+ sql = query2.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a, `TestTypeInfo` b, `TestTypeParentInfo` c WHERE (a.`Id` = 10 AND c.`Name` = 'xxx') AND (b.`ParentId` = 20)", sql);
+ query2.ToList();
+
+ //������϶����㲻��
+ query = select.Where("a.clicks > 100 and a.id = @id", new { id = 10 });
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a WHERE (a.clicks > 100 and a.id = @id)", sql);
+ query.ToList();
+ }
+ [Fact]
+ public void WhereIf() {
+ //����е�������a.Type��a.Type.Parent ���ǵ�������
+ var query = select.WhereIf(true, a => a.Id == 10);
+ var sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a WHERE (a.`Id` = 10)", sql);
+ query.ToList();
+
+ query = select.WhereIf(true, a => a.Id == 10 && a.Id > 10 || a.Clicks > 100);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a WHERE ((a.`Id` = 10 AND a.`Id` > 10 OR a.`Clicks` > 100))", sql);
+ query.ToList();
+
+ query = select.WhereIf(true, a => a.Id == 10).WhereIf(true, a => a.Clicks > 100);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a WHERE (a.`Id` = 10) AND (a.`Clicks` > 100)", sql);
+ query.ToList();
+
+ query = select.WhereIf(true, a => a.Type.Name == "typeTitle");
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` WHERE (a__Type.`Name` = 'typeTitle')", sql);
+ query.ToList();
+
+ query = select.WhereIf(true, a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` WHERE (a__Type.`Name` = 'typeTitle' AND a__Type.`Guid` = a.`TypeGuid`)", sql);
+ query.ToList();
+
+ query = select.WhereIf(true, a => a.Type.Parent.Name == "tparent");
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topic` a LEFT JOIN `TestTypeInfo` a__Type ON a__Type.`Guid` = a.`TypeGuid` LEFT JOIN `TestTypeParentInfo` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId` WHERE (a__Type__Parent.`Name` = 'tparent')", sql);
+ query.ToList();
+
+ //����һ�� From ��Ķ������
+ var query2 = select.From((s, b, c) => s
+ .WhereIf(true, a => a.Id == 10 && c.Name == "xxx")
+ .WhereIf(true, a => b.ParentId == 20));
+ sql = query2.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a, `TestTypeInfo` b, `TestTypeParentInfo` c WHERE (a.`Id` = 10 AND c.`Name` = 'xxx') AND (b.`ParentId` = 20)", sql);
+ query2.ToList();
+
+ //������϶����㲻��
+ query = select.WhereIf(true, "a.clicks > 100 and a.id = @id", new { id = 10 });
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a WHERE (a.clicks > 100 and a.id = @id)", sql);
+ query.ToList();
+
+ // ==========================================WhereIf(false)
+
+ //����е�������a.Type��a.Type.Parent ���ǵ�������
+ query = select.WhereIf(false, a => a.Id == 10);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a", sql);
+ query.ToList();
+
+ query = select.WhereIf(false, a => a.Id == 10 && a.Id > 10 || a.Clicks > 100);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a", sql);
+ query.ToList();
+
+ query = select.WhereIf(false, a => a.Id == 10).WhereIf(false, a => a.Clicks > 100);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a", sql);
+ query.ToList();
+
+ query = select.WhereIf(false, a => a.Type.Name == "typeTitle");
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a", sql);
+ query.ToList();
+
+ query = select.WhereIf(false, a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a", sql);
+ query.ToList();
+
+ query = select.WhereIf(false, a => a.Type.Parent.Name == "tparent");
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a", sql);
+ query.ToList();
+
+ //����һ�� From ��Ķ������
+ query2 = select.From((s, b, c) => s
+ .WhereIf(false, a => a.Id == 10 && c.Name == "xxx")
+ .WhereIf(false, a => b.ParentId == 20));
+ sql = query2.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a, `TestTypeInfo` b, `TestTypeParentInfo` c", sql);
+ query2.ToList();
+
+ //������϶����㲻��
+ query = select.WhereIf(false, "a.clicks > 100 and a.id = @id", new { id = 10 });
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a", sql);
+ query.ToList();
+ }
+ [Fact]
+ public void WhereExists() {
+ var sql2222 = select.Where(a => select.Where(b => b.Id == a.Id).Any()).ToList();
+
+ sql2222 = select.Where(a =>
+ select.Where(b => b.Id == a.Id && select.Where(c => c.Id == b.Id).Where(d => d.Id == a.Id).Where(e => e.Id == b.Id)
+
+ //.Offset(a.Id)
+
+ .Any()
+ ).Any()
+ ).ToList();
+ }
+ [Fact]
+ public void GroupBy() {
+ var groupby = select.From((s, b, c) => s
+ .Where(a => a.Id == 1)
+ )
+ .GroupBy((a, b, c) => new { tt2 = a.Title.Substring(0, 2), mod4 = a.Id % 4 })
+ .Having(a => a.Count() > 0 && a.Avg(a.Key.mod4) > 0 && a.Max(a.Key.mod4) > 0)
+ .Having(a => a.Count() < 300 || a.Avg(a.Key.mod4) < 100)
+ .OrderBy(a => a.Key.tt2)
+ .OrderByDescending(a => a.Count())
+ .Offset(10)
+ .Limit(2)
+ .ToList(a => new {
+ a.Key.tt2,
+ cou1 = a.Count(),
+ arg1 = a.Avg(a.Key.mod4),
+ ccc2 = a.Key.tt2 ?? "now()",
+ //ccc = Convert.ToDateTime("now()"), partby = Convert.ToDecimal("sum(num) over(PARTITION BY server_id,os,rid,chn order by id desc)")
+ ccc3 = a.Max(a.Value.Item3.Id)
+ });
+
+ var testpid1 = g.mysql.Insert().AppendData(new TestTypeInfo { Name = "Name" + DateTime.Now.ToString("yyyyMMddHHmmss") }).ExecuteIdentity();
+ g.mysql.Insert().AppendData(new TestInfo { Title = "Title" + DateTime.Now.ToString("yyyyMMddHHmmss"), CreateTime = DateTime.Now, TypeGuid = (int)testpid1 }).ExecuteAffrows();
+
+ var aggsql1 = select
+ .GroupBy(a => a.Title)
+ .ToSql(b => new {
+ b.Key,
+ cou = b.Count(),
+ sum = b.Sum(b.Key),
+ sum2 = b.Sum(b.Value.TypeGuid)
+ });
+ var aggtolist1 = select
+ .GroupBy(a => a.Title)
+ .ToList(b => new {
+ b.Key,
+ cou = b.Count(),
+ sum = b.Sum(b.Key),
+ sum2 = b.Sum(b.Value.TypeGuid)
+ });
+
+ var aggsql2 = select
+ .GroupBy(a => new { a.Title, yyyy = string.Concat(a.CreateTime.Year, '-', a.CreateTime.Month) })
+ .ToSql(b => new {
+ b.Key.Title,
+ b.Key.yyyy,
+
+ cou = b.Count(),
+ sum = b.Sum(b.Key.yyyy),
+ sum2 = b.Sum(b.Value.TypeGuid)
+ });
+ var aggtolist2 = select
+ .GroupBy(a => new { a.Title, yyyy = string.Concat(a.CreateTime.Year, '-', a.CreateTime.Month) })
+ .ToList(b => new {
+ b.Key.Title,
+ b.Key.yyyy,
+
+ cou = b.Count(),
+ sum = b.Sum(b.Key.yyyy),
+ sum2 = b.Sum(b.Value.TypeGuid)
+ });
+
+ var aggsql3 = select
+ .GroupBy(a => a.Title)
+ .ToSql(b => new {
+ b.Key,
+ cou = b.Count(),
+ sum = b.Sum(b.Key),
+ sum2 = b.Sum(b.Value.TypeGuid),
+ sum3 = b.Sum(b.Value.Type.Parent.Id)
+ });
+ }
+ [Fact]
+ public void ToAggregate() {
+ var sql = select.ToAggregate(a => new { sum = a.Sum(a.Key.Id + 11.11), avg = a.Avg(a.Key.Id), count = a.Count(), max = a.Max(a.Key.Id), min = a.Min(a.Key.Id) });
+ }
+
+ [Fact]
+ public void OrderBy() {
+ var sql = select.OrderBy(a => new Random().NextDouble()).ToList();
+ }
+ [Fact]
+ public void Skip_Offset() {
+ var sql = select.Offset(10).Limit(10).ToList();
+ }
+ [Fact]
+ public void Take_Limit() {
+ var sql = select.Limit(10).ToList();
+ }
+ [Fact]
+ public void Page() {
+ var sql1 = select.Page(1, 10).ToList();
+ var sql2 = select.Page(2, 10).ToList();
+ var sql3 = select.Page(3, 10).ToList();
+
+ var sql11 = select.OrderBy(a => new Random().NextDouble()).Page(1, 10).ToList();
+ var sql22 = select.OrderBy(a => new Random().NextDouble()).Page(2, 10).ToList();
+ var sql33 = select.OrderBy(a => new Random().NextDouble()).Page(3, 10).ToList();
+ }
+ [Fact]
+ public void Distinct() {
+ var t1 = select.Distinct().ToList(a => a.Title);
+ var t2 = select.Distinct().Limit(10).ToList(a => a.Title);
+ }
+
+ [Fact]
+ public void Sum() {
+ }
+ [Fact]
+ public void Min() {
+ }
+ [Fact]
+ public void Max() {
+ }
+ [Fact]
+ public void Avg() {
+ }
+ [Fact]
+ public void As() {
+ }
+
+ [Fact]
+ public void AsTable() {
+
+ var listt = select.AsTable((a, b) => "(select * from tb_topic where clicks > 10)").Page(1, 10).ToList();
+
+ Func tableRule = (type, oldname) => {
+ if (type == typeof(Topic)) return oldname + "AsTable1";
+ else if (type == typeof(TestTypeInfo)) return oldname + "AsTable2";
+ return oldname + "AsTable";
+ };
+
+ //����е�������a.Type��a.Type.Parent ���ǵ�������
+ var query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid).AsTable(tableRule);
+ var sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topicAsTable1` a LEFT JOIN `TestTypeInfoAsTable2` a__Type ON a__Type.`Guid` = a.`TypeGuid`", sql);
+
+ query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").AsTable(tableRule);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topicAsTable1` a LEFT JOIN `TestTypeInfoAsTable2` a__Type ON a__Type.`Guid` = a.`TypeGuid` AND a__Type.`Name` = 'xxx'", sql);
+
+ query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10).AsTable(tableRule);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topicAsTable1` a LEFT JOIN `TestTypeInfoAsTable2` a__Type ON a__Type.`Guid` = a.`TypeGuid` AND a__Type.`Name` = 'xxx' LEFT JOIN `TestTypeParentInfoAsTable` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId` WHERE (a__Type__Parent.`Id` = 10)", sql);
+
+ //���û�е�������
+ query = select.LeftJoin((a, b) => b.Guid == a.TypeGuid).AsTable(tableRule);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topicAsTable1` a LEFT JOIN `TestTypeInfoAsTable2` b ON b.`Guid` = a.`TypeGuid`", sql);
+
+ query = select.LeftJoin((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").AsTable(tableRule);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topicAsTable1` a LEFT JOIN `TestTypeInfoAsTable2` b ON b.`Guid` = a.`TypeGuid` AND b.`Name` = 'xxx'", sql);
+
+ query = select.LeftJoin((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10).AsTable(tableRule);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topicAsTable1` a LEFT JOIN `TestTypeInfoAsTable2` b ON b.`Guid` = a.`TypeGuid` AND b.`Name` = 'xxx' LEFT JOIN `TestTypeInfoAsTable2` a__Type ON a__Type.`Guid` = a.`TypeGuid` LEFT JOIN `TestTypeParentInfoAsTable` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId` WHERE (a__Type__Parent.`Id` = 10)", sql);
+
+ //�������
+ query = select
+ .LeftJoin(a => a.Type.Guid == a.TypeGuid)
+ .LeftJoin(a => a.Type.Parent.Id == a.Type.ParentId).AsTable(tableRule);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a__Type.`Guid`, a__Type.`ParentId`, a__Type.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topicAsTable1` a LEFT JOIN `TestTypeInfoAsTable2` a__Type ON a__Type.`Guid` = a.`TypeGuid` LEFT JOIN `TestTypeParentInfoAsTable` a__Type__Parent ON a__Type__Parent.`Id` = a__Type.`ParentId`", sql);
+
+ query = select
+ .LeftJoin((a, b) => b.Guid == a.TypeGuid)
+ .LeftJoin((a, c) => c.Id == a.Type.ParentId).AsTable(tableRule);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topicAsTable1` a LEFT JOIN `TestTypeInfoAsTable2` a__Type ON a__Type.`Guid` = a.`TypeGuid` LEFT JOIN `TestTypeInfoAsTable2` b ON b.`Guid` = a.`TypeGuid` LEFT JOIN `TestTypeParentInfoAsTable` c ON c.`Id` = a__Type.`ParentId`", sql);
+
+ //���û�е�������b��c������ϵ
+ var query2 = select.From((s, b, c) => s
+ .LeftJoin(a => a.TypeGuid == b.Guid)
+ .LeftJoin(a => b.ParentId == c.Id)).AsTable(tableRule);
+ sql = query2.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, b.`Guid`, b.`ParentId`, b.`Name`, a.`Title`, a.`CreateTime` FROM `tb_topicAsTable1` a LEFT JOIN `TestTypeInfoAsTable2` b ON a.`TypeGuid` = b.`Guid` LEFT JOIN `TestTypeParentInfoAsTable` c ON b.`ParentId` = c.`Id`", sql);
+
+ //������϶����㲻��
+ query = select.LeftJoin("TestTypeInfo b on b.Guid = a.TypeGuid").AsTable(tableRule);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topicAsTable1` a LEFT JOIN TestTypeInfo b on b.Guid = a.TypeGuid", sql);
+
+ query = select.LeftJoin("TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = @bname", new { bname = "xxx" }).AsTable(tableRule);
+ sql = query.ToSql().Replace("\r\n", "");
+ Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topicAsTable1` a LEFT JOIN TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = @bname", sql);
+ }
+
+ public class TestInclude_OneToManyModel1 {
+ [Column(IsIdentity = true)]
+ public int id { get; set; }
+ public virtual TestInclude_OneToManyModel2 model2 { get; set; }
+
+ public string m1name { get; set; }
+ }
+ public class TestInclude_OneToManyModel2 {
+ [Column(IsPrimary = true)]
+ public int model2id { get; set; }
+ public virtual TestInclude_OneToManyModel1 model1 { get; set; }
+
+ public string m2setting { get; set; }
+
+ public List childs { get; set; }
+ }
+ public class TestInclude_OneToManyModel3 {
+ [Column(IsIdentity = true)]
+ public int id { get; set; }
+
+ public int model2111Idaaa { get; set; }
+ public string title { get; set; }
+
+ public List childs2 { get; set; }
+ }
+ public class TestInclude_OneToManyModel4 {
+ [Column(IsIdentity = true)]
+ public int id { get; set; }
+
+ public int model3333Id333 { get; set; }
+ public string title444 { get; set; }
+ }
+
+ [Fact]
+ public void Include_OneToMany() {
+ var model1 = new TestInclude_OneToManyModel1 { m1name = DateTime.Now.Second.ToString() };
+ model1.id = (int)g.mysql.Insert(model1).ExecuteIdentity();
+ var model2 = new TestInclude_OneToManyModel2 { model2id = model1.id, m2setting = DateTime.Now.Second.ToString() };
+ g.mysql.Insert(model2).ExecuteAffrows();
+
+ var model3_1 = new TestInclude_OneToManyModel3 { model2111Idaaa = model1.id, title = "testmodel3__111" };
+ model3_1.id = (int)g.mysql.Insert(model3_1).ExecuteIdentity();
+ var model3_2 = new TestInclude_OneToManyModel3 { model2111Idaaa = model1.id, title = "testmodel3__222" };
+ model3_2.id = (int)g.mysql.Insert(model3_2).ExecuteIdentity();
+ var model3_3 = new TestInclude_OneToManyModel3 { model2111Idaaa = model1.id, title = "testmodel3__333" };
+ model3_3.id = (int)g.mysql.Insert(model3_2).ExecuteIdentity();
+
+ var model4s = new[] {
+ new TestInclude_OneToManyModel4{ model3333Id333 = model3_1.id, title444 = "testmodel3_4__111" },
+ new TestInclude_OneToManyModel4{ model3333Id333 = model3_1.id, title444 = "testmodel3_4__222" },
+ new TestInclude_OneToManyModel4{ model3333Id333 = model3_2.id, title444 = "testmodel3_4__111" },
+ new TestInclude_OneToManyModel4{ model3333Id333 = model3_2.id, title444 = "testmodel3_4__222" },
+ new TestInclude_OneToManyModel4{ model3333Id333 = model3_2.id, title444 = "testmodel3_4__333" }
+ };
+ Assert.Equal(5, g.mysql.Insert(model4s).ExecuteAffrows());
+
+ var t0 = g.mysql.Select()
+ .IncludeMany(a => a.childs.Where(m3 => m3.model2111Idaaa == a.model2id))
+ .Where(a => a.model2id <= model1.id)
+ .ToList();
+
+ var t1 = g.mysql.Select()
+ .IncludeMany(a => a.model2.childs.Where(m3 => m3.model2111Idaaa == a.model2.model2id))
+ .Where(a => a.id <= model1.id)
+ .ToList();
+
+ var t2 = g.mysql.Select()
+ .IncludeMany(a => a.model2.childs.Where(m3 => m3.model2111Idaaa == a.model2.model2id),
+ then => then.IncludeMany(m3 => m3.childs2.Where(m4 => m4.model3333Id333 == m3.id)))
+ .Where(a => a.id <= model1.id)
+ .ToList();
+
+ var t00 = g.mysql.Select()
+ .IncludeMany(a => a.childs.Take(1).Where(m3 => m3.model2111Idaaa == a.model2id))
+ .Where(a => a.model2id <= model1.id)
+ .ToList();
+
+ var t11 = g.mysql.Select()
+ .IncludeMany(a => a.model2.childs.Take(1).Where(m3 => m3.model2111Idaaa == a.model2.model2id))
+ .Where(a => a.id <= model1.id)
+ .ToList();
+
+ var t22 = g.mysql.Select()
+ .IncludeMany(a => a.model2.childs.Take(1).Where(m3 => m3.model2111Idaaa == a.model2.model2id),
+ then => then.IncludeMany(m3 => m3.childs2.Take(2).Where(m4 => m4.model3333Id333 == m3.id)))
+ .Where(a => a.id <= model1.id)
+ .ToList();
+ }
+
+ public class TestInclude_OneToManyModel11 {
+ [Column(IsIdentity = true)]
+ public int id { get; set; }
+ public int model2id { get; set; }
+ public string m3setting { get; set; }
+ public TestInclude_OneToManyModel22 model2 { get; set; }
+ public string m1name { get; set; }
+ }
+
+ public class TestInclude_OneToManyModel22 {
+ [Column(IsIdentity = true)]
+ public int id { get; set; }
+ public string m2setting { get; set; }
+ public List childs { get; set; }
+ }
+ public class TestInclude_OneToManyModel33 {
+ [Column(IsIdentity = true)]
+ public int id { get; set; }
+ public int model2Id { get; set; }
+ public string title { get; set; }
+ public string setting { get; set; }
+ }
+ [Fact]
+ public void Include_OneToMany2() {
+ string setting = "x";
+ var model2 = new TestInclude_OneToManyModel22 { m2setting = DateTime.Now.Second.ToString() };
+ model2.id = (int)g.mysql.Insert(model2).ExecuteIdentity();
+
+ var model3s = new[]
+ {
+ new TestInclude_OneToManyModel33 {model2Id = model2.id, title = "testmodel3__111", setting = setting},
+ new TestInclude_OneToManyModel33 {model2Id = model2.id, title = "testmodel3__222", setting = setting},
+ new TestInclude_OneToManyModel33 {model2Id = model2.id, title = "testmodel3__333", setting = setting}
+ };
+ Assert.Equal(3, g.mysql.Insert(model3s).ExecuteAffrows());
+
+ var model1 = new TestInclude_OneToManyModel11 { m1name = DateTime.Now.Second.ToString(), model2id = model2.id, m3setting = setting };
+ model1.id = (int)g.mysql.Insert(model1).ExecuteIdentity();
+
+ var t1 = g.mysql.Select()
+ .LeftJoin(a => a.model2id == a.model2.id)
+ .IncludeMany(a => a.model2.childs.Where(m3 => m3.model2Id == a.model2.id && m3.setting == a.m3setting))
+ .Where(a => a.id <= model1.id)
+ .ToList(true);
+
+ var t11 = g.mysql.Select()
+ .LeftJoin(a => a.model2id == a.model2.id)
+ .IncludeMany(a => a.model2.childs.Take(1).Where(m3 => m3.model2Id == a.model2.id && m3.setting == a.m3setting))
+ .Where(a => a.id <= model1.id)
+ .ToList(true);
+ }
+
+ [Fact]
+ public void Include_OneToChilds() {
+ var tag1 = new Tag {
+ Ddd = DateTime.Now.Second,
+ Name = "test_oneToChilds_01_中国"
+ };
+ tag1.Id = (int)g.mysql.Insert(tag1).ExecuteIdentity();
+ var tag1_1 = new Tag {
+ Parent_id = tag1.Id,
+ Ddd = DateTime.Now.Second,
+ Name = "test_oneToChilds_01_北京"
+ };
+ tag1_1.Id = (int)g.mysql.Insert(tag1_1).ExecuteIdentity();
+ var tag1_2 = new Tag {
+ Parent_id = tag1.Id,
+ Ddd = DateTime.Now.Second,
+ Name = "test_oneToChilds_01_上海"
+ };
+ tag1_2.Id = (int)g.mysql.Insert(tag1_2).ExecuteIdentity();
+
+ var tag2 = new Tag {
+ Ddd = DateTime.Now.Second,
+ Name = "test_oneToChilds_02_美国"
+ };
+ tag2.Id = (int)g.mysql.Insert(tag2).ExecuteIdentity();
+ var tag2_1 = new Tag {
+ Parent_id = tag2.Id,
+ Ddd = DateTime.Now.Second,
+ Name = "test_oneToChilds_02_纽约"
+ };
+ tag2_1.Id = (int)g.mysql.Insert(tag2_1).ExecuteIdentity();
+ var tag2_2 = new Tag {
+ Parent_id = tag2.Id,
+ Ddd = DateTime.Now.Second,
+ Name = "test_oneToChilds_02_华盛顿"
+ };
+ tag2_2.Id = (int)g.mysql.Insert(tag2_2).ExecuteIdentity();
+
+ var tags0 = g.mysql.Select()
+ .Include(a => a.Parent)
+ .Where(a => a.Id == tag1.Id || a.Id == tag2.Id)
+ .ToList();
+
+ var tags1 = g.mysql.Select()
+ .IncludeMany(a => a.Tags)
+ .Include(a => a.Parent)
+ .IncludeMany(a => a.Songs)
+ .Where(a => a.Id == tag1.Id || a.Id == tag2.Id)
+ .ToList();
+
+ var tags2 = g.mysql.Select()
+ .IncludeMany(a => a.Tags,
+ then => then.Include(a => a.Parent).IncludeMany(a => a.Songs))
+ .Include(a => a.Parent)
+ .IncludeMany(a => a.Songs)
+ .Where(a => a.Id == tag1.Id || a.Id == tag2.Id)
+ .ToList();
+
+ var tags3 = g.mysql.Select()
+ .IncludeMany(a => a.Tags,
+ then => then.Include(a => a.Parent).IncludeMany(a => a.Songs).IncludeMany(a => a.Tags))
+ .Include(a => a.Parent)
+ .IncludeMany(a => a.Songs)
+ .Where(a => a.Id == tag1.Id || a.Id == tag2.Id)
+ .ToList();
+
+ var tags11 = g.mysql.Select()
+ .IncludeMany(a => a.Tags.Take(1))
+ .Include(a => a.Parent)
+ .IncludeMany(a => a.Songs.Take(1))
+ .Where(a => a.Id == tag1.Id || a.Id == tag2.Id)
+ .ToList();
+
+ var tags22 = g.mysql.Select()
+ .IncludeMany(a => a.Tags.Take(1),
+ then => then.Include(a => a.Parent).IncludeMany(a => a.Songs.Take(1)))
+ .Include(a => a.Parent)
+ .IncludeMany(a => a.Songs.Take(1))
+ .Where(a => a.Id == tag1.Id || a.Id == tag2.Id)
+ .ToList();
+
+ var tags33 = g.mysql.Select()
+ .IncludeMany(a => a.Tags.Take(1),
+ then => then.Include(a => a.Parent).IncludeMany(a => a.Songs.Take(1)).IncludeMany(a => a.Tags.Take(1)))
+ .Include(a => a.Parent)
+ .IncludeMany(a => a.Songs.Take(1))
+ .Where(a => a.Id == tag1.Id || a.Id == tag2.Id)
+ .ToList();
+ }
+
+ [Fact]
+ public void Include_ManyToMany() {
+
+ var tag1 = new Tag {
+ Ddd = DateTime.Now.Second,
+ Name = "test_manytoMany_01_中国"
+ };
+ tag1.Id = (int)g.mysql.Insert(tag1).ExecuteIdentity();
+ var tag2 = new Tag {
+ Ddd = DateTime.Now.Second,
+ Name = "test_manytoMany_02_美国"
+ };
+ tag2.Id = (int)g.mysql.Insert(tag2).ExecuteIdentity();
+ var tag3 = new Tag {
+ Ddd = DateTime.Now.Second,
+ Name = "test_manytoMany_03_日本"
+ };
+ tag3.Id = (int)g.mysql.Insert(tag3).ExecuteIdentity();
+
+ var song1 = new Song {
+ Create_time = DateTime.Now,
+ Title = "test_manytoMany_01_我是中国人.mp3",
+ Url = "http://ww.baidu.com/"
+ };
+ song1.Id = (int)g.mysql.Insert(song1).ExecuteIdentity();
+ var song2 = new Song {
+ Create_time = DateTime.Now,
+ Title = "test_manytoMany_02_爱你一万年.mp3",
+ Url = "http://ww.163.com/"
+ };
+ song2.Id = (int)g.mysql.Insert(song2).ExecuteIdentity();
+ var song3 = new Song {
+ Create_time = DateTime.Now,
+ Title = "test_manytoMany_03_千年等一回.mp3",
+ Url = "http://ww.sina.com/"
+ };
+ song3.Id = (int)g.mysql.Insert(song3).ExecuteIdentity();
+
+ g.mysql.Insert(new Song_tag { Song_id = song1.Id, Tag_id = tag1.Id }).ExecuteAffrows();
+ g.mysql.Insert(new Song_tag { Song_id = song2.Id, Tag_id = tag1.Id }).ExecuteAffrows();
+ g.mysql.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag1.Id }).ExecuteAffrows();
+ g.mysql.Insert(new Song_tag { Song_id = song1.Id, Tag_id = tag2.Id }).ExecuteAffrows();
+ g.mysql.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag2.Id }).ExecuteAffrows();
+ g.mysql.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag3.Id }).ExecuteAffrows();
+
+ var songs1 = g.mysql.Select()
+ .IncludeMany(a => a.Tags)
+ .Where(a => a.Id == song1.Id || a.Id == song2.Id || a.Id == song3.Id)
+ .ToList();
+ Assert.Equal(3, songs1.Count);
+ Assert.Equal(2, songs1[0].Tags.Count);
+ Assert.Equal(1, songs1[1].Tags.Count);
+ Assert.Equal(3, songs1[2].Tags.Count);
+
+ var songs2 = g.mysql.Select()
+ .IncludeMany(a => a.Tags,
+ then => then.IncludeMany(t => t.Songs))
+ .Where(a => a.Id == song1.Id || a.Id == song2.Id || a.Id == song3.Id)
+ .ToList();
+ Assert.Equal(3, songs2.Count);
+ Assert.Equal(2, songs2[0].Tags.Count);
+ Assert.Equal(1, songs2[1].Tags.Count);
+ Assert.Equal(3, songs2[2].Tags.Count);
+
+ var tags3 = g.mysql.Select()
+ .Include(a => a.Tag.Parent)
+ .IncludeMany(a => a.Tag.Songs)
+ .Where(a => a.Tag.Id == tag1.Id || a.Tag.Id == tag2.Id)
+ .ToList(true);
+
+
+ var songs11 = g.mysql.Select()
+ .IncludeMany(a => a.Tags.Take(1))
+ .Where(a => a.Id == song1.Id || a.Id == song2.Id || a.Id == song3.Id)
+ .ToList();
+ Assert.Equal(3, songs11.Count);
+ Assert.Equal(1, songs11[0].Tags.Count);
+ Assert.Equal(1, songs11[1].Tags.Count);
+ Assert.Equal(1, songs11[2].Tags.Count);
+
+ var songs22 = g.mysql.Select()
+ .IncludeMany(a => a.Tags.Take(1),
+ then => then.IncludeMany(t => t.Songs.Take(1)))
+ .Where(a => a.Id == song1.Id || a.Id == song2.Id || a.Id == song3.Id)
+ .ToList();
+ Assert.Equal(3, songs22.Count);
+ Assert.Equal(1, songs22[0].Tags.Count);
+ Assert.Equal(1, songs22[1].Tags.Count);
+ Assert.Equal(1, songs22[2].Tags.Count);
+
+ var tags33 = g.mysql.Select()
+ .Include(a => a.Tag.Parent)
+ .IncludeMany(a => a.Tag.Songs.Take(1))
+ .Where(a => a.Tag.Id == tag1.Id || a.Tag.Id == tag2.Id)
+ .ToList(true);
+ }
+ }
+}
diff --git a/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/Curd/MySqlUpdateTest.cs b/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/Curd/MySqlUpdateTest.cs
new file mode 100644
index 00000000..f8f12a20
--- /dev/null
+++ b/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/Curd/MySqlUpdateTest.cs
@@ -0,0 +1,186 @@
+using FreeSql.DataAnnotations;
+using System;
+using System.Collections.Generic;
+using Xunit;
+
+namespace FreeSql.Tests.MySqlConnector {
+ public class MySqlUpdateTest {
+ IUpdate update => g.mysql.Update();
+
+ [Table(Name = "tb_topic")]
+ class Topic {
+ [Column(IsIdentity = true, IsPrimary = true)]
+ public int Id { get; set; }
+ public int? Clicks { get; set; }
+ public string Title { get; set; }
+ public DateTime CreateTime { get; set; }
+ }
+ class TestEnumUpdateTb {
+ [Column(IsIdentity = true)]
+ public int id { get; set; }
+ public TestEnumUpdateTbType type { get; set; }
+ public DateTime time { get; set; } = new DateTime();
+ }
+ enum TestEnumUpdateTbType { str1, biggit, sum211 }
+
+ [Fact]
+ public void Dywhere() {
+ Assert.Null(g.mysql.Update().ToSql());
+ Assert.Equal("UPDATE `tb_topic` SET title='test' \r\nWHERE (`Id` = 1 OR `Id` = 2)", g.mysql.Update(new[] { 1, 2 }).SetRaw("title='test'").ToSql());
+ Assert.Equal("UPDATE `tb_topic` SET title='test1' \r\nWHERE (`Id` = 1)", g.mysql.Update(new Topic { Id = 1, Title = "test" }).SetRaw("title='test1'").ToSql());
+ Assert.Equal("UPDATE `tb_topic` SET title='test1' \r\nWHERE (`Id` = 1 OR `Id` = 2)", g.mysql.Update(new[] { new Topic { Id = 1, Title = "test" }, new Topic { Id = 2, Title = "test" } }).SetRaw("title='test1'").ToSql());
+ Assert.Equal("UPDATE `tb_topic` SET title='test1' \r\nWHERE (`Id` = 1)", g.mysql.Update(new { id = 1 }).SetRaw("title='test1'").ToSql());
+ }
+
+ [Fact]
+ public void SetSource() {
+ var sql = update.SetSource(new Topic { Id = 1, Title = "newtitle" }).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `tb_topic` SET `Clicks` = @p_0, `Title` = @p_1, `CreateTime` = @p_2 WHERE (`Id` = 1)", sql);
+
+ var items = new List();
+ for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
+
+ sql = update.SetSource(items).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `tb_topic` SET `Clicks` = CASE `Id` WHEN 1 THEN @p_0 WHEN 2 THEN @p_1 WHEN 3 THEN @p_2 WHEN 4 THEN @p_3 WHEN 5 THEN @p_4 WHEN 6 THEN @p_5 WHEN 7 THEN @p_6 WHEN 8 THEN @p_7 WHEN 9 THEN @p_8 WHEN 10 THEN @p_9 END, `Title` = CASE `Id` WHEN 1 THEN @p_10 WHEN 2 THEN @p_11 WHEN 3 THEN @p_12 WHEN 4 THEN @p_13 WHEN 5 THEN @p_14 WHEN 6 THEN @p_15 WHEN 7 THEN @p_16 WHEN 8 THEN @p_17 WHEN 9 THEN @p_18 WHEN 10 THEN @p_19 END, `CreateTime` = CASE `Id` WHEN 1 THEN @p_20 WHEN 2 THEN @p_21 WHEN 3 THEN @p_22 WHEN 4 THEN @p_23 WHEN 5 THEN @p_24 WHEN 6 THEN @p_25 WHEN 7 THEN @p_26 WHEN 8 THEN @p_27 WHEN 9 THEN @p_28 WHEN 10 THEN @p_29 END WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))", sql);
+
+ sql = update.SetSource(items).IgnoreColumns(a => new { a.Clicks, a.CreateTime }).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `tb_topic` SET `Title` = CASE `Id` WHEN 1 THEN @p_0 WHEN 2 THEN @p_1 WHEN 3 THEN @p_2 WHEN 4 THEN @p_3 WHEN 5 THEN @p_4 WHEN 6 THEN @p_5 WHEN 7 THEN @p_6 WHEN 8 THEN @p_7 WHEN 9 THEN @p_8 WHEN 10 THEN @p_9 END WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))", sql);
+
+ sql = update.SetSource(items).Set(a => a.CreateTime, new DateTime(2020,1,1)).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `tb_topic` SET `CreateTime` = @p_0 WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))", sql);
+
+ sql = g.mysql.Insert().AppendData(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).ToSql().Replace("\r\n", "");
+ Assert.Equal("INSERT INTO `TestEnumUpdateTb`(`type`, `time`) VALUES(@type_0, @time_0)", sql);
+ var id = g.mysql.Insert().AppendData(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).ExecuteIdentity();
+ Assert.True(id > 0);
+ Assert.Equal(TestEnumUpdateTbType.sum211, g.mysql.Select().Where(a => a.id == id).First()?.type);
+
+ sql = g.mysql.Update().SetSource(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `TestEnumUpdateTb` SET `type` = @p_0, `time` = @p_1 WHERE (`id` = 0)", sql);
+ g.mysql.Update().SetSource(new TestEnumUpdateTb { id = (int)id, type = TestEnumUpdateTbType.biggit }).ExecuteAffrows();
+ Assert.Equal(TestEnumUpdateTbType.biggit, g.mysql.Select().Where(a => a.id == id).First()?.type);
+
+ sql = g.mysql.Insert().NoneParameter().AppendData(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).ToSql().Replace("\r\n", "");
+ Assert.Equal("INSERT INTO `TestEnumUpdateTb`(`type`, `time`) VALUES('sum211', '0001-01-01 00:00:00.000')", sql);
+ id = g.mysql.Insert().NoneParameter().AppendData(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).ExecuteIdentity();
+ Assert.True(id > 0);
+ Assert.Equal(TestEnumUpdateTbType.sum211, g.mysql.Select().Where(a => a.id == id).First()?.type);
+
+ sql = g.mysql.Update().NoneParameter().SetSource(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `TestEnumUpdateTb` SET `type` = 'sum211', `time` = '0001-01-01 00:00:00.000' WHERE (`id` = 0)", sql);
+ g.mysql.Update().NoneParameter().SetSource(new TestEnumUpdateTb { id = (int)id, type = TestEnumUpdateTbType.biggit }).ExecuteAffrows();
+ Assert.Equal(TestEnumUpdateTbType.biggit, g.mysql.Select().Where(a => a.id == id).First()?.type);
+ }
+ [Fact]
+ public void IgnoreColumns() {
+ var sql = update.SetSource(new Topic { Id = 1, Title = "newtitle" }).IgnoreColumns(a => new { a.Clicks, a.CreateTime }).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `tb_topic` SET `Title` = @p_0 WHERE (`Id` = 1)", sql);
+
+ sql = g.mysql.Update().SetSource(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).IgnoreColumns(a => a.time).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `TestEnumUpdateTb` SET `type` = @p_0 WHERE (`id` = 0)", sql);
+
+ sql = g.mysql.Update().NoneParameter().SetSource(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).IgnoreColumns(a => a.time).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `TestEnumUpdateTb` SET `type` = 'sum211' WHERE (`id` = 0)", sql);
+ }
+ [Fact]
+ public void UpdateColumns() {
+ var sql = update.SetSource(new Topic { Id = 1, Title = "newtitle" }).UpdateColumns(a => a.Title).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `tb_topic` SET `Title` = @p_0 WHERE (`Id` = 1)", sql);
+
+ sql = g.mysql.Update().SetSource(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).UpdateColumns(a => a.type).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `TestEnumUpdateTb` SET `type` = @p_0 WHERE (`id` = 0)", sql);
+
+ sql = g.mysql.Update().NoneParameter().SetSource(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).UpdateColumns(a => a.type).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `TestEnumUpdateTb` SET `type` = 'sum211' WHERE (`id` = 0)", sql);
+ }
+ [Fact]
+ public void Set() {
+ var sql = update.Where(a => a.Id == 1).Set(a => a.Title, "newtitle").ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `tb_topic` SET `Title` = @p_0 WHERE (`Id` = 1)", sql);
+
+ sql = update.Where(a => a.Id == 1).Set(a => a.Title, "newtitle").Set(a => a.CreateTime, new DateTime(2020, 1, 1)).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `tb_topic` SET `Title` = @p_0, `CreateTime` = @p_1 WHERE (`Id` = 1)", sql);
+
+ sql = update.Set(a => a.Clicks * 10 / 1).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `tb_topic` SET `Clicks` = ifnull(`Clicks`, 0) * 10 / 1 WHERE (`Id` = 1)", sql);
+
+ sql = update.Set(a => a.Id - 10).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `tb_topic` SET `Id` = (`Id` - 10) WHERE (`Id` = 1)", sql);
+
+ int incrv = 10;
+ sql = update.Set(a => a.Clicks * incrv / 1).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `tb_topic` SET `Clicks` = ifnull(`Clicks`, 0) * 10 / 1 WHERE (`Id` = 1)", sql);
+
+ sql = update.Set(a => a.Id - incrv).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `tb_topic` SET `Id` = (`Id` - 10) WHERE (`Id` = 1)", sql);
+
+ sql = update.Set(a => a.Clicks == a.Clicks * 10 / 1).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `tb_topic` SET `Clicks` = `Clicks` * 10 / 1 WHERE (`Id` = 1)", sql);
+
+ sql = update.Set(a => a.Id == 10).Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `tb_topic` SET `Id` = 10 WHERE (`Id` = 1)", sql);
+
+ var id = g.mysql.Insert().AppendData(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).ExecuteIdentity();
+ Assert.True(id > 0);
+ sql = g.mysql.Update().Where(a => a.id == id).Set(a => a.type, TestEnumUpdateTbType.biggit).ToSql().Replace("\r\n", "");
+ Assert.Equal($"UPDATE `TestEnumUpdateTb` SET `type` = @p_0 WHERE (`id` = {id})", sql);
+ g.mysql.Update().Where(a => a.id == id).Set(a => a.type, TestEnumUpdateTbType.biggit).ExecuteAffrows();
+ Assert.Equal(TestEnumUpdateTbType.biggit, g.mysql.Select().Where(a => a.id == id).First()?.type);
+
+ sql = g.mysql.Update().NoneParameter().Where(a => a.id == id).Set(a => a.type, TestEnumUpdateTbType.str1).ToSql().Replace("\r\n", "");
+ Assert.Equal($"UPDATE `TestEnumUpdateTb` SET `type` = 'str1' WHERE (`id` = {id})", sql);
+ g.mysql.Update().NoneParameter().Where(a => a.id == id).Set(a => a.type, TestEnumUpdateTbType.str1).ExecuteAffrows();
+ Assert.Equal(TestEnumUpdateTbType.str1, g.mysql.Select().Where(a => a.id == id).First()?.type);
+ }
+ [Fact]
+ public void SetRaw() {
+ var sql = update.Where(a => a.Id == 1).SetRaw("clicks = clicks + @incrClick", new { incrClick = 1 }).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `tb_topic` SET clicks = clicks + @incrClick WHERE (`Id` = 1)", sql);
+
+ sql = g.mysql.Update().NoneParameter().Where(a => a.id == 0).SetRaw("`type` = {0}".FormatMySql(TestEnumUpdateTbType.sum211)).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `TestEnumUpdateTb` SET `type` = 'sum211' WHERE (`id` = 0)", sql);
+ }
+ [Fact]
+ public void Where() {
+ var sql = update.Where(a => a.Id == 1).SetRaw("title='newtitle'").ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `tb_topic` SET title='newtitle' WHERE (`Id` = 1)", sql);
+
+ sql = update.Where("id = @id", new { id = 1 }).SetRaw("title='newtitle'").ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `tb_topic` SET title='newtitle' WHERE (id = @id)", sql);
+
+ var item = new Topic { Id = 1, Title = "newtitle" };
+ sql = update.Where(item).SetRaw("title='newtitle'").ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `tb_topic` SET title='newtitle' WHERE (`Id` = 1)", sql);
+
+ var items = new List();
+ for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
+ sql = update.Where(items).SetRaw("title='newtitle'").ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `tb_topic` SET title='newtitle' WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))", sql);
+
+ sql = g.mysql.Update().NoneParameter().Where(a => a.id == 0 && a.type == TestEnumUpdateTbType.str1)
+ .Set(a => a.type, TestEnumUpdateTbType.sum211).ToSql().Replace("\r\n", "");
+ Assert.Equal("UPDATE `TestEnumUpdateTb` SET `type` = 'sum211' WHERE (`id` = 0 AND `type` = 'str1')", sql);
+ }
+ [Fact]
+ public void WhereExists() {
+
+ }
+ [Fact]
+ public void ExecuteAffrows() {
+
+ }
+ [Fact]
+ public void ExecuteUpdated() {
+
+ }
+
+ [Fact]
+ public void AsTable() {
+ Assert.Null(g.mysql.Update().ToSql());
+ Assert.Equal("UPDATE `tb_topicAsTable` SET title='test' \r\nWHERE (`Id` = 1 OR `Id` = 2)", g.mysql.Update(new[] { 1, 2 }).SetRaw("title='test'").AsTable(a => "tb_topicAsTable").ToSql());
+ Assert.Equal("UPDATE `tb_topicAsTable` SET title='test1' \r\nWHERE (`Id` = 1)", g.mysql.Update(new Topic { Id = 1, Title = "test" }).SetRaw("title='test1'").AsTable(a => "tb_topicAsTable").ToSql());
+ Assert.Equal("UPDATE `tb_topicAsTable` SET title='test1' \r\nWHERE (`Id` = 1 OR `Id` = 2)", g.mysql.Update(new[] { new Topic { Id = 1, Title = "test" }, new Topic { Id = 2, Title = "test" } }).SetRaw("title='test1'").AsTable(a => "tb_topicAsTable").ToSql());
+ Assert.Equal("UPDATE `tb_topicAsTable` SET title='test1' \r\nWHERE (`Id` = 1)", g.mysql.Update(new { id = 1 }).SetRaw("title='test1'").AsTable(a => "tb_topicAsTable").ToSql());
+ }
+ }
+}
diff --git a/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/MapType/BoolNullableTest.cs b/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/MapType/BoolNullableTest.cs
new file mode 100644
index 00000000..5202b06c
--- /dev/null
+++ b/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/MapType/BoolNullableTest.cs
@@ -0,0 +1,1562 @@
+using FreeSql.DataAnnotations;
+using System;
+using Xunit;
+
+namespace FreeSql.Tests.MySqlConnectorMapType {
+ public class BoolNullableTest {
+ class BoolNullableMap {
+ public Guid id { get; set; }
+ [Column(MapType = typeof(bool))]
+ public bool? tobool { get; set; } = true;
+
+ [Column(MapType = typeof(sbyte))]
+ public bool? tosbyte { get; set; } = true;
+ [Column(MapType = typeof(sbyte?))]
+ public bool? tosbytenullable { get; set; } = true;
+
+ [Column(MapType = typeof(short))]
+ public bool? toshort { get; set; } = true;
+
+ [Column(MapType = typeof(short?))]
+ public bool? toshortnullable { get; set; } = true;
+
+ [Column(MapType = typeof(int))]
+ public bool? toint { get; set; } = true;
+
+ [Column(MapType = typeof(int?))]
+ public bool? tointnullable { get; set; } = true;
+
+ [Column(MapType = typeof(long))]
+ public bool? tolong { get; set; } = true;
+ [Column(MapType = typeof(long?))]
+ public bool? tolongnullable { get; set; } = true;
+
+ [Column(MapType = typeof(byte))]
+ public bool? tobyte { get; set; } = true;
+ [Column(MapType = typeof(byte?))]
+ public bool? tobytenullable { get; set; } = true;
+
+ [Column(MapType = typeof(ushort))]
+ public bool? toushort { get; set; } = true;
+
+ [Column(MapType = typeof(ushort?))]
+ public bool? toushortnullable { get; set; } = true;
+
+ [Column(MapType = typeof(uint))]
+ public bool? touint { get; set; } = true;
+
+ [Column(MapType = typeof(uint?))]
+ public bool? touintnullable { get; set; } = true;
+
+ [Column(MapType = typeof(ulong))]
+ public bool? toulong { get; set; } = true;
+ [Column(MapType = typeof(ulong?))]
+ public bool? toulongnullable { get; set; } = true;
+
+ [Column(MapType = typeof(string))]
+ public bool? tostring { get; set; } = true;
+ }
+ [Fact]
+ public void Bool() {
+ //insert
+ var orm = g.mysql;
+ var item = new BoolNullableMap { };
+ Assert.Equal(1, orm.Insert().AppendData(item).ExecuteAffrows());
+ var find = orm.Select().Where(a => a.id == item.id && a.tobool == true).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(item.tobool, find.tobool);
+ Assert.Equal(true, find.tobool);
+
+ item = new BoolNullableMap { tobool = false };
+ Assert.Equal(1, orm.Insert().AppendData(item).ExecuteAffrows());
+ find = orm.Select().Where(a => a.id == item.id && a.tobool == false).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(item.tobool, find.tobool);
+ Assert.Equal(false, find.tobool);
+
+ item = new BoolNullableMap { tobool = null };
+ Assert.Equal(1, orm.Insert().AppendData(item).ExecuteAffrows());
+ Assert.Null(orm.Select().Where(a => a.id == item.id && a.tobool == null).First());
+ find = orm.Select().Where(a => a.id == item.id && a.tobool == false).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.NotEqual(item.tobool, find.tobool);
+ Assert.Equal(false, find.tobool);
+
+ //update all
+ item.tobool = true;
+ Assert.Equal(1, orm.Update().SetSource(item).ExecuteAffrows());
+ find = orm.Select().Where(a => a.id == item.id && a.tobool == true).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(item.tobool, find.tobool);
+ Assert.Equal(true, find.tobool);
+
+ item.tobool = false;
+ Assert.Equal(1, orm.Update().SetSource(item).ExecuteAffrows());
+ find = orm.Select().Where(a => a.id == item.id && a.tobool == false).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(item.tobool, find.tobool);
+ Assert.Equal(false, find.tobool);
+
+ item.tobool = null;
+ Assert.Equal(1, orm.Update().SetSource(item).ExecuteAffrows());
+ Assert.Null(orm.Select().Where(a => a.id == item.id && a.tobool == null).First());
+ find = orm.Select().Where(a => a.id == item.id && a.tobool == false).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.NotEqual(item.tobool, find.tobool);
+ Assert.Equal(false, find.tobool);
+
+ //update set
+ Assert.Equal(1, orm.Update().Where(a => a.id == item.id).Set(a => a.tobool, true).ExecuteAffrows());
+ find = orm.Select().Where(a => a.id == item.id && a.tobool == true).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(true, find.tobool);
+
+ Assert.Equal(1, orm.Update().Where(a => a.id == item.id).Set(a => a.tobool, false).ExecuteAffrows());
+ find = orm.Select().Where(a => a.id == item.id && a.tobool == false).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(false, find.tobool);
+
+ Assert.Equal(1, orm.Update().Where(a => a.id == item.id).Set(a => a.tobool, null).ExecuteAffrows());
+ Assert.Null(orm.Select().Where(a => a.id == item.id && a.tobool == null).First());
+ find = orm.Select().Where(a => a.id == item.id && a.tobool == false).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(false, find.tobool);
+
+ //delete
+ Assert.Equal(0, orm.Delete().Where(a => a.id == item.id && a.tobool == true).ExecuteAffrows());
+ Assert.Equal(0, orm.Delete().Where(a => a.id == item.id && a.tobool == null).ExecuteAffrows());
+ Assert.Equal(1, orm.Delete().Where(a => a.id == item.id && a.tobool == false).ExecuteAffrows());
+ Assert.Null(orm.Select().Where(a => a.id == item.id).First());
+ }
+ [Fact]
+ public void SByte() {
+ //insert
+ var orm = g.mysql;
+ var item = new BoolNullableMap { };
+ Assert.Equal(1, orm.Insert().AppendData(item).ExecuteAffrows());
+ var find = orm.Select().Where(a => a.id == item.id && a.tosbyte == true).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(item.tosbyte, find.tosbyte);
+ Assert.Equal(true, find.tosbyte);
+
+ item = new BoolNullableMap { tosbyte = false };
+ Assert.Equal(1, orm.Insert().AppendData(item).ExecuteAffrows());
+ find = orm.Select().Where(a => a.id == item.id && a.tosbyte == false).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(item.tosbyte, find.tosbyte);
+ Assert.Equal(false, find.tosbyte);
+
+ item = new BoolNullableMap { tosbyte = null };
+ Assert.Equal(1, orm.Insert().AppendData(item).ExecuteAffrows());
+ Assert.Null(orm.Select().Where(a => a.id == item.id && a.tosbyte == null).First());
+ find = orm.Select().Where(a => a.id == item.id && a.tosbyte == false).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.NotEqual(item.tosbyte, find.tosbyte);
+ Assert.Equal(false, find.tosbyte);
+
+ //update all
+ item.tosbyte = true;
+ Assert.Equal(1, orm.Update().SetSource(item).ExecuteAffrows());
+ find = orm.Select().Where(a => a.id == item.id && a.tosbyte == true).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(item.tosbyte, find.tosbyte);
+ Assert.Equal(true, find.tosbyte);
+
+ item.tosbyte = false;
+ Assert.Equal(1, orm.Update().SetSource(item).ExecuteAffrows());
+ find = orm.Select().Where(a => a.id == item.id && a.tosbyte == false).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(item.tosbyte, find.tosbyte);
+ Assert.Equal(false, find.tosbyte);
+
+ item.tosbyte = null;
+ Assert.Equal(1, orm.Update().SetSource(item).ExecuteAffrows());
+ Assert.Null(orm.Select().Where(a => a.id == item.id && a.tosbyte == null).First());
+ find = orm.Select().Where(a => a.id == item.id && a.tosbyte == false).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.NotEqual(item.tosbyte, find.tosbyte);
+ Assert.Equal(false, find.tosbyte);
+
+ //update set
+ Assert.Equal(1, orm.Update().Where(a => a.id == item.id).Set(a => a.tosbyte, true).ExecuteAffrows());
+ find = orm.Select().Where(a => a.id == item.id && a.tosbyte == true).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(true, find.tosbyte);
+
+ Assert.Equal(1, orm.Update().Where(a => a.id == item.id).Set(a => a.tosbyte, false).ExecuteAffrows());
+ find = orm.Select().Where(a => a.id == item.id && a.tosbyte == false).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(false, find.tosbyte);
+
+ Assert.Equal(1, orm.Update().Where(a => a.id == item.id).Set(a => a.tosbyte, null).ExecuteAffrows());
+ Assert.Null(orm.Select().Where(a => a.id == item.id && a.tosbyte == null).First());
+ find = orm.Select().Where(a => a.id == item.id && a.tosbyte == false).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(false, find.tosbyte);
+
+ //delete
+ Assert.Equal(0, orm.Delete().Where(a => a.id == item.id && a.tosbyte == true).ExecuteAffrows());
+ Assert.Equal(0, orm.Delete().Where(a => a.id == item.id && a.tosbyte == null).ExecuteAffrows());
+ Assert.Equal(1, orm.Delete().Where(a => a.id == item.id && a.tosbyte == false).ExecuteAffrows());
+ Assert.Null(orm.Select().Where(a => a.id == item.id).First());
+ }
+ [Fact]
+ public void SByteNullable() {
+ //insert
+ var orm = g.mysql;
+ var item = new BoolNullableMap { };
+ Assert.Equal(1, orm.Insert().AppendData(item).ExecuteAffrows());
+ var find = orm.Select().Where(a => a.id == item.id && a.tosbytenullable == true).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(item.tosbytenullable, find.tosbytenullable);
+ Assert.Equal(true, find.tosbytenullable);
+
+ item = new BoolNullableMap { tosbytenullable = false };
+ Assert.Equal(1, orm.Insert().AppendData(item).ExecuteAffrows());
+ find = orm.Select().Where(a => a.id == item.id && a.tosbytenullable == false).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(item.tosbytenullable, find.tosbytenullable);
+ Assert.Equal(false, find.tosbytenullable);
+
+ item = new BoolNullableMap { tosbytenullable = null };
+ Assert.Equal(1, orm.Insert().AppendData(item).ExecuteAffrows());
+ Assert.Null(orm.Select().Where(a => a.id == item.id && a.tosbytenullable == false).First());
+ find = orm.Select().Where(a => a.id == item.id && a.tosbytenullable == null).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(item.tosbytenullable, find.tosbytenullable);
+ Assert.Null(find.tosbytenullable);
+
+ //update all
+ item.tosbytenullable = true;
+ Assert.Equal(1, orm.Update().SetSource(item).ExecuteAffrows());
+ find = orm.Select().Where(a => a.id == item.id && a.tosbytenullable == true).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(item.tosbytenullable, find.tosbytenullable);
+ Assert.Equal(true, find.tosbytenullable);
+
+ item.tosbytenullable = false;
+ Assert.Equal(1, orm.Update().SetSource(item).ExecuteAffrows());
+ find = orm.Select().Where(a => a.id == item.id && a.tosbytenullable == false).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(item.tosbytenullable, find.tosbytenullable);
+ Assert.Equal(false, find.tosbytenullable);
+
+ item.tosbytenullable = null;
+ Assert.Equal(1, orm.Update().SetSource(item).ExecuteAffrows());
+ Assert.Null(orm.Select().Where(a => a.id == item.id && a.tosbytenullable == false).First());
+ find = orm.Select().Where(a => a.id == item.id && a.tosbytenullable == null).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(item.tosbytenullable, find.tosbytenullable);
+ Assert.Null(find.tosbytenullable);
+
+ //update set
+ Assert.Equal(1, orm.Update().Where(a => a.id == item.id).Set(a => a.tosbytenullable, true).ExecuteAffrows());
+ find = orm.Select().Where(a => a.id == item.id && a.tosbytenullable == true).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(true, find.tosbytenullable);
+
+ Assert.Equal(1, orm.Update().Where(a => a.id == item.id).Set(a => a.tosbytenullable, false).ExecuteAffrows());
+ find = orm.Select().Where(a => a.id == item.id && a.tosbytenullable == false).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(false, find.tosbytenullable);
+
+ Assert.Equal(1, orm.Update().Where(a => a.id == item.id).Set(a => a.tosbytenullable, null).ExecuteAffrows());
+ Assert.Null(orm.Select().Where(a => a.id == item.id && a.tosbytenullable == false).First());
+ find = orm.Select().Where(a => a.id == item.id && a.tosbytenullable == null).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Null(find.tosbytenullable);
+
+ //delete
+ Assert.Equal(0, orm.Delete().Where(a => a.id == item.id && a.tosbytenullable == true).ExecuteAffrows());
+ Assert.Equal(1, orm.Delete().Where(a => a.id == item.id && a.tosbytenullable == null).ExecuteAffrows());
+ Assert.Equal(0, orm.Delete().Where(a => a.id == item.id && a.tosbytenullable == false).ExecuteAffrows());
+ Assert.Null(orm.Select().Where(a => a.id == item.id).First());
+ }
+ [Fact]
+ public void Short() {
+ //insert
+ var orm = g.mysql;
+ var item = new BoolNullableMap { };
+ Assert.Equal(1, orm.Insert().AppendData(item).ExecuteAffrows());
+ var find = orm.Select().Where(a => a.id == item.id && a.toshort == true).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(item.toshort, find.toshort);
+ Assert.Equal(true, find.toshort);
+
+ item = new BoolNullableMap { toshort = false };
+ Assert.Equal(1, orm.Insert().AppendData(item).ExecuteAffrows());
+ find = orm.Select().Where(a => a.id == item.id && a.toshort == false).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(item.toshort, find.toshort);
+ Assert.Equal(false, find.toshort);
+
+ item = new BoolNullableMap { toshort = null };
+ Assert.Equal(1, orm.Insert().AppendData(item).ExecuteAffrows());
+ Assert.Null(orm.Select().Where(a => a.id == item.id && a.toshort == null).First());
+ find = orm.Select().Where(a => a.id == item.id && a.toshort == false).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.NotEqual(item.toshort, find.toshort);
+ Assert.Equal(false, find.toshort);
+
+ //update all
+ item.toshort = true;
+ Assert.Equal(1, orm.Update().SetSource(item).ExecuteAffrows());
+ find = orm.Select().Where(a => a.id == item.id && a.toshort == true).First();
+ Assert.NotNull(find);
+ Assert.Equal(item.id, find.id);
+ Assert.Equal(item.toshort, find.toshort);
+ Assert.Equal(true, find.toshort);
+
+ item.toshort = false;
+ Assert.Equal(1, orm.Update