summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorERYoung11 <78834571+ERYoung11@users.noreply.github.com>2022-08-31 21:39:24 -0500
committerERYoung11 <78834571+ERYoung11@users.noreply.github.com>2022-08-31 21:39:24 -0500
commit1c4b2a414c6f39bfa883dc548131a98f007ace9a (patch)
tree2ec4eaf5d890c3047e6b9fed15992be18a587136
parent0f21f86838be36da53ec01ef4d2067aa5fdcb840 (diff)
fixed comments with special see Issue #1336
-rw-r--r--pgcli/pgexecute.py23
-rw-r--r--tests/test_pgexecute.py68
2 files changed, 89 insertions, 2 deletions
diff --git a/pgcli/pgexecute.py b/pgcli/pgexecute.py
index 758a3159..85a52c0d 100644
--- a/pgcli/pgexecute.py
+++ b/pgcli/pgexecute.py
@@ -306,8 +306,27 @@ class PGExecute:
if not statement: # Empty string
yield None, None, None, None, statement, False, False
- # Split the sql into separate queries and run each one.
- for sql in sqlparse.split(statement):
+ # sql parse doesn't split on a comment first + special
+ # so we're going to do it
+
+ sqltemp=[]
+ sqlarr=[]
+
+ 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)
+
+ # run each sql query
+ for sql in sqlarr:
# Remove spaces, eol and semi-colons.
sql = sql.rstrip(";")
sql = sqlparse.format(sql, strip_comments=False).strip()
diff --git a/tests/test_pgexecute.py b/tests/test_pgexecute.py
index 7fef4658..05313c72 100644
--- a/tests/test_pgexecute.py
+++ b/tests/test_pgexecute.py
@@ -2,6 +2,7 @@ from textwrap import dedent
import psycopg
import pytest
+import re
from unittest.mock import patch, MagicMock
from pgspecial.main import PGSpecial, NO_QUERY
from utils import run, dbtest, requires_json, requires_jsonb
@@ -281,6 +282,73 @@ def test_execute_from_file_io_error(os, executor, pgspecial):
assert success == False
assert is_special == True
+@dbtest
+def test_execute_from_commented_file_that_executes_another_file(executor, pgspecial, tmpdir):
+ # https://github.com/dbcli/pgcli/issues/1336
+ sqlfile1 = tmpdir.join("test01.sql")
+ sqlfile1.write("-- asdf \n\\h")
+ sqlfile2 = tmpdir.join("test00.sql")
+ sqlfile2.write("--An useless comment;\nselect now();\n-- another useless comment")
+
+ rcfile = str(tmpdir.join("rcfile"))
+ print(rcfile)
+ cli = PGCli(pgexecute=executor, pgclirc_file=rcfile)
+ assert cli != None
+ statement = "--comment\n\\h"
+ result = run(executor, statement, pgspecial=cli.pgspecial)
+ assert result != None
+ assert result[0].find("ALTER TABLE")
+
+@dbtest
+def test_execute_commented_first_line_and_special(executor, pgspecial, tmpdir):
+ # https://github.com/dbcli/pgcli/issues/1336
+
+ # just some base caes that should work also
+ statement = "--comment\nselect now();"
+ result = run(executor, statement, pgspecial=pgspecial)
+ assert result != None
+ assert result[1].find("now") >= 0
+
+ statement = "/*comment*/\nselect now();"
+ result = run(executor, statement, pgspecial=pgspecial)
+ assert result != None
+ assert result[1].find("now") >= 0
+
+ statement = "/*comment\ncomment line2*/\nselect now();"
+ result = run(executor, statement, pgspecial=pgspecial)
+ assert result != None
+ assert result[1].find("now") >= 0
+
+ statement = "--comment\n\\h"
+ result = run(executor, statement, pgspecial=pgspecial)
+ assert result != None
+ assert result[1].find("ALTER") >= 0
+ assert result[1].find("ABORT") >= 0
+
+ statement = "/*comment*/\n\h;"
+ result = run(executor, statement, pgspecial=pgspecial)
+ assert result != None
+ assert result[1].find("ALTER") >= 0
+ assert result[1].find("ABORT") >= 0
+
+ statement = " /*comment*/\n\h;"
+ result = run(executor, statement, pgspecial=pgspecial)
+ assert result != None
+ assert result[1].find("ALTER") >= 0
+ assert result[1].find("ABORT") >= 0
+
+ statement = "/*comment\ncomment line2*/\n\h;"
+ result = run(executor, statement, pgspecial=pgspecial)
+ assert result != None
+ assert result[1].find("ALTER") >= 0
+ assert result[1].find("ABORT") >= 0
+
+ statement = " /*comment\ncomment line2*/\n\h;"
+ result = run(executor, statement, pgspecial=pgspecial)
+ assert result != None
+ assert result[1].find("ALTER") >= 0
+ assert result[1].find("ABORT") >= 0
+
@dbtest
def test_multiple_queries_same_line(executor):