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

Fix chdb dbapi to properly escape slashes during inserts. #297

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions chdb/dbapi/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def escape_float(value, mapping=None):

_escape_table = [chr(x) for x in range(128)]
_escape_table[ord("'")] = u"''"
_escape_table[ord("\\")] = "\\\\"


def _escape_unicode(value, mapping=None):
Expand Down
29 changes: 29 additions & 0 deletions tests/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,35 @@ def test_select_chdb_version(self):
self.assertEqual(ver, ".".join(ver_tuple))
self.assertRegex(ver, expected_version_pattern)

def test_insert_escape_slash(self):
# make a tmp dir context
with tempfile.TemporaryDirectory() as tmpdirname:
conn = dbapi.connect(tmpdirname)
print(conn)
cur = conn.cursor()
# cur.execute("CREATE DATABASE IF NOT EXISTS test_db ENGINE = Atomic")
# cur.execute("USE test_db")
cur.execute(
"""
CREATE TABLE tmp (
s String
) ENGINE = Log"""
)

# Insert single value
s = "hello\\'world"
print("Inserting string: ", s)
cur.execute("INSERT INTO tmp VALUES (%s)", (s))

# Test fetchone
cur.execute("SELECT s FROM tmp")
row1 = cur.fetchone()
self.assertEqual(row1[0], s)

# Clean up
cur.close()
conn.close()


if __name__ == "__main__":
unittest.main()