补充 DbFirst GetTablesByDatabase 获取表备注

This commit is contained in:
28810 2019-04-02 16:20:53 +08:00
parent 0614850813
commit 880c4dcdd1
4 changed files with 26 additions and 7 deletions

View File

@ -15,6 +15,10 @@ namespace FreeSql.DatabaseModel {
/// </summary> /// </summary>
public string Name { get; internal set; } public string Name { get; internal set; }
/// <summary> /// <summary>
/// 表备注SqlServer下是扩展属性 MS_Description
/// </summary>
public string Comment { get; internal set; }
/// <summary>
/// 表/视图 /// 表/视图
/// </summary> /// </summary>
public DbTableType Type { get; set; } public DbTableType Type { get; set; }

View File

@ -154,6 +154,7 @@ select
concat(a.table_schema, '.', a.table_name) 'id', concat(a.table_schema, '.', a.table_name) 'id',
a.table_schema 'schema', a.table_schema 'schema',
a.table_name 'table', a.table_name 'table',
a.table_comment,
a.table_type 'type' a.table_type 'type'
from information_schema.tables a from information_schema.tables a
where a.table_schema in ({0})", databaseIn); 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 table_id = string.Concat(row[0]);
var schema = string.Concat(row[1]); var schema = string.Concat(row[1]);
var table = string.Concat(row[2]); 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) { if (database.Length == 1) {
table_id = table_id.Substring(table_id.IndexOf('.') + 1); table_id = table_id.Substring(table_id.IndexOf('.') + 1);
schema = ""; 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<string, DbColumnInfo>()); loc3.Add(table_id, new Dictionary<string, DbColumnInfo>());
switch (type) { switch (type) {
case DbTableType.TABLE: case DbTableType.TABLE:

View File

@ -223,9 +223,12 @@ select
b.nspname || '.' || a.tablename, b.nspname || '.' || a.tablename,
a.schemaname, a.schemaname,
a.tablename , a.tablename ,
d.description,
'TABLE' 'TABLE'
from pg_tables a from pg_tables a
inner join pg_namespace b on b.nspname = a.schemaname 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') where a.schemaname not in ('pg_catalog', 'information_schema', 'topology')
and b.nspname || '.' || a.tablename not in ('public.spatial_ref_sys') and b.nspname || '.' || a.tablename not in ('public.spatial_ref_sys')
@ -235,9 +238,11 @@ select
b.nspname || '.' || a.relname, b.nspname || '.' || a.relname,
b.nspname, b.nspname,
a.relname, a.relname,
d.description,
'VIEW' 'VIEW'
from pg_class a from pg_class a
inner join pg_namespace b on b.oid = a.relnamespace 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') 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') 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 object_id = string.Concat(row[0]);
var owner = string.Concat(row[1]); var owner = string.Concat(row[1]);
var table = string.Concat(row[2]); var table = string.Concat(row[2]);
Enum.TryParse<DbTableType>(string.Concat(row[3]), out var type); var comment = string.Concat(row[3]);
loc2.Add(object_id, new DbTableInfo { Id = object_id.ToString(), Schema = owner, Name = table, Type = type }); Enum.TryParse<DbTableType>(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<string, DbColumnInfo>()); loc3.Add(object_id, new Dictionary<string, DbColumnInfo>());
switch (type) { switch (type) {
case DbTableType.VIEW: case DbTableType.VIEW:

View File

@ -123,27 +123,33 @@ select
a.Object_id a.Object_id
,b.name 'Owner' ,b.name 'Owner'
,a.name 'Name' ,a.name 'Name'
,c.value
,'TABLE' type ,'TABLE' type
from sys.tables a from sys.tables a
inner join sys.schemas b on b.schema_id = a.schema_id 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') where not(b.name = 'dbo' and a.name = 'sysdiagrams')
union all union all
select select
a.Object_id a.Object_id
,b.name 'Owner' ,b.name 'Owner'
,a.name 'Name' ,a.name 'Name'
,c.value
,'VIEW' type ,'VIEW' type
from sys.views a from sys.views a
inner join sys.schemas b on b.schema_id = a.schema_id 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 union all
select select
a.Object_id a.Object_id
,b.name 'Owner' ,b.name 'Owner'
,a.name 'Name' ,a.name 'Name'
,c.value
,'StoreProcedure' type ,'StoreProcedure' type
from sys.procedures a from sys.procedures a
inner join sys.schemas b on b.schema_id = a.schema_id 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 order by type desc, b.name, a.name
; ;
use [{olddatabase}]; use [{olddatabase}];
@ -157,8 +163,9 @@ use [{olddatabase}];
int object_id = int.Parse(string.Concat(row[0])); int object_id = int.Parse(string.Concat(row[0]));
var owner = string.Concat(row[1]); var owner = string.Concat(row[1]);
var table = string.Concat(row[2]); var table = string.Concat(row[2]);
Enum.TryParse<DbTableType>(string.Concat(row[3]), out var type); var comment = string.Concat(row[3]);
loc2.Add(object_id, new DbTableInfo { Id = object_id.ToString(), Schema = owner, Name = table, Type = type }); Enum.TryParse<DbTableType>(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<string, DbColumnInfo>()); loc3.Add(object_id, new Dictionary<string, DbColumnInfo>());
switch (type) { switch (type) {
case DbTableType.VIEW: case DbTableType.VIEW: