mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
pgsql CodeFirst 适配中
This commit is contained in:
parent
4e45bb184e
commit
6f8a047149
18
FreeSql/DatabaseModel/DbEnumInfo.cs
Normal file
18
FreeSql/DatabaseModel/DbEnumInfo.cs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace FreeSql.DatabaseModel {
|
||||||
|
public class DbEnumInfo {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 枚举类型标识
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 枚举项
|
||||||
|
/// </summary>
|
||||||
|
public Dictionary<string, string> Labels { get; set; }
|
||||||
|
}
|
||||||
|
}
|
18
FreeSql/DatabaseModel/DbTypeInfo.cs
Normal file
18
FreeSql/DatabaseModel/DbTypeInfo.cs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace FreeSql.DatabaseModel {
|
||||||
|
public class DbTypeInfo {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 类型标识
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 枚举项
|
||||||
|
/// </summary>
|
||||||
|
public List<(string label, string value)> Labels { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -66,5 +66,12 @@ namespace FreeSql {
|
|||||||
/// <param name="column"></param>
|
/// <param name="column"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
string GetCsParse(DbColumnInfo column);
|
string GetCsParse(DbColumnInfo column);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取数据库枚举类型,适用 PostgreSQL
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="database"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
List<DbEnumInfo> GetEnumsByDatabase(params string[] database);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,17 +27,20 @@ namespace FreeSql.Internal {
|
|||||||
trytb.SelectFilter = tbattr?.SelectFilter;
|
trytb.SelectFilter = tbattr?.SelectFilter;
|
||||||
foreach (var p in trytb.Properties.Values) {
|
foreach (var p in trytb.Properties.Values) {
|
||||||
var tp = common.CodeFirst.GetDbInfo(p.PropertyType);
|
var tp = common.CodeFirst.GetDbInfo(p.PropertyType);
|
||||||
if (tp == null) continue;
|
//if (tp == null) continue;
|
||||||
var colattr = p.GetCustomAttributes(typeof(ColumnAttribute), false).LastOrDefault() as ColumnAttribute ?? new ColumnAttribute {
|
var colattr = p.GetCustomAttributes(typeof(ColumnAttribute), false).LastOrDefault() as ColumnAttribute;
|
||||||
Name = p.Name,
|
if (tp == null && colattr == null) continue;
|
||||||
DbType = tp.Value.dbtypeFull,
|
if (colattr == null)
|
||||||
IsIdentity = false,
|
colattr = new ColumnAttribute {
|
||||||
IsNullable = tp.Value.isnullable ?? false,
|
Name = p.Name,
|
||||||
IsPrimary = false,
|
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.Name)) colattr.Name = p.Name;
|
||||||
if (string.IsNullOrEmpty(colattr.DbType)) colattr.DbType = tp.Value.dbtypeFull;
|
if (string.IsNullOrEmpty(colattr.DbType)) colattr.DbType = tp?.dbtypeFull ?? "varchar(255)";
|
||||||
if (colattr.DbType.IndexOf("NOT NULL") == -1 && tp.Value.isnullable == false) colattr.DbType += " NOT NULL";
|
if (colattr.DbType.IndexOf("NOT NULL") == -1 && tp?.isnullable == false) colattr.DbType += " NOT NULL";
|
||||||
|
|
||||||
var col = new ColumnInfo {
|
var col = new ColumnInfo {
|
||||||
Table = trytb,
|
Table = trytb,
|
||||||
|
@ -373,5 +373,9 @@ where a.constraint_schema in ({1}) and a.table_name in ({0}) and not isnull(posi
|
|||||||
loc3.Clear();
|
loc3.Clear();
|
||||||
return loc1;
|
return loc1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<DbEnumInfo> GetEnumsByDatabase(params string[] database) {
|
||||||
|
return new List<DbEnumInfo>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,11 +1,16 @@
|
|||||||
using FreeSql.DatabaseModel;
|
using FreeSql.DatabaseModel;
|
||||||
using FreeSql.Internal;
|
using FreeSql.Internal;
|
||||||
using FreeSql.Internal.Model;
|
using FreeSql.Internal.Model;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using Npgsql.LegacyPostgis;
|
||||||
using NpgsqlTypes;
|
using NpgsqlTypes;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.NetworkInformation;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace FreeSql.PostgreSQL {
|
namespace FreeSql.PostgreSQL {
|
||||||
@ -22,58 +27,90 @@ namespace FreeSql.PostgreSQL {
|
|||||||
|
|
||||||
public bool IsAutoSyncStructure { get; set; } = true;
|
public bool IsAutoSyncStructure { get; set; } = true;
|
||||||
|
|
||||||
static readonly Dictionary<string, (NpgsqlDbType type, string dbtype, string dbtypeFull, bool? isUnsigned, bool? isnullable)> _dicCsToDb = new Dictionary<string, (NpgsqlDbType type, string dbtype, string dbtypeFull, bool? isUnsigned, bool? isnullable)>() {
|
static object _dicCsToDbLock = new object();
|
||||||
|
static Dictionary<string, (NpgsqlDbType type, string dbtype, string dbtypeFull, bool? isUnsigned, bool? isnullable)> _dicCsToDb = new Dictionary<string, (NpgsqlDbType type, string dbtype, string dbtypeFull, bool? isUnsigned, bool? isnullable)>() {
|
||||||
|
|
||||||
{ "System.Int16", (NpgsqlDbType.Smallint, "int2","int2 NOT NULL", false, false) },{ "System.Nullable`1[System.Int16]", (NpgsqlDbType.Smallint, "int2", "int2", false, true) },
|
{ typeof(sbyte).FullName, (NpgsqlDbType.Smallint, "int2","int2 NOT NULL", false, false) },{ typeof(sbyte?).FullName, (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) },
|
{ typeof(short).FullName, (NpgsqlDbType.Smallint, "int2","int2 NOT NULL", false, false) },{ typeof(short?).FullName, (NpgsqlDbType.Smallint, "int2", "int2", false, true) },
|
||||||
{ "System.Int64", (NpgsqlDbType.Bigint, "int8","int8 NOT NULL", false, false) },{ "System.Nullable`1[System.Int64]", (NpgsqlDbType.Bigint, "int8", "int8", 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) },
|
{ typeof(byte).FullName, (NpgsqlDbType.Smallint, "int2","int2 NOT NULL", false, false) },{ typeof(byte?).FullName, (NpgsqlDbType.Smallint, "int2", "int2", false, true) },
|
||||||
{ "System.Double", (NpgsqlDbType.Double, "float8","float8 NOT NULL", false, false) },{ "System.Nullable`1[System.Double]", (NpgsqlDbType.Double, "float8", "float8", false, true) },
|
{ typeof(ushort).FullName, (NpgsqlDbType.Smallint, "int2","int2 NOT NULL", false, false) },{ typeof(ushort?).FullName, (NpgsqlDbType.Smallint, "int2", "int2", 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(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) },
|
{ typeof(string).FullName, (NpgsqlDbType.Varchar, "varchar", "varchar(255)", false, null) },
|
||||||
{ "System.DateTime", (NpgsqlDbType.Timestamp, "timestamp", "timestamp NOT NULL", false, false) },{ "System.Nullable`1[System.DateTime]", (NpgsqlDbType.Timestamp, "timestamp", "timestamp", false, true) },
|
|
||||||
|
|
||||||
{ "System.Boolean", (NpgsqlDbType.Boolean, "bool","bool NOT NULL", null, false) },{ "System.Nullable`1[System.Boolean]", (NpgsqlDbType.Bit, "bool","bool", null, true) },
|
{ typeof(TimeSpan).FullName, (NpgsqlDbType.Time, "time","time NOT NULL", false, false) },{ typeof(TimeSpan?).FullName, (NpgsqlDbType.Time, "time", "time",false, true) },
|
||||||
{ "System.Byte[]", (NpgsqlDbType.Bytea, "bytea", "bytea", false, null) },
|
{ typeof(DateTime).FullName, (NpgsqlDbType.Timestamp, "timestamp", "timestamp NOT NULL", false, false) },{ typeof(DateTime?).FullName, (NpgsqlDbType.Timestamp, "timestamp", "timestamp", false, true) },
|
||||||
{ "System.BitArray", (NpgsqlDbType.Varbit, "varbit", "varbit(255)", false, null) },
|
|
||||||
|
|
||||||
{ "NpgsqlTypes.NpgsqlPoint", (NpgsqlDbType.Point, "point", "point", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlPoint]", (NpgsqlDbType.Point, "point", "point", false, true) },
|
{ typeof(bool).FullName, (NpgsqlDbType.Boolean, "bool","bool NOT NULL", null, false) },{ typeof(bool?).FullName, (NpgsqlDbType.Bit, "bool","bool", null, true) },
|
||||||
{ "NpgsqlTypes.NpgsqlLine", (NpgsqlDbType.Line, "line", "line", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlLine]", (NpgsqlDbType.Line, "line", "line", false, true) },
|
{ typeof(Byte[]).FullName, (NpgsqlDbType.Bytea, "bytea", "bytea", false, null) },
|
||||||
{ "NpgsqlTypes.NpgsqlLSeg", (NpgsqlDbType.LSeg, "lseg", "lseg", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlLSeg]", (NpgsqlDbType.LSeg, "lseg", "lseg", false, true) },
|
{ typeof(BitArray).FullName, (NpgsqlDbType.Varbit, "varbit", "varbit(64)", false, null) },
|
||||||
{ "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) },
|
|
||||||
|
|
||||||
{ "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) },
|
{ typeof(NpgsqlPoint).FullName, (NpgsqlDbType.Point, "point", "point NOT NULL", false, false) },{ typeof(NpgsqlPoint?).FullName, (NpgsqlDbType.Point, "point", "point", false, true) },
|
||||||
{ "System.Net.IPAddress", (NpgsqlDbType.Inet, "inet", "inet", false, null) },
|
{ typeof(NpgsqlLine).FullName, (NpgsqlDbType.Line, "line", "line NOT NULL", false, false) },{ typeof(NpgsqlLine?).FullName, (NpgsqlDbType.Line, "line", "line", false, true) },
|
||||||
{ "System.Net.NetworkInformation.PhysicalAddress", (NpgsqlDbType.MacAddr, "macaddr", "macaddr", false, null) },
|
{ 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) },
|
{ 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) },
|
||||||
{ "Newtonsoft.Json.Linq.JObject", (NpgsqlDbType.Jsonb, "jsonb", "jsonb", false, null) },
|
{ typeof(IPAddress).FullName, (NpgsqlDbType.Inet, "inet", "inet", false, null) },
|
||||||
{ "Newtonsoft.Json.Linq.JArray", (NpgsqlDbType.Jsonb, "jsonb", "jsonb", false, null) },
|
{ typeof(PhysicalAddress).FullName, (NpgsqlDbType.MacAddr, "macaddr", "macaddr", false, null) },
|
||||||
{ "System.Guid", (NpgsqlDbType.Uuid, "uuid", "uuid", false, false) },{ "System.Nullable`1[System.Guid]", (NpgsqlDbType.Uuid, "uuid", "uuid", false, true) },
|
|
||||||
|
|
||||||
{ "NpgsqlTypes.NpgsqlRange<int>", (NpgsqlDbType.Range | NpgsqlDbType.Integer, "int4range", "int4range", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlRange<int>]", (NpgsqlDbType.Range | NpgsqlDbType.Integer, "int4range", "int4range", false, true) },
|
{ typeof(JToken).FullName, (NpgsqlDbType.Jsonb, "jsonb", "jsonb", false, null) },
|
||||||
{ "NpgsqlTypes.NpgsqlRange<long>", (NpgsqlDbType.Range | NpgsqlDbType.Bigint, "int8range", "int8range", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlRange<long>]", (NpgsqlDbType.Range | NpgsqlDbType.Bigint, "int8range", "int8range", false, true) },
|
{ typeof(JObject).FullName, (NpgsqlDbType.Jsonb, "jsonb", "jsonb", false, null) },
|
||||||
{ "NpgsqlTypes.NpgsqlRange<decimal>", (NpgsqlDbType.Range | NpgsqlDbType.Numeric, "numrange", "numrange", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlRange<decimal>]", (NpgsqlDbType.Range | NpgsqlDbType.Numeric, "numrange", "numrange", false, true) },
|
{ typeof(JArray).FullName, (NpgsqlDbType.Jsonb, "jsonb", "jsonb", false, null) },
|
||||||
{ "NpgsqlTypes.NpgsqlRange<DateTime>", (NpgsqlDbType.Range | NpgsqlDbType.Timestamp, "tsrange", "tsrange", false, false) },{ "System.Nullable`1[NpgsqlTypes.NpgsqlRange<DateTime>]", (NpgsqlDbType.Range | NpgsqlDbType.Timestamp, "tsrange", "tsrange", false, true) },
|
{ typeof(Guid).FullName, (NpgsqlDbType.Uuid, "uuid", "uuid NOT NULL", false, false) },{ typeof(Guid?).FullName, (NpgsqlDbType.Uuid, "uuid", "uuid", false, true) },
|
||||||
|
|
||||||
{ "Dictionary<string, string>", (NpgsqlDbType.Hstore, "hstore", "hstore", false, null) },
|
{ typeof(NpgsqlRange<int>).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Integer, "int4range", "int4range NOT NULL", false, false) },{ typeof(NpgsqlRange<int>?).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Integer, "int4range", "int4range", false, true) },
|
||||||
{ "Npgsql.LegacyPostgis.PostgisGeometry", (NpgsqlDbType.Geometry, "geometry", "geometry", false, null) },
|
{ typeof(NpgsqlRange<long>).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Bigint, "int8range", "int8range NOT NULL", false, false) },{ typeof(NpgsqlRange<long>?).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Bigint, "int8range", "int8range", false, true) },
|
||||||
|
{ typeof(NpgsqlRange<decimal>).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Numeric, "numrange", "numrange NOT NULL", false, false) },{ typeof(NpgsqlRange<decimal>?).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Numeric, "numrange", "numrange", false, true) },
|
||||||
|
{ typeof(NpgsqlRange<DateTime>).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Timestamp, "tsrange", "tsrange NOT NULL", false, false) },{ typeof(NpgsqlRange<DateTime>?).FullName, (NpgsqlDbType.Range | NpgsqlDbType.Timestamp, "tsrange", "tsrange", false, true) },
|
||||||
|
|
||||||
|
{ typeof(Dictionary<string, string>).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) {
|
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;
|
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 && type.FullName.StartsWith("System.Nullable`1[") && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments.First().IsEnum) enumType = type.GenericTypeArguments.First();
|
||||||
if (enumType != null) {
|
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<TEntity>() => this.GetComparisonDDLStatements(typeof(TEntity));
|
public string GetComparisonDDLStatements<TEntity>() => this.GetComparisonDDLStatements(typeof(TEntity));
|
||||||
@ -88,9 +125,9 @@ namespace FreeSql.PostgreSQL {
|
|||||||
var isRenameTable = false;
|
var isRenameTable = false;
|
||||||
var tbname = tb.DbName.Split(new[] { '.' }, 2);
|
var tbname = tb.DbName.Split(new[] { '.' }, 2);
|
||||||
if (tbname.Length == 1) tbname = new[] { "public", tbname[0] };
|
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");
|
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;
|
isRenameTable = true;
|
||||||
@ -126,8 +163,9 @@ a.attname,
|
|||||||
t.typname,
|
t.typname,
|
||||||
case when a.atttypmod > 0 and a.atttypmod < 32767 then a.atttypmod - 4 else a.attlen end len,
|
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 t.typelem = 0 then t.typname else t2.typname end,
|
||||||
case when a.attnotnull then 0 else 1 end as is_nullable,
|
case when a.attnotnull then '0' else '1' end as is_nullable,
|
||||||
e.adsrc as is_identity
|
case when e.adsrc = 1 then '1' else '0' end as is_identity,
|
||||||
|
a.attndims
|
||||||
from pg_class c
|
from pg_class c
|
||||||
inner join pg_attribute a on a.attnum > 0 and a.attrelid = c.oid
|
inner join pg_attribute a on a.attnum > 0 and a.attrelid = c.oid
|
||||||
inner join pg_type t on t.oid = a.atttypid
|
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
|
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 ns on ns.oid = c.relnamespace
|
||||||
inner join pg_namespace ns2 on ns2.oid = t.typnamespace
|
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);
|
var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql);
|
||||||
foreach (var row in ds) {
|
foreach (var row in ds) {
|
||||||
string column = string.Concat(row[0]);
|
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]));
|
long max_length = long.Parse(string.Concat(row[2]));
|
||||||
bool is_nullable = string.Concat(row[4]) == "1";
|
bool is_nullable = string.Concat(row[4]) == "1";
|
||||||
bool is_identity = string.Concat(row[5]).StartsWith(@"nextval('") && string.Concat(row[6]).EndsWith(@"_seq'::regclass)");
|
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 (addcols.TryGetValue(column, out var trycol)) {
|
||||||
if (trycol.Attribute.DbType.ToLower().StartsWith(sqlType.ToLower()) == false ||
|
if (trycol.Attribute.DbType.ToLower().StartsWith(sqlType.ToLower()) == false ||
|
||||||
|
@ -372,5 +372,25 @@ where a.constraint_schema in ({1}) and a.table_name in ({0}) and not isnull(posi
|
|||||||
loc3.Clear();
|
loc3.Clear();
|
||||||
return loc1;
|
return loc1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<DbEnumInfo> GetEnumsByDatabase(params string[] database) {
|
||||||
|
if (database == null || database.Length == 0) return new List<DbEnumInfo>();
|
||||||
|
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<string, Dictionary<string, string>>();
|
||||||
|
foreach (var dr in drs) {
|
||||||
|
if (ret.TryGetValue(dr.name, out var labels) == false) ret.Add(dr.name, labels = new Dictionary<string, string>());
|
||||||
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -410,5 +410,9 @@ use {olddatabase};
|
|||||||
}
|
}
|
||||||
return tables;
|
return tables;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<DbEnumInfo> GetEnumsByDatabase(params string[] database) {
|
||||||
|
return new List<DbEnumInfo>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user