Skip to content

Commit 588be27

Browse files
committed
Move refresh routines outside the loop.
1 parent d324b27 commit 588be27

File tree

2 files changed

+25
-35
lines changed

2 files changed

+25
-35
lines changed

TODO

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

pgcli/main.py

+25-29
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import os
66
import traceback
77
import logging
8-
import click
8+
from time import time
99

10+
import click
11+
import sqlparse
1012
from prompt_toolkit import CommandLineInterface, AbortAction, Exit
1113
from prompt_toolkit.document import Document
1214
from prompt_toolkit.layout import Layout
@@ -29,7 +31,6 @@
2931
from .key_bindings import pgcli_bindings
3032
from .encodingutils import utf8tounicode
3133

32-
from time import time
3334

3435
try:
3536
from urlparse import urlparse
@@ -193,11 +194,12 @@ def run_cli(self):
193194
try:
194195
logger.debug('sql: %r', document.text)
195196
successful = False
196-
start = time()
197197
# Initialized to [] because res might never get initialized
198198
# if an exception occurs in pgexecute.run(). Which causes
199199
# finally clause to fail.
200200
res = []
201+
start = time()
202+
# Run the query.
201203
res = pgexecute.run(document.text)
202204
duration = time() - start
203205
successful = True
@@ -222,11 +224,6 @@ def run_cli(self):
222224
total += end - start
223225
mutating = mutating or is_mutating(status)
224226

225-
if need_search_path_refresh(document.text, status):
226-
logger.debug('Refreshing search path')
227-
completer.set_search_path(pgexecute.search_path())
228-
logger.debug('Search path: %r', completer.search_path)
229-
230227
except KeyboardInterrupt:
231228
# Restart connection to the database
232229
pgexecute.connect()
@@ -251,6 +248,12 @@ def run_cli(self):
251248
prompt = '%s> ' % pgexecute.dbname
252249
self.refresh_completions()
253250

251+
# Refresh search_path to set default schema.
252+
if need_search_path_refresh(document.text):
253+
logger.debug('Refreshing search path')
254+
completer.set_search_path(pgexecute.search_path())
255+
logger.debug('Search path: %r', completer.search_path)
256+
254257
query = Query(document.text, successful, mutating)
255258
self.query_history.append(query)
256259

@@ -337,28 +340,21 @@ def format_output(cur, headers, status, table_format):
337340
output.append(status)
338341
return output
339342

340-
def need_completion_refresh(sql):
341-
try:
342-
first_token = sql.split()[0]
343-
return first_token.lower() in ('alter', 'create', 'use', '\c', 'drop')
344-
except Exception:
345-
return False
346-
347-
def need_search_path_refresh(sql, status):
348-
# note that sql may be a multi-command query, but status belongs to an
349-
# individual query, since pgexecute handles splitting up multi-commands
350-
try:
351-
status = status.split()[0]
352-
if status.lower() == 'set':
353-
# Since sql could be a multi-line query, it's hard to robustly
354-
# pick out the variable name that's been set. Err on the side of
355-
# false positives here, since the worst case is we refresh the
356-
# search path when it's not necessary
357-
return 'search_path' in sql.lower()
358-
else:
343+
def need_completion_refresh(queries):
344+
"""Determines if the completion needs a refresh by checking if the sql
345+
statement is an alter, create, drop or change db."""
346+
for query in sqlparse.split(queries):
347+
try:
348+
first_token = query.split()[0]
349+
return first_token.lower() in ('alter', 'create', 'use', '\c',
350+
'drop')
351+
except Exception:
359352
return False
360-
except Exception:
361-
return False
353+
354+
def need_search_path_refresh(sql):
355+
"""Determines if the search_path should be refreshed by checking if the
356+
sql has 'set search_path'."""
357+
return 'set search_path' in sql.lower()
362358

363359
def is_mutating(status):
364360
"""Determines if the statement is mutating based on the status."""

0 commit comments

Comments
 (0)