Merge branch 'master' of github.com:dotnetcore/FreeSql

This commit is contained in:
igeekfan
2022-04-16 17:37:25 +08:00
133 changed files with 3284 additions and 1726 deletions

View File

@ -593,6 +593,55 @@ namespace FreeSql.Tests
public Guid Id { get; set; }
public string Name { get; set; }
}
[Fact]
public void BeginEditIdentity()
{
g.sqlite.Delete<BeginEdit02>().Where("1=1").ExecuteAffrows();
var repo = g.sqlite.GetRepository<BeginEdit02>();
var cts = new[] {
new BeginEdit02 { Name = "<22><><EFBFBD><EFBFBD>1" },
new BeginEdit02 { Name = "<22><><EFBFBD><EFBFBD>1_1" },
new BeginEdit02 { Name = "<22><><EFBFBD><EFBFBD>1_2" },
new BeginEdit02 { Name = "<22><><EFBFBD><EFBFBD>1_3" },
new BeginEdit02 { Name = "<22><><EFBFBD><EFBFBD>2" },
new BeginEdit02 { Name = "<22><><EFBFBD><EFBFBD>2_1" },
new BeginEdit02 { Name = "<22><><EFBFBD><EFBFBD>2_2" }
}.ToList();
repo.Insert(cts);
repo.BeginEdit(cts);
cts.Add(new BeginEdit02 { Name = "<22><><EFBFBD><EFBFBD>2_3" });
cts[0].Name = "123123";
cts.RemoveAt(1);
Assert.Equal(3, repo.EndEdit());
g.sqlite.Delete<BeginEdit02>().Where("1=1").ExecuteAffrows();
repo = g.sqlite.GetRepository<BeginEdit02>();
cts = repo.Select.ToList();
repo.BeginEdit(cts);
cts.AddRange(new[] {
new BeginEdit02 { Name = "<22><><EFBFBD><EFBFBD>1" },
new BeginEdit02 { Name = "<22><><EFBFBD><EFBFBD>1_1" },
new BeginEdit02 { Name = "<22><><EFBFBD><EFBFBD>1_2" },
new BeginEdit02 { Name = "<22><><EFBFBD><EFBFBD>1_3" },
new BeginEdit02 { Name = "<22><><EFBFBD><EFBFBD>2" },
new BeginEdit02 { Name = "<22><><EFBFBD><EFBFBD>2_1" },
new BeginEdit02 { Name = "<22><><EFBFBD><EFBFBD>2_2" }
});
Assert.Equal(7, repo.EndEdit());
}
class BeginEdit02
{
[Column(IsIdentity = true)]
public int Id { get; set; }
public string Name { get; set; }
[Column(ServerTime = DateTimeKind.Utc)]
public DateTime UpdateTime { get; set; }
}
[Fact]
public void OrmScoped()

View File

