From 880c4dcdd1bf79ab5b262179a6de919f2586f664 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Tue, 2 Apr 2019 16:20:53 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=20DbFirst=20GetTablesByDatab?= =?UTF-8?q?ase=20=E8=8E=B7=E5=8F=96=E8=A1=A8=E5=A4=87=E6=B3=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql/DatabaseModel/DBTableInfo.cs | 4 ++++ FreeSql/MySql/MySqlDbFirst.cs | 6 ++++-- FreeSql/PostgreSQL/PostgreSQLDbFirst.cs | 10 ++++++++-- FreeSql/SqlServer/SqlServerDbFirst.cs | 13 ++++++++++--- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/FreeSql/DatabaseModel/DBTableInfo.cs b/FreeSql/DatabaseModel/DBTableInfo.cs index 6ecc139f..3176bc6f 100644 --- a/FreeSql/DatabaseModel/DBTableInfo.cs +++ b/FreeSql/DatabaseModel/DBTableInfo.cs @@ -15,6 +15,10 @@ namespace FreeSql.DatabaseModel { /// public string Name { get; internal set; } /// + /// 表备注,SqlServer下是扩展属性 MS_Description + /// + public string Comment { get; internal set; } + /// /// 表/视图 /// public DbTableType Type { get; set; } diff --git a/FreeSql/MySql/MySqlDbFirst.cs b/FreeSql/MySql/MySqlDbFirst.cs index 69163802..9c62ba65 100644 --- a/FreeSql/MySql/MySqlDbFirst.cs +++ b/FreeSql/MySql/MySqlDbFirst.cs @@ -154,6 +154,7 @@ select concat(a.table_schema, '.', a.table_name) 'id', a.table_schema 'schema', a.table_name 'table', +a.table_comment, a.table_type 'type' from information_schema.tables a where a.table_schema in ({0})", databaseIn); @@ -166,12 +167,13 @@ where a.table_schema in ({0})", databaseIn); var table_id = string.Concat(row[0]); var schema = string.Concat(row[1]); var table = string.Concat(row[2]); - var type = string.Concat(row[3]) == "VIEW" ? DbTableType.VIEW : DbTableType.TABLE; + var comment = string.Concat(row[3]); + var type = string.Concat(row[4]) == "VIEW" ? DbTableType.VIEW : DbTableType.TABLE; if (database.Length == 1) { table_id = table_id.Substring(table_id.IndexOf('.') + 1); schema = ""; } - loc2.Add(table_id, new DbTableInfo { Id = table_id, Schema = schema, Name = table, Type = type }); + loc2.Add(table_id, new DbTableInfo { Id = table_id, Schema = schema, Name = table, Comment = comment, Type = type }); loc3.Add(table_id, new Dictionary()); switch (type) { case DbTableType.TABLE: diff --git a/FreeSql/PostgreSQL/PostgreSQLDbFirst.cs b/FreeSql/PostgreSQL/PostgreSQLDbFirst.cs index 42bba4f0..ab02131b 100644 --- a/FreeSql/PostgreSQL/PostgreSQLDbFirst.cs +++ b/FreeSql/PostgreSQL/PostgreSQLDbFirst.cs @@ -223,9 +223,12 @@ select b.nspname || '.' || a.tablename, a.schemaname, a.tablename , +d.description, 'TABLE' from pg_tables a inner join pg_namespace b on b.nspname = a.schemaname +inner join pg_class c on c.relnamespace = b.oid and c.relname = a.tablename +left join pg_description d on d.objoid = c.oid and objsubid = 0 where a.schemaname not in ('pg_catalog', 'information_schema', 'topology') and b.nspname || '.' || a.tablename not in ('public.spatial_ref_sys') @@ -235,9 +238,11 @@ select b.nspname || '.' || a.relname, b.nspname, a.relname, +d.description, 'VIEW' from pg_class a inner join pg_namespace b on b.oid = a.relnamespace +left join pg_description d on d.objoid = a.oid and objsubid = 0 where b.nspname not in ('pg_catalog', 'information_schema') and a.relkind in ('m','v') and b.nspname || '.' || a.relname not in ('public.geography_columns','public.geometry_columns','public.raster_columns','public.raster_overviews') "; @@ -250,8 +255,9 @@ and b.nspname || '.' || a.relname not in ('public.geography_columns','public.geo var object_id = string.Concat(row[0]); var owner = string.Concat(row[1]); var table = string.Concat(row[2]); - Enum.TryParse(string.Concat(row[3]), out var type); - loc2.Add(object_id, new DbTableInfo { Id = object_id.ToString(), Schema = owner, Name = table, Type = type }); + var comment = string.Concat(row[3]); + Enum.TryParse(string.Concat(row[4]), out var type); + loc2.Add(object_id, new DbTableInfo { Id = object_id.ToString(), Schema = owner, Name = table, Comment = comment, Type = type }); loc3.Add(object_id, new Dictionary()); switch (type) { case DbTableType.VIEW: diff --git a/FreeSql/SqlServer/SqlServerDbFirst.cs b/FreeSql/SqlServer/SqlServerDbFirst.cs index f7005a06..5531ebf9 100644 --- a/FreeSql/SqlServer/SqlServerDbFirst.cs +++ b/FreeSql/SqlServer/SqlServerDbFirst.cs @@ -123,27 +123,33 @@ select a.Object_id ,b.name 'Owner' ,a.name 'Name' +,c.value ,'TABLE' type from sys.tables a inner join sys.schemas b on b.schema_id = a.schema_id +left join sys.extended_properties AS c ON c.major_id = a.object_id AND c.minor_id = 0 AND c.name = 'MS_Description' where not(b.name = 'dbo' and a.name = 'sysdiagrams') union all select a.Object_id ,b.name 'Owner' ,a.name 'Name' +,c.value ,'VIEW' type from sys.views a inner join sys.schemas b on b.schema_id = a.schema_id +left join sys.extended_properties AS c ON c.major_id = a.object_id AND c.minor_id = 0 AND c.name = 'MS_Description' union all select a.Object_id ,b.name 'Owner' ,a.name 'Name' +,c.value ,'StoreProcedure' type from sys.procedures a inner join sys.schemas b on b.schema_id = a.schema_id -where a.type = 'P' and charindex('$NPSP', a.name) = 0 and charindex('diagram', a.name) = 0 +left join sys.extended_properties AS c ON c.major_id = a.object_id AND c.minor_id = 0 AND c.name = 'MS_Description' +where a.type = 'P' and charindex('diagram', a.name) = 0 order by type desc, b.name, a.name ; use [{olddatabase}]; @@ -157,8 +163,9 @@ use [{olddatabase}]; int object_id = int.Parse(string.Concat(row[0])); var owner = string.Concat(row[1]); var table = string.Concat(row[2]); - Enum.TryParse(string.Concat(row[3]), out var type); - loc2.Add(object_id, new DbTableInfo { Id = object_id.ToString(), Schema = owner, Name = table, Type = type }); + var comment = string.Concat(row[3]); + Enum.TryParse(string.Concat(row[4]), out var type); + loc2.Add(object_id, new DbTableInfo { Id = object_id.ToString(), Schema = owner, Name = table, Comment = comment, Type = type }); loc3.Add(object_id, new Dictionary()); switch (type) { case DbTableType.VIEW: