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

Method get_indexes

lib/sqlalchemy/dialects/sqlite/base.py:2915–3002  ·  lib/sqlalchemy/dialects/sqlite/base.py::SQLiteDialect.get_indexes
(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, class="st">"index_list", table_name, schema=schema
2918 )
2919 indexes = []
2920
2921 class="cm"># regular expression to extract the filter predicate of a partial
2922 class="cm"># index. this could fail to extract the predicate correctly on
2923 class="cm"># indexes created like
2924 class="cm"># CREATE INDEX i ON t (col || class="st">') where') WHERE col <> class="st">''
2925 class="cm"># but as this function does not support expression-based indexes
2926 class="cm"># this case does not occur.
2927 partial_pred_re = re.compile(rclass="st">"\)\s+where\s+(.+)", re.IGNORECASE)
2928
2929 if schema:
2930 schema_expr = class="st">"%s." % self.identifier_preparer.quote_identifier(
2931 schema
2932 )
2933 else:
2934 schema_expr = class="st">""
2935
2936 include_auto_indexes = kw.pop(class="st">"include_auto_indexes", False)
2937 for row in pragma_indexes:
2938 class="cm"># ignore implicit primary key index.
2939 class="cm"># https://www.mail-archive.com/sqlite-users@sqlite.org/msg30517.html
2940 if not include_auto_indexes and row[1].startswith(
2941 class="st">"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 class="cm"># check partial indexes
2954 if len(row) >= 5 and row[4]:
2955 s = (
2956 class="st">"SELECT sql FROM %(schema)ssqlite_master "
2957 class="st">"WHERE name = ? "
2958 class="st">"AND type = &class="cm">#x27;index'" % {class="st">"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 class="cm"># unless the regex is broken this case shouldn't happen
2965 class="cm"># because we know this is a partial index, so the
2966 class="cm"># definition sql should match the regex
2967 util.warn(
2968 class="st">"Failed to look up filter predicate of "
2969 class="st">"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