Support mysql ignore insert

This commit is contained in:
sunkaixuan 2023-03-10 17:55:52 +08:00
parent de0446270a
commit 329a684ae2
4 changed files with 27 additions and 4 deletions

View File

@ -473,6 +473,12 @@ namespace SqlSugar
return this;
}
public IInsertable<T> MySqlIgnore()
{
this.InsertBuilder.MySqlIgnore = true;
return this;
}
public IInsertable<T> InsertColumns(Expression<Func<T, object>> columns)
{
if (columns == null) return this;

View File

@ -129,6 +129,9 @@ namespace SqlSugar
return result;
}
}
public bool MySqlIgnore { get; internal set; }
public virtual ExpressionResult GetExpressionValue(Expression expression, ResolveExpressType resolveType)
{
ILambdaExpressions resolveExpress = this.LambdaExpressions;

View File

@ -54,6 +54,6 @@ namespace SqlSugar
SplitInsertable<T> SplitTable();
SplitInsertable<T> SplitTable(SplitType splitType);
void AddQueue();
IInsertable<T> MySqlIgnore();
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -127,7 +128,9 @@ namespace SqlSugar
{
string columnParametersString = string.Join(",", this.DbColumnInfoList.Select(it =>base.GetDbColumn(it, Builder.SqlParameterKeyWord + it.DbColumnName)));
ActionMinDate();
return string.Format(SqlTemplate, GetTableNameString, columnsString, columnParametersString);
var result= string.Format(SqlTemplate, GetTableNameString, columnsString, columnParametersString);
result = GetMySqlIgnore(result);
return result;
}
else
{
@ -140,7 +143,7 @@ namespace SqlSugar
foreach (var item in groupList)
{
batchInsetrSql.Append("(");
insertColumns = string.Join(",", item.Select(it =>base.GetDbColumn(it, FormatValue(it.Value,it.PropertyName))));
insertColumns = string.Join(",", item.Select(it => base.GetDbColumn(it, FormatValue(it.Value, it.PropertyName))));
batchInsetrSql.Append(insertColumns);
if (groupList.Last() == item)
{
@ -151,11 +154,22 @@ namespace SqlSugar
batchInsetrSql.Append("), ");
}
}
batchInsetrSql.AppendLine(";select @@IDENTITY");
var result = batchInsetrSql.ToString();
result = GetMySqlIgnore(result);
return result;
}
}
private string GetMySqlIgnore(string result)
{
if (this.MySqlIgnore)
{
result = result.Replace("INSERT INTO", " INSERT IGNORE INTO");
}
return result;
}
}
}