From 6f8a047149cb630af2c09066859965718b36b187 Mon Sep 17 00:00:00 2001
From: 28810 <28810@YEXIANGQIN>
Date: Wed, 19 Dec 2018 19:34:52 +0800
Subject: [PATCH] =?UTF-8?q?pgsql=20CodeFirst=20=E9=80=82=E9=85=8D=E4=B8=AD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FreeSql/DatabaseModel/DbEnumInfo.cs | 18 ++++
FreeSql/DatabaseModel/DbTypeInfo.cs | 18 ++++
FreeSql/Interface/iDbFirst.cs | 7 ++
FreeSql/Internal/Utils.cs | 23 +++--
FreeSql/MySql/MySqlDbFirst.cs | 4 +
FreeSql/PostgreSQL/PostgreSQLCodeFirst.cs | 120 ++++++++++++++--------
FreeSql/PostgreSQL/PostgreSQLDbFirst.cs | 20 ++++
FreeSql/SqlServer/SqlServerDbFirst.cs | 4 +
8 files changed, 164 insertions(+), 50 deletions(-)
create mode 100644 FreeSql/DatabaseModel/DbEnumInfo.cs
create mode 100644 FreeSql/DatabaseModel/DbTypeInfo.cs
diff --git a/FreeSql/DatabaseModel/DbEnumInfo.cs b/FreeSql/DatabaseModel/DbEnumInfo.cs
new file mode 100644
index 00000000..bd237cf8
--- /dev/null
+++ b/FreeSql/DatabaseModel/DbEnumInfo.cs
@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace FreeSql.DatabaseModel {
+ public class DbEnumInfo {
+
+ ///
+ /// 枚举类型标识
+ ///
+ public string Name { get; set; }
+
+ ///
+ /// 枚举项
+ ///
+ public Dictionary Labels { get; set; }
+ }
+}
diff --git a/FreeSql/DatabaseModel/DbTypeInfo.cs b/FreeSql/DatabaseModel/DbTypeInfo.cs
new file mode 100644
index 00000000..2440283d
--- /dev/null
+++ b/FreeSql/DatabaseModel/DbTypeInfo.cs
@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace FreeSql.DatabaseModel {
+ public class DbTypeInfo {
+
+ ///
+ /// 类型标识
+ ///
+ public string Name { get; set; }
+
+ ///
+ /// 枚举项
+ ///
+ public List<(string label, string value)> Labels { get; set; }
+ }
+}
diff --git a/FreeSql/Interface/iDbFirst.cs b/FreeSql/Interface/iDbFirst.cs
index 96604795..ac8f0fbd 100644
--- a/FreeSql/Interface/iDbFirst.cs
+++ b/FreeSql/Interface/iDbFirst.cs
@@ -66,5 +66,12 @@ namespace FreeSql {
///
///
string GetCsParse(DbColumnInfo column);
+
+ ///
+ /// 获取数据库枚举类型,适用 PostgreSQL
+ ///
+ ///
+ ///
+ List GetEnumsByDatabase(params string[] database);
}
}
diff --git a/FreeSql/Internal/Utils.cs b/FreeSql/Internal/Utils.cs
index 76c46087..e8fb0fa7 100644
--- a/FreeSql/Internal/Utils.cs
+++ b/FreeSql/Internal/Utils.cs
@@ -27,17 +27,20 @@ namespace FreeSql.Internal {
trytb.SelectFilter = tbattr?.SelectFilter;
foreach (var p in trytb.Properties.Values) {
var tp = common.CodeFirst.GetDbInfo(p.PropertyType);
- if (tp == null) continue;
- var colattr = p.GetCustomAttributes(typeof(ColumnAttribute), false).LastOrDefault() as ColumnAttribute ?? new ColumnAttribute {
- Name = p.Name,
- DbType = tp.Value.dbtypeFull,
- IsIdentity = false,
- IsNullable = tp.Value.isnullable ?? false,
- IsPrimary = false,
- };
+ //if (tp == null) continue;
+ var colattr = p.GetCustomAttributes(typeof(ColumnAttribute), false).LastOrDefault() as ColumnAttribute;
+ if (tp == null && colattr == null) continue;
+ if (colattr == null)
+ colattr = new ColumnAttribute {
+ Name = p.Name,
+ DbType = tp.Value.dbtypeFull,
+ IsIdentity = false,
+ IsNullable = tp.Value.isnullable ?? false,
+ IsPrimary = false,
+ };
if (string.IsNullOrEmpty(colattr.Name)) colattr.Name = p.Name;
- if (string.IsNullOrEmpty(colattr.DbType)) colattr.DbType = tp.Value.dbtypeFull;
- if (colattr.DbType.IndexOf("NOT NULL") == -1 && tp.Value.isnullable == false) colattr.DbType += " NOT NULL";
+ if (string.IsNullOrEmpty(colattr.DbType)) colattr.DbType = tp?.dbtypeFull ?? "varchar(255)";
+ if (colattr.DbType.IndexOf("NOT NULL") == -1 && tp?.isnullable == false) colattr.DbType += " NOT NULL";
var col = new ColumnInfo {
Table = trytb,
diff --git a/FreeSql/MySql/MySqlDbFirst.cs b/FreeSql/MySql/MySqlDbFirst.cs
index 455e27ae..5221bed1 100644
--- a/FreeSql/MySql/MySqlDbFirst.cs
+++ b/FreeSql/MySql/MySqlDbFirst.cs
@@ -373,5 +373,9 @@ where a.constraint_schema in ({1}) and a.table_name in ({0}) and not isnull(posi
loc3.Clear();
return loc1;
}
+
+ public List GetEnumsByDatabase(params string[] database) {
+ return new List();
+ }
}
}
\ No newline at end of file
diff --git a/FreeSql/PostgreSQL/PostgreSQLCodeFirst.cs b/FreeSql/PostgreSQL/PostgreSQLCodeFirst.cs
index 668979e6..2d4991ce 100644
--- a/FreeSql/PostgreSQL/PostgreSQLCodeFirst.cs
+++ b/FreeSql/PostgreSQL/PostgreSQLCodeFirst.cs
@@ -1,11 +1,16 @@
using FreeSql.DatabaseModel;
using FreeSql.Internal;
using FreeSql.Internal.Model;
+using Newtonsoft.Json.Linq;
+using Npgsql.LegacyPostgis;
using NpgsqlTypes;
using System;
+using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
+using System.Net;
+using System.Net.NetworkInformation;
using System.Text;
namespace FreeSql.PostgreSQL {
@@ -22,58 +27,90 @@ namespace FreeSql.PostgreSQL {
public bool IsAutoSyncStructure { get; set; } = true;
- static readonly Dictionary _dicCsToDb = new Dictionary() {
+ static object _dicCsToDbLock = new object();
+ static Dictionary _dicCsToDb = new Dictionary() {
- { "System.Int16", (NpgsqlDbType.Smallint, "int2","int2 NOT NULL", false, false) },{ "System.Nullable`1[System.Int16]", (NpgsqlDbType.Smallint, "int2", "int2", false, true) },
- { "System.Int32", (NpgsqlDbType.Integer, "int4","int4 NOT NULL", false, false) },{ "System.Nullable`1[System.Int32]", (NpgsqlDbType.Integer, "int4", "int4", false, true) },
- { "System.Int64", (NpgsqlDbType.Bigint, "int8","int8 NOT NULL", false, false) },{ "System.Nullable`1[System.Int64]", (NpgsqlDbType.Bigint, "int8", "int8", false, true) },
+ { typeof(sbyte).FullName, (NpgsqlDbType.Smallint, "int2","int2 NOT NULL", false, false) },{ typeof(sbyte?).FullName, (NpgsqlDbType.Smallint, "int2", "int2", false, true) },
+ { typeof(short).FullName, (NpgsqlDbType.Smallint, "int2","int2 NOT NULL", false, false) },{ typeof(short?).FullName, (NpgsqlDbType.Smallint, "int2", "int2", false, true) },
+ { typeof(int).FullName, (NpgsqlDbType.Integer, "int4","int4 NOT NULL", false, false) },{ typeof(int?).FullName, (NpgsqlDbType.Integer, "int4", "int4", false, true) },
+ { typeof(long).FullName, (NpgsqlDbType.Bigint, "int8","int8 NOT NULL", false, false) },{ typeof(long?).FullName, (NpgsqlDbType.Bigint, "int8", "int8", false, true) },
- { "System.Single", (NpgsqlDbType.Real, "float4","float4 NOT NULL", false, false) },{ "System.Nullable`1[System.Single]", (NpgsqlDbType.Real, "float4", "float4", false, true) },
- { "System.Double", (NpgsqlDbType.Double, "float8","float8 NOT NULL", false, false) },{ "System.Nullable`1[System.Double]", (NpgsqlDbType.Double, "float8", "float8", false, true) },
- { "System.Decimal", (NpgsqlDbType.Numeric, "numeric", "numeric(10,2) NOT NULL", false, false) },{ "System.Nullable`1[System.Decimal]", (NpgsqlDbType.Numeric, "numeric", "numeric(10,2)", false, true) },
+ { typeof(byte).FullName, (NpgsqlDbType.Smallint, "int2","int2 NOT NULL", false, false) },{ typeof(byte?).FullName, (NpgsqlDbType.Smallint, "int2", "int2", false, true) },
+ { typeof(ushort).FullName, (NpgsqlDbType.Smallint, "int2","int2 NOT NULL", false, false) },{ typeof(ushort?).FullName, (NpgsqlDbType.Smallint, "int2", "int2", false, true) },
+ { typeof(uint).FullName, (NpgsqlDbType.Integer, "int4","int4 NOT NULL", false, false) },{ typeof(uint?).FullName, (NpgsqlDbType.Integer, "int4", "int4", false, true) },
+ { typeof(ulong).FullName, (NpgsqlDbType.Bigint, "int8","int8 NOT NULL", false, false) },{ typeof(ulong?).FullName, (NpgsqlDbType.Bigint, "int8", "int8", false, true) },
- { "System.String", (NpgsqlDbType.Varchar, "varchar", "varchar(255)", false, null) },
+ { typeof(float).FullName, (NpgsqlDbType.Real, "float4","float4 NOT NULL", false, false) },{ typeof(float?).FullName, (NpgsqlDbType.Real, "float4", "float4", false, true) },
+ { typeof(double).FullName, (NpgsqlDbType.Double, "float8","float8 NOT NULL", false, false) },{ typeof(double?).FullName, (NpgsqlDbType.Double, "float8", "float8", false, true) },
+ { typeof(decimal).FullName, (NpgsqlDbType.Numeric, "numeric", "numeric(10,2) NOT NULL", false, false) },{ typeof(decimal?).FullName, (NpgsqlDbType.Numeric, "numeric", "numeric(10,2)", false, true) },
- { "System.TimeSpan", (NpgsqlDbType.Time, "time","time NOT NULL", false, false) },{ "System.Nullable`1[System.TimeSpan]", (NpgsqlDbType.Time, "time", "time",false, true) },
- { "System.DateTime", (NpgsqlDbType.Timestamp, "timestamp", "timestamp NOT NULL", false, false) },{ "System.Nullable`1[System.DateTime]", (NpgsqlDbType.Timestamp, "timestamp", "timestamp", false, true) },
+ { typeof(string).FullName, (NpgsqlDbType.Varchar, "varchar", "varchar(255)", false, null) },
- { "System.Boolean", (NpgsqlDbType.Boolean, "bool","bool NOT NULL", null, false) },{ "System.Nullable`1[System.Boolean]", (NpgsqlDbType.Bit, "bool","bool", null, true) },
- { "System.Byte[]", (NpgsqlDbType.Bytea, "bytea", "bytea", false, null) },
- { "System.BitArray", (NpgsqlDbType.Varbit, "varbit", "varbit(255)", false, null) },
+ { typeof(TimeSpan).FullName, (NpgsqlDbType.Time, "time","time NOT NULL", false, false) },{ typeof(TimeSpan?).FullName, (NpgsqlDbType.Time, "time", "time",false, true) },
+ { typeof(DateTime).FullName, (NpgsqlDbType.Timestamp, "timestamp", "timestamp NOT NULL", false, false) },{ typeof(DateTime?).FullName, (NpgsqlDbType.Timestamp, "timestamp", "timestamp", false, true) },
- { "NpgsqlTypes.NpgsqlPoint", (NpgsqlDbType.Point, "point", "point", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlPoint]", (NpgsqlDbType.Point, "point", "point", false, true) },
- { "NpgsqlTypes.NpgsqlLine", (NpgsqlDbType.Line, "line", "line", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlLine]", (NpgsqlDbType.Line, "line", "line", false, true) },
- { "NpgsqlTypes.NpgsqlLSeg", (NpgsqlDbType.LSeg, "lseg", "lseg", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlLSeg]", (NpgsqlDbType.LSeg, "lseg", "lseg", false, true) },
- { "NpgsqlTypes.NpgsqlBox", (NpgsqlDbType.Box, "box", "box", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlBox]", (NpgsqlDbType.Box, "box", "box", false, true) },
- { "NpgsqlTypes.NpgsqlPath", (NpgsqlDbType.Path, "path", "path", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlPath]", (NpgsqlDbType.Path, "path", "path", false, true) },
- { "NpgsqlTypes.NpgsqlPolygon", (NpgsqlDbType.Polygon, "polygon", "polygon", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlPolygon]", (NpgsqlDbType.Polygon, "polygon", "polygon", false, true) },
- { "NpgsqlTypes.NpgsqlCircle", (NpgsqlDbType.Circle, "circle", "circle", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlCircle]", (NpgsqlDbType.Circle, "circle", "circle", false, true) },
+ { typeof(bool).FullName, (NpgsqlDbType.Boolean, "bool","bool NOT NULL", null, false) },{ typeof(bool?).FullName, (NpgsqlDbType.Bit, "bool","bool", null, true) },
+ { typeof(Byte[]).FullName, (NpgsqlDbType.Bytea, "bytea", "bytea", false, null) },
+ { typeof(BitArray).FullName, (NpgsqlDbType.Varbit, "varbit", "varbit(64)", false, null) },
- { "System.ValueTuple`2[[System.Net.IPAddress, System.Int32]]", (NpgsqlDbType.Cidr, "cidr", "cidr", false, false) },{ "System.Nullable`1[System.ValueTuple`2[[System.Net.IPAddress, System.Int32]]]", (NpgsqlDbType.Cidr, "cidr", "cidr", false, true) },
- { "System.Net.IPAddress", (NpgsqlDbType.Inet, "inet", "inet", false, null) },
- { "System.Net.NetworkInformation.PhysicalAddress", (NpgsqlDbType.MacAddr, "macaddr", "macaddr", false, null) },
+ { typeof(NpgsqlPoint).FullName, (NpgsqlDbType.Point, "point", "point NOT NULL", false, false) },{ typeof(NpgsqlPoint?).FullName, (NpgsqlDbType.Point, "point", "point", false, true) },
+ { typeof(NpgsqlLine).FullName, (NpgsqlDbType.Line, "line", "line NOT NULL", false, false) },{ typeof(NpgsqlLine?).FullName, (NpgsqlDbType.Line, "line", "line", false, true) },
+ { typeof(NpgsqlLSeg).FullName, (NpgsqlDbType.LSeg, "lseg", "lseg NOT NULL", false, false) },{ typeof(NpgsqlLSeg?).FullName, (NpgsqlDbType.LSeg, "lseg", "lseg", false, true) },
+ { typeof(NpgsqlBox).FullName, (NpgsqlDbType.Box, "box", "box NOT NULL", false, false) },{ typeof(NpgsqlBox?).FullName, (NpgsqlDbType.Box, "box", "box", false, true) },
+ { typeof(NpgsqlPath).FullName, (NpgsqlDbType.Path, "path", "path NOT NULL", false, false) },{ typeof(NpgsqlPath?).FullName, (NpgsqlDbType.Path, "path", "path", false, true) },
+ { typeof(NpgsqlPolygon).FullName, (NpgsqlDbType.Polygon, "polygon", "polygon NOT NULL", false, false) },{ typeof(NpgsqlPolygon?).FullName, (NpgsqlDbType.Polygon, "polygon", "polygon", false, true) },
+ { typeof(NpgsqlCircle).FullName, (NpgsqlDbType.Circle, "circle", "circle NOT NULL", false, false) },{ typeof(NpgsqlCircle?).FullName, (NpgsqlDbType.Circle, "circle", "circle", false, true) },
- { "Newtonsoft.Json.Linq.JToken", (NpgsqlDbType.Jsonb, "jsonb", "jsonb", false, null) },
- { "Newtonsoft.Json.Linq.JObject", (NpgsqlDbType.Jsonb, "jsonb", "jsonb", false, null) },
- { "Newtonsoft.Json.Linq.JArray", (NpgsqlDbType.Jsonb, "jsonb", "jsonb", false, null) },
- { "System.Guid", (NpgsqlDbType.Uuid, "uuid", "uuid", false, false) },{ "System.Nullable`1[System.Guid]", (NpgsqlDbType.Uuid, "uuid", "uuid", false, true) },
+ { typeof((IPAddress Address, int Subnet)).FullName, (NpgsqlDbType.Cidr, "cidr", "cidr NOT NULL", false, false) },{ typeof((IPAddress Address, int Subnet)?).FullName, (NpgsqlDbType.Cidr, "cidr", "cidr", false, true) },
+ { typeof(IPAddress).FullName, (NpgsqlDbType.Inet, "inet", "inet", false, null) },
+ { typeof(PhysicalAddress).FullName, (NpgsqlDbType.MacAddr, "macaddr", "macaddr", false, null) },
- { "NpgsqlTypes.NpgsqlRange", (NpgsqlDbType.Range | NpgsqlDbType.Integer, "int4range", "int4range", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlRange]", (NpgsqlDbType.Range | NpgsqlDbType.Integer, "int4range", "int4range", false, true) },
- { "NpgsqlTypes.NpgsqlRange", (NpgsqlDbType.Range | NpgsqlDbType.Bigint, "int8range", "int8range", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlRange]", (NpgsqlDbType.Range | NpgsqlDbType.Bigint, "int8range", "int8range", false, true) },
- { "NpgsqlTypes.NpgsqlRange", (NpgsqlDbType.Range | NpgsqlDbType.Numeric, "numrange", "numrange", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlRange]", (NpgsqlDbType.Range | NpgsqlDbType.Numeric, "numrange", "numrange", false, true) },
- { "NpgsqlTypes.NpgsqlRange", (NpgsqlDbType.Range | NpgsqlDbType.Timestamp, "tsrange", "tsrange", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlRange]", (NpgsqlDbType.Range | NpgsqlDbType.Timestamp, "tsrange", "tsrange", false, true) },
+ { typeof(JToken).FullName, (NpgsqlDbType.Jsonb, "jsonb", "jsonb", false, null) },
+ { typeof(JObject).FullName, (NpgsqlDbType.Jsonb, "jsonb", "jsonb", false, null) },
+ { typeof(JArray).FullName, (NpgsqlDbType.Jsonb, "jsonb", "jsonb", false, null) },
+ { typeof(Guid).FullName, (NpgsqlDbType.Uuid, "uuid", "uuid NOT NULL", false, false) },{ typeof(Guid?).FullName, (NpgsqlDbType.Uuid, "uuid", "uuid", false, true) },
- { "Dictionary", (NpgsqlDbType.Hstore, "hstore", "hstore", false, null) },
- { "Npgsql.LegacyPostgis.PostgisGeometry", (NpgsqlDbType.Geometry, "geometry", "geometry", false, null) },
+ { typeof(NpgsqlRange).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Integer, "int4range", "int4range NOT NULL", false, false) },{ typeof(NpgsqlRange?).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Integer, "int4range", "int4range", false, true) },
+ { typeof(NpgsqlRange).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Bigint, "int8range", "int8range NOT NULL", false, false) },{ typeof(NpgsqlRange?).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Bigint, "int8range", "int8range", false, true) },
+ { typeof(NpgsqlRange).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Numeric, "numrange", "numrange NOT NULL", false, false) },{ typeof(NpgsqlRange?).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Numeric, "numrange", "numrange", false, true) },
+ { typeof(NpgsqlRange).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Timestamp, "tsrange", "tsrange NOT NULL", false, false) },{ typeof(NpgsqlRange?).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Timestamp, "tsrange", "tsrange", false, true) },
+
+ { typeof(Dictionary).FullName, (NpgsqlDbType.Hstore, "hstore", "hstore", false, null) },
+ { typeof(PostgisPoint).FullName, (NpgsqlDbType.Geometry, "geometry", "geometry", false, null) },
+ { typeof(PostgisLineString).FullName, (NpgsqlDbType.Geometry, "geometry", "geometry", false, null) },
+ { typeof(PostgisPolygon).FullName, (NpgsqlDbType.Geometry, "geometry", "geometry", false, null) },
+ { typeof(PostgisMultiPoint).FullName, (NpgsqlDbType.Geometry, "geometry", "geometry", false, null) },
+ { typeof(PostgisMultiLineString).FullName, (NpgsqlDbType.Geometry, "geometry", "geometry", false, null) },
+ { typeof(PostgisMultiPolygon).FullName, (NpgsqlDbType.Geometry, "geometry", "geometry", false, null) },
+ { typeof(PostgisGeometry).FullName, (NpgsqlDbType.Geometry, "geometry", "geometry", false, null) },
+ { typeof(PostgisGeometryCollection).FullName, (NpgsqlDbType.Geometry, "geometry", "geometry", false, null) },
};
public (int type, string dbtype, string dbtypeFull, bool? isnullable)? GetDbInfo(Type type) {
+ var elementType = type.IsArray ? type.GetElementType() : type;
+ var info = GetDbInfoNoneArray(elementType);
+ if (info == null) return null;
+ if (type.IsArray == false) return ((int)info.Value.type, info.Value.dbtype, info.Value.dbtypeFull, info.Value.isnullable);
+ var dbype = $"{info.Value.dbtype}[]";
+ return ((int)(info.Value.type | NpgsqlDbType.Array), dbype, info.Value.dbtypeFull.Replace(info.Value.dbtype, dbype), info.Value.isnullable);
+ }
+ (NpgsqlDbType type, string dbtype, string dbtypeFull, bool? isnullable)? GetDbInfoNoneArray(Type type) {
+ if (_dicCsToDb.TryGetValue(type.FullName, out var trydc)) return new (NpgsqlDbType, string, string, bool?)?((trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable));
var enumType = type.IsEnum ? type : null;
if (enumType == null && type.FullName.StartsWith("System.Nullable`1[") && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments.First().IsEnum) enumType = type.GenericTypeArguments.First();
if (enumType != null) {
- return ((int)NpgsqlDbType.Integer, "int4", $"int4{(type.IsEnum ? " NOT NULL" : "")}", type.IsEnum ? false : true);
+ var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ?
+ (NpgsqlDbType.Bigint, "int8", $"int8{(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true) :
+ (NpgsqlDbType.Integer, "int4", $"int4{(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true);
+ if (_dicCsToDb.ContainsKey(type.FullName) == false) {
+ lock (_dicCsToDbLock) {
+ if (_dicCsToDb.ContainsKey(type.FullName) == false)
+ _dicCsToDb.Add(type.FullName, newItem);
+ }
+ }
+ return (newItem.Item1, newItem.Item2, newItem.Item3, newItem.Item5);
}
- return _dicCsToDb.TryGetValue(type.FullName, out var trydc) ? new (int, string, string, bool?)?(((int)trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable)) : null;
+ return null;
}
public string GetComparisonDDLStatements() => this.GetComparisonDDLStatements(typeof(TEntity));
@@ -88,9 +125,9 @@ namespace FreeSql.PostgreSQL {
var isRenameTable = false;
var tbname = tb.DbName.Split(new[] { '.' }, 2);
if (tbname.Length == 1) tbname = new[] { "public", tbname[0] };
- if (_orm.Ado.ExecuteScalar(CommandType.Text, "select 1 from pg_tables a inner join pg_namespace b on b.nspname = a.schemaname where b.nspname || '.' || a.tablename = {0}.{1}".FormatMySql(tbname)) == null) { //表不存在
+ if (_orm.Ado.ExecuteScalar(CommandType.Text, "select 1 from pg_tables a inner join pg_namespace b on b.nspname = a.schemaname where b.nspname || '.' || a.tablename = {0}.{1}".FormatPostgreSQL(tbname)) == null) { //表不存在
- if (tboldname != null && _orm.Ado.ExecuteScalar(CommandType.Text, "select 1 from pg_tables a inner join pg_namespace b on b.nspname = a.schemaname where b.nspname || '.' || a.tablename = {0}.{1}".FormatMySql(tboldname)) != null) { //旧表存在
+ if (tboldname != null && _orm.Ado.ExecuteScalar(CommandType.Text, "select 1 from pg_tables a inner join pg_namespace b on b.nspname = a.schemaname where b.nspname || '.' || a.tablename = {0}.{1}".FormatPostgreSQL(tboldname)) != null) { //旧表存在
//修改表名
sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tboldname[0]}.{tboldname[1]}")).Append(" RENAME TO ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(";\r\n");
isRenameTable = true;
@@ -126,8 +163,9 @@ a.attname,
t.typname,
case when a.atttypmod > 0 and a.atttypmod < 32767 then a.atttypmod - 4 else a.attlen end len,
case when t.typelem = 0 then t.typname else t2.typname end,
-case when a.attnotnull then 0 else 1 end as is_nullable,
-e.adsrc as is_identity
+case when a.attnotnull then '0' else '1' end as is_nullable,
+case when e.adsrc = 1 then '1' else '0' end as is_identity,
+a.attndims
from pg_class c
inner join pg_attribute a on a.attnum > 0 and a.attrelid = c.oid
inner join pg_type t on t.oid = a.atttypid
@@ -136,7 +174,7 @@ left join pg_description d on d.objoid = a.attrelid and d.objsubid = a.attnum
left join pg_attrdef e on e.adrelid = a.attrelid and e.adnum = a.attnum
inner join pg_namespace ns on ns.oid = c.relnamespace
inner join pg_namespace ns2 on ns2.oid = t.typnamespace
-where ns.nspname = {0} and c.relname = {1}".FormatMySql(isRenameTable ? tboldname : tbname);
+where ns.nspname = {0} and c.relname = {1}".FormatPostgreSQL(isRenameTable ? tboldname : tbname);
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
foreach (var row in ds) {
string column = string.Concat(row[0]);
@@ -144,6 +182,8 @@ where ns.nspname = {0} and c.relname = {1}".FormatMySql(isRenameTable ? tboldnam
long max_length = long.Parse(string.Concat(row[2]));
bool is_nullable = string.Concat(row[4]) == "1";
bool is_identity = string.Concat(row[5]).StartsWith(@"nextval('") && string.Concat(row[6]).EndsWith(@"_seq'::regclass)");
+ var attndims = long.Parse(string.Concat(row[6]));
+ if (attndims > 0) sqlType += "[]";
if (addcols.TryGetValue(column, out var trycol)) {
if (trycol.Attribute.DbType.ToLower().StartsWith(sqlType.ToLower()) == false ||
diff --git a/FreeSql/PostgreSQL/PostgreSQLDbFirst.cs b/FreeSql/PostgreSQL/PostgreSQLDbFirst.cs
index 4325abf3..d48cedb5 100644
--- a/FreeSql/PostgreSQL/PostgreSQLDbFirst.cs
+++ b/FreeSql/PostgreSQL/PostgreSQLDbFirst.cs
@@ -372,5 +372,25 @@ where a.constraint_schema in ({1}) and a.table_name in ({0}) and not isnull(posi
loc3.Clear();
return loc1;
}
+
+ public List GetEnumsByDatabase(params string[] database) {
+ if (database == null || database.Length == 0) return new List();
+ var drs = _orm.Ado.Query<(string name, string label)>(CommandType.Text, @"select
+ns.nspname || '.' || a.typname,
+b.enumlabel
+from pg_type a
+inner join pg_enum b on b.enumtypid = a.oid
+inner join pg_namespace ns on ns.oid = a.typnamespace
+where a.typtype = 'e' and ns.nspname in (SELECT ""schema_name"" FROM information_schema.schemata where catalog_name in {0})".FormatPostgreSQL(database));
+ var ret = new Dictionary>();
+ foreach (var dr in drs) {
+ if (ret.TryGetValue(dr.name, out var labels) == false) ret.Add(dr.name, labels = new Dictionary());
+ var key = dr.label;
+ if (Regex.IsMatch(key, @"^[\u0391-\uFFE5a-zA-Z_\$][\u0391-\uFFE5a-zA-Z_\$\d]*$") == false)
+ key = $"Unkown{ret[dr.name].Count + 1}";
+ if (labels.ContainsKey(key) == false) labels.Add(key, dr.label);
+ }
+ return ret.Select(a => new DbEnumInfo { Name = a.Key, Labels = a.Value }).ToList();
+ }
}
}
\ No newline at end of file
diff --git a/FreeSql/SqlServer/SqlServerDbFirst.cs b/FreeSql/SqlServer/SqlServerDbFirst.cs
index 02036f2f..9b51886e 100644
--- a/FreeSql/SqlServer/SqlServerDbFirst.cs
+++ b/FreeSql/SqlServer/SqlServerDbFirst.cs
@@ -410,5 +410,9 @@ use {olddatabase};
}
return tables;
}
+
+ public List GetEnumsByDatabase(params string[] database) {
+ return new List();
+ }
}
}
\ No newline at end of file