From 14ef1200655da59323fa331dd1c420d19b5fcbf1 Mon Sep 17 00:00:00 2001 From: han0 Date: Mon, 9 Aug 2021 11:47:29 +0800 Subject: [PATCH] =?UTF-8?q?feat(sqlalchemy):=20=E6=96=B0=E5=A2=9E=20sqlalc?= =?UTF-8?q?hemy.type=20=E7=9B=B8=E5=85=B3=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nc_http/core/sqlalchemy/__init__.py | 0 nc_http/core/sqlalchemy/type/__init__.py | 0 nc_http/core/sqlalchemy/type/dot_set.py | 28 ++++++++++++++++++++++++ nc_http/core/sqlalchemy/type/json.py | 26 ++++++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 nc_http/core/sqlalchemy/__init__.py create mode 100644 nc_http/core/sqlalchemy/type/__init__.py create mode 100644 nc_http/core/sqlalchemy/type/dot_set.py create mode 100644 nc_http/core/sqlalchemy/type/json.py 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