Skip to content

Commit

Permalink
index with data support
Browse files Browse the repository at this point in the history
  • Loading branch information
daimor committed Nov 15, 2022
1 parent 389dcaa commit 4a1f299
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
53 changes: 52 additions & 1 deletion sqlalchemy_iris/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,30 @@ def get_column_specification(self, column, **kwargs):
def post_create_table(self, table):
return " WITH %CLASSPARAMETER ALLOWIDENTITYINSERT = 1"

def visit_create_index(
self, create, include_schema=False, include_table_schema=True, **kw
):
text = super().visit_create_index(create, include_schema, include_table_schema, **kw)

index = create.element
preparer = self.preparer

# handle other included columns
includeclause = index.dialect_options["iris"]["include"]
if includeclause:
inclusions = [
index.table.c[col]
if isinstance(col, util.string_types)
else col
for col in includeclause
]

text += " WITH DATA (%s)" % ", ".join(
[preparer.quote(c.name) for c in inclusions]
)

return text


class IRISTypeCompiler(compiler.GenericTypeCompiler):
def visit_boolean(self, type_, **kw):
Expand Down Expand Up @@ -667,6 +691,10 @@ class IRISDialect(default.DefaultDialect):
type_compiler = IRISTypeCompiler
execution_ctx_cls = IRISExecutionContext

construct_arguments = [
(schema.Index, {"include": None}),
]

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

Expand Down Expand Up @@ -808,6 +836,8 @@ def has_table(self, connection, table_name, schema=None, **kw):
def get_indexes(self, connection, table_name, schema=None, unique=False, **kw):
schema_name = self.get_schema(schema)
indexes = ischema.indexes
tables = ischema.tables
index_def = ischema.index_definition

s = (
sql.select(
Expand All @@ -816,6 +846,20 @@ def get_indexes(self, connection, table_name, schema=None, unique=False, **kw):
indexes.c.primary_key,
indexes.c.non_unique,
indexes.c.asc_or_desc,
index_def.c.Data,
)
.select_from(indexes)
.outerjoin(
index_def,
sql.and_(
index_def.c.SqlName == indexes.c.index_name,
index_def.c.parent ==
sql.select(tables.c.classname)
.where(
indexes.c.table_name == tables.c.table_name,
indexes.c.table_schema == tables.c.table_schema,
).scalar_subquery()
),
)
.where(
sql.and_(
Expand All @@ -838,6 +882,7 @@ def get_indexes(self, connection, table_name, schema=None, unique=False, **kw):
_,
nuniq,
_,
include,
) = row

indexrec = indexes[idxname]
Expand All @@ -849,6 +894,12 @@ def get_indexes(self, connection, table_name, schema=None, unique=False, **kw):
indexrec["column_names"].append(
self.normalize_name(colname)
)
include = include.split(',') if include else []
indexrec["include_columns"] = include
if include:
indexrec["dialect_options"] = {
"iris_include": include
}

indexes = list(indexes.values())
return indexes
Expand Down Expand Up @@ -1007,7 +1058,7 @@ def get_columns(self, connection, table_name, schema=None, **kw):
schema_name = self.get_schema(schema)
tables = ischema.tables
columns = ischema.columns
property = ischema.property
property = ischema.property_definition

whereclause = sql.and_(
columns.c.table_name == str(table_name),
Expand Down
11 changes: 10 additions & 1 deletion sqlalchemy_iris/information_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def process_result_value(self, value, dialect):
Column("DESCIPTION", String, key="desciption"),
schema="INFORMATION_SCHEMA",
)
property = Table(
property_definition = Table(
"PropertyDefinition",
ischema,
Column("parent", String),
Expand Down Expand Up @@ -101,6 +101,15 @@ def process_result_value(self, value, dialect):
schema="INFORMATION_SCHEMA",
)

index_definition = Table(
"IndexDefinition",
ischema,
Column("parent", String),
Column("SqlName", String),
Column("Data", String),
schema="%Dictionary",
)

key_constraints = Table(
"KEY_COLUMN_USAGE",
ischema,
Expand Down

0 comments on commit 4a1f299

Please sign in to comment.