MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / get_indexes

Method get_indexes

lib/sqlalchemy/dialects/sqlite/base.py:2915–3002  ·  view source on GitHub ↗
(self, connection, table_name, schema=None, **kw)

Source from the content-addressed store, hash-verified

2913
2914 @reflection.cache
2915 def get_indexes(self, connection, table_name, schema=None, **kw):
2916 pragma_indexes = self._get_table_pragma(
2917 connection, "index_list", table_name, schema=schema
2918 )
2919 indexes = []
2920
2921 # regular expression to extract the filter predicate of a partial
2922 # index. this could fail to extract the predicate correctly on
2923 # indexes created like
2924 # CREATE INDEX i ON t (col || ') where') WHERE col <> ''
2925 # but as this function does not support expression-based indexes
2926 # this case does not occur.
2927 partial_pred_re = re.compile(r"\)\s+where\s+(.+)", re.IGNORECASE)
2928
2929 if schema:
2930 schema_expr = "%s." % self.identifier_preparer.quote_identifier(
2931 schema
2932 )
2933 else:
2934 schema_expr = ""
2935
2936 include_auto_indexes = kw.pop("include_auto_indexes", False)
2937 for row in pragma_indexes:
2938 # ignore implicit primary key index.
2939 # https://www.mail-archive.com/sqlite-users@sqlite.org/msg30517.html
2940 if not include_auto_indexes and row[1].startswith(
2941 "sqlite_autoindex"
2942 ):
2943 continue
2944 indexes.append(
2945 dict(
2946 name=row[1],
2947 column_names=[],
2948 unique=row[2],
2949 dialect_options={},
2950 )
2951 )
2952
2953 # check partial indexes
2954 if len(row) >= 5 and row[4]:
2955 s = (
2956 "SELECT sql FROM %(schema)ssqlite_master "
2957 "WHERE name = ? "
2958 "AND type = 'index'" % {"schema": schema_expr}
2959 )
2960 rs = connection.exec_driver_sql(s, (row[1],))
2961 index_sql = rs.scalar()
2962 predicate_match = partial_pred_re.search(index_sql)
2963 if predicate_match is None:
2964 # unless the regex is broken this case shouldn't happen
2965 # because we know this is a partial index, so the
2966 # definition sql should match the regex
2967 util.warn(
2968 "Failed to look up filter predicate of "
2969 "partial index %s" % row[1]
2970 )
2971 else:
2972 predicate = predicate_match.group(1)

Callers 1

Calls 14

_get_table_pragmaMethod · 0.95
has_tableMethod · 0.95
textFunction · 0.85
quote_identifierMethod · 0.80
indexesMethod · 0.80
compileMethod · 0.45
popMethod · 0.45
startswithMethod · 0.45
appendMethod · 0.45
exec_driver_sqlMethod · 0.45
scalarMethod · 0.45
warnMethod · 0.45

Tested by

no test coverage detected