@ -95,7 +95,7 @@ WHERE (a__Parent__Parent.Name = '粤语')", t0);
var t1 = g.gbase.Select<Tag>().Where(a => a.Tags.AsSelect().Any(t => t.Parent.Id == 10)).ToSql();
Assert.Equal(@"SELECT a.Id, a.Parent_id, a.Ddd, a.Name
FROM Tag a
WHERE (exists(SELECT FIRST 1 1
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)))", t1);
@ -104,9 +104,9 @@ WHERE (exists(SELECT FIRST 1 1
var t2 = g.gbase.Select<Song>().Where(s => s.Tags.AsSelect().Any(t => t.Name == "国语")).ToSql();
Assert.Equal(@"SELECT a.Id, a.Create_time, a.Is_deleted, a.Title, a.Url
FROM Song a
WHERE (exists(SELECT FIRST 1 1
WHERE (exists(SELECT 1
FROM Song_tag Mt_Ms
WHERE (Mt_Ms.Song_id = a.Id) AND (exists(SELECT FIRST 1 1
WHERE (Mt_Ms.Song_id = a.Id) AND (exists(SELECT 1
FROM Tag t
WHERE (t.Name = '国语') AND (t.Id = Mt_Ms.Tag_id)))))", t2);
}

View File

@ -35,4 +35,6 @@ public class g
});
public static IFreeSql gbase => gbaseLazy.Value;
//su - gbasedbt
//oninit -vy
}

View File

@ -46,5 +46,112 @@ namespace FreeSql.Tests.Internal
Assert.Equal("p", ps2[0].ParameterName);
Assert.Equal(typeof(SqlParameter), ps2[0].GetType());
}
[Fact]
public void TestReplaceSqlConstString()
{
var dict = new Dictionary<string, string>();
string sql1 = "", sql2 = "", sql3 = "";
sql2 = FreeSql.Internal.Utils.ReplaceSqlConstString(sql1 = @"UPDATE ""as_table_log_202201"" SET ""msg"" = 'msg01', ""createtime"" = '2022-01-01 13:00:11'
WHERE (""id"" = '6252a2e6-5df3-bb10-00c1-bda60c4053fe')", dict);
Assert.Equal(3, dict.Count);
sql3 = sql2;
dict.Select(a => sql3 = sql3.Replace(a.Key, "{0}".FormatMySql(a.Value))).ToList();
Assert.Equal(sql1, sql3);
dict.Clear();
sql2 = FreeSql.Internal.Utils.ReplaceSqlConstString(sql1 = @"INSERT INTO ""as_table_log_202201""(""id"", ""msg"", ""createtime"") VALUES('6252a2e6-5df3-bb10-00c1-bda60c4053fe', 'msg01', '2022-01-01 13:00:11'), ('6252a2e6-5df3-bb10-00c1-bda773467785', 'msg02', '2022-01-02 14:00:12')", dict);
Assert.Equal(6, dict.Count);
sql3 = sql2;
dict.Select(a => sql3 = sql3.Replace(a.Key, "{0}".FormatMySql(a.Value))).ToList();
Assert.Equal(sql1, sql3);
dict.Clear();
sql2 = FreeSql.Internal.Utils.ReplaceSqlConstString(sql1 = @"DELETE FROM ""as_table_log_202205"" WHERE (""id"" = @exp_0 AND ""createtime"" between '2022-03-01 00:00:00' and '2022-05-01 00:00:00')", dict);
Assert.Equal(2, dict.Count);
sql3 = sql2;
dict.Select(a => sql3 = sql3.Replace(a.Key, "{0}".FormatMySql(a.Value))).ToList();
Assert.Equal(sql1, sql3);
dict.Clear();
sql2 = FreeSql.Internal.Utils.ReplaceSqlConstString(sql1 = @"UPDATE ""as_table_log_202202"" SET ""msg"" = CASE ""id""
WHEN '6252a2e6-5df3-bb10-00c1-bda818f4b93f' THEN 'msg03'
WHEN '6252a2e6-5df3-bb10-00c1-bda95dbadefd' THEN 'msg04' END, ""createtime"" = CASE ""id""
WHEN '6252a2e6-5df3-bb10-00c1-bda818f4b93f' THEN '2022-02-02 15:00:13'
WHEN '6252a2e6-5df3-bb10-00c1-bda95dbadefd' THEN '2022-02-08 15:00:13' END
WHERE (""id"" IN ('6252a2e6-5df3-bb10-00c1-bda818f4b93f','6252a2e6-5df3-bb10-00c1-bda95dbadefd'))", dict);
Assert.Equal(6, dict.Count);
sql3 = sql2;
dict.Select(a => sql3 = sql3.Replace(a.Key, "{0}".FormatMySql(a.Value))).ToList();
Assert.Equal(sql1, sql3);
dict.Clear();
sql2 = FreeSql.Internal.Utils.ReplaceSqlConstString(sql1 = @"UPDATE ""as_table_log_202207"" SET ""msg"" = 'msg07', ""createtime"" = '2022-07-01 00:00:00'
WHERE (""id"" = '6252a2e6-5df3-bb10-00c1-bdad01a608fb')", dict);
Assert.Equal(3, dict.Count);
sql3 = sql2;
dict.Select(a => sql3 = sql3.Replace(a.Key, "{0}".FormatMySql(a.Value))).ToList();
Assert.Equal(sql1, sql3);
dict.Clear();
sql2 = FreeSql.Internal.Utils.ReplaceSqlConstString(sql1 = @"UPDATE ""as_table_log_202207"" SET ""msg"" = 'newmsg'
WHERE (""id"" = 'acc5df07-11a5-45b5-8af1-7b1ffac19f68')", dict);
Assert.Equal(2, dict.Count);
sql3 = sql2;
dict.Select(a => sql3 = sql3.Replace(a.Key, "{0}".FormatMySql(a.Value))).ToList();
Assert.Equal(sql1, sql3);
dict.Clear();
sql2 = FreeSql.Internal.Utils.ReplaceSqlConstString(sql1 = @"UPDATE ""as_table_log_202203"" SET ""msg"" = 'newmsg'
WHERE (""id"" = '29bf2df7-3dfc-4005-a2e3-0421e50b2910') AND (""createtime"" between '2022-03-01 00:00:00' and '2022-05-01 00:00:00')", dict);
Assert.Equal(4, dict.Count);
sql3 = sql2;
dict.Select(a => sql3 = sql3.Replace(a.Key, "{0}".FormatMySql(a.Value))).ToList();
Assert.Equal(sql1, sql3);
dict.Clear();
sql2 = FreeSql.Internal.Utils.ReplaceSqlConstString(sql1 = @"UPDATE ""as_table_log_202203"" SET ""msg"" = 'newmsg'
WHERE (""id"" = '4c9b5b32-49b2-44ee-beee-1e399e86b933') AND (""createtime"" > '2022-03-01 00:00:00' AND ""createtime"" < '2022-05-01 00:00:00')", dict);
Assert.Equal(4, dict.Count);
sql3 = sql2;
dict.Select(a => sql3 = sql3.Replace(a.Key, "{0}".FormatMySql(a.Value))).ToList();
Assert.Equal(sql1, sql3);
dict.Clear();
sql2 = FreeSql.Internal.Utils.ReplaceSqlConstString(sql1 = @"UPDATE ""as_table_log_202201"" SET ""msg"" = 'newmsg'
WHERE (""id"" = '15d2a84f-bd72-4d73-8ad1-466ba8beea60') AND (""createtime"" < '2022-05-01 00:00:00')", dict);
Assert.Equal(3, dict.Count);
sql3 = sql2;
dict.Select(a => sql3 = sql3.Replace(a.Key, "{0}".FormatMySql(a.Value))).ToList();
Assert.Equal(sql1, sql3);
dict.Clear();
sql2 = FreeSql.Internal.Utils.ReplaceSqlConstString(sql1 = @"SELECT * from (SELECT a.""id"", a.""msg"", a.""createtime""
FROM ""as_table_log_202204"" a
WHERE (a.""createtime"" < '2022-05-01 00:00:00')) ftb
UNION ALL
SELECT * from (SELECT a.""id"", a.""msg"", a.""createtime""
FROM ""as_table_log_202203"" a
WHERE (a.""createtime"" < '2022-05-01 00:00:00')) ftb
UNION ALL
SELECT * from (SELECT a.""id"", a.""msg"", a.""createtime""
FROM ""as_table_log_202202"" a
WHERE (a.""createtime"" < '2022-05-01 00:00:00')) ftb
UNION ALL
SELECT * from (SELECT a.""id"", a.""msg"", a.""createtime""
FROM ""as_table_log_202201"" a
WHERE (a.""createtime"" < '2022-05-01 00:00:00')) ftb", dict);
Assert.Equal(1, dict.Count);
sql3 = sql2;
dict.Select(a => sql3 = sql3.Replace(a.Key, "{0}".FormatMySql(a.Value))).ToList();
Assert.Equal(sql1, sql3);
}
}
}

View File

@ -106,7 +106,7 @@ WHERE (strftime('%Y',a."CreateTime") = 2021)
var d1 = fsql.Select<Users>().Where(r => r.CreateTime.Month == month).ToList();
var d2 = fsql.Select<Users>().Where(r => r.CreateTime.Year == year).ToList();
var d5 = fsql.Select<Users>().Where(r => r.CreateTime.Year == 2021).ToList();
var d5 = fsql.Select<Users>().Where(r => r.CreateTime.Year == 2022).ToList();
Assert.Single(dmonth1);
Assert.Single(d1);

View File

@ -77,6 +77,8 @@ namespace FreeSql.Tests.SqliteExpression
{
IEnumerable<int> testlinqlist = new List<int>(new[] { 1, 2, 3 });
var testlinq = select.Where(a => testlinqlist.Contains(a.Int)).ToList();
var testlinq2list = new string[] { };
var testlinq2 = g.sqlite.Delete<TableAllType>().Where(a => testlinq2list.Contains(a.String)).ToSql();
//in not in
var sql111 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.Int)).ToList();

View File

@ -33,7 +33,7 @@ namespace FreeSql.Tests
{
is_lock = test.ratio < 1 //这里生成的SQL语句有问题 ratio = 0.9 或 1.9 或 2.1 等等都是生成的是1
}).Where(m => test.ratio < 1).ToSql();
Assert.Equal(@"UPDATE TestDto SET is_lock = 2.1 < 1
Assert.Equal(@"UPDATE `TestDto` SET `is_lock` = 2.1 < 1
WHERE (2.1 < 1)", sql);
}