Skip to content

Commit

Permalink
fix support older engine
Browse files Browse the repository at this point in the history
  • Loading branch information
daimor committed Jun 21, 2024
1 parent 1113925 commit b69e366
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 210 deletions.
15 changes: 9 additions & 6 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,22 @@ jobs:
image:
- intersystemsdc/iris-community:latest
- intersystemsdc/iris-community:preview
- intersystemsdc/iris-community:2024.1-preview
engine:
- old
- new
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install requirements
run: |
pip install -r requirements-dev.txt \
-r requirements-iris.txt \
-e .
pip install tox
- name: Run Tests
run: |
pytest --container ${{ matrix.image }}
tox -e py311${{ matrix.engine }} -- --container ${{ matrix.image }}
deploy:
needs: test
if: github.event_name != 'pull_request'
Expand Down
5 changes: 3 additions & 2 deletions sqlalchemy_iris/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ def check_constraints(cls):
from .types import IRISTimeStamp
from .types import IRISDate
from .types import IRISDateTime
from .types import IRISUniqueIdentifier
from .types import IRISListBuild # noqa
from .types import IRISVector # noqa

Expand Down Expand Up @@ -819,8 +818,10 @@ def create_cursor(self):
sqltypes.DateTime: IRISDateTime,
sqltypes.TIMESTAMP: IRISTimeStamp,
sqltypes.Time: IRISTime,
sqltypes.UUID: IRISUniqueIdentifier,
}
if sqlalchemy_version.startswith("2."):
from .types import IRISUniqueIdentifier
colspecs[sqltypes.UUID] = IRISUniqueIdentifier


class IRISExact(ReturnTypeFromArgs):
Expand Down
117 changes: 62 additions & 55 deletions sqlalchemy_iris/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
from decimal import Decimal
from sqlalchemy import func, text
from sqlalchemy.sql import sqltypes
from sqlalchemy.types import UserDefinedType, Float
from sqlalchemy.types import UserDefinedType
from uuid import UUID as _python_UUID
from intersystems_iris import IRISList
from sqlalchemy import __version__ as sqlalchemy_version

HOROLOG_ORDINAL = datetime.date(1840, 12, 31).toordinal()

Expand Down Expand Up @@ -134,73 +135,79 @@ def process(value):
return process


class IRISUniqueIdentifier(sqltypes.Uuid):
def literal_processor(self, dialect):
if not self.as_uuid:
if sqlalchemy_version.startswith("2."):

def process(value):
return f"""'{value.replace("'", "''")}'"""

return process
else:

def process(value):
return f"""'{str(value).replace("'", "''")}'"""

return process

def bind_processor(self, dialect):
character_based_uuid = not dialect.supports_native_uuid or not self.native_uuid

if character_based_uuid:
if self.as_uuid:
class IRISUniqueIdentifier(sqltypes.Uuid):
def literal_processor(self, dialect):
if not self.as_uuid:

def process(value):
if value is not None:
value = str(value)
return value
return f"""'{value.replace("'", "''")}'"""

return process
else:

def process(value):
return value
return f"""'{str(value).replace("'", "''")}'"""

return process
else:
return None

def result_processor(self, dialect, coltype):
character_based_uuid = not dialect.supports_native_uuid or not self.native_uuid
def bind_processor(self, dialect):
character_based_uuid = (
not dialect.supports_native_uuid or not self.native_uuid
)

if character_based_uuid:
if self.as_uuid:
if character_based_uuid:
if self.as_uuid:

def process(value):
if value and not isinstance(value, _python_UUID):
value = _python_UUID(value)
return value
def process(value):
if value is not None:
value = str(value)
return value

return process
return process
else:

def process(value):
return value

return process
else:
return None

def process(value):
if value and isinstance(value, _python_UUID):
value = str(value)
return value
def result_processor(self, dialect, coltype):
character_based_uuid = (
not dialect.supports_native_uuid or not self.native_uuid
)

return process
else:
if not self.as_uuid:
if character_based_uuid:
if self.as_uuid:

def process(value):
if value and isinstance(value, _python_UUID):
value = str(value)
return value
def process(value):
if value and not isinstance(value, _python_UUID):
value = _python_UUID(value)
return value

return process
return process
else:

def process(value):
if value and isinstance(value, _python_UUID):
value = str(value)
return value

return process
else:
return None
if not self.as_uuid:

def process(value):
if value and isinstance(value, _python_UUID):
value = str(value)
return value

return process
else:
return None


class IRISListBuild(UserDefinedType):
Expand Down Expand Up @@ -267,9 +274,7 @@ def __init__(self, max_items: int = None, item_type: type = float):
item_type_server = (
"decimal"
if self.item_type is float
else "float"
if self.item_type is Decimal
else "int"
else "float" if self.item_type is Decimal else "int"
)
self.item_type_server = item_type_server

Expand Down Expand Up @@ -304,19 +309,21 @@ class comparator_factory(UserDefinedType.Comparator):
# return self.func('vector_l2', other)

def max_inner_product(self, other):
return self.func('vector_dot_product', other)
return self.func("vector_dot_product", other)

def cosine_distance(self, other):
return self.func('vector_cosine', other)
return self.func("vector_cosine", other)

def cosine(self, other):
return (1 - self.func('vector_cosine', other))
return 1 - self.func("vector_cosine", other)

def func(self, funcname: str, other):
if not isinstance(other, list) and not isinstance(other, tuple):
raise ValueError("expected list or tuple, got '%s'" % type(other))
othervalue = f"[{','.join([str(v) for v in other])}]"
return getattr(func, funcname)(self, func.to_vector(othervalue, text(self.type.item_type_server)))
return getattr(func, funcname)(
self, func.to_vector(othervalue, text(self.type.item_type_server))
)


class BIT(sqltypes.TypeEngine):
Expand Down
Loading

0 comments on commit b69e366

Please sign in to comment.