diff options
author | darikg <darikg@users.noreply.github.com> | 2015-08-19 11:00:25 -0400 |
---|---|---|
committer | darikg <darikg@users.noreply.github.com> | 2015-08-19 11:00:25 -0400 |
commit | f8fa8497f3bd192b97dfdba071b60d47a06dad00 (patch) | |
tree | d8cef18a4a9c4c59d0b6b27442cd737a850b09d3 | |
parent | 2e9e268632297fe553ddc2d00acfc98b0797561e (diff) | |
parent | f2e436ad2f2acea17a69411580caef42e4c3fe5b (diff) |
Merge pull request #336 from dbcli/amjith/run-commands
Multiple queries with syntax errors
-rw-r--r-- | pgcli/pgexecute.py | 12 | ||||
-rw-r--r-- | tests/test_pgexecute.py | 19 |
2 files changed, 18 insertions, 13 deletions
diff --git a/pgcli/pgexecute.py b/pgcli/pgexecute.py index 2b226489..fe6a3d16 100644 --- a/pgcli/pgexecute.py +++ b/pgcli/pgexecute.py @@ -1,10 +1,12 @@ +import traceback import logging import psycopg2 import psycopg2.extras import psycopg2.extensions as ext import sqlparse from .packages import pgspecial as special -from .encodingutils import unicode2utf8, PY2 +from .encodingutils import unicode2utf8, PY2, utf8tounicode +import click _logger = logging.getLogger(__name__) @@ -247,7 +249,13 @@ class PGExecute(object): def execute_normal_sql(self, split_sql): _logger.debug('Regular sql statement. sql: %r', split_sql) cur = self.conn.cursor() - cur.execute(split_sql) + try: + cur.execute(split_sql) + except psycopg2.ProgrammingError as e: + _logger.error("sql: %r, error: %r", split_sql, e) + _logger.error("traceback: %r", traceback.format_exc()) + return (None, None, None, click.style(utf8tounicode(str(e)), fg='red')) + try: title = self.conn.notices.pop() except IndexError: diff --git a/tests/test_pgexecute.py b/tests/test_pgexecute.py index 355096b4..1d159902 100644 --- a/tests/test_pgexecute.py +++ b/tests/test_pgexecute.py @@ -1,7 +1,6 @@ # coding=UTF-8 import pytest -import psycopg2 from pgcli.packages.pgspecial import PGSpecial from textwrap import dedent from utils import run, dbtest, requires_json, requires_jsonb @@ -87,15 +86,13 @@ def test_database_list(executor): @dbtest def test_invalid_syntax(executor): - with pytest.raises(psycopg2.ProgrammingError) as excinfo: - run(executor, 'invalid syntax!') - assert 'syntax error at or near "invalid"' in str(excinfo.value) + result = run(executor, 'invalid syntax!') + assert 'syntax error at or near "invalid"' in result[0] @dbtest def test_invalid_column_name(executor): - with pytest.raises(psycopg2.ProgrammingError) as excinfo: - run(executor, 'select invalid command') - assert 'column "invalid" does not exist' in str(excinfo.value) + result = run(executor, 'select invalid command') + assert 'column "invalid" does not exist' in result[0] @pytest.fixture(params=[True, False]) @@ -130,9 +127,9 @@ def test_multiple_queries_with_special_command_same_line(executor, pgspecial): @dbtest def test_multiple_queries_same_line_syntaxerror(executor): - with pytest.raises(psycopg2.ProgrammingError) as excinfo: - run(executor, "select 'foo'; invalid syntax") - assert 'syntax error at or near "invalid"' in str(excinfo.value) + result = run(executor, u"select 'fooé'; invalid syntax é") + assert u'fooé' in result[0] + assert 'syntax error at or near "invalid"' in result[-1] @pytest.fixture @@ -199,4 +196,4 @@ def test_describe_special(executor, command, verbose, pattern): # We don't have any tests for the output of any of the special commands, # but we can at least make sure they run without error sql = r'\{command}{verbose} {pattern}'.format(**locals()) - executor.run(sql)
\ No newline at end of file + executor.run(sql) |