mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 01:14:31 +02:00
.indexes property for introspecting indexes
This commit is contained in:
parent
e04f509c51
commit
c611ed80d2
3 changed files with 46 additions and 1 deletions
|
|
@ -167,6 +167,16 @@ The ``.schema`` property outputs the table's schema as a SQL string::
|
|||
FOREIGN KEY ("qCareAssistant") REFERENCES [qCareAssistant](id),
|
||||
FOREIGN KEY ("qLegalStatus") REFERENCES [qLegalStatus](id))
|
||||
|
||||
The ``.indexes`` property shows you all indexes created for a table::
|
||||
|
||||
>>> db["Street_Tree_List"].indexes
|
||||
[Index(seq=0, name='"Street_Tree_List_qLegalStatus"', unique=0, origin='c', partial=0, columns=['qLegalStatus']),
|
||||
Index(seq=1, name='"Street_Tree_List_qCareAssistant"', unique=0, origin='c', partial=0, columns=['qCareAssistant']),
|
||||
Index(seq=2, name='"Street_Tree_List_qSiteInfo"', unique=0, origin='c', partial=0, columns=['qSiteInfo']),
|
||||
Index(seq=3, name='"Street_Tree_List_qSpecies"', unique=0, origin='c', partial=0, columns=['qSpecies']),
|
||||
Index(seq=4, name='"Street_Tree_List_qCaretaker"', unique=0, origin='c', partial=0, columns=['qCaretaker']),
|
||||
Index(seq=5, name='"Street_Tree_List_PlantType"', unique=0, origin='c', partial=0, columns=['PlantType'])]
|
||||
|
||||
Enabling full-text search
|
||||
=========================
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ Column = namedtuple(
|
|||
ForeignKey = namedtuple(
|
||||
"ForeignKey", ("table", "column", "other_table", "other_column")
|
||||
)
|
||||
Index = namedtuple("Index", ("seq", "name", "unique", "origin", "partial", "columns"))
|
||||
|
||||
|
||||
class Database:
|
||||
|
|
@ -122,6 +123,18 @@ class Table:
|
|||
"select sql from sqlite_master where name = ?", (self.name,)
|
||||
).fetchone()[0]
|
||||
|
||||
@property
|
||||
def indexes(self):
|
||||
sql = "select * from pragma_index_list(?)"
|
||||
indexes = []
|
||||
for row in list(self.db.conn.execute(sql, (self.name,)).fetchall()):
|
||||
column_sql = "select name from pragma_index_info(?) order by seqno"
|
||||
columns = [
|
||||
r[0] for r in self.db.conn.execute(column_sql, (row[1],)).fetchall()
|
||||
]
|
||||
indexes.append(Index(*(row + (columns,))))
|
||||
return indexes
|
||||
|
||||
def create(self, columns, pk=None, foreign_keys=None):
|
||||
columns = {name: value for (name, value) in columns.items()}
|
||||
self.db.create_table(self.name, columns, pk=pk, foreign_keys=foreign_keys)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
from .fixtures import existing_db
|
||||
from .fixtures import existing_db, fresh_db
|
||||
from sqlite_utils.db import Index
|
||||
|
||||
|
||||
def test_table_names(existing_db):
|
||||
|
|
@ -27,3 +28,24 @@ def test_schema(existing_db):
|
|||
|
||||
def test_table_repr(existing_db):
|
||||
assert "<Table foo>" == repr(existing_db["foo"])
|
||||
|
||||
|
||||
def test_indexes(fresh_db):
|
||||
fresh_db.conn.executescript(
|
||||
"""
|
||||
create table Gosh (c1 text, c2 text, c3 text);
|
||||
create index Gosh_c1 on Gosh(c1);
|
||||
create index Gosh_c2c3 on Gosh(c2, c3);
|
||||
"""
|
||||
)
|
||||
assert [
|
||||
Index(
|
||||
seq=0,
|
||||
name="Gosh_c2c3",
|
||||
unique=0,
|
||||
origin="c",
|
||||
partial=0,
|
||||
columns=["c2", "c3"],
|
||||
),
|
||||
Index(seq=1, name="Gosh_c1", unique=0, origin="c", partial=0, columns=["c1"]),
|
||||
] == fresh_db["Gosh"].indexes
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue