1
1
mirror of https://github.com/dbcli/pgcli.git synced 2024-11-30 11:17:12 +03:00

Move refresh routines outside the loop.

This commit is contained in:
Amjith Ramanujam 2015-01-31 13:46:32 -08:00
parent d324b27e41
commit 588be27797
2 changed files with 25 additions and 35 deletions

6
TODO
View File

@ -7,12 +7,6 @@
* [ ] Check how to add the name of the table before printing the table. * [ ] Check how to add the name of the table before printing the table.
* [ ] Add a new trigger for M-/ that does naive completion. * [ ] Add a new trigger for M-/ that does naive completion.
* [ ] New Feature List - Write the current version to config file. At launch if the version has changed, display the changelog between the two versions. * [ ] New Feature List - Write the current version to config file. At launch if the version has changed, display the changelog between the two versions.
* [ ] need_search_path_refresh doesn't need to split the document.text, cur.query returns the current sql.
* [ ] See if the need_search_path_refresh can be done outside the loop.
* [ ] Add a test for 'select * from custom.abc where custom.abc.' should suggest columns from abc. * [ ] Add a test for 'select * from custom.abc where custom.abc.' should suggest columns from abc.
* [ ] Search for Dataframe in the source and replace them.
* [ ] pgexecute columns(), tables() etc can be just cursors instead of fetchall() * [ ] pgexecute columns(), tables() etc can be just cursors instead of fetchall()
* [ ] Add unicode tests.
* [ ] Add colorschemes in config file. * [ ] Add colorschemes in config file.
* [X] Add \timing in config file.
* [X] Add table format to config file.

View File

@ -5,8 +5,10 @@ from __future__ import print_function
import os import os
import traceback import traceback
import logging import logging
import click from time import time
import click
import sqlparse
from prompt_toolkit import CommandLineInterface, AbortAction, Exit from prompt_toolkit import CommandLineInterface, AbortAction, Exit
from prompt_toolkit.document import Document from prompt_toolkit.document import Document
from prompt_toolkit.layout import Layout from prompt_toolkit.layout import Layout
@ -29,7 +31,6 @@ from .config import write_default_config, load_config
from .key_bindings import pgcli_bindings from .key_bindings import pgcli_bindings
from .encodingutils import utf8tounicode from .encodingutils import utf8tounicode
from time import time
try: try:
from urlparse import urlparse from urlparse import urlparse
@ -193,11 +194,12 @@ class PGCli(object):
try: try:
logger.debug('sql: %r', document.text) logger.debug('sql: %r', document.text)
successful = False successful = False
start = time()
# Initialized to [] because res might never get initialized # Initialized to [] because res might never get initialized
# if an exception occurs in pgexecute.run(). Which causes # if an exception occurs in pgexecute.run(). Which causes
# finally clause to fail. # finally clause to fail.
res = [] res = []
start = time()
# Run the query.
res = pgexecute.run(document.text) res = pgexecute.run(document.text)
duration = time() - start duration = time() - start
successful = True successful = True
@ -222,11 +224,6 @@ class PGCli(object):
total += end - start total += end - start
mutating = mutating or is_mutating(status) mutating = mutating or is_mutating(status)
if need_search_path_refresh(document.text, status):
logger.debug('Refreshing search path')
completer.set_search_path(pgexecute.search_path())
logger.debug('Search path: %r', completer.search_path)
except KeyboardInterrupt: except KeyboardInterrupt:
# Restart connection to the database # Restart connection to the database
pgexecute.connect() pgexecute.connect()
@ -251,6 +248,12 @@ class PGCli(object):
prompt = '%s> ' % pgexecute.dbname prompt = '%s> ' % pgexecute.dbname
self.refresh_completions() self.refresh_completions()
# Refresh search_path to set default schema.
if need_search_path_refresh(document.text):
logger.debug('Refreshing search path')
completer.set_search_path(pgexecute.search_path())
logger.debug('Search path: %r', completer.search_path)
query = Query(document.text, successful, mutating) query = Query(document.text, successful, mutating)
self.query_history.append(query) self.query_history.append(query)
@ -337,28 +340,21 @@ def format_output(cur, headers, status, table_format):
output.append(status) output.append(status)
return output return output
def need_completion_refresh(sql): def need_completion_refresh(queries):
try: """Determines if the completion needs a refresh by checking if the sql
first_token = sql.split()[0] statement is an alter, create, drop or change db."""
return first_token.lower() in ('alter', 'create', 'use', '\c', 'drop') for query in sqlparse.split(queries):
except Exception: try:
return False first_token = query.split()[0]
return first_token.lower() in ('alter', 'create', 'use', '\c',
def need_search_path_refresh(sql, status): 'drop')
# note that sql may be a multi-command query, but status belongs to an except Exception:
# individual query, since pgexecute handles splitting up multi-commands
try:
status = status.split()[0]
if status.lower() == 'set':
# Since sql could be a multi-line query, it's hard to robustly
# pick out the variable name that's been set. Err on the side of
# false positives here, since the worst case is we refresh the
# search path when it's not necessary
return 'search_path' in sql.lower()
else:
return False return False
except Exception:
return False def need_search_path_refresh(sql):
"""Determines if the search_path should be refreshed by checking if the
sql has 'set search_path'."""
return 'set search_path' in sql.lower()
def is_mutating(status): def is_mutating(status):
"""Determines if the statement is mutating based on the status.""" """Determines if the statement is mutating based on the status."""