Skip to content

Commit

Permalink
- 增加 人大金仓 OdbcKingbaseES 实现;#325
Browse files Browse the repository at this point in the history
  • Loading branch information
28810 authored and 28810 committed May 26, 2020
1 parent 0d6ebc1 commit 7d8457a
Show file tree
Hide file tree
Showing 57 changed files with 11,687 additions and 188 deletions.
9 changes: 0 additions & 9 deletions Examples/base_entity/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ public class Products : BaseEntity<Products, int>

static void Main(string[] args)
{
var conn = new OdbcConnection("Driver={KingbaseES 8.2 ODBC Driver ANSI};Server=127.0.0.1;Port=54321;UID=USER2;PWD=123456789;database=TEST");
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandText = "insert into cc2.IDETB01(C1) values(1),(2)";
cmd.ExecuteNonQuery();
cmd.CommandText = "select C1 from cc2.\"IDETB01\" where rownum < 2";
var idw = cmd.ExecuteScalar();
conn.Close();

#region 初始化 IFreeSql
var fsql = new FreeSql.FreeSqlBuilder()
.UseAutoSyncStructure(true)
Expand Down
9 changes: 9 additions & 0 deletions FreeSql.DbContext/FreeSql.DbContext.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace FreeSql.Tests.Odbc.KingbaseES
{
public class KingbaseESDeleteTest
{

IDelete<Topic> delete => g.kingbaseES.Delete<Topic>(); //��������

[Table(Name = "tb_topic22211")]
class Topic
{
[Column(IsIdentity = true, IsPrimary = true)]
public int Id { get; set; }
public int? Clicks { get; set; }
public TestTypeInfo Type { get; set; }
public string Title { get; set; }
public DateTime CreateTime { get; set; }
}

[Fact]
public void Dywhere()
{
Assert.Null(g.kingbaseES.Delete<Topic>().ToSql());
var sql = g.kingbaseES.Delete<Topic>(new[] { 1, 2 }).ToSql();
Assert.Equal("DELETE FROM \"TB_TOPIC22211\" WHERE (\"ID\" = 1 OR \"ID\" = 2)", sql);

sql = g.kingbaseES.Delete<Topic>(new Topic { Id = 1, Title = "test" }).ToSql();
Assert.Equal("DELETE FROM \"TB_TOPIC22211\" WHERE (\"ID\" = 1)", sql);

sql = g.kingbaseES.Delete<Topic>(new[] { new Topic { Id = 1, Title = "test" }, new Topic { Id = 2, Title = "test" } }).ToSql();
Assert.Equal("DELETE FROM \"TB_TOPIC22211\" WHERE (\"ID\" = 1 OR \"ID\" = 2)", sql);

sql = g.kingbaseES.Delete<Topic>(new { id = 1 }).ToSql();
Assert.Equal("DELETE FROM \"TB_TOPIC22211\" WHERE (\"ID\" = 1)", sql);

sql = g.kingbaseES.Delete<MultiPkTopic>(new[] { new { Id1 = 1, Id2 = 10 }, new { Id1 = 2, Id2 = 20 } }).ToSql();
Assert.Equal("DELETE FROM \"MULTIPKTOPIC\" WHERE (\"ID1\" = 1 AND \"ID2\" = 10 OR \"ID1\" = 2 AND \"ID2\" = 20)", sql);
}
class MultiPkTopic
{
[Column(IsPrimary = true)]
public int Id1 { get; set; }
[Column(IsPrimary = true)]
public int Id2 { get; set; }
public int Clicks { get; set; }
public string Title { get; set; }
public DateTime CreateTime { get; set; }
}

[Fact]
public void Where()
{
var sql = delete.Where(a => a.Id == 1).ToSql().Replace("\r\n", "");
Assert.Equal("DELETE FROM \"TB_TOPIC22211\" WHERE (\"ID\" = 1)", sql);

sql = delete.Where("id = :id", new { id = 1 }).ToSql().Replace("\r\n", "");
Assert.Equal("DELETE FROM \"TB_TOPIC22211\" 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_TOPIC22211\" WHERE (\"ID\" = 1)", sql);

var items = new List<Topic>();
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_TOPIC22211\" WHERE (\"ID\" IN (1,2,3,4,5,6,7,8,9,10))", sql);
}
[Fact]
public void ExecuteAffrows()
{

var id = g.kingbaseES.Insert<Topic>(new Topic { Title = "xxxx", CreateTime = DateTime.Now }).ExecuteIdentity();
Assert.Equal(1, delete.Where(a => a.Id == id).ExecuteAffrows());
}
[Fact]
public void ExecuteDeleted()
{

//var item = g.kingbaseES.Insert<Topic>(new Topic { Title = "xxxx", CreateTime = DateTime.Now }).ExecuteInserted();
//Assert.Equal(item[0].Id, delete.Where(a => a.Id == item[0].Id).ExecuteDeleted()[0].Id);
}

[Fact]
public void AsTable()
{
Assert.Null(g.kingbaseES.Delete<Topic>().ToSql());
var sql = g.kingbaseES.Delete<Topic>(new[] { 1, 2 }).AsTable(a => "TopicAsTable").ToSql();
Assert.Equal("DELETE FROM \"TOPICASTABLE\" WHERE (\"ID\" = 1 OR \"ID\" = 2)", sql);

sql = g.kingbaseES.Delete<Topic>(new Topic { Id = 1, Title = "test" }).AsTable(a => "TopicAsTable").ToSql();
Assert.Equal("DELETE FROM \"TOPICASTABLE\" WHERE (\"ID\" = 1)", sql);

sql = g.kingbaseES.Delete<Topic>(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.kingbaseES.Delete<Topic>(new { id = 1 }).AsTable(a => "TopicAsTable").ToSql();
Assert.Equal("DELETE FROM \"TOPICASTABLE\" WHERE (\"ID\" = 1)", sql);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace FreeSql.Tests.Odbc.KingbaseES
{
public class KingbaseESInsertOrUpdateTest
{
IFreeSql fsql => g.kingbaseES;

[Fact]
public void InsertOrUpdate_OnlyPrimary()
{
fsql.Delete<tbiou01>().Where("1=1").ExecuteAffrows();
var iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new tbiou01 { id = 1 });
var sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""TBIOU01""(""ID"") VALUES(1)
ON CONFLICT(""ID"") DO NOTHING", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new tbiou01 { id = 1 });
sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""TBIOU01""(""ID"") VALUES(1)
ON CONFLICT(""ID"") DO NOTHING", sql);
Assert.Equal(0, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new tbiou01 { id = 2 });
sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""TBIOU01""(""ID"") VALUES(2)
ON CONFLICT(""ID"") DO NOTHING", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new[] { new tbiou01 { id = 1 }, new tbiou01 { id = 2 }, new tbiou01 { id = 3 }, new tbiou01 { id = 4 } });
sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""TBIOU01""(""ID"") VALUES(1), (2), (3), (4)
ON CONFLICT(""ID"") DO NOTHING", sql);
Assert.Equal(2, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou01>().SetSource(new[] { new tbiou01 { id = 1 }, new tbiou01 { id = 2 }, new tbiou01 { id = 3 }, new tbiou01 { id = 4 } });
sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""TBIOU01""(""ID"") VALUES(1), (2), (3), (4)
ON CONFLICT(""ID"") DO NOTHING", sql);
Assert.Equal(0, iou.ExecuteAffrows());
}
class tbiou01
{
public int id { get; set; }
}

[Fact]
public void InsertOrUpdate_OnePrimary()
{
fsql.Delete<tbiou02>().Where("1=1").ExecuteAffrows();
var iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 1, name = "01" });
var sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""TBIOU02""(""ID"", ""NAME"") VALUES(1, '01')
ON CONFLICT(""ID"") DO UPDATE SET
""NAME"" = EXCLUDED.""NAME""", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 1, name = "011" });
sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""TBIOU02""(""ID"", ""NAME"") VALUES(1, '011')
ON CONFLICT(""ID"") DO UPDATE SET
""NAME"" = EXCLUDED.""NAME""", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 2, name = "02" });
sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""TBIOU02""(""ID"", ""NAME"") VALUES(2, '02')
ON CONFLICT(""ID"") DO UPDATE SET
""NAME"" = EXCLUDED.""NAME""", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new[] { new tbiou02 { id = 1, name = "01" }, new tbiou02 { id = 2, name = "02" }, new tbiou02 { id = 3, name = "03" }, new tbiou02 { id = 4, name = "04" } });
sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""TBIOU02""(""ID"", ""NAME"") VALUES(1, '01'), (2, '02'), (3, '03'), (4, '04')
ON CONFLICT(""ID"") DO UPDATE SET
""NAME"" = EXCLUDED.""NAME""", sql);
Assert.Equal(4, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new[] { new tbiou02 { id = 1, name = "001" }, new tbiou02 { id = 2, name = "002" }, new tbiou02 { id = 3, name = "003" }, new tbiou02 { id = 4, name = "004" } });
sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""TBIOU02""(""ID"", ""NAME"") VALUES(1, '001'), (2, '002'), (3, '003'), (4, '004')
ON CONFLICT(""ID"") DO UPDATE SET
""NAME"" = EXCLUDED.""NAME""", sql);
Assert.Equal(4, iou.ExecuteAffrows());
var lst = fsql.Select<tbiou02>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count());
}
class tbiou02
{
public int id { get; set; }
public string name { get; set; }
}

[Fact]
public void InsertOrUpdate_TwoPrimary()
{
fsql.Delete<tbiou03>().Where("1=1").ExecuteAffrows();
var iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 1, id2 = "01", name = "01" });
var sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""TBIOU03""(""ID1"", ""ID2"", ""NAME"") VALUES(1, '01', '01')
ON CONFLICT(""ID1"", ""ID2"") DO UPDATE SET
""NAME"" = EXCLUDED.""NAME""", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 1, id2 = "02", name = "011" });
sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""TBIOU03""(""ID1"", ""ID2"", ""NAME"") VALUES(1, '02', '011')
ON CONFLICT(""ID1"", ""ID2"") DO UPDATE SET
""NAME"" = EXCLUDED.""NAME""", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new tbiou03 { id1 = 2, id2 = "02", name = "02" });
sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""TBIOU03""(""ID1"", ""ID2"", ""NAME"") VALUES(2, '02', '02')
ON CONFLICT(""ID1"", ""ID2"") DO UPDATE SET
""NAME"" = EXCLUDED.""NAME""", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new[] { new tbiou03 { id1 = 1, id2 = "01", name = "01" }, new tbiou03 { id1 = 2, id2 = "02", name = "02" }, new tbiou03 { id1 = 3, id2 = "03", name = "03" }, new tbiou03 { id1 = 4, id2 = "04", name = "04" } });
sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""TBIOU03""(""ID1"", ""ID2"", ""NAME"") VALUES(1, '01', '01'), (2, '02', '02'), (3, '03', '03'), (4, '04', '04')
ON CONFLICT(""ID1"", ""ID2"") DO UPDATE SET
""NAME"" = EXCLUDED.""NAME""", sql);
Assert.Equal(4, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou03>().SetSource(new[] { new tbiou03 { id1 = 1, id2 = "01", name = "001" }, new tbiou03 { id1 = 2, id2 = "02", name = "002" }, new tbiou03 { id1 = 3, id2 = "03", name = "003" }, new tbiou03 { id1 = 4, id2 = "04", name = "004" } });
sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""TBIOU03""(""ID1"", ""ID2"", ""NAME"") VALUES(1, '01', '001'), (2, '02', '002'), (3, '03', '003'), (4, '04', '004')
ON CONFLICT(""ID1"", ""ID2"") DO UPDATE SET
""NAME"" = EXCLUDED.""NAME""", sql);
Assert.Equal(4, iou.ExecuteAffrows());
var lst = fsql.Select<tbiou03>().Where(a => a.id1 == 1 && a.id2 == "01" || a.id1 == 2 && a.id2 == "02" || a.id1 == 3 && a.id2 == "03" || a.id1 == 4 && a.id2 == "04").ToList();
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id1).Count());
}
class tbiou03
{
[Column(IsPrimary = true)]
public int id1 { get; set; }
[Column(IsPrimary = true)]
public string id2 { get; set; }
public string name { get; set; }
}

