mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-18 14:34:24 +02:00
Add an option to create triggers for fts table.
This commit is contained in:
parent
83ca4c802f
commit
68e63ad07f
1 changed files with 24 additions and 2 deletions
|
|
@ -710,8 +710,8 @@ class Table(Queryable):
|
|||
)
|
||||
self.db.add_foreign_keys([(self.name, column, other_table, other_column)])
|
||||
|
||||
def enable_fts(self, columns, fts_version="FTS5"):
|
||||
"Enables FTS on the specified columns"
|
||||
def enable_fts(self, columns, fts_version="FTS5", create_update_triggers=False):
|
||||
"Enables FTS on the specified columns."
|
||||
sql = """
|
||||
CREATE VIRTUAL TABLE "{table}_fts" USING {fts_version} (
|
||||
{columns},
|
||||
|
|
@ -724,6 +724,28 @@ class Table(Queryable):
|
|||
)
|
||||
self.db.conn.executescript(sql)
|
||||
self.populate_fts(columns)
|
||||
|
||||
if create_update_triggers:
|
||||
old_cols = ", ".join("old.[{}]".format(c) for c in columns)
|
||||
new_cols = ", ".join("new.[{}]".format(c) for c in columns)
|
||||
triggers = """
|
||||
CREATE TRIGGER "{table}_ai" AFTER INSERT ON "{table}" BEGIN
|
||||
INSERT INTO "{table}_fts" (rowid, {columns}) VALUES (new.rowid, {new_cols});
|
||||
END;
|
||||
CREATE TRIGGER "{table}_ad" AFTER DELETE ON "{table}" BEGIN
|
||||
INSERT INTO "{table}_fts" ("{table}_fts", rowid, {columns}) VALUES('delete', old.rowid, {old_cols});
|
||||
END;
|
||||
CREATE TRIGGER "{table}_au" AFTER UPDATE ON "{table}" BEGIN
|
||||
INSERT INTO "{table}_fts" ("{table}_fts", rowid, {columns}) VALUES('delete', old.rowid, {old_cols});
|
||||
INSERT INTO "{table}_fts" (rowid, {columns}) VALUES (new.rowid, {new_cols});
|
||||
END;
|
||||
""".format(
|
||||
table=self.name,
|
||||
columns=", ".join("[{}]".format(c) for c in columns),
|
||||
old_cols=old_cols,
|
||||
new_cols=new_cols,
|
||||
)
|
||||
self.db.conn.executescript(triggers)
|
||||
return self
|
||||
|
||||
def populate_fts(self, columns):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue