Skip to content

Commit

Permalink
Merge pull request #297 from bocharov/alex/fix-escape-slash
Browse files Browse the repository at this point in the history
Fix chdb dbapi to properly escape slashes during inserts.
  • Loading branch information
auxten authored Feb 20, 2025
2 parents d389885 + 8371c33 commit 687afa9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
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()

0 comments on commit 687afa9

Please sign in to comment.