Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 93b6a2b

Browse files
remove spaces after comma in PG json, refactor
1 parent 973a63d commit 93b6a2b

File tree

3 files changed

+11
-66
lines changed

3 files changed

+11
-66
lines changed

sqeleton/abcs/mixins.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from abc import ABC, abstractmethod
2-
from .database_types import TemporalType, FractionalType, ColType_UUID, Boolean, ColType, String_UUID
2+
from .database_types import TemporalType, FractionalType, ColType_UUID, Boolean, ColType, String_UUID, JSONType
33
from .compiler import Compilable
44

55

@@ -49,6 +49,10 @@ def normalize_uuid(self, value: str, coltype: ColType_UUID) -> str:
4949
return f"TRIM({value})"
5050
return self.to_string(value)
5151

52+
def normalize_json(self, value: str, _coltype: JSONType) -> str:
53+
"""Creates an SQL expression, that converts 'value' to its minified json string representation."""
54+
raise NotImplementedError()
55+
5256
def normalize_value_by_type(self, value: str, coltype: ColType) -> str:
5357
"""Creates an SQL expression, that converts 'value' to a normalized representation.
5458
@@ -73,6 +77,8 @@ def normalize_value_by_type(self, value: str, coltype: ColType) -> str:
7377
return self.normalize_uuid(value, coltype)
7478
elif isinstance(coltype, Boolean):
7579
return self.normalize_boolean(value, coltype)
80+
elif isinstance(coltype, JSONType):
81+
return self.normalize_json(value, coltype)
7682
return self.to_string(value)
7783

7884

sqeleton/databases/postgresql.py

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
from ..abcs.database_types import (
2-
ColType,
32
Timestamp,
43
TimestampTZ,
54
Float,
65
Decimal,
76
Integer,
87
TemporalType,
9-
ColType_UUID,
108
Native_UUID,
119
Text,
1210
FractionalType,
@@ -52,35 +50,7 @@ def normalize_boolean(self, value: str, _coltype: Boolean) -> str:
5250
return self.to_string(f"{value}::int")
5351

5452
def normalize_json(self, value: str, _coltype: PostgresqlJSON) -> str:
55-
return f"replace({value}::text, '\": \"', '\":\"')" # minified json
56-
57-
def normalize_value_by_type(self, value: str, coltype: ColType) -> str:
58-
"""Creates an SQL expression, that converts 'value' to a normalized representation.
59-
60-
The returned expression must accept any SQL value, and return a string.
61-
62-
The default implementation dispatches to a method according to `coltype`:
63-
64-
::
65-
66-
TemporalType -> normalize_timestamp()
67-
FractionalType -> normalize_number()
68-
*else* -> to_string()
69-
70-
(`Integer` falls in the *else* category)
71-
72-
"""
73-
if isinstance(coltype, TemporalType):
74-
return self.normalize_timestamp(value, coltype)
75-
elif isinstance(coltype, FractionalType):
76-
return self.normalize_number(value, coltype)
77-
elif isinstance(coltype, ColType_UUID):
78-
return self.normalize_uuid(value, coltype)
79-
elif isinstance(coltype, Boolean):
80-
return self.normalize_boolean(value, coltype)
81-
elif isinstance(coltype, PostgresqlJSON):
82-
return self.normalize_json(value, coltype)
83-
return self.to_string(value)
53+
return f"replace(replace({value}::text, '\": ', '\":'), ', \"', ',\"')"
8454

8555

8656
class PostgresqlDialect(BaseDialect, Mixin_Schema):

sqeleton/databases/redshift.py

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
TemporalType,
55
FractionalType,
66
DbPath,
7-
RedShiftSuper,
8-
ColType,
9-
ColType_UUID,
10-
Boolean
7+
RedShiftSuper
118
)
129
from ..abcs.mixins import AbstractMixin_MD5
1310
from .postgresql import (
@@ -49,36 +46,8 @@ def normalize_timestamp(self, value: str, coltype: TemporalType) -> str:
4946
def normalize_number(self, value: str, coltype: FractionalType) -> str:
5047
return self.to_string(f"{value}::decimal(38,{coltype.precision})")
5148

52-
def normalize_super(self, value: str, _coltype: RedShiftSuper) -> str:
53-
return f'nvl2({value}, json_serialize({value}), NULL)' # only ::varchar causes redshift to return null
54-
55-
def normalize_value_by_type(self, value: str, coltype: ColType) -> str:
56-
"""Creates an SQL expression, that converts 'value' to a normalized representation.
57-
58-
The returned expression must accept any SQL value, and return a string.
59-
60-
The default implementation dispatches to a method according to `coltype`:
61-
62-
::
63-
64-
TemporalType -> normalize_timestamp()
65-
FractionalType -> normalize_number()
66-
*else* -> to_string()
67-
68-
(`Integer` falls in the *else* category)
69-
70-
"""
71-
if isinstance(coltype, TemporalType):
72-
return self.normalize_timestamp(value, coltype)
73-
elif isinstance(coltype, FractionalType):
74-
return self.normalize_number(value, coltype)
75-
elif isinstance(coltype, ColType_UUID):
76-
return self.normalize_uuid(value, coltype)
77-
elif isinstance(coltype, Boolean):
78-
return self.normalize_boolean(value, coltype)
79-
elif isinstance(coltype, RedShiftSuper):
80-
return self.normalize_super(value, coltype)
81-
return self.to_string(value)
49+
def normalize_json(self, value: str, _coltype: RedShiftSuper) -> str:
50+
return f'nvl2({value}, json_serialize({value}), NULL)'
8251

8352

8453
class Dialect(PostgresqlDialect):

0 commit comments

Comments
 (0)