Skip to content

Commit b338efc

Browse files
2881028810
authored andcommitted
- 增加 MySql 特有功能 Insert Ignore Into;
1 parent f7474c6 commit b338efc

File tree

9 files changed

+84
-189
lines changed

9 files changed

+84
-189
lines changed

Examples/base_entity/Program.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,41 @@ public class S_SysConfig<T> : BaseEntity<S_SysConfig<T>>
2626
public T Config { get; set; }
2727
}
2828

29+
public class Products : BaseEntity<Products, int>
30+
{
31+
public string title { get; set; }
32+
}
33+
2934
static void Main(string[] args)
3035
{
3136
#region 初始化 IFreeSql
32-
BaseEntity.Initialization(new FreeSql.FreeSqlBuilder()
37+
var fsql = new FreeSql.FreeSqlBuilder()
3338
.UseAutoSyncStructure(true)
3439
.UseNoneCommandParameter(true)
3540
.UseConnectionString(FreeSql.DataType.Sqlite, "data source=test.db;max pool size=5")
36-
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=2")
37-
.Build());
41+
//.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=2")
42+
.Build();
43+
BaseEntity.Initialization(fsql);
3844
#endregion
3945

46+
new Products { title = "product-1" }.Save();
47+
new Products { title = "product-2" }.Save();
48+
new Products { title = "product-3" }.Save();
49+
new Products { title = "product-4" }.Save();
50+
new Products { title = "product-5" }.Save();
51+
52+
var items1 = Products.Select.Limit(10).OrderByDescending(a => a.CreateTime).ToList();
53+
var items2 = fsql.Select<Products>().Limit(10).OrderByDescending(a => a.CreateTime).ToList();
54+
4055
BaseEntity.Orm.UseJsonMap();
4156

4257
new S_SysConfig<TestConfig> { Name = "testkey11", Config = new TestConfig { clicks = 11, title = "testtitle11" } }.Save();
4358
new S_SysConfig<TestConfig> { Name = "testkey22", Config = new TestConfig { clicks = 22, title = "testtitle22" } }.Save();
4459
new S_SysConfig<TestConfig> { Name = "testkey33", Config = new TestConfig { clicks = 33, title = "testtitle33" } }.Save();
4560
var testconfigs11 = S_SysConfig<TestConfig>.Select.ToList();
4661

62+
var repo = BaseEntity.Orm.Select<TestConfig>().Limit(10).ToList();
63+
4764
Task.Run(async () =>
4865
{
4966
using (var uow = BaseEntity.Begin())
@@ -111,6 +128,8 @@ static void Main(string[] args)
111128

112129
}).Wait();
113130

131+
132+
114133
Console.WriteLine("按任意键结束。。。");
115134
Console.ReadKey();
116135
}

FreeSql.DbContext/FreeSql.DbContext.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql.Tests/FreeSql.Tests.Provider.Odbc/g.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class g
1818

1919
static Lazy<IFreeSql> sqlserverLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
2020
.UseConnectionString(FreeSql.DataType.OdbcSqlServer, "Driver={SQL Server};Server=.;Persist Security Info=False;Trusted_Connection=Yes;Integrated Security=True;DATABASE=freesqlTest_odbc;Pooling=true;Max pool size=3")
21+
//.UseConnectionString(FreeSql.DataType.OdbcSqlServer, "Driver={SQL Server};Server=192.168.164.129;Persist Security Info=False;Trusted_Connection=Yes;UID=sa;PWD=123456;DATABASE=ds_shop;")
2122
.UseAutoSyncStructure(true)
2223
.UseMonitorCommand(
2324
cmd => Trace.WriteLine(cmd.CommandText), //监听SQL命令对象,在执行前

FreeSql.Tests/FreeSql.Tests/MySql/Curd/MySqlInsertTest.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,29 @@ public void ExecuteInserted()
129129
//insert.AppendData(items.First()).ExecuteInserted();
130130
}
131131

