diff --git a/nc_http/core/sqlalchemy/__init__.py b/nc_http/core/sqlalchemy/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nc_http/core/sqlalchemy/type/__init__.py b/nc_http/core/sqlalchemy/type/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nc_http/core/sqlalchemy/type/dot_set.py b/nc_http/core/sqlalchemy/type/dot_set.py new file mode 100644 index 0000000..9b50b88 --- /dev/null +++ b/nc_http/core/sqlalchemy/type/dot_set.py @@ -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 [] diff --git a/nc_http/core/sqlalchemy/type/json.py b/nc_http/core/sqlalchemy/type/json.py new file mode 100644 index 0000000..9dac9db --- /dev/null +++ b/nc_http/core/sqlalchemy/type/json.py @@ -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