Skip to content

Commit

Permalink
Move code to datatype.py
Browse files Browse the repository at this point in the history
  • Loading branch information
laserkaplan committed Sep 19, 2022
1 parent 20f41d2 commit be7788c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 30 deletions.
2 changes: 1 addition & 1 deletion tests/integration/test_sqlalchemy_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import sqlalchemy as sqla
from sqlalchemy.sql import and_, or_, not_

from trino.sqlalchemy.types import JSON
from trino.sqlalchemy.datatype import JSON


@pytest.fixture
Expand Down
19 changes: 17 additions & 2 deletions trino/sqlalchemy/datatype.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import re
from typing import Iterator, List, Optional, Tuple, Type, Union, Dict, Any

from sqlalchemy import util
from sqlalchemy.sql import sqltypes
from sqlalchemy.sql.type_api import TypeEngine
from sqlalchemy.sql.type_api import TypeDecorator, TypeEngine
from sqlalchemy.types import String

SQLType = Union[TypeEngine, Type[TypeEngine]]

Expand Down Expand Up @@ -71,6 +73,19 @@ def __init__(self, precision=None, timezone=False):
self.precision = precision


class JSON(TypeDecorator):
impl = String

def process_bind_param(self, value, dialect):
return json.dumps(value)

def process_result_value(self, value, dialect):
return json.loads(value)

def get_col_spec(self, **kw):
return 'JSON'


# https://trino.io/docs/current/language/types.html
_type_map = {
# === Boolean ===
Expand All @@ -90,7 +105,7 @@ def __init__(self, precision=None, timezone=False):
"varchar": sqltypes.VARCHAR,
"char": sqltypes.CHAR,
"varbinary": sqltypes.VARBINARY,
"json": sqltypes.JSON,
"json": JSON,
# === Date and time ===
"date": sqltypes.DATE,
"time": TIME,
Expand Down
27 changes: 0 additions & 27 deletions trino/sqlalchemy/types.py

This file was deleted.

0 comments on commit be7788c

Please sign in to comment.