From 615023f012a8d14db0beba1363de09fdb745dc35 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Mon, 14 Oct 2019 13:21:47 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BF=AE=E5=A4=8D=20postgresql=2012=20?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=20pg=5Fattrdef.adsrc=20=E5=88=97=EF=BC=8C?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=20CodeFirst=20=E6=96=B9=E6=B3=95=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E7=9A=84=20bug=EF=BC=9B=20-=20=E5=A2=9E=E5=8A=A0=20Ao?= =?UTF-8?q?p.ConfigEntity=20=E5=B1=9E=E6=80=A7=20ModifyIndexResult=20?= =?UTF-8?q?=E7=8E=B0=E5=AE=9E=20IndexAttribute=20=E7=9A=84=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PostgreSQL/PostgreSQLCodeFirstTest.cs | 2 +- FreeSql/FreeSql.xml | 5 ++++ FreeSql/Interface/IAop.cs | 5 ++++ FreeSql/Internal/CommonUtils.cs | 28 +++++++++++++------ .../PostgreSQL/OdbcPostgreSQLCodeFirst.cs | 5 ++-- .../PostgreSQL/OdbcPostgreSQLDbFirst.cs | 6 ++-- .../PostgreSQLCodeFirst.cs | 5 ++-- .../PostgreSQLDbFirst.cs | 6 ++-- 8 files changed, 43 insertions(+), 19 deletions(-) diff --git a/FreeSql.Tests/FreeSql.Tests/PostgreSQL/PostgreSQLCodeFirstTest.cs b/FreeSql.Tests/FreeSql.Tests/PostgreSQL/PostgreSQLCodeFirstTest.cs index d91dc319..c3dd0d2b 100644 --- a/FreeSql.Tests/FreeSql.Tests/PostgreSQL/PostgreSQLCodeFirstTest.cs +++ b/FreeSql.Tests/FreeSql.Tests/PostgreSQL/PostgreSQLCodeFirstTest.cs @@ -351,7 +351,7 @@ namespace FreeSql.Tests.PostgreSQL [Table(Name = "tb_alltype")] class TableAllType { - [Column(IsIdentity = true, IsPrimary = true)] + [Column(IsPrimary = true)] public int Id { get; set; } public bool testFieldBool { get; set; } diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index 53d85f09..6179f8fa 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -2185,6 +2185,11 @@ 实体配置 + + + 索引配置 + + 实体类型 diff --git a/FreeSql/Interface/IAop.cs b/FreeSql/Interface/IAop.cs index c1c5c3f4..ce18c3ae 100644 --- a/FreeSql/Interface/IAop.cs +++ b/FreeSql/Interface/IAop.cs @@ -115,6 +115,7 @@ namespace FreeSql.Aop { this.EntityType = entityType; this.ModifyResult = new TableAttribute(); + this.ModifyIndexResult = new List(); } /// @@ -125,6 +126,10 @@ namespace FreeSql.Aop /// 实体配置 /// public TableAttribute ModifyResult { get; } + /// + /// 索引配置 + /// + public List ModifyIndexResult { get; } } public class ConfigEntityPropertyEventArgs : EventArgs { diff --git a/FreeSql/Internal/CommonUtils.cs b/FreeSql/Internal/CommonUtils.cs index 7c9ed240..c19b4c28 100644 --- a/FreeSql/Internal/CommonUtils.cs +++ b/FreeSql/Internal/CommonUtils.cs @@ -87,7 +87,6 @@ namespace FreeSql.Internal if (!string.IsNullOrEmpty(trytb.OldName)) attr.OldName = trytb.OldName; if (!string.IsNullOrEmpty(trytb.SelectFilter)) attr.SelectFilter = trytb.SelectFilter; if (trytb._DisableSyncStructure != null) attr._DisableSyncStructure = trytb.DisableSyncStructure; - } var attrs = type.GetCustomAttributes(typeof(TableAttribute), false); foreach (var tryattrobj in attrs) @@ -190,24 +189,37 @@ namespace FreeSql.Internal } public IndexAttribute[] GetEntityIndexAttribute(Type type) { - var ret = new Dictionary(); ; + var ret = new Dictionary(); + if (_orm.Aop.ConfigEntity != null) + { + var aope = new Aop.ConfigEntityEventArgs(type); + _orm.Aop.ConfigEntity(_orm, aope); + foreach (var idxattr in aope.ModifyIndexResult) + if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields)) + { + if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name); + ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique }); + } + } if (dicConfigEntity.TryGetValue(type, out var trytb)) { foreach (var idxattr in trytb._indexs.Values) - { if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields)) + { + if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name); ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique }); - } + } } var attrs = type.GetCustomAttributes(typeof(IndexAttribute), true); foreach (var tryattrobj in attrs) { var idxattr = tryattrobj as IndexAttribute; if (idxattr == null) continue; - if (string.IsNullOrEmpty(idxattr.Name)) continue; - if (string.IsNullOrEmpty(idxattr.Fields)) continue; - if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name); - ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique }); + if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields)) + { + if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name); + ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique }); + } } return ret.Values.ToArray(); } diff --git a/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLCodeFirst.cs b/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLCodeFirst.cs index fdd063a8..2d04cd90 100644 --- a/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLCodeFirst.cs +++ b/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLCodeFirst.cs @@ -165,7 +165,8 @@ 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 and t.typinput::varchar = 'array_in' then t2.typname else t.typname end, case when a.attnotnull then '0' else '1' end as is_nullable, -e.adsrc, +coalesce((select 1 from pg_sequences where sequencename = {0} || '_' || {1} || '_' || a.attname || '_sequence_name' limit 1),0) is_identity, +--e.adsrc, a.attndims, d.description as comment from pg_class c @@ -201,7 +202,7 @@ where ns.nspname = {0} and c.relname = {1}", tboldname ?? tbname); sqlType = string.Concat(sqlType), max_length = long.Parse(string.Concat(a[2])), is_nullable = string.Concat(a[4]) == "1", - is_identity = string.Concat(a[5]).StartsWith(@"nextval('") && string.Concat(a[5]).EndsWith(@"'::regclass)"), + is_identity = string.Concat(a[5]) == "1", //string.Concat(a[5]).StartsWith(@"nextval('") && string.Concat(a[5]).EndsWith(@"'::regclass)"), attndims, comment = string.Concat(a[7]) }; diff --git a/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLDbFirst.cs b/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLDbFirst.cs index 148d9d0d..d4229572 100644 --- a/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLDbFirst.cs +++ b/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLDbFirst.cs @@ -208,8 +208,8 @@ 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 e.adsrc = 'nextval(''' || case when ns.nspname = 'public' then '' else ns.nspname || '.' end || c.relname || '_' || a.attname || '_seq''::regclass)' then 1 else 0 end as is_identity, +coalesce((select 1 from pg_sequences where sequencename = {0} || '_' || {1} || '_' || a.attname || '_sequence_name' limit 1),0) is_identity, +--e.adsrc as is_identity, d.description as comment, a.attndims, case when t.typelem = 0 then t.typtype else t2.typtype end, @@ -235,7 +235,7 @@ where {loc8.ToString().Replace("a.table_name", "ns.nspname || '.' || c.relname") var max_length = int.Parse(string.Concat(row[3])); var sqlType = string.Concat(row[4]); var is_nullable = string.Concat(row[5]) == "1"; - var is_identity = string.Concat(row[6]).StartsWith(@"nextval('") && string.Concat(row[6]).EndsWith(@"'::regclass)"); + var is_identity = string.Concat(row[6]) == "1"; //string.Concat(row[6]).StartsWith(@"nextval('") && string.Concat(row[6]).EndsWith(@"'::regclass)"); var comment = string.Concat(row[7]); int attndims = int.Parse(string.Concat(row[8])); string typtype = string.Concat(row[9]); diff --git a/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLCodeFirst.cs b/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLCodeFirst.cs index 1fd94504..961ef8b5 100644 --- a/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLCodeFirst.cs +++ b/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLCodeFirst.cs @@ -207,7 +207,8 @@ 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 and t.typinput::varchar = 'array_in' then t2.typname else t.typname end, case when a.attnotnull then '0' else '1' end as is_nullable, -e.adsrc, +coalesce((select 1 from pg_sequences where sequencename = {0} || '_' || {1} || '_' || a.attname || '_sequence_name' limit 1),0) is_identity, +--e.adsrc, a.attndims, d.description as comment from pg_class c @@ -243,7 +244,7 @@ where ns.nspname = {0} and c.relname = {1}", tboldname ?? tbname); sqlType = string.Concat(sqlType), max_length = long.Parse(string.Concat(a[2])), is_nullable = string.Concat(a[4]) == "1", - is_identity = string.Concat(a[5]).StartsWith(@"nextval('") && string.Concat(a[5]).EndsWith(@"'::regclass)"), + is_identity = string.Concat(a[5]) == "1", //string.Concat(a[5]).StartsWith(@"nextval('") && string.Concat(a[5]).EndsWith(@"'::regclass)"), attndims, comment = string.Concat(a[7]) }; diff --git a/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLDbFirst.cs b/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLDbFirst.cs index 97734719..5910c1d3 100644 --- a/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLDbFirst.cs +++ b/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLDbFirst.cs @@ -319,8 +319,8 @@ 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 e.adsrc = 'nextval(''' || case when ns.nspname = 'public' then '' else ns.nspname || '.' end || c.relname || '_' || a.attname || '_seq''::regclass)' then 1 else 0 end as is_identity, +coalesce((select 1 from pg_sequences where sequencename = {0} || '_' || {1} || '_' || a.attname || '_sequence_name' limit 1),0) is_identity, +--e.adsrc as is_identity, d.description as comment, a.attndims, case when t.typelem = 0 then t.typtype else t2.typtype end, @@ -346,7 +346,7 @@ where {loc8.ToString().Replace("a.table_name", "ns.nspname || '.' || c.relname") var max_length = int.Parse(string.Concat(row[3])); var sqlType = string.Concat(row[4]); var is_nullable = string.Concat(row[5]) == "1"; - var is_identity = string.Concat(row[6]).StartsWith(@"nextval('") && string.Concat(row[6]).EndsWith(@"'::regclass)"); + var is_identity = string.Concat(row[6]) == "1"; //string.Concat(row[6]).StartsWith(@"nextval('") && string.Concat(row[6]).EndsWith(@"'::regclass)"); var comment = string.Concat(row[7]); int attndims = int.Parse(string.Concat(row[8])); string typtype = string.Concat(row[9]);