From 1c4b2a414c6f39bfa883dc548131a98f007ace9a Mon Sep 17 00:00:00 2001 From: ERYoung11 <78834571+ERYoung11@users.noreply.github.com> Date: Wed, 31 Aug 2022 21:39:24 -0500 Subject: [PATCH 1/6] fixed comments with special see Issue #1336 --- pgcli/pgexecute.py | 23 ++++++++++++-- tests/test_pgexecute.py | 68 +++++++++++++++++++++++++++++++++++++++++ 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): From f7b1621e34dc890db4b6948d2373113dbaef659f Mon Sep 17 00:00:00 2001 From: ERYoung11 <78834571+ERYoung11@users.noreply.github.com> Date: Wed, 31 Aug 2022 21:39:24 -0500 Subject: [PATCH 2/6] fixed comments with special see Issue #1362 --- pgcli/pgexecute.py | 23 ++++++++++++-- tests/test_pgexecute.py | 68 +++++++++++++++++++++++++++++++++++++++++ 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): From 7a022a5f7430ba690742d976f87b32d9006136d4 Mon Sep 17 00:00:00 2001 From: ERYoung11 <78834571+ERYoung11@users.noreply.github.com> Date: Wed, 31 Aug 2022 21:44:27 -0500 Subject: [PATCH 3/6] Removed unused re --- tests/test_pgexecute.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_pgexecute.py b/tests/test_pgexecute.py index 05313c72..67959633 100644 --- a/tests/test_pgexecute.py +++ b/tests/test_pgexecute.py @@ -2,7 +2,6 @@ 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 From f5669e756b0d741d3287647bc1f62606762bd55b Mon Sep 17 00:00:00 2001 From: ERYoung11 <78834571+ERYoung11@users.noreply.github.com> Date: Wed, 31 Aug 2022 21:46:26 -0500 Subject: [PATCH 4/6] Updated changelog.rst. --- changelog.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog.rst b/changelog.rst index 1126f44f..dd3a5f1d 100644 --- a/changelog.rst +++ b/changelog.rst @@ -5,6 +5,7 @@ Bug fixes: ---------- * Fix exception when retrieving password from keyring ([issue 1338](https://github.com/dbcli/pgcli/issues/1338)). +* Fix using comments with special commands ([issue 1362](https://github.com/dbcli/pgcli/issues/1362)). Internal: --------- From 2850c8331050e502039cd9ca71407026fe2f409f Mon Sep 17 00:00:00 2001 From: ERYoung11 <78834571+ERYoung11@users.noreply.github.com> Date: Wed, 31 Aug 2022 21:48:06 -0500 Subject: [PATCH 5/6] black'd the code. --- pgcli/pgexecute.py | 14 +++++++------- tests/test_pgexecute.py | 10 +++++++--- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/pgcli/pgexecute.py b/pgcli/pgexecute.py index 85a52c0d..8f2968d6 100644 --- a/pgcli/pgexecute.py +++ b/pgcli/pgexecute.py @@ -308,22 +308,22 @@ class PGExecute: # sql parse doesn't split on a comment first + special # so we're going to do it - - sqltemp=[] - sqlarr=[] + + sqltemp = [] + sqlarr = [] if statement.startswith("--"): - sqltemp=statement.split("\n") + 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]+"*/" + sqltemp = statement.split("*/") + sqltemp[0] = sqltemp[0] + "*/" for i in sqlparse.split(sqltemp[1]): sqlarr.append(i) else: - sqlarr = sqlparse.split(statement) + sqlarr = sqlparse.split(statement) # run each sql query for sql in sqlarr: diff --git a/tests/test_pgexecute.py b/tests/test_pgexecute.py index 67959633..51f9034c 100644 --- a/tests/test_pgexecute.py +++ b/tests/test_pgexecute.py @@ -281,8 +281,11 @@ 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): +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") @@ -296,7 +299,8 @@ def test_execute_from_commented_file_that_executes_another_file(executor, pgspec statement = "--comment\n\\h" result = run(executor, statement, pgspecial=cli.pgspecial) assert result != None - assert result[0].find("ALTER TABLE") + assert result[0].find("ALTER TABLE") + @dbtest def test_execute_commented_first_line_and_special(executor, pgspecial, tmpdir): @@ -316,7 +320,7 @@ def test_execute_commented_first_line_and_special(executor, pgspecial, tmpdir): statement = "/*comment\ncomment line2*/\nselect now();" result = run(executor, statement, pgspecial=pgspecial) assert result != None - assert result[1].find("now") >= 0 + assert result[1].find("now") >= 0 statement = "--comment\n\\h" result = run(executor, statement, pgspecial=pgspecial) From c93b15e98f994bfdb50bb83964a0f9d41f9796e0 Mon Sep 17 00:00:00 2001 From: ERYoung11 <78834571+ERYoung11@users.noreply.github.com> Date: Wed, 31 Aug 2022 21:51:09 -0500 Subject: [PATCH 6/6] oy, updated the issue. --- tests/test_pgexecute.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_pgexecute.py b/tests/test_pgexecute.py index 51f9034c..d6d2f931 100644 --- a/tests/test_pgexecute.py +++ b/tests/test_pgexecute.py @@ -304,7 +304,7 @@ def test_execute_from_commented_file_that_executes_another_file( @dbtest def test_execute_commented_first_line_and_special(executor, pgspecial, tmpdir): - # https://github.com/dbcli/pgcli/issues/1336 + # https://github.com/dbcli/pgcli/issues/1362 # just some base caes that should work also statement = "--comment\nselect now();"