Skip to content

Commit

Permalink
Load iidbcapabilities and use to build column definition involving un…
Browse files Browse the repository at this point in the history
…ique constraints (#58)

* Load iidbcapabilities
* with local scope of iidbcapabilities to per session
  • Loading branch information
hab6 authored Jul 15, 2024
1 parent 262e6e8 commit e4d4ed7
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions lib/sqlalchemy_ingres/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,28 @@ def get_column_specification(self, column, **kwargs):
if (
column.identity is not None
or column.primary_key is True
or column.unique is True
or not column.nullable
):
colspec += " NOT NULL"
else:
for x in column.table.constraints:
if x.contains_column(column):
colspec += " NOT NULL"
break

# For columns specified as unique:
# - If table is Ingres, column cannot be nullable
# - If table is X100, column can be nullable
# https://docs.actian.com/actianx/12.0/index.html#page/SQLRef/Constraints.htm#ww124554
for constraint in column.table.constraints:
if constraint.contains_column(column): # Maybe redundant since checked again in for loop
for constraint_column in constraint:
if constraint_column == column:
if (
constraint_column.nullable is False
or constraint_column.unique is True
or constraint_column.primary_key is True
or isinstance(constraint, sqlalchemy.sql.schema.UniqueConstraint)
):
if self.dialect.iidbcapabilities['DBMS_TYPE'] == 'INGRES':
colspec += " NOT NULL"
break
return colspec

def visit_create_index(self, create):
Expand Down Expand Up @@ -392,12 +405,38 @@ class IngresDialect(default.DefaultDialect):
requires_name_normalization = True
sequences_optional = False
_isolation_lookup = isolation_lookup
iidbcapabilities = None
# TODO get_isolation_level()
# TODO _check_max_identifier_length()

def __init__(self, **kwargs):
default.DefaultDialect.__init__(self, **kwargs)

def initialize(self, connection):
super().initialize(connection)
self._load_iidbcapabilities(connection)

def _load_iidbcapabilities(self, connection):
sqltext = """
SELECT
cap_capability,
cap_value
FROM
iidbcapabilities"""

rs = None
try:
rs = connection.exec_driver_sql(sqltext)
self.iidbcapabilities = {}
for row in rs.fetchall():
coldata = {}
self.iidbcapabilities[row[0].rstrip()] = row[1].rstrip()

rs.close()
finally:
if rs:
rs.close()

def get_isolation_level_values(self, connection):
return list(self._isolation_lookup)

Expand Down

0 comments on commit e4d4ed7

Please sign in to comment.