Skip to content

Commit

Permalink
Add logic to escape Map data type to support insert statements. (#266)
Browse files Browse the repository at this point in the history
* Add logic to escape Map data type to support insert statements.
  • Loading branch information
subkanthi authored Oct 5, 2023
1 parent 1c7553d commit ddf9531
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
7 changes: 7 additions & 0 deletions clickhouse_sqlalchemy/drivers/http/escaper.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ def escape_item(self, item):
return "[" + ", ".join(
[str(self.escape_item(x)) for x in item]
) + "]"
elif isinstance(item, dict):
return "{" + ", ".join(
["{}: {}".format(
self.escape_item(k),
self.escape_item(v)
) for k, v in item.items()]
) + "}"
elif isinstance(item, enum.Enum):
return self.escape_string(item.name)
else:
Expand Down
2 changes: 1 addition & 1 deletion tests/drivers/http/test_escaping.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_escaper(self):
self.assertEqual(e.escape([Decimal('10')]), '[10.0]')
self.assertEqual(e.escape([10.0]), '[10.0]')
self.assertEqual(e.escape([date(2017, 1, 2)]), "['2017-01-02']")

self.assertEqual(e.escape(dict(x=10, y=20)), {'x': 10, 'y': 20})
with self.assertRaises(Exception) as ex:
e.escape([object()])

Expand Down
19 changes: 19 additions & 0 deletions tests/sql/test_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,22 @@ def test_insert(self):

rv = self.session.execute(select(table.c.x)).scalar()
self.assertEqual(rv, 'test')

@require_server_version(21, 1, 3)
def test_insert_map(self):
table = Table(
't', self.metadata(),
Column('x', types.Map(types.String, types.Int32),
primary_key=True),
engines.Memory()
)

with self.create_table(table):
dict_map = dict(key1=1, Key2=2)
x = [
{'x': dict_map}
]
self.session.execute(table.insert(), x)

rv = self.session.execute(select(table.c.x)).scalar()
self.assertEqual(rv, dict_map)

0 comments on commit ddf9531

Please sign in to comment.