fix #I9E72C 不能获取pgSql数据库结构

This commit is contained in:
wintel 2024-04-05 20:15:15 +08:00
parent 062c2b36e0
commit ff35c5d303

View File

@ -120,6 +120,10 @@ namespace OpenAuth.App
{ {
return GetSqlServerStructure(tableName); return GetSqlServerStructure(tableName);
} }
else if (dbtype == Define.DBTYPE_PostgreSQL)
{
return GetPostgreStructure(tableName);
}
else else
{ {
return GetOracleStructure(tableName); return GetOracleStructure(tableName);
@ -258,6 +262,82 @@ order by column_id; ";
} }
/// <summary>
/// 获取Mysql表结构信息
/// </summary>
private IList<SysTableColumn> GetPostgreStructure(string tableName)
{
var sql = $@"select attr.attrelid
, schema.nspname as schemaname
, class.relname as tablename --
, attr.attname as columnname --
, attr.atttypid --ID
, attr.atttypmod
, case when con.conname is not null then 1 else 0 end as iskey --
, case when attr.attnotnull is true then 0 else 1 end as isnull --
, t.typname columntype --
, case
when typname in ('BIT', 'BOOL', 'bit', 'bool') then
'bool'
when typname in ('smallint', 'SMALLINT') then 'short'
when typname in ('tinyint', 'TINYINT') then 'bool'
when typname in ('int4', 'int', 'INT') then
'int'
when typname in ('BIGINT', 'bigint') then
'bigint'
when typname in ('FLOAT', 'DOUBLE', 'DECIMAL', 'float', 'double', 'decimal') then
'decimal'
when typname in ('varchar', 'text') then
'string'
when typname in ('Date', 'DateTime', 'TimeStamp', 'date', 'datetime', 'timestamp') then
'DateTime'
else 'string'
end as entitytype
, comment.description as comment
, 1 as iscolumndata
, case
when t.typname = 'varchar' or t.typname = 'bpchar' then attr.atttypmod - 4
else 10
end as columnwidth
, 0 as orderno
, case
when t.typname = 'varchar' or t.typname = 'bpchar' then attr.atttypmod - 4
else 10
end as maxlength
, case
when attname in ('createid', 'modifyid', '')
or con.conname is null then 0
else 1
end as isdisplay --
from pg_catalog.pg_class class
inner join pg_catalog.pg_attribute attr on attr.attrelid = class.oid
left join pg_catalog.pg_constraint con
on con.conrelid = class.oid and attr.attnum = any (con.conkey) and con.contype = 'p'
left join pg_catalog.pg_type t on attr.atttypid = t.oid
inner join pg_catalog.pg_description comment
on comment.objoid = attr.attrelid and comment.objsubid = attr.attnum
inner join pg_catalog.pg_namespace schema on schema.oid = class.relnamespace
where attr.attnum > 0
and not attr.attisdropped
and schema.nspname = 'public' -- replace 'your_schema' with your schema name
and class.relname = '{tableName}'";
foreach (var context in _contexts)
{
var columns = context.Set<SysTableColumn>().FromSqlRaw(sql);
var columnList = columns?.ToList();
if (columnList != null && columnList.Any())
{
return columnList;
}
}
return new List<SysTableColumn>();
}
/// <summary> /// <summary>
/// 获取SqlServer表结构信息 /// 获取SqlServer表结构信息
/// </summary> /// </summary>