Skip to content

Commit

Permalink
switch to new intersystems_iris package
Browse files Browse the repository at this point in the history
  • Loading branch information
daimor committed Nov 8, 2022
1 parent 585a500 commit dac7893
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 62 deletions.
71 changes: 11 additions & 60 deletions sqlalchemy_iris/base.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import datetime
from telnetlib import BINARY
from iris.dbapi._DBAPI import Cursor
from iris.dbapi._Column import _Column
from iris.dbapi._ResultSetRow import _ResultSetRow
from iris.dbapi._DBAPI import SQLType as IRISSQLType
import iris._IRISNative as irisnative
import iris.dbapi._DBAPI as dbapi
import intersystems_iris.dbapi._DBAPI as dbapi
from . import information_schema as ischema
from sqlalchemy import exc
from sqlalchemy.orm import aliased
Expand Down Expand Up @@ -519,63 +514,19 @@ def __init__(self, dialect):
dialect, omit_schema=False)


class CursorWrapper(Cursor):
def __init__(self, connection):
super(CursorWrapper, self).__init__(connection)

_types = {
IRISSQLType.INTEGER: int,
IRISSQLType.BIGINT: int,

IRISSQLType.VARCHAR: str,
}

# Workaround for issue, when type of variable not the same as column type
def _fix_type(self, value, sql_type: IRISSQLType):
if value is None:
return value

try:
expected_type = self._types.get(sql_type)
if expected_type and not isinstance(value, expected_type):
value = expected_type(value)
except Exception:
pass

return value

def fetchone(self):
retval = super(CursorWrapper, self).fetchone()
if retval is None:
return None
if not isinstance(retval, _ResultSetRow.DataRow):
return retval
# return retval[:]

# Workaround for fetchone, which returns values in row not from 0
row = []
self._columns: list[_Column]
for c in self._columns:
value = retval[c.name]
if c.tableName != 'None' and c.schema != 'None':
# print('_fix_type', [c.name, value, c.type, type(value), c.isAliased, c.isExpression, c.isKeyColumn, c.isIdentity, c.tableName is None, c.schema])
value = self._fix_type(value, c.type)
row.append(value)
return row


class IRISExecutionContext(default.DefaultExecutionContext):

def get_lastrowid(self):
cursor = self.create_cursor()
cursor.execute("SELECT LAST_IDENTITY()")
lastrowid = cursor.fetchone()[0]
cursor.close()
return lastrowid
try:
return self.cursor.lastrowid
except Exception:
cursor = self.cursor
cursor.execute("SELECT LAST_IDENTITY()")
lastrowid = cursor.fetchone()[0]
return lastrowid

def create_cursor(self):
# cursor = self._dbapi_connection.cursor()
cursor = CursorWrapper(self._dbapi_connection)
cursor = self._dbapi_connection.cursor()
return cursor


Expand Down Expand Up @@ -700,7 +651,7 @@ def __init__(self, **kwargs):
)

def _get_option(self, connection, option):
cursor = CursorWrapper(connection)
cursor = connection.cursor()
# cursor = connection.cursor()
cursor.execute('SELECT %SYSTEM_SQL.Util_GetOption(?)', [option, ])
row = cursor.fetchone()
Expand All @@ -709,7 +660,7 @@ def _get_option(self, connection, option):
return None

def _set_option(self, connection, option, value):
cursor = CursorWrapper(connection)
cursor = connection.cursor()
# cursor = connection.cursor()
cursor.execute('SELECT %SYSTEM_SQL.Util_SetOption(?, ?)', [option, value, ])
row = cursor.fetchone()
Expand Down
4 changes: 2 additions & 2 deletions sqlalchemy_iris/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def emulated_lastrowid(self):
inserted id, would return closed/fail/skip for this.
"""
return exclusions.closed()
return exclusions.open()

@property
def emulated_lastrowid_even_with_sequences(self):
Expand All @@ -328,7 +328,7 @@ def dbapi_lastrowid(self):
cursor object.
"""
return exclusions.closed()
return exclusions.open()

@property
def views(self):
Expand Down

0 comments on commit dac7893

Please sign in to comment.