- 增加 PostgreSQL 特有功能 On Conflict Do Update 功能;

This commit is contained in:
28810 2019-11-13 16:35:13 +08:00
parent e0030b0c00
commit 24e2c098a4
2 changed files with 20 additions and 21 deletions

View File

@ -21,7 +21,7 @@ namespace FreeSql.Tests.PostgreSQL
{
g.pgsql.Delete<TestOnConflictDoUpdateInfo>(new[] { 100, 101, 102 }).ExecuteAffrows();
var odku1 = g.pgsql.Insert(new TestOnConflictDoUpdateInfo { id = 100, title = "title-100", time = DateTime.Parse("2000-01-01") }).NoneParameter().OnConflictDoUpdate();
Assert.Equal(odku1.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo"" AS _ftb_ (""id"", ""title"", ""time"") VALUES(100, 'title-100', '2000-01-01 00:00:00.000000')
Assert.Equal(odku1.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"", ""time"") VALUES(100, 'title-100', '2000-01-01 00:00:00.000000')
ON CONFLICT(""id"") DO UPDATE SET
""title"" = EXCLUDED.""title"",
""time"" = EXCLUDED.""time""");
@ -32,7 +32,7 @@ ON CONFLICT(""id"") DO UPDATE SET
new TestOnConflictDoUpdateInfo { id = 101, title = "title-101", time = DateTime.Parse("2000-01-01") },
new TestOnConflictDoUpdateInfo { id = 102, title = "title-102", time = DateTime.Parse("2000-01-01") }
}).NoneParameter().OnConflictDoUpdate();
Assert.Equal(odku2.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo"" AS _ftb_ (""id"", ""title"", ""time"") VALUES(100, 'title-100', '2000-01-01 00:00:00.000000'), (101, 'title-101', '2000-01-01 00:00:00.000000'), (102, 'title-102', '2000-01-01 00:00:00.000000')
Assert.Equal(odku2.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"", ""time"") VALUES(100, 'title-100', '2000-01-01 00:00:00.000000'), (101, 'title-101', '2000-01-01 00:00:00.000000'), (102, 'title-102', '2000-01-01 00:00:00.000000')
ON CONFLICT(""id"") DO UPDATE SET
""title"" = EXCLUDED.""title"",
""time"" = EXCLUDED.""time""");
@ -44,7 +44,7 @@ ON CONFLICT(""id"") DO UPDATE SET
{
g.pgsql.Delete<TestOnConflictDoUpdateInfo>(new[] { 200, 201, 202 }).ExecuteAffrows();
var odku1 = g.pgsql.Insert(new TestOnConflictDoUpdateInfo { id = 200, title = "title-200", time = DateTime.Parse("2000-01-01") }).IgnoreColumns(a => a.time).NoneParameter().OnConflictDoUpdate();
Assert.Equal(odku1.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo"" AS _ftb_ (""id"", ""title"") VALUES(200, 'title-200')
Assert.Equal(odku1.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"") VALUES(200, 'title-200')
ON CONFLICT(""id"") DO UPDATE SET
""title"" = EXCLUDED.""title"",
""time"" = '2000-01-01 00:00:00.000000'");
@ -55,7 +55,7 @@ ON CONFLICT(""id"") DO UPDATE SET
new TestOnConflictDoUpdateInfo { id = 201, title = "title-201", time = DateTime.Parse("2000-01-01") },
new TestOnConflictDoUpdateInfo { id = 202, title = "title-202", time = DateTime.Parse("2000-01-01") }
}).IgnoreColumns(a => a.time).NoneParameter().OnConflictDoUpdate();
Assert.Equal(odku2.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo"" AS _ftb_ (""id"", ""title"") VALUES(200, 'title-200'), (201, 'title-201'), (202, 'title-202')
Assert.Equal(odku2.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"") VALUES(200, 'title-200'), (201, 'title-201'), (202, 'title-202')
ON CONFLICT(""id"") DO UPDATE SET
""title"" = EXCLUDED.""title"",
""time"" = CASE EXCLUDED.""id""
@ -67,7 +67,7 @@ WHEN 202 THEN '2000-01-01 00:00:00.000000' END::timestamp");
g.pgsql.Delete<TestOnConflictDoUpdateInfo>(new[] { 200, 201, 202 }).ExecuteAffrows();
odku1 = g.pgsql.Insert(new TestOnConflictDoUpdateInfo { id = 200, title = "title-200", time = DateTime.Parse("2000-01-01") }).IgnoreColumns(a => a.time).NoneParameter().OnConflictDoUpdate().IgnoreColumns(a => a.title);
Assert.Equal(odku1.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo"" AS _ftb_ (""id"", ""title"") VALUES(200, 'title-200')
Assert.Equal(odku1.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"") VALUES(200, 'title-200')
ON CONFLICT(""id"") DO UPDATE SET
""time"" = '2000-01-01 00:00:00.000000'");
Assert.Equal(1, odku1.ExecuteAffrows());
@ -77,7 +77,7 @@ ON CONFLICT(""id"") DO UPDATE SET
new TestOnConflictDoUpdateInfo { id = 201, title = "title-201", time = DateTime.Parse("2000-01-01") },
new TestOnConflictDoUpdateInfo { id = 202, title = "title-202", time = DateTime.Parse("2000-01-01") }
}).IgnoreColumns(a => a.time).NoneParameter().OnConflictDoUpdate().IgnoreColumns(a => a.title);
Assert.Equal(odku2.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo"" AS _ftb_ (""id"", ""title"") VALUES(200, 'title-200'), (201, 'title-201'), (202, 'title-202')
Assert.Equal(odku2.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"") VALUES(200, 'title-200'), (201, 'title-201'), (202, 'title-202')
ON CONFLICT(""id"") DO UPDATE SET
""time"" = CASE EXCLUDED.""id""
WHEN 200 THEN '2000-01-01 00:00:00.000000'
@ -91,7 +91,7 @@ WHEN 202 THEN '2000-01-01 00:00:00.000000' END::timestamp");
{
g.pgsql.Delete<TestOnConflictDoUpdateInfo>(new[] { 300, 301, 302 }).ExecuteAffrows();
var odku1 = g.pgsql.Insert(new TestOnConflictDoUpdateInfo { id = 300, title = "title-300", time = DateTime.Parse("2000-01-01") }).InsertColumns(a => a.title).NoneParameter().OnConflictDoUpdate();
Assert.Equal(odku1.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo"" AS _ftb_ (""id"", ""title"") VALUES(300, 'title-300')
Assert.Equal(odku1.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"") VALUES(300, 'title-300')
ON CONFLICT(""id"") DO UPDATE SET
""title"" = EXCLUDED.""title"",
""time"" = '2000-01-01 00:00:00.000000'");
@ -102,7 +102,7 @@ ON CONFLICT(""id"") DO UPDATE SET
new TestOnConflictDoUpdateInfo { id = 301, title = "title-301", time = DateTime.Parse("2000-01-01") },
new TestOnConflictDoUpdateInfo { id = 302, title = "title-302", time = DateTime.Parse("2000-01-01") }
}).InsertColumns(a => a.title).NoneParameter().OnConflictDoUpdate();
Assert.Equal(odku2.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo"" AS _ftb_ (""id"", ""title"") VALUES(300, 'title-300'), (301, 'title-301'), (302, 'title-302')
Assert.Equal(odku2.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"") VALUES(300, 'title-300'), (301, 'title-301'), (302, 'title-302')
ON CONFLICT(""id"") DO UPDATE SET
""title"" = EXCLUDED.""title"",
""time"" = CASE EXCLUDED.""id""
@ -114,7 +114,7 @@ WHEN 302 THEN '2000-01-01 00:00:00.000000' END::timestamp");
g.pgsql.Delete<TestOnConflictDoUpdateInfo>(new[] { 300, 301, 302 }).ExecuteAffrows();
odku1 = g.pgsql.Insert(new TestOnConflictDoUpdateInfo { id = 300, title = "title-300", time = DateTime.Parse("2000-01-01") }).InsertColumns(a => a.title).NoneParameter().OnConflictDoUpdate().UpdateColumns(a => a.time);
Assert.Equal(odku1.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo"" AS _ftb_ (""id"", ""title"") VALUES(300, 'title-300')
Assert.Equal(odku1.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"") VALUES(300, 'title-300')
ON CONFLICT(""id"") DO UPDATE SET
""time"" = '2000-01-01 00:00:00.000000'");
Assert.Equal(1, odku1.ExecuteAffrows());
@ -124,7 +124,7 @@ ON CONFLICT(""id"") DO UPDATE SET
new TestOnConflictDoUpdateInfo { id = 301, title = "title-301", time = DateTime.Parse("2000-01-01") },
new TestOnConflictDoUpdateInfo { id = 302, title = "title-302", time = DateTime.Parse("2000-01-01") }
}).InsertColumns(a => a.title).NoneParameter().OnConflictDoUpdate().UpdateColumns(a => a.time);
Assert.Equal(odku2.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo"" AS _ftb_ (""id"", ""title"") VALUES(300, 'title-300'), (301, 'title-301'), (302, 'title-302')
Assert.Equal(odku2.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"") VALUES(300, 'title-300'), (301, 'title-301'), (302, 'title-302')
ON CONFLICT(""id"") DO UPDATE SET
""time"" = CASE EXCLUDED.""id""
WHEN 300 THEN '2000-01-01 00:00:00.000000'
@ -138,7 +138,7 @@ WHEN 302 THEN '2000-01-01 00:00:00.000000' END::timestamp");
{
g.pgsql.Delete<TestOnConflictDoUpdateInfo>(new[] { 400, 401, 402 }).ExecuteAffrows();
var odku1 = g.pgsql.Insert(new TestOnConflictDoUpdateInfo { id = 400, title = "title-400", time = DateTime.Parse("2000-01-01") }).NoneParameter().OnConflictDoUpdate().Set(a => a.time, DateTime.Parse("2020-1-1"));
Assert.Equal(odku1.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo"" AS _ftb_ (""id"", ""title"", ""time"") VALUES(400, 'title-400', '2000-01-01 00:00:00.000000')
Assert.Equal(odku1.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"", ""time"") VALUES(400, 'title-400', '2000-01-01 00:00:00.000000')
ON CONFLICT(""id"") DO UPDATE SET
""time"" = '2020-01-01 00:00:00.000000'");
Assert.Equal(1, odku1.ExecuteAffrows());
@ -148,7 +148,7 @@ ON CONFLICT(""id"") DO UPDATE SET
new TestOnConflictDoUpdateInfo { id = 401, title = "title-401", time = DateTime.Parse("2000-01-01") },
new TestOnConflictDoUpdateInfo { id = 402, title = "title-402", time = DateTime.Parse("2000-01-01") }
}).NoneParameter().OnConflictDoUpdate().Set(a => a.time, DateTime.Parse("2020-1-1"));
Assert.Equal(odku2.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo"" AS _ftb_ (""id"", ""title"", ""time"") VALUES(400, 'title-400', '2000-01-01 00:00:00.000000'), (401, 'title-401', '2000-01-01 00:00:00.000000'), (402, 'title-402', '2000-01-01 00:00:00.000000')
Assert.Equal(odku2.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"", ""time"") VALUES(400, 'title-400', '2000-01-01 00:00:00.000000'), (401, 'title-401', '2000-01-01 00:00:00.000000'), (402, 'title-402', '2000-01-01 00:00:00.000000')
ON CONFLICT(""id"") DO UPDATE SET
""time"" = '2020-01-01 00:00:00.000000'");
odku2.ExecuteAffrows();
@ -157,7 +157,7 @@ ON CONFLICT(""id"") DO UPDATE SET
// var dt2020 = DateTime.Parse("2020-1-1");
// g.pgsql.Delete<TestOnConflictDoUpdateInfo>(new[] { 400, 401, 402 }).ExecuteAffrows();
// odku1 = g.pgsql.Insert(new TestOnConflictDoUpdateInfo { id = 400, title = "title-400", time = DateTime.Parse("2000-01-01") }).NoneParameter().OnConflictDoUpdate().Set(a => a.time == dt2020);
// Assert.Equal(odku1.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo"" AS _ftb_ (""id"", ""title"", ""time"") VALUES(400, 'title-400', '2000-01-01 00:00:00.000000')
// Assert.Equal(odku1.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"", ""time"") VALUES(400, 'title-400', '2000-01-01 00:00:00.000000')
//ON CONFLICT(""id"") DO UPDATE SET
//""time"" = '2020-01-01 00:00:00.000000'");
// Assert.Equal(1, odku1.ExecuteAffrows());
@ -167,7 +167,7 @@ ON CONFLICT(""id"") DO UPDATE SET
// new TestOnConflictDoUpdateInfo { id = 401, title = "title-401", time = DateTime.Parse("2000-01-01") },
// new TestOnConflictDoUpdateInfo { id = 402, title = "title-402", time = DateTime.Parse("2000-01-01") }
// }).NoneParameter().OnConflictDoUpdate().Set(a => a.time == dt2020);
// Assert.Equal(odku2.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo"" AS _ftb_ (""id"", ""title"", ""time"") VALUES(400, 'title-400', '2000-01-01 00:00:00.000000'), (401, 'title-401', '2000-01-01 00:00:00.000000'), (402, 'title-402', '2000-01-01 00:00:00.000000')
// Assert.Equal(odku2.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"", ""time"") VALUES(400, 'title-400', '2000-01-01 00:00:00.000000'), (401, 'title-401', '2000-01-01 00:00:00.000000'), (402, 'title-402', '2000-01-01 00:00:00.000000')
//ON CONFLICT(""id"") DO UPDATE SET
//""time"" = '2020-01-01 00:00:00.000000'");
// odku2.ExecuteAffrows();
@ -175,9 +175,9 @@ ON CONFLICT(""id"") DO UPDATE SET
// g.pgsql.Delete<TestOnConflictDoUpdateInfo>(new[] { 400, 401, 402 }).ExecuteAffrows();
// odku1 = g.pgsql.Insert(new TestOnConflictDoUpdateInfo { id = 400, title = "title-400", time = DateTime.Parse("2000-01-01") }).NoneParameter().OnConflictDoUpdate().Set(a => new { time = dt2020, title = a.title + "123" });
// Assert.Equal(odku1.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo"" AS _ftb_ (""id"", ""title"", ""time"") VALUES(400, 'title-400', '2000-01-01 00:00:00.000000')
// Assert.Equal(odku1.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"", ""time"") VALUES(400, 'title-400', '2000-01-01 00:00:00.000000')
//ON CONFLICT(""id"") DO UPDATE SET
//""time"" = '2020-01-01 00:00:00.000000', ""title"" = _ftb_.""title"" || '123'");
//""time"" = '2020-01-01 00:00:00.000000', ""title"" = ""testonconflictdoupdateinfo"".""title"" || '123'");
// Assert.Equal(1, odku1.ExecuteAffrows());
// odku2 = g.pgsql.Insert(new[] {
@ -185,9 +185,9 @@ ON CONFLICT(""id"") DO UPDATE SET
// new TestOnConflictDoUpdateInfo { id = 401, title = "title-401", time = DateTime.Parse("2000-01-01") },
// new TestOnConflictDoUpdateInfo { id = 402, title = "title-402", time = DateTime.Parse("2000-01-01") }
// }).NoneParameter().OnConflictDoUpdate().Set(a => new { time = dt2020, title = a.title + "123" });
// Assert.Equal(odku2.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo"" AS _ftb_ (""id"", ""title"", ""time"") VALUES(400, 'title-400', '2000-01-01 00:00:00.000000'), (401, 'title-401', '2000-01-01 00:00:00.000000'), (402, 'title-402', '2000-01-01 00:00:00.000000')
// Assert.Equal(odku2.ToSql(), @"INSERT INTO ""testonconflictdoupdateinfo""(""id"", ""title"", ""time"") VALUES(400, 'title-400', '2000-01-01 00:00:00.000000'), (401, 'title-401', '2000-01-01 00:00:00.000000'), (402, 'title-402', '2000-01-01 00:00:00.000000')
//ON CONFLICT(""id"") DO UPDATE SET
//""time"" = '2020-01-01 00:00:00.000000', ""title"" = _ftb_.""title"" || '123'");
//""time"" = '2020-01-01 00:00:00.000000', ""title"" = ""testonconflictdoupdateinfo"".""title"" || '123'");
// odku2.ExecuteAffrows();
}

View File

@ -89,8 +89,7 @@ namespace FreeSql.PostgreSQL.Curd
public string ToSql()
{
var sb = new StringBuilder();
var insertSql = _pgsqlInsert.ToSql();
sb.Append(insertSql).Insert(insertSql.IndexOf('('), " AS _ftb_ ").Append("\r\nON CONFLICT(");
sb.Append(_pgsqlInsert.ToSql()).Append("\r\nON CONFLICT(");
for (var a = 0; a < _columns.Length; a++)
{
if (a > 0) sb.Append(", ");
@ -117,7 +116,7 @@ namespace FreeSql.PostgreSQL.Curd
if (col.Attribute.IsVersion == true)
{
var field = _pgsqlInsert.InternalCommonUtils.QuoteSqlName(col.Attribute.Name);
sb.Append(field).Append(" = _ftb_.").Append(field).Append(" + 1");
sb.Append(field).Append(" = ").Append(_pgsqlInsert.InternalCommonUtils.QuoteSqlName(_pgsqlInsert.InternalTable.DbName)).Append(".").Append(field).Append(" + 1");
}
else if (_pgsqlInsert.InternalIgnore.ContainsKey(col.Attribute.Name))
{