Skip to content

Commit 687afa9

Browse files
authored
Merge pull request #297 from bocharov/alex/fix-escape-slash
Fix chdb dbapi to properly escape slashes during inserts.
2 parents d389885 + 8371c33 commit 687afa9

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

chdb/dbapi/converters.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def escape_float(value, mapping=None):
5959

6060
_escape_table = [chr(x) for x in range(128)]
6161
_escape_table[ord("'")] = u"''"
62+
_escape_table[ord("\\")] = "\\\\"
6263

6364

6465
def _escape_unicode(value, mapping=None):

tests/test_dbapi.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,35 @@ def test_select_chdb_version(self):
9090
self.assertEqual(ver, ".".join(ver_tuple))
9191
self.assertRegex(ver, expected_version_pattern)
9292

93+
def test_insert_escape_slash(self):
94+
# make a tmp dir context
95+
with tempfile.TemporaryDirectory() as tmpdirname:
96+
conn = dbapi.connect(tmpdirname)
97+
print(conn)
98+
cur = conn.cursor()
99+
# cur.execute("CREATE DATABASE IF NOT EXISTS test_db ENGINE = Atomic")
100+
# cur.execute("USE test_db")
101+
cur.execute(
102+
"""
103+
CREATE TABLE tmp (
104+
s String
105+
) ENGINE = Log"""
106+
)
107+
108+
# Insert single value
109+
s = "hello\\'world"
110+
print("Inserting string: ", s)
111+
cur.execute("INSERT INTO tmp VALUES (%s)", (s))
112+
113+
# Test fetchone
114+
cur.execute("SELECT s FROM tmp")
115+
row1 = cur.fetchone()
116+
self.assertEqual(row1[0], s)
117+
118+
# Clean up
119+
cur.close()
120+
conn.close()
121+
93122

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

0 commit comments

Comments
 (0)