mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 01:14:31 +02:00
enable_fts(), populate_fts() and search() methods
This commit is contained in:
parent
c446e22f34
commit
f4907f6df5
4 changed files with 100 additions and 1 deletions
|
|
@ -133,6 +133,28 @@ class Table:
|
|||
self.db.conn.commit()
|
||||
return result
|
||||
|
||||
def enable_fts(self, columns):
|
||||
"Enables FTS on the specified columns"
|
||||
sql = """
|
||||
CREATE VIRTUAL TABLE "{table}_fts" USING FTS4 (
|
||||
{columns},
|
||||
content="{table}"
|
||||
);
|
||||
""".format(
|
||||
table=self.name, columns=", ".join(columns)
|
||||
)
|
||||
self.db.conn.executescript(sql)
|
||||
self.populate_fts(columns)
|
||||
|
||||
def populate_fts(self, columns):
|
||||
sql = """
|
||||
INSERT INTO "{table}_fts" (rowid, {columns})
|
||||
SELECT rowid, {columns} FROM {table};
|
||||
""".format(
|
||||
table=self.name, columns=", ".join(columns)
|
||||
)
|
||||
self.db.conn.executescript(sql)
|
||||
|
||||
def detect_column_types(self, records):
|
||||
all_column_types = {}
|
||||
for record in records:
|
||||
|
|
@ -155,6 +177,18 @@ class Table:
|
|||
column_types[key] = t
|
||||
return column_types
|
||||
|
||||
def search(self, q):
|
||||
sql = """
|
||||
select * from {table} where rowid in (
|
||||
select rowid from [{table}_fts]
|
||||
where [{table}_fts] match :search
|
||||
)
|
||||
order by rowid
|
||||
""".format(
|
||||
table=self.name
|
||||
)
|
||||
return self.db.conn.execute(sql, (q,)).fetchall()
|
||||
|
||||
def insert(self, record, pk=None, foreign_keys=None, upsert=False):
|
||||
return self.insert_all(
|
||||
[record], pk=pk, foreign_keys=foreign_keys, upsert=upsert
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue