|
1 | 1 | import datetime |
2 | 2 | from telnetlib import BINARY |
3 | 3 | from iris.dbapi._DBAPI import Cursor |
| 4 | +from iris.dbapi._Column import _Column |
4 | 5 | from iris.dbapi._ResultSetRow import _ResultSetRow |
5 | 6 | from iris.dbapi._DBAPI import SQLType as IRISSQLType |
6 | 7 | import iris._IRISNative as irisnative |
@@ -516,21 +517,42 @@ def __init__(self, dialect): |
516 | 517 | class CursorWrapper(Cursor): |
517 | 518 | def __init__(self, connection): |
518 | 519 | super(CursorWrapper, self).__init__(connection) |
| 520 | + |
| 521 | + _types = { |
| 522 | + IRISSQLType.INTEGER: int, |
| 523 | + IRISSQLType.BIGINT: int, |
| 524 | + |
| 525 | + IRISSQLType.VARCHAR: str, |
| 526 | + } |
| 527 | + |
| 528 | + # Workaround for issue, when type of variable not the same as column type |
| 529 | + def _fix_type(self, value, sql_type: IRISSQLType): |
| 530 | + if value is None: |
| 531 | + return value |
| 532 | + |
| 533 | + try: |
| 534 | + expected_type = self._types.get(sql_type) |
| 535 | + if expected_type and not isinstance(value, expected_type): |
| 536 | + value = expected_type(value) |
| 537 | + except Exception: |
| 538 | + pass |
| 539 | + |
| 540 | + return value |
519 | 541 |
|
520 | 542 | def fetchone(self): |
521 | 543 | retval = super(CursorWrapper, self).fetchone() |
522 | 544 | if retval is None: |
523 | 545 | return None |
524 | 546 | if not isinstance(retval, _ResultSetRow.DataRow): |
525 | 547 | return retval |
| 548 | + # return retval[:] |
526 | 549 |
|
527 | 550 | # Workaround for fetchone, which returns values in row not from 0 |
528 | 551 | row = [] |
| 552 | + self._columns: list[_Column] |
529 | 553 | for c in self._columns: |
530 | | - value = retval[c.name] |
531 | | - # Workaround for issue, when int returned as string |
532 | | - if value is not None and c.type in (IRISSQLType.INTEGER, IRISSQLType.BIGINT,) and type(value) is not int: |
533 | | - value = int(value) |
| 554 | + value = retval[c.name] |
| 555 | + value = self._fix_type(value, c.type) |
534 | 556 | row.append(value) |
535 | 557 | return row |
536 | 558 |
|
@@ -846,14 +868,22 @@ def get_indexes(self, connection, table_name, schema=None, unique=False, **kw): |
846 | 868 |
|
847 | 869 | indexes = util.defaultdict(dict) |
848 | 870 | for row in rs: |
849 | | - indexrec = indexes[row["INDEX_NAME"]] |
| 871 | + ( |
| 872 | + idxname, |
| 873 | + colname, |
| 874 | + _, |
| 875 | + nuniq, |
| 876 | + _, |
| 877 | + ) = row |
| 878 | + |
| 879 | + indexrec = indexes[idxname] |
850 | 880 | if "name" not in indexrec: |
851 | | - indexrec["name"] = self.normalize_name(row["INDEX_NAME"]) |
| 881 | + indexrec["name"] = self.normalize_name(idxname) |
852 | 882 | indexrec["column_names"] = [] |
853 | | - indexrec["unique"] = not row["NON_UNIQUE"] |
| 883 | + indexrec["unique"] = not nuniq |
854 | 884 |
|
855 | 885 | indexrec["column_names"].append( |
856 | | - self.normalize_name(row["COLUMN_NAME"]) |
| 886 | + self.normalize_name(colname) |
857 | 887 | ) |
858 | 888 |
|
859 | 889 | indexes = list(indexes.values()) |
@@ -890,8 +920,12 @@ def get_pk_constraint(self, connection, table_name, schema=None, **kw): |
890 | 920 | constraint_name = None |
891 | 921 | pkfields = [] |
892 | 922 | for row in rs: |
893 | | - constraint_name = self.normalize_name(row["CONSTRAINT_NAME"]) |
894 | | - pkfields.append(self.normalize_name(row["COLUMN_NAME"])) |
| 923 | + ( |
| 924 | + name, |
| 925 | + colname, |
| 926 | + ) = row |
| 927 | + constraint_name = self.normalize_name(name) |
| 928 | + pkfields.append(self.normalize_name(colname)) |
895 | 929 |
|
896 | 930 | if pkfields: |
897 | 931 | return { |
@@ -1038,7 +1072,7 @@ def get_columns(self, connection, table_name, schema=None, **kw): |
1038 | 1072 | for row in c.mappings(): |
1039 | 1073 | name = row[columns.c.column_name] |
1040 | 1074 | type_ = row[columns.c.data_type].upper() |
1041 | | - nullable = row[columns.c.is_nullable] == "YES" |
| 1075 | + nullable = row[columns.c.is_nullable] |
1042 | 1076 | charlen = row[columns.c.character_maximum_length] |
1043 | 1077 | numericprec = row[columns.c.numeric_precision] |
1044 | 1078 | numericscale = row[columns.c.numeric_scale] |
|
0 commit comments