1
1
mirror of https://github.com/dbcli/pgcli.git synced 2024-10-06 10:17:15 +03:00

Add Exit and Quit commands.

This commit is contained in:
Amjith Ramanujam 2014-12-05 22:04:39 -08:00
parent 6633ca882d
commit 091d7f97f7
4 changed files with 34 additions and 8 deletions

3
TODO
View File

@ -1,7 +1,10 @@
* [] Vendor in tabulate. * [] Vendor in tabulate.
* [] Add MySQL commands (use, describe etc)
* [X] Add exit keyword.
* [] Show only table sensitive columns. * [] Show only table sensitive columns.
* [] Add logging. * [] Add logging.
* [] Add some tests. Sanity, Unit, Completion, Config. * [] Add some tests. Sanity, Unit, Completion, Config.
* [] Setup the pgcli.com website.
* [ ] Add a new trigger for M-/ that does dumb completion. * [ ] Add a new trigger for M-/ that does dumb completion.
* [] Improve the smart completion for Insert statement. * [] Improve the smart completion for Insert statement.
* [] Improve the smart completion for Update statement. * [] Improve the smart completion for Update statement.

View File

@ -60,6 +60,13 @@ def cli(database, user, password, host, port):
try: try:
while True: while True:
document = cli.read_input(on_exit=AbortAction.RAISE_EXCEPTION) document = cli.read_input(on_exit=AbortAction.RAISE_EXCEPTION)
# The reason we check here instead of inside the pgexecute is
# because we want to raise the Exit exception which will be caught
# by the try/except block that wraps the pgexecute.run() statement.
if (document.text.strip() == 'exit'
or document.text.strip() == 'quit'):
raise Exit
try: try:
rows, headers, status = pgexecute.run(document.text) rows, headers, status = pgexecute.run(document.text)
if rows: if rows:

View File

@ -10,7 +10,7 @@ class PGLine(Line):
Dynamically determine whether we're in multiline mode. Dynamically determine whether we're in multiline mode.
""" """
if self.text.rstrip().endswith(';'): if _multiline_exception(self.text):
return False return False
return True return True
@ -18,3 +18,12 @@ class PGLine(Line):
@is_multiline.setter @is_multiline.setter
def is_multiline(self, value): def is_multiline(self, value):
pass pass
def _multiline_exception(text):
text = text.strip()
return (text.startswith('\\') or # Special Command
text.endswith(';') or # Ended with a semi-colon
(text == 'exit') or # Exit and Quit doesn't need semi-colon
(text == 'quit')
)

View File

@ -25,15 +25,21 @@ def describe_table_details(cur, pattern, verbose):
""" """
Returns (rows, headers, status) Returns (rows, headers, status)
""" """
schema, relname = sql_name_pattern(pattern)
sql = '''SELECT c.oid, n.nspname, c.relname FROM pg_catalog.pg_class c LEFT sql = '''SELECT c.oid, n.nspname, c.relname FROM pg_catalog.pg_class c LEFT
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace ''' JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace '''
if relname:
sql += ' WHERE c.relname ~ ' + relname
if schema:
sql += ' AND n.nspname ~ ' + schema
sql += ' AND pg_catalog.pg_table_is_visible(c.oid) ' if pattern:
schema, relname = sql_name_pattern(pattern)
if relname:
sql += ' WHERE c.relname ~ ' + relname
if schema:
sql += ' AND n.nspname ~ ' + schema
sql += ' AND pg_catalog.pg_table_is_visible(c.oid) '
else:
sql += """WHERE n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'"""
sql += ' ORDER BY 2,3' sql += ' ORDER BY 2,3'
# Execute the sql, get the results and call describe_one_table_details on each table. # Execute the sql, get the results and call describe_one_table_details on each table.
@ -248,4 +254,5 @@ if __name__ == '__main__':
import psycopg2 import psycopg2
con = psycopg2.connect(database='misago_testforum') con = psycopg2.connect(database='misago_testforum')
cur = con.cursor() cur = con.cursor()
print describe_table_details(cur, 'django_migrations', False) #print describe_table_details(cur, 'django_migrations', False)
print describe_table_details(cur, None, False)