132+
[Fact]
133+
public void MySqlIgnoreInto()
134+
{
135+
var items = new List<Topic>();
136+
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100, CreateTime = DateTime.Now });
137+
138+
Assert.Equal(1, insert.MySqlIgnoreInto().AppendData(items.First()).ExecuteAffrows());
139+
Assert.Equal(10, insert.MySqlIgnoreInto().AppendData(items).ExecuteAffrows());
140+
141+
Assert.Equal(1, g.mysql.Insert<TestEnumInsertTb>().MySqlIgnoreInto().AppendData(new TestEnumInsertTb { type = TestEnumInserTbType.sum211 }).ExecuteAffrows());
142+
Assert.Equal(1, g.mysql.Insert<TestEnumInsertTb>().MySqlIgnoreInto().AppendData(new TestEnumInsertTb { type = TestEnumInserTbType.sum211 }).NoneParameter().ExecuteAffrows());
143+
144+
items = new List<Topic>();
145+
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
146+
147+
Assert.NotEqual(0, insert.MySqlIgnoreInto().AppendData(items.First()).ExecuteIdentity());
148+
149+
var id = g.mysql.Insert<TestEnumInsertTb>().MySqlIgnoreInto().AppendData(new TestEnumInsertTb { type = TestEnumInserTbType.sum211 }).ExecuteIdentity();
150+
Assert.Equal(TestEnumInserTbType.sum211, g.mysql.Select<TestEnumInsertTb>().Where(a => a.id == id).First()?.type);
151+
id = g.mysql.Insert<TestEnumInsertTb>().MySqlIgnoreInto().AppendData(new TestEnumInsertTb { type = TestEnumInserTbType.sum211 }).NoneParameter().ExecuteIdentity();
152+
Assert.Equal(TestEnumInserTbType.sum211, g.mysql.Select<TestEnumInsertTb>().Where(a => a.id == id).First()?.type);
153+
}
154+
132155
[Fact]
133156
public void AsTable()
134157
{

FreeSql.Tests/FreeSql.Tests/g.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class g
3535

3636
static Lazy<IFreeSql> sqlserverLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
3737
.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3")
38+
//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=192.168.164.129;uid=sa;pwd=123456;Initial Catalog=ds_shop;Pooling=true;Max Pool Size=3")
3839
.UseAutoSyncStructure(true)
3940
.UseMonitorCommand(
4041
cmd => Trace.WriteLine(cmd.CommandText), //监听SQL命令对象,在执行前

FreeSql/FreeSql.xml

Lines changed: 0 additions & 131 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Providers/FreeSql.Provider.MySql/Curd/MySqlInsert.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public MySqlInsert(IFreeSql orm, CommonUtils commonUtils, CommonExpression commo
1717
{
1818
}
1919

20+
internal bool InternalIsIgnoreInto = false;
2021
internal IFreeSql InternalOrm => _orm;
2122
internal TableInfo InternalTable => _table;
2223
internal DbParameter[] InternalParams => _params;
@@ -33,6 +34,13 @@ public MySqlInsert(IFreeSql orm, CommonUtils commonUtils, CommonExpression commo
3334
public override List<T1> ExecuteInserted() => base.SplitExecuteInserted(5000, 3000);
3435

3536

37+
public override string ToSql()
38+
{
39+
if (InternalIsIgnoreInto == false) return base.ToSqlValuesOrSelectUnionAll();
40+
var sql = base.ToSqlValuesOrSelectUnionAll();
41+
return $"INSERT IGNORE INTO {sql.Substring(12)}";
42+
}
43+
3644
protected override long RawExecuteIdentity()
3745
{
3846
var sql = this.ToSql();

Providers/FreeSql.Provider.MySql/MySqlExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using FreeSql;
22
using FreeSql.MySql.Curd;
3+
using System;
34

45
public static partial class FreeSqlMySqlGlobalExtensions
56
{
@@ -22,4 +23,17 @@ public static partial class FreeSqlMySqlGlobalExtensions
2223
/// <returns></returns>
2324
public static OnDuplicateKeyUpdate<T1> OnDuplicateKeyUpdate<T1>(this IInsert<T1> that) where T1 : class => new FreeSql.MySql.Curd.OnDuplicateKeyUpdate<T1>(that.InsertIdentity());
2425

26+
/// <summary>
27+
/// MySql 特有的功能,Insert Ignore Into
28+
/// </summary>
29+
/// <typeparam name="T1"></typeparam>
30+
/// <param name="that"></param>
31+
/// <returns></returns>
32+
public static IInsert<T1> MySqlIgnoreInto<T1>(this IInsert<T1> that) where T1 : class
33+
{
34+
var _mysqlInsert = that as MySqlInsert<T1>;
35+
if (_mysqlInsert == null) throw new Exception("MySqlIgnoreInto 是 FreeSql.Provider.MySql/FreeSql.Provider.MySqlConnector 特有的功能");
36+
_mysqlInsert.InternalIsIgnoreInto = true;
37+
return that;
38+
}
2539
}

0 commit comments

Comments
 (0)