feat(sqlalchemy): 新增 sqlalchemy.type 相关内容

This commit is contained in:
han0
2021-08-09 11:47:29 +08:00
parent 9c118247e5
commit 14ef120065
4 changed files with 54 additions and 0 deletions

View File

View File

View 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 []

View 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