mirror of
https://github.com/dbcli/pgcli.git
synced 2024-09-19 09:48:19 +03:00
Add defaults to column metadata
This commit is contained in:
parent
3081caa1aa
commit
2b0ca684d4
@ -1,6 +1,19 @@
|
||||
from collections import namedtuple
|
||||
|
||||
ColumnMetadata = namedtuple('ColumnMetadata', ['name', 'datatype', 'foreignkeys'])
|
||||
_ColumnMetadata = namedtuple(
|
||||
'ColumnMetadata',
|
||||
['name', 'datatype', 'foreignkeys', 'default', 'has_default', 'default_value']
|
||||
)
|
||||
|
||||
|
||||
def ColumnMetadata(
|
||||
name, datatype, foreignkeys=None, default=None, has_default=False, default_value=None
|
||||
):
|
||||
return _ColumnMetadata(
|
||||
name, datatype, foreignkeys or [], default, has_default, default_value
|
||||
)
|
||||
|
||||
|
||||
ForeignKey = namedtuple('ForeignKey', ['parentschema', 'parenttable',
|
||||
'parentcolumn', 'childschema', 'childtable', 'childcolumn'])
|
||||
TableMetadata = namedtuple('TableMetadata', 'name columns')
|
||||
|
@ -415,12 +415,17 @@ class PGExecute(object):
|
||||
SELECT nsp.nspname schema_name,
|
||||
cls.relname table_name,
|
||||
att.attname column_name,
|
||||
att.atttypid::regtype::text type_name
|
||||
att.atttypid::regtype::text type_name,
|
||||
att.atthasdef AS has_default,
|
||||
def.adsrc as default_value
|
||||
FROM pg_catalog.pg_attribute att
|
||||
INNER JOIN pg_catalog.pg_class cls
|
||||
ON att.attrelid = cls.oid
|
||||
INNER JOIN pg_catalog.pg_namespace nsp
|
||||
ON cls.relnamespace = nsp.oid
|
||||
LEFT OUTER JOIN pg_attrdef def
|
||||
ON def.adrelid = att.attrelid
|
||||
AND def.adnum = att.attnum
|
||||
WHERE cls.relkind = ANY(%s)
|
||||
AND NOT att.attisdropped
|
||||
AND att.attnum > 0
|
||||
@ -430,7 +435,9 @@ class PGExecute(object):
|
||||
SELECT nsp.nspname schema_name,
|
||||
cls.relname table_name,
|
||||
att.attname column_name,
|
||||
typ.typname type_name
|
||||
typ.typname type_name,
|
||||
NULL AS has_default,
|
||||
NULL AS default_value
|
||||
FROM pg_catalog.pg_attribute att
|
||||
INNER JOIN pg_catalog.pg_class cls
|
||||
ON att.attrelid = cls.oid
|
||||
|
@ -182,13 +182,13 @@ class MetaData(object):
|
||||
for tbl, cols in tbls.items():
|
||||
tables.append((sch, tbl))
|
||||
# Let all columns be text columns
|
||||
tbl_cols.extend([(sch, tbl, col, 'text') for col in cols])
|
||||
tbl_cols.extend([(sch, tbl, col, 'text', False, None) for col in cols])
|
||||
|
||||
for sch, tbls in metadata.get('views', {}).items():
|
||||
for tbl, cols in tbls.items():
|
||||
views.append((sch, tbl))
|
||||
# Let all columns be text columns
|
||||
view_cols.extend([(sch, tbl, col, 'text') for col in cols])
|
||||
view_cols.extend([(sch, tbl, col, 'text', False, None) for col in cols])
|
||||
|
||||
functions = [
|
||||
FunctionMetadata(sch, *func_meta)
|
||||
|
@ -40,7 +40,7 @@ def test_schemata_table_views_and_columns_query(executor):
|
||||
run(executor, "create table b(z text)")
|
||||
run(executor, "create view d as select 1 as e")
|
||||
run(executor, "create schema schema1")
|
||||
run(executor, "create table schema1.c (w text)")
|
||||
run(executor, "create table schema1.c (w text DEFAULT 'meow')")
|
||||
run(executor, "create schema schema2")
|
||||
|
||||
# schemata
|
||||
@ -55,15 +55,18 @@ def test_schemata_table_views_and_columns_query(executor):
|
||||
('public', 'a'), ('public', 'b'), ('schema1', 'c')])
|
||||
|
||||
assert set(executor.table_columns()) >= set([
|
||||
('public', 'a', 'x', 'text'), ('public', 'a', 'y', 'text'),
|
||||
('public', 'b', 'z', 'text'), ('schema1', 'c', 'w', 'text')])
|
||||
('public', 'a', 'x', 'text', False, None),
|
||||
('public', 'a', 'y', 'text', False, None),
|
||||
('public', 'b', 'z', 'text', False, None),
|
||||
('schema1', 'c', 'w', 'text', True, "'meow'::text"),
|
||||
])
|
||||
|
||||
# views
|
||||
assert set(executor.views()) >= set([
|
||||
('public', 'd')])
|
||||
|
||||
assert set(executor.view_columns()) >= set([
|
||||
('public', 'd', 'e', 'integer')])
|
||||
('public', 'd', 'e', 'integer', False, None)])
|
||||
|
||||
|
||||
@dbtest
|
||||
|
@ -313,17 +313,32 @@ def test_into_suggests_tables_and_schemas():
|
||||
])
|
||||
def test_insert_into_lparen_suggests_cols(text):
|
||||
suggestions = suggest_type(text, 'INSERT INTO abc (')
|
||||
assert suggestions ==(Column(table_refs=((None, 'abc', None, False),)),)
|
||||
assert suggestions == (
|
||||
Column(
|
||||
table_refs=((None, 'abc', None, False),),
|
||||
context='insert'
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def test_insert_into_lparen_partial_text_suggests_cols():
|
||||
suggestions = suggest_type('INSERT INTO abc (i', 'INSERT INTO abc (i')
|
||||
assert suggestions ==(Column(table_refs=((None, 'abc', None, False),)),)
|
||||
assert suggestions == (
|
||||
Column(
|
||||
table_refs=((None, 'abc', None, False),),
|
||||
context='insert'
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def test_insert_into_lparen_comma_suggests_cols():
|
||||
suggestions = suggest_type('INSERT INTO abc (id,', 'INSERT INTO abc (id,')
|
||||
assert suggestions ==(Column(table_refs=((None, 'abc', None, False),)),)
|
||||
assert suggestions == (
|
||||
Column(
|
||||
table_refs=((None, 'abc', None, False),),
|
||||
context='insert'
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def test_partially_typed_col_name_suggests_col_names():
|
||||
|
Loading…
Reference in New Issue
Block a user