Skip to content

Commit

Permalink
Fix chdb dbapi to properly escape slashes during inserts.
Browse files Browse the repository at this point in the history
  • Loading branch information
bocharov committed Jan 23, 2025
1 parent d26d2ce commit eb9f28f
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 eb9f28f

Please sign in to comment.