Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@special_command('\\dv', '\\dv[+] [pattern]', 'List views.')
def list_views(cur, pattern, verbose):
return list_objects(cur, pattern, verbose, ['v', 's', ''])
@special_command('\\dT', '\\dT[S+] [pattern]', 'List data types')
def list_datatypes(cur, pattern, verbose):
assert True
sql = '''SELECT n.nspname as "Schema",
pg_catalog.format_type(t.oid, NULL) AS "Name", '''
if verbose:
sql += r''' t.typname AS "Internal name",
CASE
WHEN t.typrelid != 0
THEN CAST('tuple' AS pg_catalog.text)
WHEN t.typlen < 0
THEN CAST('var' AS pg_catalog.text)
ELSE CAST(t.typlen AS pg_catalog.text)
END AS "Size",
pg_catalog.array_to_string(
ARRAY(
@special_command('\\n', '\\n[+] [name]', 'List or execute named queries.')
def execute_named_query(cur, pattern, **_):
"""Returns (title, rows, headers, status)"""
if pattern == '':
return list_named_queries(True)
query = namedqueries.get(pattern)
title = '> {}'.format(query)
if query is None:
message = "No named query: {}".format(pattern)
return [(None, None, None, message)]
cur.execute(query)
if cur.description:
headers = [x[0] for x in cur.description]
return [(title, cur, headers, cur.statusmessage)]
else:
return [(title, None, None, cur.statusmessage)]
@special_command('\\dt', '\\dt[+] [pattern]', 'List tables.')
def list_tables(cur, pattern, verbose):
return list_objects(cur, pattern, verbose, ['r', ''])
@special_command('\\df', '\\df[+] [pattern]', 'List functions.')
def list_functions(cur, pattern, verbose):
if verbose:
verbose_columns = '''
,CASE
WHEN p.provolatile = 'i' THEN 'immutable'
WHEN p.provolatile = 's' THEN 'stable'
WHEN p.provolatile = 'v' THEN 'volatile'
END as "Volatility",
pg_catalog.pg_get_userbyid(p.proowner) as "Owner",
l.lanname as "Language",
p.prosrc as "Source code",
pg_catalog.obj_description(p.oid, 'pg_proc') as "Description" '''
verbose_table = ''' LEFT JOIN pg_catalog.pg_language l
ON l.oid = p.prolang'''
@special_command('\\dn', '\\dn[+] [pattern]', 'List schemas.')
def list_schemas(cur, pattern, verbose):
"""
Returns (title, rows, headers, status)
"""
sql = '''SELECT n.nspname AS "Name",
pg_catalog.pg_get_userbyid(n.nspowner) AS "Owner"''' + (''',
pg_catalog.array_to_string(n.nspacl, E'\\n') AS "Access privileges",
pg_catalog.obj_description(n.oid, 'pg_namespace') AS "Description"''' if verbose else '') + """
FROM pg_catalog.pg_namespace n WHERE n.nspname """
params = []
if pattern:
_, schema = sql_name_pattern(pattern)
sql += '~ %s'
params.append(schema)
@special_command('\\ds', '\\ds[+] [pattern]', 'List sequences.')
def list_sequences(cur, pattern, verbose):
return list_objects(cur, pattern, verbose, ['S', 's', ''])
@special_command('\\di', '\\di[+] [pattern]', 'List indexes.')
def list_indexes(cur, pattern, verbose):
return list_objects(cur, pattern, verbose, ['i', 's', ''])
@special_command('\\d', '\\d [pattern]', 'List or describe tables, views and sequences.')
def describe_table_details(cur, pattern, verbose):
"""
Returns (title, rows, headers, status)
"""
# This is a simple \d command. No table name to follow.
if not pattern:
sql = """SELECT n.nspname as "Schema", c.relname as "Name",
CASE c.relkind WHEN 'r' THEN 'table'
WHEN 'v' THEN 'view'
WHEN 'm' THEN 'materialized view'
WHEN 'i' THEN 'index'
WHEN 'S' THEN 'sequence'
WHEN 's' THEN 'special'
WHEN 'f' THEN 'foreign table'
END as "Type",
@special_command('\\du', '\\du[+] [pattern]', 'List roles.')
def list_roles(cur, pattern, verbose):
"""
Returns (title, rows, headers, status)
"""
sql = '''SELECT r.rolname, r.rolsuper, r.rolinherit,
r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,
r.rolconnlimit, r.rolvaliduntil,
ARRAY(SELECT b.rolname
FROM pg_catalog.pg_auth_members m
JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)
WHERE m.member = r.oid) as memberof''' + (''',
pg_catalog.shobj_description(r.oid, 'pg_authid') AS description'''
if verbose else '') + """, r.rolreplication
FROM pg_catalog.pg_roles r """
params = []