From 83a9a8c2601598f806906465d46e0a8e11a6ae56 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Tue, 20 Jun 2023 15:58:08 +0800 Subject: [PATCH] Oracle 12C+ Identity --- .../CodeFirstProvider/CodeFirstProvider.cs | 4 ++ .../EntityMaintenance/EntityMaintenance.cs | 5 ++ .../InsertableProvider/InsertableHelper.cs | 3 +- .../SqlSugar/Entities/ConnMoreSettings.cs | 1 + .../DbMaintenance/OracleDbMaintenance.cs | 4 ++ .../Oracle/Insertable/OracleInsertable.cs | 68 +++++++++++++++++++ .../Oracle/SqlBuilder/OracleInsertBuilder.cs | 5 ++ Src/Asp.Net/SqlSugar/SqlSugar.nuspec | 2 +- Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs | 3 +- 9 files changed, 92 insertions(+), 3 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs index 6a5d9fe85..dac379001 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs @@ -619,6 +619,10 @@ namespace SqlSugar { return false; } + if (this.Context.CurrentConnectionConfig?.MoreSettings?.EnableOracleIdentity==true&&properyTypeName?.ToLower() == "int" && dataType?.ToLower() == "decimal") + { + return false; + } if (properyTypeName?.ToLower() == "int" && dataType?.ToLower() == "decimal"&&dc.Length==22&&dc.Scale==0&&this.Context.CurrentConnectionConfig.DbType==DbType.Oracle) { return false; diff --git a/Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs b/Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs index 97b14c92d..98ba27e92 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs @@ -386,6 +386,11 @@ namespace SqlSugar column.Length = 0; column.DecimalDigits = 0; } + if (column.OracleSequenceName.HasValue() && + this.Context.CurrentConnectionConfig?.MoreSettings?.EnableOracleIdentity == true) + { + column.OracleSequenceName = null; + } result.Columns.Add(column); } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs index 28ad0ed62..21b77b1fb 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs @@ -248,7 +248,8 @@ namespace SqlSugar InsertSql = column.InsertSql, InsertServerTime = column.InsertServerTime, DataType=column.DataType, - SqlParameterDbType= column.SqlParameterDbType + SqlParameterDbType= column.SqlParameterDbType , + IsIdentity= column.IsIdentity }; if (column.DbColumnName == null) diff --git a/Src/Asp.Net/SqlSugar/Entities/ConnMoreSettings.cs b/Src/Asp.Net/SqlSugar/Entities/ConnMoreSettings.cs index 07d2809b3..9a8fa3495 100644 --- a/Src/Asp.Net/SqlSugar/Entities/ConnMoreSettings.cs +++ b/Src/Asp.Net/SqlSugar/Entities/ConnMoreSettings.cs @@ -23,5 +23,6 @@ namespace SqlSugar public bool IsAutoUpdateQueryFilter { get; set; } public bool IsAutoDeleteQueryFilter { get; set; } public bool EnableModelFuncMappingColumn { get; set; } + public bool EnableOracleIdentity { get; set; } } } diff --git a/Src/Asp.Net/SqlSugar/Realization/Oracle/DbMaintenance/OracleDbMaintenance.cs b/Src/Asp.Net/SqlSugar/Realization/Oracle/DbMaintenance/OracleDbMaintenance.cs index 9dc707645..f55fa4e50 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Oracle/DbMaintenance/OracleDbMaintenance.cs +++ b/Src/Asp.Net/SqlSugar/Realization/Oracle/DbMaintenance/OracleDbMaintenance.cs @@ -535,6 +535,10 @@ namespace SqlSugar { item.Length = 50; } + if (item.IsIdentity && this.Context.CurrentConnectionConfig?.MoreSettings?.EnableOracleIdentity == true) + { + item.DataType = "NUMBER GENERATED ALWAYS AS IDENTITY"; + } } } string sql = GetCreateTableSql(tableName, columns); diff --git a/Src/Asp.Net/SqlSugar/Realization/Oracle/Insertable/OracleInsertable.cs b/Src/Asp.Net/SqlSugar/Realization/Oracle/Insertable/OracleInsertable.cs index 881c71f55..69683f37d 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Oracle/Insertable/OracleInsertable.cs +++ b/Src/Asp.Net/SqlSugar/Realization/Oracle/Insertable/OracleInsertable.cs @@ -28,6 +28,18 @@ namespace SqlSugar InsertBuilder.IsReturnIdentity = true; PreToSql(); string sql = InsertBuilder.ToSqlString(); + if (isIdEntityEnable()) + { + if (sql?.StartsWith("INSERT ALL")==true) + { + return this.UseParameter().ExecuteCommand(); + } + else + { + sql = sql + " RETURNING ID INTO :newId01 "; + } + InsertBuilder.Parameters.Add(new SugarParameter(":newId01", 0,true)); + } RestoreMapping(); var isDisableMasterSlaveSeparation = this.Context.Ado.IsDisableMasterSlaveSeparation; this.Context.Ado.IsDisableMasterSlaveSeparation = true; @@ -36,8 +48,16 @@ namespace SqlSugar this.Context.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation; After(sql,result); AutoEnd(oldIsAuto); + if (isIdEntityEnable()) + { + return this.InsertBuilder.Parameters.FirstOrDefault(it => it.ParameterName == ":newId01")?.Value?.ObjToInt()??0; + } return result; } + private bool isIdEntityEnable() + { + return this.Context.CurrentConnectionConfig?.MoreSettings?.EnableOracleIdentity == true; + } public override long ExecuteReturnBigIdentity() { @@ -46,6 +66,18 @@ namespace SqlSugar InsertBuilder.IsReturnIdentity = true; PreToSql(); string sql = InsertBuilder.ToSqlString(); + if (isIdEntityEnable()) + { + if (sql?.StartsWith("INSERT ALL") == true) + { + return this.UseParameter().ExecuteCommand(); + } + else + { + sql = sql + " RETURNING ID INTO :newId01 "; + } + InsertBuilder.Parameters.Add(new SugarParameter(":newId01", Convert.ToInt64(0), true)); + } RestoreMapping(); var isDisableMasterSlaveSeparation = this.Context.Ado.IsDisableMasterSlaveSeparation; this.Context.Ado.IsDisableMasterSlaveSeparation = true; @@ -54,6 +86,10 @@ namespace SqlSugar this.Context.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation; After(sql, result); AutoEnd(oldIsAuto); + if (isIdEntityEnable()) + { + return this.InsertBuilder.Parameters.FirstOrDefault(it => it.ParameterName == ":newId01")?.Value?.ObjToLong() ?? 0; + } return result; } @@ -64,6 +100,18 @@ namespace SqlSugar InsertBuilder.IsReturnIdentity = true; PreToSql(); string sql = InsertBuilder.ToSqlString(); + if (isIdEntityEnable()) + { + if (sql?.StartsWith("INSERT ALL") == true) + { + return await this.UseParameter().ExecuteCommandAsync(); + } + else + { + sql = sql + " RETURNING ID INTO :newId01 "; + } + InsertBuilder.Parameters.Add(new SugarParameter(":newId01", 0, true)); + } RestoreMapping(); var isDisableMasterSlaveSeparation = this.Context.Ado.IsDisableMasterSlaveSeparation; this.Context.Ado.IsDisableMasterSlaveSeparation = true; @@ -72,6 +120,10 @@ namespace SqlSugar this.Context.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation; After(sql, result); AutoEnd(oldIsAuto); + if (isIdEntityEnable()) + { + return this.InsertBuilder.Parameters.FirstOrDefault(it => it.ParameterName == ":newId01")?.Value?.ObjToInt() ?? 0; + } return result; } @@ -82,6 +134,18 @@ namespace SqlSugar InsertBuilder.IsReturnIdentity = true; PreToSql(); string sql = InsertBuilder.ToSqlString(); + if (isIdEntityEnable()) + { + if (sql?.StartsWith("INSERT ALL") == true) + { + return await this.UseParameter().ExecuteCommandAsync(); + } + else + { + sql = sql + " RETURNING ID INTO :newId01 "; + } + InsertBuilder.Parameters.Add(new SugarParameter(":newId01", Convert.ToInt64(0), true)); + } RestoreMapping(); var isDisableMasterSlaveSeparation = this.Context.Ado.IsDisableMasterSlaveSeparation; this.Context.Ado.IsDisableMasterSlaveSeparation = true; @@ -90,6 +154,10 @@ namespace SqlSugar this.Context.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation; After(sql, result); AutoEnd(oldIsAuto); + if (isIdEntityEnable()) + { + return this.InsertBuilder.Parameters.FirstOrDefault(it => it.ParameterName == ":newId01")?.Value?.ObjToLong() ?? 0; + } return result; } diff --git a/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleInsertBuilder.cs b/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleInsertBuilder.cs index 70a52abb1..12c509786 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleInsertBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleInsertBuilder.cs @@ -33,11 +33,16 @@ namespace SqlSugar { DbColumnInfoList = DbColumnInfoList.Where(it => it.Value != null).ToList(); } + if (this.Context.CurrentConnectionConfig?.MoreSettings?.EnableOracleIdentity == true) + { + this.DbColumnInfoList = this.DbColumnInfoList.Where(it => it.IsIdentity == false).ToList(); + } var groupList = DbColumnInfoList.GroupBy(it => it.TableId).ToList(); var isSingle = groupList.Count() == 1; string columnsString = string.Join(",", groupList.First().Select(it => Builder.GetTranslationColumnName(it.DbColumnName))); if (isSingle && this.EntityInfo.EntityName != "Dictionary`2") { + string columnParametersString = string.Join(",", this.DbColumnInfoList.Select(it => base.GetDbColumn(it, Builder.SqlParameterKeyWord + it.DbColumnName))); if (identities.HasValue()) { diff --git a/Src/Asp.Net/SqlSugar/SqlSugar.nuspec b/Src/Asp.Net/SqlSugar/SqlSugar.nuspec index 95f20f0f3..5f0a679bb 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugar.nuspec +++ b/Src/Asp.Net/SqlSugar/SqlSugar.nuspec @@ -2,7 +2,7 @@ SqlSugar - 5.1.4.84 + 5.1.4.85-preview07 .Net Framework 安装此版本, 5.0.3.3-max 最低要求 .Net Framework 4.6 | 5.0.0.2-5.0.3.2 最低要求 .Net Framework 4.5 | 4.0-4.9.11 最低要求 .Net Framework 4.0+ .NET ORM sun kaixuan landa diff --git a/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs b/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs index db5d5f72b..412ddd83d 100644 --- a/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs +++ b/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs @@ -350,7 +350,8 @@ namespace SqlSugar IsAutoToUpper=it.MoreSettings.IsAutoToUpper, IsAutoDeleteQueryFilter=it.MoreSettings.IsAutoDeleteQueryFilter, IsAutoUpdateQueryFilter = it.MoreSettings.IsAutoUpdateQueryFilter, - EnableModelFuncMappingColumn=it.MoreSettings.EnableModelFuncMappingColumn + EnableModelFuncMappingColumn=it.MoreSettings.EnableModelFuncMappingColumn, + EnableOracleIdentity = it.MoreSettings.EnableOracleIdentity }, SqlMiddle = it.SqlMiddle == null ? null : new SqlMiddle