This commit is contained in:
yumoqing 2020-10-14 14:48:42 +08:00
parent 735e7d6227
commit 1432450e74

View File

@ -67,34 +67,46 @@ from (
where row_id >=$[from_line]$ and row_id < $[end_line]$""" where row_id >=$[from_line]$ and row_id < $[end_line]$"""
def tablesSQL(self): def tablesSQL(self):
""" sqlcmd = """select x.name, y.description as title
列出表名 from
SELECT tablename FROM pg_tables; (select a.name,c.oid
WHERE tablename NOT LIKE 'pg%' from
AND tablename NOT LIKE 'sql_%' (select lower(tablename) as name from tables where schemaname='public') a
ORDER BY tablename; pg_class c
""" where a.name = c.relname) x
sqlcmd = """select left join pg_description y
lower(table_name) as name, on x.oid=y.objoid
lower(decode(comments,null,table_name,comments)) as title and y.objsubid='0'"""
from USER_TAB_COMMENTS where table_type = 'TABLE'"""
return sqlcmd return sqlcmd
def fieldsSQL(self,tablename=None): def fieldsSQL(self,tablename=None):
"""SELECT col_description(a.attrelid,a.attnum) as comment,pg_type.typname as typename,a.attname as name, a.attnotnull as notnull sqlcmd="""SELECT
FROM pg_class as c,pg_attribute as a inner join pg_type on pg_type.oid = a.atttypid a.attname AS name,
where c.relname = 'tablename' and a.attrelid = c.oid and a.attnum>0 t.typname AS type,
""" case t.typname
sqlcmd="""select lower(utc.COLUMN_NAME) name when 'varchar' then a.atttypmod - 4
,utc.DATA_TYPE type when 'numeric' then (a.atttypmod - 4) / 65536
,utc.DATA_LENGTH length else null
,utc.data_scale dec end as length,
,case when utc.nullable = 'Y' then 'yes' else 'no' end nullable case t.typname
,lower(nvl(ucc.comments,utc.COLUMN_NAME)) title when 'numeric' then (a.atttypmod - 4) % 65536
,lower(utc.table_name) as table_name else null
from user_tab_cols utc left join USER_COL_COMMENTS ucc on utc.table_name = ucc.table_name and utc.COLUMN_NAME = ucc.COLUMN_NAME""" end as dec,
if tablename is not None: case a.attnotnull
sqlcmd = sqlcmd + """ where lower(utc.table_name) = '%s'""" % tablename.lower() when 't' then 'no'
when 'f' then 'yes'
end as nullable
b.description AS title
FROM pg_class c, pg_attribute a
LEFT JOIN pg_description b
ON a.attrelid = b.objoid
AND a.attnum = b.objsubid, pg_type t
WHERE lower(c.relname) = '%s'
AND a.attnum > 0
AND a.attrelid = c.oid
AND a.atttypid = t.oid
ORDER BY a.attnum;
""" % tablename.lower()
return sqlcmd return sqlcmd
def fkSQL(self,tablename=None): def fkSQL(self,tablename=None):
@ -116,35 +128,46 @@ where
return sqlcmd return sqlcmd
def pkSQL(self,tablename=None): def pkSQL(self,tablename=None):
"""
select pg_attribute.attname as colname,pg_type.typname as typename,pg_constraint.conname as pk_name from
pg_constraint inner join pg_class
on pg_constraint.conrelid = pg_class.oid
inner join pg_attribute on pg_attribute.attrelid = pg_class.oid
and pg_attribute.attnum = pg_constraint.conkey[1]
inner join pg_type on pg_type.oid = pg_attribute.atttypid
where pg_class.relname = 'tablename'
and pg_constraint.contype='p'
"""
sqlcmd=""" sqlcmd="""
select select
lower(col.table_name) table_name, pg_attribute.attname as field_name,
lower(col.column_name) as field_name lower(pg_class.relname) as table_name
from from pg_constraint
user_constraints con,user_cons_columns col inner join pg_class
where on pg_constraint.conrelid = pg_class.oid
con.constraint_name=col.constraint_name and con.constraint_type='P'""" inner join pg_attribute
if tablename is not None: on pg_attribute.attrelid = pg_class.oid
sqlcmd = sqlcmd + """ and lower(col.table_name)='%s'""" % tablename.lower() and pg_attribute.attnum = pg_constraint.conkey[1]
inner join pg_type
on pg_type.oid = pg_attribute.atttypid
where lower(pg_class.relname) = '%s'
and pg_constraint.contype='p'
""" % tablename.lower()
return sqlcmd return sqlcmd
def indexesSQL(self,tablename=None): def indexesSQL(self,tablename=None):
sqlcmd = """select sqlcmd = """select
lower(a.index_name) index_name, i.relname as index_name,
lower(a.UNIQUENESS) is_unique, case ix.INDISUNIQUE
lower(b.column_name) column_name when 't' then 'unique'
from user_indexes a, user_ind_columns b else ''
where a.index_name = b.index_name""" end as is_unique,
a.attname as column_name
from
pg_class t,
pg_class i,
pg_index ix,
pg_attribute a
where
t.oid = ix.indrelid
and i.oid = ix.indexrelid
and a.attrelid = t.oid
and a.attnum = ANY(ix.indkey)
and t.relkind = 'r'
and lower(t.relname) = '%s'
order by
t.relname,
i.relname""" % tablename.lower()
if tablename is not None: if tablename is not None:
sqlcmd += """ and lower(a.table_name) = lower('%s')""" % tablename.lower() sqlcmd += """ and lower(a.table_name) = lower('%s')""" % tablename.lower()
return sqlcmd return sqlcmd