diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index 9bc1e21be..afe1eb18d 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -557,7 +557,11 @@ namespace SqlSugar } return this; } - + public IInsertable PostgreSQLConflictNothing(string[] columns) + { + this.InsertBuilder.ConflictNothing = columns; + return this; + } public IInsertable MySqlIgnore() { this.InsertBuilder.MySqlIgnore = true; diff --git a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs index 955650e0a..5f0eed6c8 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs @@ -133,6 +133,7 @@ namespace SqlSugar public bool MySqlIgnore { get; internal set; } public bool IsWithAttr { get; internal set; } + public string[] ConflictNothing { get; set; } public virtual ExpressionResult GetExpressionValue(Expression expression, ResolveExpressType resolveType) { diff --git a/Src/Asp.Net/SqlSugar/Interface/Insertable.cs b/Src/Asp.Net/SqlSugar/Interface/Insertable.cs index b7791887c..28c2223ab 100644 --- a/Src/Asp.Net/SqlSugar/Interface/Insertable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/Insertable.cs @@ -64,6 +64,7 @@ namespace SqlSugar SplitInsertable SplitTable(SplitType splitType); void AddQueue(); IInsertable MySqlIgnore(); + IInsertable PostgreSQLConflictNothing(string[] columns); IInsertable OffIdentity(); IInsertable OffIdentity(bool isSetOn); InsertablePage PageSize(int pageSize); diff --git a/Src/Asp.Net/SqlSugar/Realization/PostgreSQL/SqlBuilder/PostgreSQLInsertBuilder.cs b/Src/Asp.Net/SqlSugar/Realization/PostgreSQL/SqlBuilder/PostgreSQLInsertBuilder.cs index 8a78e7101..00fc65f46 100644 --- a/Src/Asp.Net/SqlSugar/Realization/PostgreSQL/SqlBuilder/PostgreSQLInsertBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Realization/PostgreSQL/SqlBuilder/PostgreSQLInsertBuilder.cs @@ -49,7 +49,7 @@ 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); + return GetIgnoreSql(string.Format(SqlTemplate, GetTableNameString, columnsString, columnParametersString)); } else { @@ -116,7 +116,7 @@ namespace SqlSugar pageIndex++; batchInsetrSql.Remove(batchInsetrSql.Length - 1,1).Append("\r\n;\r\n"); } - return batchInsetrSql.ToString(); + return GetIgnoreSql(batchInsetrSql.ToString()); } } @@ -182,6 +182,15 @@ namespace SqlSugar { return "'" + ((DateTimeOffset)value).ToString("o") + "'"; } + + private string GetIgnoreSql(string sql) + { + if (this.ConflictNothing?.Any() == true) + { + sql = sql.Replace(";", $" ON CONFLICT ({string.Join(",", this.ConflictNothing)}) DO NOTHING;"); + } + return sql; + } } }