Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_substitute_params: restore parameter sequence code from prior to 5b5fb66f1212 #35

Conversation

zmedico
Copy link
Collaborator

@zmedico zmedico commented Feb 28, 2021

Restore parameter sequence code from prior to commit
5b5fb66. This unit test case demonstrates the problem
reported in #30:

This unit test case demonstrates the problem reported in
#30:

    def test_CheckRowcountExecute(self):
        self.cu.execute("delete from test")
        self.cu.execute("insert into test(name, income) values (?, ?)", ("?", "1"))
        self.cu.execute("select name from test where name=?", ("?",))
        self.assertEqual(self.cu.rowcount, 1,
            msg="test failed for https://github.com/rqlite/pyrqlite/issues/30")
src/test/test_dbapi.py:346:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <pyrqlite.cursors.Cursor object at 0x7f16e1abc400>, operation = "insert into test(name, income) values (''1'', ?)", parameters = ('?', '1')

    def execute(self, operation, parameters=None):
        if not isinstance(operation, basestring):
            raise ValueError(
                             "argument must be a string, not '{}'".format(type(operation).__name__))

        operation = self._substitute_params(operation, parameters)

        command = self._get_sql_command(operation)
        if command in ('SELECT', 'PRAGMA'):
            payload = self._request("GET",
                                    "/db/query?" + _urlencode({'q': operation}))
        else:
            payload = self._request("POST", "/db/execute?transaction",
                                    headers={'Content-Type': 'application/json'}, body=json.dumps([operation]))

        last_insert_id = None
        rows_affected = -1
        payload_rows = {}
        try:
            results = payload["results"]
        except KeyError:
            pass
        else:
            rows_affected = 0
            for item in results:
                if 'error' in item:
                    logging.getLogger(__name__).error(json.dumps(item))
>                   raise Error(json.dumps(item))
E                   sqlite3.Error: {"error": "near \"1\": syntax error"}

pyrqlite/cursors.py:178: Error

Note that a similar problem still exists for named parameters, so #30 is not entirely fixed.

Reported-by: @jaysonlarose
See: #30
Fixes: 5b5fb66 ("Adding support for named parameters")

…fb66

Restore parameter sequence code from prior to commit
5b5fb66. This unit test case demonstrates the problem
reported in rqlite#30:

    def test_CheckRowcountExecute(self):
        self.cu.execute("delete from test")
        self.cu.execute("insert into test(name, income) values (?, ?)", ("?", "1"))
        self.cu.execute("select name from test where name=?", ("?",))
        self.assertEqual(self.cu.rowcount, 1,
            msg="test failed for rqlite#30")

src/test/test_dbapi.py:346:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <pyrqlite.cursors.Cursor object at 0x7f16e1abc400>, operation = "insert into test(name, income) values (''1'', ?)", parameters = ('?', '1')

    def execute(self, operation, parameters=None):
        if not isinstance(operation, basestring):
            raise ValueError(
                             "argument must be a string, not '{}'".format(type(operation).__name__))

        operation = self._substitute_params(operation, parameters)

        command = self._get_sql_command(operation)
        if command in ('SELECT', 'PRAGMA'):
            payload = self._request("GET",
                                    "/db/query?" + _urlencode({'q': operation}))
        else:
            payload = self._request("POST", "/db/execute?transaction",
                                    headers={'Content-Type': 'application/json'}, body=json.dumps([operation]))

        last_insert_id = None
        rows_affected = -1
        payload_rows = {}
        try:
            results = payload["results"]
        except KeyError:
            pass
        else:
            rows_affected = 0
            for item in results:
                if 'error' in item:
                    logging.getLogger(__name__).error(json.dumps(item))
>                   raise Error(json.dumps(item))
E                   sqlite3.Error: {"error": "near \"1\": syntax error"}

pyrqlite/cursors.py:178: Error

Note that a similar problem still exists for named
parameters, so rqlite#30
is not entirely fixed.

Reported-by: @jaysonlarose
See: rqlite#30
Fixes: 5b5fb66 ("Adding support for named parameters")
@zmedico zmedico merged commit b0bc6ab into rqlite:master Feb 28, 2021
@zmedico zmedico deleted the fix-incorrect-parameterization-for-parameter-sequence-issue-30 branch February 28, 2021 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant