Skip to content

Commit fc358c8

Browse files
committed
Quotes in JSON property lookup πŸ§™πŸΎβ€β™€οΈ
1 parent 1e0387c commit fc358c8

File tree

5 files changed

+18
-10
lines changed

5 files changed

+18
-10
lines changed

β€ŽCHANGELOG.mdβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.7.1 : 2020-08-27
4+
5+
- **Fix**: Use quotes in JSON column type keys in `Property`
6+
37
## 0.7.0 : 2020-08-27
48

59
- **Feature**: Now is possible to pass `Distinct` to `Count` to have `COUNT(DISTINCT property)` syntax in pSQL

β€Žduckql/properties/property.pyβ€Ž

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pydantic import validator
1+
from exceptions import ParseError
22

33
try:
44
from typing import Literal
@@ -19,11 +19,15 @@ class Config:
1919
'description': "Object representation of SQL column/property"
2020
}
2121

22-
@validator('name', pre=True)
23-
def escape_name(cls, v):
24-
return cls.escape(v)
25-
2622
def to_sql(self) -> str:
23+
if '->' in self.name:
24+
bits = self.name.split('->')
25+
if len(bits) != 2:
26+
raise ParseError("duckQL doesn't support nested JSON lookups.")
27+
self.name = f"{self.escape(bits[0].strip())} -> '{self.escape(bits[1].strip())}'"
28+
else:
29+
self.name = self.escape(self.name)
30+
2731
sql = f"{self.name}"
2832

2933
if self.alias is not None:

β€Žduckql/properties/tests/test_property.pyβ€Ž

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ def test_json_field():
2323
json_property = Property(
2424
name='users.additional_data -> title'
2525
)
26-
assert str(json_property) == 'users.additional_data -> title'
26+
assert str(json_property) == "users.additional_data -> 'title'"
2727

2828

2929
def test_json_field_parser():
3030
json_property = Property.parse_raw(
3131
'{"obj": "properties.Property", "name": "users.additional_data -> title", "alias": "title"}'
3232
)
33-
assert json_property.name == 'users.additional_data -> title'
34-
assert str(json_property) == 'users.additional_data -> title AS "title"'
33+
assert json_property.name == "users.additional_data -> title"
34+
assert str(json_property) == "users.additional_data -> 'title' AS \"title\""
3535

3636

3737
def test_json_parse(valid_instance: Property):

β€Žduckql/version.pyβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.7.0'
1+
__version__ = '0.7.1'

β€Žpyproject.tomlβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "duckql-python"
3-
version = "0.7.0"
3+
version = "0.7.1"
44
description = "JSON declarative SQL conversion library"
55
authors = ["Jakub Dubec <jakub.dubec@gmail.com>"]
66
license = "MIT"

0 commit comments

Comments
Β (0)