feat(sqlalchemy): 新增 sqlalchemy.type 相关内容
This commit is contained in:
0
nc_http/core/sqlalchemy/__init__.py
Normal file
0
nc_http/core/sqlalchemy/__init__.py
Normal file
0
nc_http/core/sqlalchemy/type/__init__.py
Normal file
0
nc_http/core/sqlalchemy/type/__init__.py
Normal file
28
nc_http/core/sqlalchemy/type/dot_set.py
Normal file
28
nc_http/core/sqlalchemy/type/dot_set.py
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
from sqlalchemy.types import TypeDecorator, String
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidDotSetException(Exception):
|
||||||
|
"""
|
||||||
|
无效的 DotSet
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class DotSet(TypeDecorator):
|
||||||
|
impl = String
|
||||||
|
|
||||||
|
def process_bind_param(self, value, dialect):
|
||||||
|
if value:
|
||||||
|
if isinstance(value, list):
|
||||||
|
value = ','.join(str(i) for i in value)
|
||||||
|
else:
|
||||||
|
raise InvalidDotSetException
|
||||||
|
return value
|
||||||
|
|
||||||
|
return ''
|
||||||
|
|
||||||
|
def process_result_value(self, value, dialect):
|
||||||
|
if value:
|
||||||
|
value = value.split(',')
|
||||||
|
return value
|
||||||
|
|
||||||
|
return []
|
26
nc_http/core/sqlalchemy/type/json.py
Normal file
26
nc_http/core/sqlalchemy/type/json.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
from sqlalchemy.types import TypeDecorator, Text
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidJsonException(Exception):
|
||||||
|
"""
|
||||||
|
无效的 Json
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class Json(TypeDecorator):
|
||||||
|
impl = Text
|
||||||
|
|
||||||
|
def process_bind_param(self, value, dialect):
|
||||||
|
if value is not None:
|
||||||
|
if isinstance(value, (dict, list)):
|
||||||
|
value = json.dumps(value)
|
||||||
|
else:
|
||||||
|
raise InvalidJsonException
|
||||||
|
return value
|
||||||
|
|
||||||
|
def process_result_value(self, value, dialect):
|
||||||
|
if value is not None:
|
||||||
|
value = json.loads(value)
|
||||||
|
return value
|
Reference in New Issue
Block a user