From 6c1087b043f8638204dda23e09d90e632d3c8b81 Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Fri, 27 Oct 2023 10:35:33 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BF=AE=E5=A4=8D=20Xugu=20=E4=B8=8D?= =?UTF-8?q?=E6=94=AF=E6=8C=81=20Enum=20=E6=98=A0=E5=B0=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Oracle/CustomOracleCodeFirst.cs | 4 +- .../DamengCodeFirst.cs | 4 +- .../Dameng/OdbcDamengCodeFirst.cs | 4 +- .../Oracle/OdbcOracleCodeFirst.cs | 4 +- .../OracleCodeFirst.cs | 4 +- .../FreeSql.Provider.Xugu.csproj | 2 +- .../FreeSql.Provider.Xugu/XuguCodeFirst.cs | 71 ++++++++++++------- 7 files changed, 57 insertions(+), 36 deletions(-) diff --git a/Providers/FreeSql.Provider.Custom/Oracle/CustomOracleCodeFirst.cs b/Providers/FreeSql.Provider.Custom/Oracle/CustomOracleCodeFirst.cs index fc03a10c..4877de4b 100644 --- a/Providers/FreeSql.Provider.Custom/Oracle/CustomOracleCodeFirst.cs +++ b/Providers/FreeSql.Provider.Custom/Oracle/CustomOracleCodeFirst.cs @@ -59,8 +59,8 @@ namespace FreeSql.Custom.Oracle if (enumType != null) { var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ? - CsToDb.New(DbType.Int32, "number", $"number(16){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()) : - CsToDb.New(DbType.Int64, "number", $"number(32){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()); + CsToDb.New(DbType.Int32, "number", $"number(32){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()) : + CsToDb.New(DbType.Int64, "number", $"number(16){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()); if (_dicCsToDb.ContainsKey(type.FullName) == false) { lock (_dicCsToDbLock) diff --git a/Providers/FreeSql.Provider.Dameng/DamengCodeFirst.cs b/Providers/FreeSql.Provider.Dameng/DamengCodeFirst.cs index 0724ba61..2ac88b6e 100644 --- a/Providers/FreeSql.Provider.Dameng/DamengCodeFirst.cs +++ b/Providers/FreeSql.Provider.Dameng/DamengCodeFirst.cs @@ -60,8 +60,8 @@ namespace FreeSql.Dameng if (enumType != null) { var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ? - CsToDb.New(DmDbType.Int32, "number", $"number(16){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()) : - CsToDb.New(DmDbType.Int64, "number", $"number(32){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()); + CsToDb.New(DmDbType.Int32, "number", $"number(32){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()) : + CsToDb.New(DmDbType.Int64, "number", $"number(16){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()); if (_dicCsToDb.ContainsKey(type.FullName) == false) { lock (_dicCsToDbLock) diff --git a/Providers/FreeSql.Provider.Odbc/Dameng/OdbcDamengCodeFirst.cs b/Providers/FreeSql.Provider.Odbc/Dameng/OdbcDamengCodeFirst.cs index 2de3d900..52fb7a4d 100644 --- a/Providers/FreeSql.Provider.Odbc/Dameng/OdbcDamengCodeFirst.cs +++ b/Providers/FreeSql.Provider.Odbc/Dameng/OdbcDamengCodeFirst.cs @@ -61,8 +61,8 @@ namespace FreeSql.Odbc.Dameng if (enumType != null) { var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ? - CsToDb.New(OdbcType.Int, "number", $"number(16){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()) : - CsToDb.New(OdbcType.BigInt, "number", $"number(32){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()); + CsToDb.New(OdbcType.Int, "number", $"number(32){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()) : + CsToDb.New(OdbcType.BigInt, "number", $"number(16){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()); if (_dicCsToDb.ContainsKey(type.FullName) == false) { lock (_dicCsToDbLock) diff --git a/Providers/FreeSql.Provider.Odbc/Oracle/OdbcOracleCodeFirst.cs b/Providers/FreeSql.Provider.Odbc/Oracle/OdbcOracleCodeFirst.cs index 0ce0c843..12ad0db3 100644 --- a/Providers/FreeSql.Provider.Odbc/Oracle/OdbcOracleCodeFirst.cs +++ b/Providers/FreeSql.Provider.Odbc/Oracle/OdbcOracleCodeFirst.cs @@ -61,8 +61,8 @@ namespace FreeSql.Odbc.Oracle if (enumType != null) { var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ? - CsToDb.New(OdbcType.Int, "number", $"number(16){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()) : - CsToDb.New(OdbcType.BigInt, "number", $"number(32){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()); + CsToDb.New(OdbcType.Int, "number", $"number(32){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()) : + CsToDb.New(OdbcType.BigInt, "number", $"number(16){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()); if (_dicCsToDb.ContainsKey(type.FullName) == false) { lock (_dicCsToDbLock) diff --git a/Providers/FreeSql.Provider.Oracle/OracleCodeFirst.cs b/Providers/FreeSql.Provider.Oracle/OracleCodeFirst.cs index 80f32817..358b0c19 100644 --- a/Providers/FreeSql.Provider.Oracle/OracleCodeFirst.cs +++ b/Providers/FreeSql.Provider.Oracle/OracleCodeFirst.cs @@ -96,8 +96,8 @@ namespace FreeSql.Oracle if (enumType != null) { var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ? - CsToDb.New(_dicCsToDb[typeof(int)].type, "number", $"number(16){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()) : - CsToDb.New(_dicCsToDb[typeof(long)].type, "number", $"number(32){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()); + CsToDb.New(_dicCsToDb[typeof(int)].type, "number", $"number(32){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()) : + CsToDb.New(_dicCsToDb[typeof(long)].type, "number", $"number(16){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()); if (_dicCsToDb.ContainsKey(type) == false) { lock (_dicCsToDbLock) diff --git a/Providers/FreeSql.Provider.Xugu/FreeSql.Provider.Xugu.csproj b/Providers/FreeSql.Provider.Xugu/FreeSql.Provider.Xugu.csproj index 7b574332..f1243f98 100644 --- a/Providers/FreeSql.Provider.Xugu/FreeSql.Provider.Xugu.csproj +++ b/Providers/FreeSql.Provider.Xugu/FreeSql.Provider.Xugu.csproj @@ -15,7 +15,7 @@ $(AssemblyName) true true - 3.2.802-preview20231024 + 3.2.802-preview20231027 diff --git a/Providers/FreeSql.Provider.Xugu/XuguCodeFirst.cs b/Providers/FreeSql.Provider.Xugu/XuguCodeFirst.cs index 06641b8e..6f6a5e9e 100644 --- a/Providers/FreeSql.Provider.Xugu/XuguCodeFirst.cs +++ b/Providers/FreeSql.Provider.Xugu/XuguCodeFirst.cs @@ -22,39 +22,35 @@ namespace FreeSql.Xugu } static object _dicCsToDbLock = new object(); - static Dictionary> _dicCsToDb = new Dictionary>() { + static Dictionary> _dicCsToDb = new Dictionary>() { - { - typeof(byte).FullName, - CsToDb.New(XGDbType.SmallInt, "TINYINT","TINYINT NOT NULL", false, false, 0) - }, - - { typeof(byte?).FullName, CsToDb.New(XGDbType.SmallInt, "TINYINT", "TINYINT", false, true, null) }, - { typeof(short).FullName, CsToDb.New(XGDbType.SmallInt, "SMALLINT","SMALLINT NOT NULL", false, false, 0) },{ typeof(short?).FullName, CsToDb.New(XGDbType.SmallInt, "SMALLINT", "SMALLINT", false, true, null) }, - { typeof(int).FullName, CsToDb.New(XGDbType.Int, "INTEGER","INTEGER NOT NULL", false, false, 0) },{ typeof(int?).FullName, CsToDb.New(XGDbType.Int, "INTEGER", "INTEGER", false, true, null) }, - { typeof(long).FullName, CsToDb.New(XGDbType.BigInt, "BIGINT","BIGINT NOT NULL", false, false, 0) },{ typeof(long?).FullName, CsToDb.New(XGDbType.BigInt, "BIGINT", "BIGINT", false, true, null) }, + { typeof(byte), CsToDb.New(XGDbType.SmallInt, "TINYINT","TINYINT NOT NULL", false, false, 0) }, + { typeof(byte?), CsToDb.New(XGDbType.SmallInt, "TINYINT", "TINYINT", false, true, null) }, + { typeof(short), CsToDb.New(XGDbType.SmallInt, "SMALLINT","SMALLINT NOT NULL", false, false, 0) },{ typeof(short?), CsToDb.New(XGDbType.SmallInt, "SMALLINT", "SMALLINT", false, true, null) }, + { typeof(int), CsToDb.New(XGDbType.Int, "INTEGER","INTEGER NOT NULL", false, false, 0) },{ typeof(int?), CsToDb.New(XGDbType.Int, "INTEGER", "INTEGER", false, true, null) }, + { typeof(long), CsToDb.New(XGDbType.BigInt, "BIGINT","BIGINT NOT NULL", false, false, 0) },{ typeof(long?), CsToDb.New(XGDbType.BigInt, "BIGINT", "BIGINT", false, true, null) }, - { typeof(ushort).FullName, CsToDb.New(XGDbType.Int, "INT","INT NOT NULL", false, false, 0) },{ typeof(ushort?).FullName, CsToDb.New(XGDbType.Int, "INT", "INT", false, true, null) }, - { typeof(uint).FullName, CsToDb.New(XGDbType.BigInt, "BIGINT","BIGINT NOT NULL", false, false, 0) },{ typeof(uint?).FullName, CsToDb.New(XGDbType.BigInt, "BIGINT", "BIGINT", false, true, null) }, - { typeof(ulong).FullName, CsToDb.New(XGDbType.Numeric, "NUMERIC","NUMERIC(20,0) NOT NULL", false, false, 0) },{ typeof(ulong?).FullName, CsToDb.New(XGDbType.Numeric, "NUMERIC", "NUMERIC(20,0)", false, true, null) }, + { typeof(ushort), CsToDb.New(XGDbType.Int, "INT","INT NOT NULL", false, false, 0) },{ typeof(ushort?), CsToDb.New(XGDbType.Int, "INT", "INT", false, true, null) }, + { typeof(uint), CsToDb.New(XGDbType.BigInt, "BIGINT","BIGINT NOT NULL", false, false, 0) },{ typeof(uint?), CsToDb.New(XGDbType.BigInt, "BIGINT", "BIGINT", false, true, null) }, + { typeof(ulong), CsToDb.New(XGDbType.Numeric, "NUMERIC","NUMERIC(20,0) NOT NULL", false, false, 0) },{ typeof(ulong?), CsToDb.New(XGDbType.Numeric, "NUMERIC", "NUMERIC(20,0)", false, true, null) }, - { typeof(float).FullName, CsToDb.New(XGDbType.Real, "FLOAT","FLOAT NOT NULL", false, false, 0) },{ typeof(float?).FullName, CsToDb.New(XGDbType.Real, "FLOAT", "FLOAT", false, true, null) }, - { typeof(double).FullName, CsToDb.New(XGDbType.Double, "DOUBLE","DOUBLE NOT NULL", false, false, 0) },{ typeof(double?).FullName, CsToDb.New(XGDbType.Double, "DOUBLE", "DOUBLE", false, true, null) }, - { typeof(decimal).FullName, CsToDb.New(XGDbType.Numeric, "NUMERIC", "NUMERIC(10,2) NOT NULL", false, false, 0) },{ typeof(decimal?).FullName, CsToDb.New(XGDbType.Numeric, "NUMERIC", "NUMERIC(10,2)", false, true, null) }, + { typeof(float), CsToDb.New(XGDbType.Real, "FLOAT","FLOAT NOT NULL", false, false, 0) },{ typeof(float?), CsToDb.New(XGDbType.Real, "FLOAT", "FLOAT", false, true, null) }, + { typeof(double), CsToDb.New(XGDbType.Double, "DOUBLE","DOUBLE NOT NULL", false, false, 0) },{ typeof(double?), CsToDb.New(XGDbType.Double, "DOUBLE", "DOUBLE", false, true, null) }, + { typeof(decimal), CsToDb.New(XGDbType.Numeric, "NUMERIC", "NUMERIC(10,2) NOT NULL", false, false, 0) },{ typeof(decimal?), CsToDb.New(XGDbType.Numeric, "NUMERIC", "NUMERIC(10,2)", false, true, null) }, - { typeof(string).FullName, CsToDb.New(XGDbType.VarChar, "VARCHAR", "VARCHAR(255)", false, null, "") }, + { typeof(string), CsToDb.New(XGDbType.VarChar, "VARCHAR", "VARCHAR(255)", false, null, "") }, - { typeof(char).FullName, CsToDb.New(XGDbType.Char, "CHAR", "CHAR(1)", false, null, '\0') }, + { typeof(char), CsToDb.New(XGDbType.Char, "CHAR", "CHAR(1)", false, null, '\0') }, - //{ typeof(TimeSpan).FullName, CsToDb.New(XGDbType.Time, "time","time NOT NULL", false, false, 0) },{ typeof(TimeSpan?).FullName, CsToDb.New(XGDbType.Time, "time", "time",false, true, null) }, - { typeof(DateTime).FullName, CsToDb.New(XGDbType.DateTime, "DATETIME", "DATETIME NOT NULL", false, false, new DateTime(1970,1,1)) }, - { typeof(DateTime?).FullName, CsToDb.New(XGDbType.DateTime, "DATETIME", "DATETIME", false, true, null) }, + //{ typeof(TimeSpan), CsToDb.New(XGDbType.Time, "time","time NOT NULL", false, false, 0) },{ typeof(TimeSpan?), CsToDb.New(XGDbType.Time, "time", "time",false, true, null) }, + { typeof(DateTime), CsToDb.New(XGDbType.DateTime, "DATETIME", "DATETIME NOT NULL", false, false, new DateTime(1970,1,1)) }, + { typeof(DateTime?), CsToDb.New(XGDbType.DateTime, "DATETIME", "DATETIME", false, true, null) }, - { typeof(bool).FullName, CsToDb.New(XGDbType.Bool, "BOOLEAN","BOOLEAN NOT NULL", null, false, false) },{ typeof(bool?).FullName, CsToDb.New(XGDbType.Bool, "BOOLEAN","BOOLEAN", null, true, null) }, + { typeof(bool), CsToDb.New(XGDbType.Bool, "BOOLEAN","BOOLEAN NOT NULL", null, false, false) },{ typeof(bool?), CsToDb.New(XGDbType.Bool, "BOOLEAN","BOOLEAN", null, true, null) }, - { typeof(byte[]).FullName, CsToDb.New(XGDbType.VarBinary, "blob", "blob NULL", false, null, new byte[0]) }, - { typeof(Guid).FullName, CsToDb.New(XGDbType.Char, "char", "char(36) NOT NULL", false, false, Guid.Empty) },{ typeof(Guid?).FullName, CsToDb.New(XGDbType.Char, "char", "char(36) NULL", false, true, null) }, + { typeof(byte[]), CsToDb.New(XGDbType.VarBinary, "blob", "blob NULL", false, null, new byte[0]) }, + { typeof(Guid), CsToDb.New(XGDbType.Char, "char", "char(36) NOT NULL", false, false, Guid.Empty) },{ typeof(Guid?), CsToDb.New(XGDbType.Char, "char", "char(36) NULL", false, true, null) }, @@ -65,9 +61,34 @@ namespace FreeSql.Xugu public override DbInfoResult GetDbInfo(Type type) { - _dicCsToDb.TryGetValue(type.FullName, out var info); + _dicCsToDb.TryGetValue(type, out var info); if (info == null) return null; return new DbInfoResult((int)info.type, info.dbtype, info.dbtypeFull, info.isnullable, info.defaultValue); + + if (_dicCsToDb.TryGetValue(type, out var trydc)) return new DbInfoResult((int)trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable, trydc.defaultValue); + if (type.IsArray) return null; + var enumType = type.IsEnum ? type : null; + if (enumType == null && type.IsNullableType()) + { + var genericTypes = type.GetGenericArguments(); + if (genericTypes.Length == 1 && genericTypes.First().IsEnum) enumType = genericTypes.First(); + } + if (enumType != null) + { + var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ? + CsToDb.New(_dicCsToDb[typeof(int)].type, "INT", $"INT{(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()) : + CsToDb.New(_dicCsToDb[typeof(long)].type, "BIGINT", $"BIGINT{(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, enumType.CreateInstanceGetDefaultValue()); + if (_dicCsToDb.ContainsKey(type) == false) + { + lock (_dicCsToDbLock) + { + if (_dicCsToDb.ContainsKey(type) == false) + _dicCsToDb.Add(type, newItem); + } + } + return new DbInfoResult((int)newItem.type, newItem.dbtype, newItem.dbtypeFull, newItem.isnullable, newItem.defaultValue); + } + return null; } internal static string GetXuguSqlTypeFullName(object[] row) {