mirror of https://github.com/dbcli/pgcli
Add a title field and report the notices. Closes #177
This commit is contained in:
parent
979b5e711a
commit
a80eb4ea19
|
@ -1,3 +1,11 @@
|
|||
0.16.3
|
||||
======
|
||||
|
||||
Bug Fixes:
|
||||
----------
|
||||
* Add more SQL keywords for auto-complete suggestion.
|
||||
* Notice messages raised as part of stored procedures are no longer ignored.
|
||||
|
||||
0.16.2
|
||||
======
|
||||
|
||||
|
|
|
@ -214,7 +214,7 @@ class PGCli(object):
|
|||
successful = True
|
||||
output = []
|
||||
total = 0
|
||||
for cur, headers, status in res:
|
||||
for title, cur, headers, status in res:
|
||||
logger.debug("headers: %r", headers)
|
||||
logger.debug("rows: %r", cur)
|
||||
logger.debug("status: %r", status)
|
||||
|
@ -227,7 +227,8 @@ class PGCli(object):
|
|||
if not click.confirm('Do you want to continue?'):
|
||||
click.secho("Aborted!", err=True, fg='red')
|
||||
break
|
||||
output.extend(format_output(cur, headers, status, self.table_format))
|
||||
output.extend(format_output(title, cur, headers,
|
||||
status, self.table_format))
|
||||
end = time()
|
||||
total += end - start
|
||||
mutating = mutating or is_mutating(status)
|
||||
|
@ -343,8 +344,10 @@ def cli(database, user, host, port, prompt_passwd, never_prompt, dbname,
|
|||
|
||||
pgcli.run_cli()
|
||||
|
||||
def format_output(cur, headers, status, table_format):
|
||||
def format_output(title, cur, headers, status, table_format):
|
||||
output = []
|
||||
if title: # Only print the title if it's not None.
|
||||
output.append(title)
|
||||
if cur:
|
||||
headers = [utf8tounicode(x) for x in headers]
|
||||
if is_expanded_output():
|
||||
|
|
|
@ -48,7 +48,7 @@ def list_schemas(cur, pattern, verbose):
|
|||
cur.execute(sql)
|
||||
if cur.description:
|
||||
headers = [x[0] for x in cur.description]
|
||||
return [(cur, headers, cur.statusmessage)]
|
||||
return [(None, cur, headers, cur.statusmessage)]
|
||||
|
||||
def list_objects(cur, pattern, verbose, relkinds):
|
||||
"""
|
||||
|
@ -106,7 +106,7 @@ def list_objects(cur, pattern, verbose, relkinds):
|
|||
|
||||
if cur.description:
|
||||
headers = [x[0] for x in cur.description]
|
||||
return [(cur, headers, cur.statusmessage)]
|
||||
return [(None, cur, headers, cur.statusmessage)]
|
||||
|
||||
|
||||
def list_tables(cur, pattern, verbose):
|
||||
|
@ -185,7 +185,7 @@ def list_functions(cur, pattern, verbose):
|
|||
|
||||
if cur.description:
|
||||
headers = [x[0] for x in cur.description]
|
||||
return [(cur, headers, cur.statusmessage)]
|
||||
return [(None, cur, headers, cur.statusmessage)]
|
||||
|
||||
|
||||
def describe_table_details(cur, pattern, verbose):
|
||||
|
@ -218,7 +218,7 @@ def describe_table_details(cur, pattern, verbose):
|
|||
cur.execute(sql)
|
||||
if cur.description:
|
||||
headers = [x[0] for x in cur.description]
|
||||
return [(cur, headers, cur.statusmessage)]
|
||||
return [(None, cur, headers, cur.statusmessage)]
|
||||
|
||||
# This is a \d <tablename> command. A royal pain in the ass.
|
||||
schema, relname = sql_name_pattern(pattern)
|
||||
|
@ -248,7 +248,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, None, 'Did not find any relation named %s.' % pattern)]
|
||||
|
||||
results = []
|
||||
for oid, nspname, relname in cur.fetchall():
|
||||
|
@ -282,7 +282,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, 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':
|
||||
|
@ -291,7 +291,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, None, 'Something went wrong.')
|
||||
|
||||
seq_values = cur.fetchone()
|
||||
|
||||
|
@ -839,7 +839,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 (None, cells, headers, "".join(status))
|
||||
|
||||
def sql_name_pattern(pattern):
|
||||
"""
|
||||
|
@ -898,7 +898,7 @@ def show_help(cur, arg, verbose): # All the parameters are ignored.
|
|||
for command, value in sorted(CASE_SENSITIVE_COMMANDS.items()):
|
||||
if value[1]:
|
||||
result.append(value[1])
|
||||
return [(result, headers, None)]
|
||||
return [(None, result, headers, None)]
|
||||
|
||||
def change_db(cur, arg, verbose):
|
||||
raise NotImplementedError
|
||||
|
@ -908,14 +908,14 @@ def expanded_output(cur, arg, verbose):
|
|||
use_expanded_output = not use_expanded_output
|
||||
message = u"Expanded display is "
|
||||
message += u"on." if use_expanded_output else u"off."
|
||||
return [(None, None, message)]
|
||||
return [(None, None, None, message)]
|
||||
|
||||
def toggle_timing(cur, arg, verbose):
|
||||
global TIMING_ENABLED
|
||||
TIMING_ENABLED = not TIMING_ENABLED
|
||||
message = "Timing is "
|
||||
message += "on." if TIMING_ENABLED else "off."
|
||||
return [(None, None, message)]
|
||||
return [(None, None, None, message)]
|
||||
|
||||
CASE_SENSITIVE_COMMANDS = {
|
||||
'\?': (show_help, ['\?', 'Help on pgcli commands.']),
|
||||
|
@ -959,9 +959,9 @@ def execute(cur, sql):
|
|||
cur.execute(command_executor)
|
||||
if cur.description:
|
||||
headers = [x[0] for x in cur.description]
|
||||
return [(cur, headers, cur.statusmessage)]
|
||||
return [(None, cur, headers, cur.statusmessage)]
|
||||
else:
|
||||
return [(None, None, cur.statusmessage)]
|
||||
return [(None, None, None, cur.statusmessage)]
|
||||
|
||||
if __name__ == '__main__':
|
||||
import psycopg2
|
||||
|
|
|
@ -167,7 +167,7 @@ class PGExecute(object):
|
|||
# Remove spaces and EOL
|
||||
statement = statement.strip()
|
||||
if not statement: # Empty string
|
||||
yield (None, None, None)
|
||||
yield (None, None, None, None)
|
||||
|
||||
# Split the sql into separate queries and run each one.
|
||||
for sql in sqlparse.split(statement):
|
||||
|
@ -187,7 +187,7 @@ class PGExecute(object):
|
|||
self.connect(database=dbname)
|
||||
self.dbname = dbname
|
||||
_logger.debug('Successfully switched to DB: %r', dbname)
|
||||
yield (None, None, 'You are now connected to database "%s" as '
|
||||
yield (None, None, None, 'You are now connected to database "%s" as '
|
||||
'user "%s"' % (self.dbname, self.user))
|
||||
else:
|
||||
try: # Special command
|
||||
|
@ -202,14 +202,18 @@ class PGExecute(object):
|
|||
_logger.debug('Regular sql statement. sql: %r', split_sql)
|
||||
cur = self.conn.cursor()
|
||||
cur.execute(split_sql)
|
||||
try:
|
||||
title = self.conn.notices.pop()
|
||||
except IndexError:
|
||||
title = None
|
||||
# cur.description will be None for operations that do not return
|
||||
# rows.
|
||||
if cur.description:
|
||||
headers = [x[0] for x in cur.description]
|
||||
return (cur, headers, cur.statusmessage)
|
||||
return (title, cur, headers, cur.statusmessage)
|
||||
else:
|
||||
_logger.debug('No rows in result.')
|
||||
return (None, None, cur.statusmessage)
|
||||
return (title, None, None, cur.statusmessage)
|
||||
|
||||
def search_path(self):
|
||||
"""Returns the current search path as a list of schema names"""
|
||||
|
|
|
@ -61,8 +61,8 @@ def drop_tables(conn):
|
|||
def run(executor, sql, join=False):
|
||||
" Return string output for the sql to be run "
|
||||
result = []
|
||||
for rows, headers, status in executor.run(sql):
|
||||
result.extend(format_output(rows, headers, status, 'psql'))
|
||||
for title, rows, headers, status in executor.run(sql):
|
||||
result.extend(format_output(title, rows, headers, status, 'psql'))
|
||||
if join:
|
||||
result = '\n'.join(result)
|
||||
return result
|
||||
|
|
Loading…
Reference in New Issue