[Fact]
public void InsertOrUpdate_OnePrimaryAndVersionAndCanUpdate()
{
fsql.Delete<tbiou04>().Where("1=1").ExecuteAffrows();
var iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 1, name = "01" });
var sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""TBIOU04""(""ID"", ""NAME"", ""VERSION"", ""CREATETIME"") VALUES(1, '01', 0, current_timestamp)
ON CONFLICT(""ID"") DO UPDATE SET
""NAME"" = EXCLUDED.""NAME"",
""VERSION"" = ""TBIOU04"".""VERSION"" + 1", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 1, name = "011" });
sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""TBIOU04""(""ID"", ""NAME"", ""VERSION"", ""CREATETIME"") VALUES(1, '011', 0, current_timestamp)
ON CONFLICT(""ID"") DO UPDATE SET
""NAME"" = EXCLUDED.""NAME"",
""VERSION"" = ""TBIOU04"".""VERSION"" + 1", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new tbiou04 { id = 2, name = "02" });
sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""TBIOU04""(""ID"", ""NAME"", ""VERSION"", ""CREATETIME"") VALUES(2, '02', 0, current_timestamp)
ON CONFLICT(""ID"") DO UPDATE SET
""NAME"" = EXCLUDED.""NAME"",
""VERSION"" = ""TBIOU04"".""VERSION"" + 1", sql);
Assert.Equal(1, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new[] { new tbiou04 { id = 1, name = "01" }, new tbiou04 { id = 2, name = "02" }, new tbiou04 { id = 3, name = "03" }, new tbiou04 { id = 4, name = "04" } });
sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""TBIOU04""(""ID"", ""NAME"", ""VERSION"", ""CREATETIME"") VALUES(1, '01', 0, current_timestamp), (2, '02', 0, current_timestamp), (3, '03', 0, current_timestamp), (4, '04', 0, current_timestamp)
ON CONFLICT(""ID"") DO UPDATE SET
""NAME"" = EXCLUDED.""NAME"",
""VERSION"" = ""TBIOU04"".""VERSION"" + 1", sql);
Assert.Equal(4, iou.ExecuteAffrows());

iou = fsql.InsertOrUpdate<tbiou04>().SetSource(new[] { new tbiou04 { id = 1, name = "001" }, new tbiou04 { id = 2, name = "002" }, new tbiou04 { id = 3, name = "003" }, new tbiou04 { id = 4, name = "004" } });
sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""TBIOU04""(""ID"", ""NAME"", ""VERSION"", ""CREATETIME"") VALUES(1, '001', 0, current_timestamp), (2, '002', 0, current_timestamp), (3, '003', 0, current_timestamp), (4, '004', 0, current_timestamp)
ON CONFLICT(""ID"") DO UPDATE SET
""NAME"" = EXCLUDED.""NAME"",
""VERSION"" = ""TBIOU04"".""VERSION"" + 1", sql);
Assert.Equal(4, iou.ExecuteAffrows());
var lst = fsql.Select<tbiou04>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count());
}
class tbiou04
{
public int id { get; set; }
public string name { get; set; }
[Column(IsVersion = true)]
public int version { get; set; }
[Column(CanUpdate = false, ServerTime = DateTimeKind.Local)]
public DateTime CreateTime { get; set; }
}
}
}
Loading

0 comments on commit 7d8457a

Please sign in to comment.