summaryrefslogtreecommitdiffstats
path: root/pgcli
diff options
context:
space:
mode:
Diffstat (limited to 'pgcli')
-rw-r--r--pgcli/__init__.py2
-rw-r--r--pgcli/auth.py4
-rw-r--r--pgcli/completion_refresher.py3
-rw-r--r--pgcli/explain_output_formatter.py3
-rw-r--r--pgcli/magic.py2
-rw-r--r--pgcli/main.py162
-rw-r--r--pgcli/packages/formatter/sqlformatter.py9
-rw-r--r--pgcli/packages/parseutils/__init__.py48
-rw-r--r--pgcli/packages/prompt_utils.py14
-rw-r--r--pgcli/packages/sqlcompletion.py3
-rw-r--r--pgcli/pgclirc48
-rw-r--r--pgcli/pgcompleter.py33
-rw-r--r--pgcli/pgexecute.py58
-rw-r--r--pgcli/pgtoolbar.py8
-rw-r--r--pgcli/pyev.py4
15 files changed, 296 insertions, 105 deletions
diff --git a/pgcli/__init__.py b/pgcli/__init__.py
index dcbfb52..76ad18b 100644
--- a/pgcli/__init__.py
+++ b/pgcli/__init__.py
@@ -1 +1 @@
-__version__ = "3.5.0"
+__version__ = "4.0.1"
diff --git a/pgcli/auth.py b/pgcli/auth.py
index 342c412..2f1e552 100644
--- a/pgcli/auth.py
+++ b/pgcli/auth.py
@@ -26,7 +26,9 @@ def keyring_initialize(keyring_enabled, *, logger):
try:
keyring = importlib.import_module("keyring")
- except Exception as e: # ImportError for Python 2, ModuleNotFoundError for Python 3
+ except (
+ ModuleNotFoundError
+ ) as e: # ImportError for Python 2, ModuleNotFoundError for Python 3
logger.warning("import keyring failed: %r.", e)
diff --git a/pgcli/completion_refresher.py b/pgcli/completion_refresher.py
index 1039d51..c887cb6 100644
--- a/pgcli/completion_refresher.py
+++ b/pgcli/completion_refresher.py
@@ -6,7 +6,6 @@ from .pgcompleter import PGCompleter
class CompletionRefresher:
-
refreshers = OrderedDict()
def __init__(self):
@@ -39,7 +38,7 @@ class CompletionRefresher:
args=(executor, special, callbacks, history, settings),
name="completion_refresh",
)
- self._completer_thread.setDaemon(True)
+ self._completer_thread.daemon = True
self._completer_thread.start()
return [
(None, None, None, "Auto-completion refresh started in the background.")
diff --git a/pgcli/explain_output_formatter.py b/pgcli/explain_output_formatter.py
index b14cf44..ce45b4f 100644
--- a/pgcli/explain_output_formatter.py
+++ b/pgcli/explain_output_formatter.py
@@ -10,7 +10,8 @@ class ExplainOutputFormatter:
self.max_width = max_width
def format_output(self, cur, headers, **output_kwargs):
- (data,) = cur.fetchone()
+ # explain query results should always contain 1 row each
+ [(data,)] = list(cur)
explain_list = json.loads(data)
visualizer = Visualizer(self.max_width)
for explain in explain_list:
diff --git a/pgcli/magic.py b/pgcli/magic.py
index 6e58f28..09902a2 100644
--- a/pgcli/magic.py
+++ b/pgcli/magic.py
@@ -43,7 +43,7 @@ def pgcli_line_magic(line):
u = conn.session.engine.url
_logger.debug("New pgcli: %r", str(u))
- pgcli.connect(u.database, u.host, u.username, u.port, u.password)
+ pgcli.connect_uri(str(u._replace(drivername="postgres")))
conn._pgcli = pgcli
# For convenience, print the connection alias
diff --git a/pgcli/main.py b/pgcli/main.py
index 0fa264f..f95c800 100644
--- a/pgcli/main.py
+++ b/pgcli/main.py
@@ -63,15 +63,14 @@ from .config import (
)
from .key_bindings import pgcli_bindings
from .packages.formatter.sqlformatter import register_new_formatter
-from .packages.prompt_utils import confirm_destructive_query
+from .packages.prompt_utils import confirm, confirm_destructive_query
+from .packages.parseutils import is_destructive
+from .packages.parseutils import parse_destructive_warning
from .__init__ import __version__
click.disable_unicode_literals_warning = True
-try:
- from urlparse import urlparse, unquote, parse_qs
-except ImportError:
- from urllib.parse import urlparse, unquote, parse_qs
+from urllib.parse import urlparse
from getpass import getuser
@@ -201,6 +200,9 @@ class PGCli:
self.multiline_mode = c["main"].get("multi_line_mode", "psql")
self.vi_mode = c["main"].as_bool("vi")
self.auto_expand = auto_vertical_output or c["main"].as_bool("auto_expand")
+ self.auto_retry_closed_connection = c["main"].as_bool(
+ "auto_retry_closed_connection"
+ )
self.expanded_output = c["main"].as_bool("expand")
self.pgspecial.timing_enabled = c["main"].as_bool("timing")
if row_limit is not None:
@@ -224,11 +226,16 @@ class PGCli:
self.syntax_style = c["main"]["syntax_style"]
self.cli_style = c["colors"]
self.wider_completion_menu = c["main"].as_bool("wider_completion_menu")
- self.destructive_warning = warn or c["main"]["destructive_warning"]
- # also handle boolean format of destructive warning
- self.destructive_warning = {"true": "all", "false": "off"}.get(
- self.destructive_warning.lower(), self.destructive_warning
+ self.destructive_warning = parse_destructive_warning(
+ warn or c["main"].as_list("destructive_warning")
+ )
+ self.destructive_warning_restarts_connection = c["main"].as_bool(
+ "destructive_warning_restarts_connection"
+ )
+ self.destructive_statements_require_transaction = c["main"].as_bool(
+ "destructive_statements_require_transaction"
)
+
self.less_chatty = bool(less_chatty) or c["main"].as_bool("less_chatty")
self.null_string = c["main"].get("null_string", "<null>")
self.prompt_format = (
@@ -258,6 +265,9 @@ class PGCli:
# Initialize completer
smart_completion = c["main"].as_bool("smart_completion")
keyword_casing = c["main"]["keyword_casing"]
+ single_connection = single_connection or c["main"].as_bool(
+ "always_use_single_connection"
+ )
self.settings = {
"casing_file": get_casing_file(c),
"generate_casing_file": c["main"].as_bool("generate_casing_file"),
@@ -269,6 +279,7 @@ class PGCli:
"single_connection": single_connection,
"less_chatty": less_chatty,
"keyword_casing": keyword_casing,
+ "alias_map_file": c["main"]["alias_map_file"] or None,
}
completer = PGCompleter(
@@ -292,7 +303,6 @@ class PGCli:
raise PgCliQuitError
def register_special_commands(self):
-
self.pgspecial.register(
self.change_db,
"\\c",
@@ -354,6 +364,23 @@ class PGCli:
"Change the table format used to output results",
)
+ self.pgspecial.register(
+ self.echo,
+ "\\echo",
+ "\\echo [string]",
+ "Echo a string to stdout",
+ )
+
+ self.pgspecial.register(
+ self.echo,
+ "\\qecho",
+ "\\qecho [string]",
+ "Echo a string to the query output channel.",
+ )
+
+ def echo(self, pattern, **_):
+ return [(None, None, None, pattern)]
+
def change_table_format(self, pattern, **_):
try:
if pattern not in TabularOutputFormatter().supported_formats:
@@ -423,12 +450,20 @@ class PGCli:
except OSError as e:
return [(None, None, None, str(e), "", False, True)]
- if (
- self.destructive_warning != "off"
- and confirm_destructive_query(query, self.destructive_warning) is False
- ):
- message = "Wise choice. Command execution stopped."
- return [(None, None, None, message)]
+ if self.destructive_warning:
+ if (
+ self.destructive_statements_require_transaction
+ and not self.pgexecute.valid_transaction()
+ and is_destructive(query, self.destructive_warning)
+ ):
+ message = "Destructive statements must be run within a transaction. Command execution stopped."
+ return [(None, None, None, message)]
+ destroy = confirm_destructive_query(
+ query, self.destructive_warning, self.dsn_alias
+ )
+ if destroy is False:
+ message = "Wise choice. Command execution stopped."
+ return [(None, None, None, message)]
on_error_resume = self.on_error == "RESUME"
return self.pgexecute.run(
@@ -456,7 +491,6 @@ class PGCli:
return [(None, None, None, message, "", True, True)]
def initialize_logging(self):
-
log_file = self.config["main"]["log_file"]
if log_file == "default":
log_file = config_location() + "log"
@@ -687,34 +721,52 @@ class PGCli:
editor_command = special.editor_command(text)
return text
- def execute_command(self, text):
+ def execute_command(self, text, handle_closed_connection=True):
logger = self.logger
query = MetaQuery(query=text, successful=False)
try:
- if self.destructive_warning != "off":
- destroy = confirm = confirm_destructive_query(
- text, self.destructive_warning
+ if self.destructive_warning:
+ if (
+ self.destructive_statements_require_transaction
+ and not self.pgexecute.valid_transaction()
+ and is_destructive(text, self.destructive_warning)
+ ):
+ click.secho(
+ "Destructive statements must be run within a transaction."
+ )
+ raise KeyboardInterrupt
+ destroy = confirm_destructive_query(
+ text, self.destructive_warning, self.dsn_alias
)
if destroy is False:
click.secho("Wise choice!")
raise KeyboardInterrupt
elif destroy:
click.secho("Your call!")
+
output, query = self._evaluate_command(text)
except KeyboardInterrupt:
- # Restart connection to the database
- self.pgexecute.connect()
- logger.debug("cancelled query, sql: %r", text)
- click.secho("cancelled query", err=True, fg="red")
+ if self.destructive_warning_restarts_connection:
+ # Restart connection to the database
+ self.pgexecute.connect()
+ logger.debug("cancelled query and restarted connection, sql: %r", text)
+ click.secho(
+ "cancelled query and restarted connection", err=True, fg="red"
+ )
+ else:
+ logger.debug("cancelled query, sql: %r", text)
+ click.secho("cancelled query", err=True, fg="red")
except NotImplementedError:
click.secho("Not Yet Implemented.", fg="yellow")
except OperationalError as e:
logger.error("sql: %r, error: %r", text, e)
logger.error("traceback: %r", traceback.format_exc())
- self._handle_server_closed_connection(text)
- except (PgCliQuitError, EOFError) as e:
+ click.secho(str(e), err=True, fg="red")
+ if handle_closed_connection:
+ self._handle_server_closed_connection(text)
+ except (PgCliQuitError, EOFError):
raise
except Exception as e:
logger.error("sql: %r, error: %r", text, e)
@@ -722,7 +774,9 @@ class PGCli:
click.secho(str(e), err=True, fg="red")
else:
try:
- if self.output_file and not text.startswith(("\\o ", "\\? ")):
+ if self.output_file and not text.startswith(
+ ("\\o ", "\\? ", "\\echo ")
+ ):
try:
with open(self.output_file, "a", encoding="utf-8") as f:
click.echo(text, file=f)
@@ -766,6 +820,34 @@ class PGCli:
logger.debug("Search path: %r", self.completer.search_path)
return query
+ def _check_ongoing_transaction_and_allow_quitting(self):
+ """Return whether we can really quit, possibly by asking the
+ user to confirm so if there is an ongoing transaction.
+ """
+ if not self.pgexecute.valid_transaction():
+ return True
+ while 1:
+ try:
+ choice = click.prompt(
+ "A transaction is ongoing. Choose `c` to COMMIT, `r` to ROLLBACK, `a` to abort exit.",
+ default="a",
+ )
+ except click.Abort:
+ # Print newline if user aborts with `^C`, otherwise
+ # pgcli's prompt will be printed on the same line
+ # (just after the confirmation prompt).
+ click.echo(None, err=False)
+ choice = "a"
+ choice = choice.lower()
+ if choice == "a":
+ return False # do not quit
+ if choice == "c":
+ query = self.execute_command("commit")
+ return query.successful # quit only if query is successful
+ if choice == "r":
+ query = self.execute_command("rollback")
+ return query.successful # quit only if query is successful
+
def run_cli(self):
logger = self.logger
@@ -788,6 +870,10 @@ class PGCli:
text = self.prompt_app.prompt()
except KeyboardInterrupt:
continue
+ except EOFError:
+ if not self._check_ongoing_transaction_and_allow_quitting():
+ continue
+ raise
try:
text = self.handle_editor_command(text)
@@ -797,7 +883,12 @@ class PGCli:
click.secho(str(e), err=True, fg="red")
continue
- self.handle_watch_command(text)
+ try:
+ self.handle_watch_command(text)
+ except PgCliQuitError:
+ if not self._check_ongoing_transaction_and_allow_quitting():
+ continue
+ raise
self.now = dt.datetime.today()
@@ -1036,10 +1127,17 @@ class PGCli:
click.secho("Reconnecting...", fg="green")
self.pgexecute.connect()
click.secho("Reconnected!", fg="green")
- self.execute_command(text)
except OperationalError as e:
click.secho("Reconnect Failed", fg="red")
click.secho(str(e), err=True, fg="red")
+ else:
+ retry = self.auto_retry_closed_connection or confirm(
+ "Run the query from before reconnecting?"
+ )
+ if retry:
+ click.secho("Running query...", fg="green")
+ # Don't get stuck in a retry loop
+ self.execute_command(text, handle_closed_connection=False)
def refresh_completions(self, history=None, persist_priorities="all"):
"""Refresh outdated completions
@@ -1266,7 +1364,6 @@ class PGCli:
@click.option(
"--warn",
default=None,
- type=click.Choice(["all", "moderate", "off"]),
help="Warn before running a destructive query.",
)
@click.option(
@@ -1575,7 +1672,8 @@ def format_output(title, cur, headers, status, settings, explain_mode=False):
first_line = next(formatted)
formatted = itertools.chain([first_line], formatted)
if (
- not expanded
+ not explain_mode
+ and not expanded
and max_width
and len(strip_ansi(first_line)) > max_width
and headers
diff --git a/pgcli/packages/formatter/sqlformatter.py b/pgcli/packages/formatter/sqlformatter.py
index 5bf25fe..5224eff 100644
--- a/pgcli/packages/formatter/sqlformatter.py
+++ b/pgcli/packages/formatter/sqlformatter.py
@@ -14,10 +14,13 @@ preprocessors = ()
def escape_for_sql_statement(value):
+ if value is None:
+ return "NULL"
+
if isinstance(value, bytes):
return f"X'{value.hex()}'"
- else:
- return "'{}'".format(value)
+
+ return "'{}'".format(value)
def adapter(data, headers, table_format=None, **kwargs):
@@ -29,7 +32,7 @@ def adapter(data, headers, table_format=None, **kwargs):
else:
table_name = table[1]
else:
- table_name = '"DUAL"'
+ table_name = "DUAL"
if table_format == "sql-insert":
h = '", "'.join(headers)
yield 'INSERT INTO "{}" ("{}") VALUES'.format(table_name, h)
diff --git a/pgcli/packages/parseutils/__init__.py b/pgcli/packages/parseutils/__init__.py
index 1acc008..023e13b 100644
--- a/pgcli/packages/parseutils/__init__.py
+++ b/pgcli/packages/parseutils/__init__.py
@@ -1,6 +1,17 @@
import sqlparse
+BASE_KEYWORDS = [
+ "drop",
+ "shutdown",
+ "delete",
+ "truncate",
+ "alter",
+ "unconditional_update",
+]
+ALL_KEYWORDS = BASE_KEYWORDS + ["update"]
+
+
def query_starts_with(formatted_sql, prefixes):
"""Check if the query starts with any item from *prefixes*."""
prefixes = [prefix.lower() for prefix in prefixes]
@@ -13,22 +24,35 @@ def query_is_unconditional_update(formatted_sql):
return bool(tokens) and tokens[0] == "update" and "where" not in tokens
-def query_is_simple_update(formatted_sql):
- """Check if the query starts with UPDATE."""
- tokens = formatted_sql.split()
- return bool(tokens) and tokens[0] == "update"
-
-
-def is_destructive(queries, warning_level="all"):
+def is_destructive(queries, keywords):
"""Returns if any of the queries in *queries* is destructive."""
- keywords = ("drop", "shutdown", "delete", "truncate", "alter")
for query in sqlparse.split(queries):
if query:
formatted_sql = sqlparse.format(query.lower(), strip_comments=True).strip()
- if query_starts_with(formatted_sql, keywords):
- return True
- if query_is_unconditional_update(formatted_sql):
+ if "unconditional_update" in keywords and query_is_unconditional_update(
+ formatted_sql
+ ):
return True
- if warning_level == "all" and query_is_simple_update(formatted_sql):
+ if query_starts_with(formatted_sql, keywords):
return True
return False
+
+
+def parse_destructive_warning(warning_level):
+ """Converts a deprecated destructive warning option to a list of command keywords."""
+ if not warning_level:
+ return []
+
+ if not isinstance(warning_level, list):
+ if "," in warning_level:
+ return warning_level.split(",")
+ warning_level = [warning_level]
+
+ return {
+ "true": ALL_KEYWORDS,
+ "false": [],
+ "all": ALL_KEYWORDS,
+ "moderate": BASE_KEYWORDS,
+ "off": [],
+ "": [],
+ }.get(warning_level[0], warning_level)
diff --git a/pgcli/packages/prompt_utils.py b/pgcli/packages/prompt_utils.py
index e8589de..997b86e 100644
--- a/pgcli/packages/prompt_utils.py
+++ b/pgcli/packages/prompt_utils.py
@@ -3,7 +3,7 @@ import click
from .parseutils import is_destructive
-def confirm_destructive_query(queries, warning_level):
+def confirm_destructive_query(queries, keywords, alias):
"""Check if the query is destructive and prompts the user to confirm.
Returns:
@@ -12,11 +12,13 @@ def confirm_destructive_query(queries, warning_level):
* False if the query is destructive and the user doesn't want to proceed.
"""
- prompt_text = (
- "You're about to run a destructive command.\n" "Do you want to proceed? (y/n)"
- )
- if is_destructive(queries, warning_level) and sys.stdin.isatty():
- return prompt(prompt_text, type=bool)
+ info = "You're about to run a destructive command"
+ if alias:
+ info += f" in {click.style(alias, fg='red')}"
+
+ prompt_text = f"{info}.\nDo you want to proceed?"
+ if is_destructive(queries, keywords) and sys.stdin.isatty():
+ return confirm(prompt_text)
def confirm(*args, **kwargs):
diff --git a/pgcli/packages/sqlcompletion.py b/pgcli/packages/sqlcompletion.py
index be4933a..b78edd6 100644
--- a/pgcli/packages/sqlcompletion.py
+++ b/pgcli/packages/sqlcompletion.py
@@ -290,7 +290,6 @@ def suggest_special(text):
def suggest_based_on_last_token(token, stmt):
-
if isinstance(token, str):
token_v = token.lower()
elif isinstance(token, Comparison):
@@ -399,7 +398,6 @@ def suggest_based_on_last_token(token, stmt):
elif (token_v.endswith("join") and token.is_keyword) or (
token_v in ("copy", "from", "update", "into", "describe", "truncate")
):
-
schema = stmt.get_identifier_schema()
tables = extract_tables(stmt.text_before_cursor)
is_join = token_v.endswith("join") and token.is_keyword
@@ -436,7 +434,6 @@ def suggest_based_on_last_token(token, stmt):
try:
prev = stmt.get_previous_token(token).value.lower()
if prev in ("drop", "alter", "create", "create or replace"):
-
# Suggest functions from either the currently-selected schema or the
# public schema if no schema has been specified
suggest = []
diff --git a/pgcli/pgclirc b/pgcli/pgclirc
index dcff63d..51f7eae 100644
--- a/pgcli/pgclirc
+++ b/pgcli/pgclirc
@@ -9,6 +9,10 @@ smart_completion = True
# visible.)
wider_completion_menu = False
+# Do not create new connections for refreshing completions; Equivalent to
+# always running with the --single-connection flag.
+always_use_single_connection = False
+
# Multi-line mode allows breaking up the sql statements into multiple lines. If
# this is set to True, then the end of the statements must have a semi-colon.
# If this is set to False then sql statements can't be split into multiple
@@ -22,14 +26,22 @@ multi_line = False
# a command.
multi_line_mode = psql
-# Destructive warning mode will alert you before executing a sql statement
+# Destructive warning will alert you before executing a sql statement
# that may cause harm to the database such as "drop table", "drop database",
# "shutdown", "delete", or "update".
-# Possible values:
-# "all" - warn on data definition statements, server actions such as SHUTDOWN, DELETE or UPDATE
-# "moderate" - skip warning on UPDATE statements, except for unconditional updates
-# "off" - skip all warnings
-destructive_warning = all
+# You can pass a list of destructive commands or leave it empty if you want to skip all warnings.
+# "unconditional_update" will warn you of update statements that don't have a where clause
+destructive_warning = drop, shutdown, delete, truncate, alter, update, unconditional_update
+
+# Destructive warning can restart the connection if this is enabled and the
+# user declines. This means that any current uncommitted transaction can be
+# aborted if the user doesn't want to proceed with a destructive_warning
+# statement.
+destructive_warning_restarts_connection = False
+
+# When this option is on (and if `destructive_warning` is not empty),
+# destructive statements are not executed when outside of a transaction.
+destructive_statements_require_transaction = False
# Enables expand mode, which is similar to `\x` in psql.
expand = False
@@ -37,9 +49,21 @@ expand = False
# Enables auto expand mode, which is similar to `\x auto` in psql.
auto_expand = False
+# Auto-retry queries on connection failures and other operational errors. If
+# False, will prompt to rerun the failed query instead of auto-retrying.
+auto_retry_closed_connection = True
+
# If set to True, table suggestions will include a table alias
generate_aliases = False
+# Path to a json file that specifies specific table aliases to use when generate_aliases is set to True
+# the format for this file should be:
+# {
+# "some_table_name": "desired_alias",
+# "some_other_table_name": "another_alias"
+# }
+alias_map_file =
+
# log_file location.
# In Unix/Linux: ~/.config/pgcli/log
# In Windows: %USERPROFILE%\AppData\Local\dbcli\pgcli\log
@@ -83,9 +107,10 @@ qualify_columns = if_more_than_one_table
# When no schema is entered, only suggest objects in search_path
search_path_filter = False
-# Default pager.
-# By default 'PAGER' environment variable is used
-# pager = less -SRXF
+# Default pager. See https://www.pgcli.com/pager for more information on settings.
+# By default 'PAGER' environment variable is used. If the pager is less, and the 'LESS'
+# environment variable is not set, then LESS='-SRXF' will be automatically set.
+# pager = less
# Timing of sql statements and table rendering.
timing = True
@@ -140,7 +165,7 @@ less_chatty = False
# \i - Postgres PID
# \# - "@" sign if logged in as superuser, '>' in other case
# \n - Newline
-# \dsn_alias - name of dsn alias if -D option is used (empty otherwise)
+# \dsn_alias - name of dsn connection string alias if -D option is used (empty otherwise)
# \x1b[...m - insert ANSI escape sequence
# eg: prompt = '\x1b[35m\u@\x1b[32m\h:\x1b[36m\d>'
prompt = '\u@\h:\d> '
@@ -198,7 +223,8 @@ output.null = "#808080"
# Named queries are queries you can execute by name.
[named queries]
-# DSN to call by -D option
+# Here's where you can provide a list of connection string aliases.
+# You can use it by passing the -D option. `pgcli -D example_dsn`
[alias_dsn]
# example_dsn = postgresql://[user[:password]@][netloc][:port][/dbname]
diff --git a/pgcli/pgcompleter.py b/pgcli/pgcompleter.py
index e66c3dc..17fc540 100644
--- a/pgcli/pgcompleter.py
+++ b/pgcli/pgcompleter.py
@@ -1,3 +1,4 @@
+import json
import logging
import re
from itertools import count, repeat, chain
@@ -61,18 +62,38 @@ arg_default_type_strip_regex = re.compile(r"::[\w\.]+(\[\])?$")
normalize_ref = lambda ref: ref if ref[0] == '"' else '"' + ref.lower() + '"'
-def generate_alias(tbl):
+def generate_alias(tbl, alias_map=None):
"""Generate a table alias, consisting of all upper-case letters in
the table name, or, if there are no upper-case letters, the first letter +
all letters preceded by _
param tbl - unescaped name of the table to alias
"""
+ if alias_map and tbl in alias_map:
+ return alias_map[tbl]
return "".join(
[l for l in tbl if l.isupper()]
or [l for l, prev in zip(tbl, "_" + tbl) if prev == "_" and l != "_"]
)
+class InvalidMapFile(ValueError):
+ pass
+
+
+def load_alias_map_file(path):
+ try:
+ with open(path) as fo:
+ alias_map = json.load(fo)
+ except FileNotFoundError as err:
+ raise InvalidMapFile(
+ f"Cannot read alias_map_file - {err.filename} does not exist"
+ )
+ except json.JSONDecodeError:
+ raise InvalidMapFile(f"Cannot read alias_map_file - {path} is not valid json")
+ else:
+ return alias_map
+
+
class PGCompleter(Completer):
# keywords_tree: A dict mapping keywords to well known following keywords.
# e.g. 'CREATE': ['TABLE', 'USER', ...],
@@ -100,6 +121,11 @@ class PGCompleter(Completer):
self.call_arg_oneliner_max = settings.get("call_arg_oneliner_max", 2)
self.search_path_filter = settings.get("search_path_filter")
self.generate_aliases = settings.get("generate_aliases")
+ alias_map_file = settings.get("alias_map_file")
+ if alias_map_file is not None:
+ self.alias_map = load_alias_map_file(alias_map_file)
+ else:
+ self.alias_map = None
self.casing_file = settings.get("casing_file")
self.insert_col_skip_patterns = [
re.compile(pattern)
@@ -157,7 +183,6 @@ class PGCompleter(Completer):
self.all_completions.update(additional_keywords)
def extend_schemata(self, schemata):
-
# schemata is a list of schema names
schemata = self.escaped_names(schemata)
metadata = self.dbmetadata["tables"]
@@ -226,7 +251,6 @@ class PGCompleter(Completer):
self.all_completions.add(colname)
def extend_functions(self, func_data):
-
# func_data is a list of function metadata namedtuples
# dbmetadata['schema_name']['functions']['function_name'] should return
@@ -260,7 +284,6 @@ class PGCompleter(Completer):
}
def extend_foreignkeys(self, fk_data):
-
# fk_data is a list of ForeignKey namedtuples, with fields
# parentschema, childschema, parenttable, childtable,
# parentcolumns, childcolumns
@@ -283,7 +306,6 @@ class PGCompleter(Completer):
parcolmeta.foreignkeys.append(fk)
def extend_datatypes(self, type_data):
-
# dbmetadata['datatypes'][schema_name][type_name] should store type
# metadata, such as composite type field names. Currently, we're not
# storing any metadata beyond typename, so just store None
@@ -697,7 +719,6 @@ class PGCompleter(Completer):
return self.find_matches(word_before_cursor, conds, meta="join")
def get_function_matches(self, suggestion, word_before_cursor, alias=False):
-
if suggestion.usage == "from":
# Only suggest functions allowed in FROM clause
diff --git a/pgcli/pgexecute.py b/pgcli/pgexecute.py
index 8f2968d..497d681 100644
--- a/pgcli/pgexecute.py
+++ b/pgcli/pgexecute.py
@@ -1,7 +1,7 @@
import logging
import traceback
from collections import namedtuple
-
+import re
import pgspecial as special
import psycopg
import psycopg.sql
@@ -17,6 +17,27 @@ ViewDef = namedtuple(
)
+# we added this funcion to strip beginning comments
+# because sqlparse didn't handle tem well. It won't be needed if sqlparse
+# does parsing of this situation better
+
+
+def remove_beginning_comments(command):
+ # Regular expression pattern to match comments
+ pattern = r"^(/\*.*?\*/|--.*?)(?:\n|$)"
+
+ # Find and remove all comments from the beginning
+ cleaned_command = command
+ comments = []
+ match = re.match(pattern, cleaned_command, re.DOTALL)
+ while match:
+ comments.append(match.group())
+ cleaned_command = cleaned_command[len(match.group()) :].lstrip()
+ match = re.match(pattern, cleaned_command, re.DOTALL)
+
+ return [cleaned_command, comments]
+
+
def register_typecasters(connection):
"""Casts date and timestamp values to string, resolves issues with out-of-range
dates (e.g. BC) which psycopg can't handle"""
@@ -76,7 +97,6 @@ class ProtocolSafeCursor(psycopg.Cursor):
class PGExecute:
-
# The boolean argument to the current_schemas function indicates whether
# implicit schemas, e.g. pg_catalog
search_path_query = """
@@ -180,7 +200,6 @@ class PGExecute:
dsn=None,
**kwargs,
):
-
conn_params = self._conn_params.copy()
new_params = {
@@ -203,7 +222,11 @@ class PGExecute:
conn_params.update({k: v for k, v in new_params.items() if v})
- conn_info = make_conninfo(**conn_params)
+ if "dsn" in conn_params:
+ other_params = {k: v for k, v in conn_params.items() if k != "dsn"}
+ conn_info = make_conninfo(conn_params["dsn"], **other_params)
+ else:
+ conn_info = make_conninfo(**conn_params)
conn = psycopg.connect(conn_info)
conn.cursor_factory = ProtocolSafeCursor
@@ -309,21 +332,20 @@ class PGExecute:
# sql parse doesn't split on a comment first + special
# so we're going to do it
- sqltemp = []
+ removed_comments = []
sqlarr = []
+ cleaned_command = ""
- if statement.startswith("--"):
- sqltemp = statement.split("\n")
- sqlarr.append(sqltemp[0])
- for i in sqlparse.split(sqltemp[1]):
- sqlarr.append(i)
- elif statement.startswith("/*"):
- sqltemp = statement.split("*/")
- sqltemp[0] = sqltemp[0] + "*/"
- for i in sqlparse.split(sqltemp[1]):
- sqlarr.append(i)
- else:
- sqlarr = sqlparse.split(statement)
+ # could skip if statement doesn't match ^-- or ^/*
+ cleaned_command, removed_comments = remove_beginning_comments(statement)
+
+ sqlarr = sqlparse.split(cleaned_command)
+
+ # now re-add the beginning comments if there are any, so that they show up in
+ # log files etc when running these commands
+
+ if len(removed_comments) > 0:
+ sqlarr = removed_comments + sqlarr
# run each sql query
for sql in sqlarr:
@@ -470,7 +492,7 @@ class PGExecute:
return (
psycopg.sql.SQL(template)
.format(
- name=psycopg.sql.Identifier(f"{result.nspname}.{result.relname}"),
+ name=psycopg.sql.Identifier(result.nspname, result.relname),
stmt=psycopg.sql.SQL(result.viewdef),
)
.as_string(self.conn)
diff --git a/pgcli/pgtoolbar.py b/pgcli/pgtoolbar.py
index 7b5883e..4a12ff4 100644
--- a/pgcli/pgtoolbar.py
+++ b/pgcli/pgtoolbar.py
@@ -1,18 +1,14 @@
-from pkg_resources import packaging
-
-import prompt_toolkit
from prompt_toolkit.key_binding.vi_state import InputMode
from prompt_toolkit.application import get_app
-parse_version = packaging.version.parse
-
vi_modes = {
InputMode.INSERT: "I",
InputMode.NAVIGATION: "N",
InputMode.REPLACE: "R",
InputMode.INSERT_MULTIPLE: "M",
}
-if parse_version(prompt_toolkit.__version__) >= parse_version("3.0.6"):
+# REPLACE_SINGLE is available in prompt_toolkit >= 3.0.6
+if "REPLACE_SINGLE" in {e.name for e in InputMode}:
vi_modes[InputMode.REPLACE_SINGLE] = "R"
diff --git a/pgcli/pyev.py b/pgcli/pyev.py
index 202947f..2886c9c 100644
--- a/pgcli/pyev.py
+++ b/pgcli/pyev.py
@@ -146,7 +146,7 @@ class Visualizer:
elif self.explain.get("Max Rows") < plan["Actual Rows"]:
self.explain["Max Rows"] = plan["Actual Rows"]
- if not self.explain.get("MaxCost"):
+ if not self.explain.get("Max Cost"):
self.explain["Max Cost"] = plan["Actual Cost"]
elif self.explain.get("Max Cost") < plan["Actual Cost"]:
self.explain["Max Cost"] = plan["Actual Cost"]
@@ -171,7 +171,7 @@ class Visualizer:
return self.warning_format("%.2f ms" % value)
elif value < 60000:
return self.critical_format(
- "%.2f s" % (value / 2000.0),
+ "%.2f s" % (value / 1000.0),
)
else:
return self.critical_format(