1
0
Fork 0

Return list of tuples for \d special command.

This commit is contained in:
Amjith Ramanujam 2014-12-08 22:59:25 -08:00
parent 60cbbdcb28
commit 393fb49c92
4 changed files with 21 additions and 15 deletions

4
TODO
View File

@ -1,3 +1,7 @@
* [X] Fix the \d <tablename>. Seems broken.
* [X] Check what happens when \d django* is run.
* [] \c is broken.
* [] Check why Indexes have a invalid at the end when \d is called on them.
* [X] Vendor in tabulate.
* [X] Create a separate pgspecial package.
* [X] Vendor in pgspecial package.

View File

@ -71,10 +71,11 @@ def cli(database, user, password, host, port):
or document.text.strip() == '\q'):
raise Exit
try:
rows, headers, status = pgexecute.run(document.text)
if rows:
print(tabulate(rows, headers, tablefmt='psql'))
print(status)
res = pgexecute.run(document.text)
for rows, headers, status in res:
if rows:
print(tabulate(rows, headers, tablefmt='psql'))
print(status)
except Exception as e:
click.secho(e.message, err=True, fg='red')

View File

@ -41,7 +41,7 @@ def describe_table_details(cur, pattern, verbose):
cur.execute(sql)
if cur.description:
headers = [x[0] for x in cur.description]
return cur.fetchall(), headers, cur.statusmessage
return [(cur.fetchall(), headers, cur.statusmessage)]
# This is a \d <tablename> command. A royal pain in the ass.
sql = '''SELECT c.oid, n.nspname, c.relname FROM pg_catalog.pg_class c LEFT
@ -59,7 +59,7 @@ def describe_table_details(cur, pattern, verbose):
log.debug(sql)
cur.execute(sql)
if not (cur.rowcount > 0):
return None, None, 'Did not find any relation named %s.' % pattern
return [(None, None, 'Did not find any relation named %s.' % pattern)]
results = []
for oid, nspname, relname in cur.fetchall():
@ -87,7 +87,7 @@ def describe_one_table_details(cur, schema_name, relation_name, oid, verbose):
if (cur.rowcount > 0):
tableinfo = TableInfo._make(cur.fetchone())
else:
return None, None, 'Did not find any relation with OID %s.' % oid
return (None, None, 'Did not find any relation with OID %s.' % oid)
# If it's a seq, fetch it's value and store it for later.
if tableinfo.relkind == 'S':
@ -96,7 +96,7 @@ def describe_one_table_details(cur, schema_name, relation_name, oid, verbose):
log.debug(sql)
cur.execute(sql)
if not (cur.rowcount > 0):
return None, None, 'Something went wrong.'
return (None, None, 'Something went wrong.')
seq_values = cur.fetchone()
@ -251,7 +251,7 @@ def describe_one_table_details(cur, schema_name, relation_name, oid, verbose):
condeferrable, (NOT i.indimmediate) AND EXISTS (SELECT 1 FROM
pg_catalog.pg_constraint WHERE conrelid = i.indrelid AND conindid =
i.indexrelid AND contype IN ('p','u','x') AND condeferred) AS
condeferred a.amname, c2.relname, pg_catalog.pg_get_expr(i.indpred,
condeferred, a.amname, c2.relname, pg_catalog.pg_get_expr(i.indpred,
i.indrelid, true) FROM pg_catalog.pg_index i, pg_catalog.pg_class c,
pg_catalog.pg_class c2, pg_catalog.pg_am a WHERE i.indexrelid = c.oid
AND c.oid = '%s' AND c.relam = a.oid AND i.indrelid = c2.oid;""" % oid
@ -645,7 +645,7 @@ def describe_one_table_details(cur, schema_name, relation_name, oid, verbose):
if (verbose and tableinfo.reloptions):
status.append("Options: %s\n" % tableinfo.reloptions)
return cells, headers, "".join(status)
return (cells, headers, "".join(status))
def sql_name_pattern(pattern):
"""

View File

@ -43,12 +43,13 @@ class PGExecute(object):
user=self.user, password=self.password, host=self.host,
port=self.port)
self.dbname = dbname
return (None, None, 'You are now connected to database "%s" as '
'user "%s"' % (self.dbname, self.user))
return [(None, None, 'You are now connected to database "%s" as '
'user "%s"' % (self.dbname, self.user))]
with self.conn.cursor() as cur:
try:
return pgspecial.execute(cur, *self.parse_pattern(sql))
results = pgspecial.execute(cur, *self.parse_pattern(sql))
return results
except KeyError:
cur.execute(sql)
@ -56,9 +57,9 @@ class PGExecute(object):
# rows.
if cur.description:
headers = [x[0] for x in cur.description]
return cur.fetchall(), headers, cur.statusmessage
return [cur.fetchall(), headers, cur.statusmessage]
else:
return None, None, cur.statusmessage
return [None, None, cur.statusmessage]
def tables(self):
with self.conn.cursor() as cur: