mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-27 12:24:11 +02:00
database.triggers_dict, closes #216
This commit is contained in:
parent
3d041d34d5
commit
de08096989
3 changed files with 23 additions and 6 deletions
|
|
@ -1476,6 +1476,15 @@ The ``table.triggers_dict`` property returns the triggers for that table as a di
|
||||||
'authors_ad': 'CREATE TRIGGER [authors_ad] AFTER DELETE...',
|
'authors_ad': 'CREATE TRIGGER [authors_ad] AFTER DELETE...',
|
||||||
'authors_au': 'CREATE TRIGGER [authors_au] AFTER UPDATE'}
|
'authors_au': 'CREATE TRIGGER [authors_au] AFTER UPDATE'}
|
||||||
|
|
||||||
|
The same property exists on the database, and will return all triggers across all tables:
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
>>> db.triggers_dict
|
||||||
|
{'authors_ai': 'CREATE TRIGGER [authors_ai] AFTER INSERT...',
|
||||||
|
'authors_ad': 'CREATE TRIGGER [authors_ad] AFTER DELETE...',
|
||||||
|
'authors_au': 'CREATE TRIGGER [authors_au] AFTER UPDATE'}
|
||||||
|
|
||||||
The ``detect_fts()`` method returns the associated SQLite FTS table name, if one exists for this table. If the table has not been configured for full-text search it returns ``None``.
|
The ``detect_fts()`` method returns the associated SQLite FTS table name, if one exists for this table. If the table has not been configured for full-text search it returns ``None``.
|
||||||
|
|
||||||
::
|
::
|
||||||
|
|
|
||||||
|
|
@ -272,6 +272,11 @@ class Database:
|
||||||
).fetchall()
|
).fetchall()
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def triggers_dict(self):
|
||||||
|
"Returns {trigger_name: sql} dictionary"
|
||||||
|
return {trigger.name: trigger.sql for trigger in self.triggers}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def journal_mode(self):
|
def journal_mode(self):
|
||||||
return self.execute("PRAGMA journal_mode;").fetchone()[0]
|
return self.execute("PRAGMA journal_mode;").fetchone()[0]
|
||||||
|
|
|
||||||
|
|
@ -127,9 +127,10 @@ def test_triggers_and_triggers_dict(fresh_db):
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
fresh_db["other"].insert({"foo": "bar"})
|
fresh_db["other"].insert({"foo": "bar"})
|
||||||
assert [] == authors.triggers
|
assert authors.triggers == []
|
||||||
assert {} == authors.triggers_dict
|
assert authors.triggers_dict == {}
|
||||||
assert [] == fresh_db["other"].triggers
|
assert fresh_db["other"].triggers == []
|
||||||
|
assert fresh_db.triggers_dict == {}
|
||||||
authors.enable_fts(
|
authors.enable_fts(
|
||||||
["name", "famous_works"], fts_version="FTS4", create_triggers=True
|
["name", "famous_works"], fts_version="FTS4", create_triggers=True
|
||||||
)
|
)
|
||||||
|
|
@ -142,13 +143,15 @@ def test_triggers_and_triggers_dict(fresh_db):
|
||||||
assert expected_triggers == {
|
assert expected_triggers == {
|
||||||
(t.name, t.table) for t in fresh_db["authors"].triggers
|
(t.name, t.table) for t in fresh_db["authors"].triggers
|
||||||
}
|
}
|
||||||
assert authors.triggers_dict == {
|
expected_triggers = {
|
||||||
"authors_ai": "CREATE TRIGGER [authors_ai] AFTER INSERT ON [authors] BEGIN\n INSERT INTO [authors_fts] (rowid, [name], [famous_works]) VALUES (new.rowid, new.[name], new.[famous_works]);\nEND",
|
"authors_ai": "CREATE TRIGGER [authors_ai] AFTER INSERT ON [authors] BEGIN\n INSERT INTO [authors_fts] (rowid, [name], [famous_works]) VALUES (new.rowid, new.[name], new.[famous_works]);\nEND",
|
||||||
"authors_ad": "CREATE TRIGGER [authors_ad] AFTER DELETE ON [authors] BEGIN\n INSERT INTO [authors_fts] ([authors_fts], rowid, [name], [famous_works]) VALUES('delete', old.rowid, old.[name], old.[famous_works]);\nEND",
|
"authors_ad": "CREATE TRIGGER [authors_ad] AFTER DELETE ON [authors] BEGIN\n INSERT INTO [authors_fts] ([authors_fts], rowid, [name], [famous_works]) VALUES('delete', old.rowid, old.[name], old.[famous_works]);\nEND",
|
||||||
"authors_au": "CREATE TRIGGER [authors_au] AFTER UPDATE ON [authors] BEGIN\n INSERT INTO [authors_fts] ([authors_fts], rowid, [name], [famous_works]) VALUES('delete', old.rowid, old.[name], old.[famous_works]);\n INSERT INTO [authors_fts] (rowid, [name], [famous_works]) VALUES (new.rowid, new.[name], new.[famous_works]);\nEND",
|
"authors_au": "CREATE TRIGGER [authors_au] AFTER UPDATE ON [authors] BEGIN\n INSERT INTO [authors_fts] ([authors_fts], rowid, [name], [famous_works]) VALUES('delete', old.rowid, old.[name], old.[famous_works]);\n INSERT INTO [authors_fts] (rowid, [name], [famous_works]) VALUES (new.rowid, new.[name], new.[famous_works]);\nEND",
|
||||||
}
|
}
|
||||||
assert [] == fresh_db["other"].triggers
|
assert authors.triggers_dict == expected_triggers
|
||||||
assert {} == fresh_db["other"].triggers_dict
|
assert fresh_db["other"].triggers == []
|
||||||
|
assert fresh_db["other"].triggers_dict == {}
|
||||||
|
assert fresh_db.triggers_dict == expected_triggers
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue