mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-17 05:54:23 +02:00
Neater SQL indentation, because tracer means it could be visible now
This commit is contained in:
parent
6104cbd91a
commit
2797b3ac6c
1 changed files with 39 additions and 19 deletions
|
|
@ -867,11 +867,17 @@ class Table(Queryable):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def populate_fts(self, columns):
|
def populate_fts(self, columns):
|
||||||
sql = """
|
sql = (
|
||||||
|
textwrap.dedent(
|
||||||
|
"""
|
||||||
INSERT INTO [{table}_fts] (rowid, {columns})
|
INSERT INTO [{table}_fts] (rowid, {columns})
|
||||||
SELECT rowid, {columns} FROM [{table}];
|
SELECT rowid, {columns} FROM [{table}];
|
||||||
""".format(
|
"""
|
||||||
table=self.name, columns=", ".join("[{}]".format(c) for c in columns)
|
)
|
||||||
|
.strip()
|
||||||
|
.format(
|
||||||
|
table=self.name, columns=", ".join("[{}]".format(c) for c in columns)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
self.db.executescript(sql)
|
self.db.executescript(sql)
|
||||||
return self
|
return self
|
||||||
|
|
@ -881,12 +887,16 @@ class Table(Queryable):
|
||||||
if fts_table:
|
if fts_table:
|
||||||
self.db[fts_table].drop()
|
self.db[fts_table].drop()
|
||||||
# Now delete the triggers that related to that table
|
# Now delete the triggers that related to that table
|
||||||
sql = """
|
sql = (
|
||||||
|
textwrap.dedent(
|
||||||
|
"""
|
||||||
SELECT name FROM sqlite_master
|
SELECT name FROM sqlite_master
|
||||||
WHERE type = 'trigger'
|
WHERE type = 'trigger'
|
||||||
AND sql LIKE '% INSERT INTO [{}]%'
|
AND sql LIKE '% INSERT INTO [{}]%'
|
||||||
""".format(
|
"""
|
||||||
fts_table
|
)
|
||||||
|
.strip()
|
||||||
|
.format(fts_table)
|
||||||
)
|
)
|
||||||
trigger_names = []
|
trigger_names = []
|
||||||
for row in self.db.execute(sql).fetchall():
|
for row in self.db.execute(sql).fetchall():
|
||||||
|
|
@ -897,7 +907,9 @@ class Table(Queryable):
|
||||||
|
|
||||||
def detect_fts(self):
|
def detect_fts(self):
|
||||||
"Detect if table has a corresponding FTS virtual table and return it"
|
"Detect if table has a corresponding FTS virtual table and return it"
|
||||||
sql = """
|
sql = (
|
||||||
|
textwrap.dedent(
|
||||||
|
"""
|
||||||
SELECT name FROM sqlite_master
|
SELECT name FROM sqlite_master
|
||||||
WHERE rootpage = 0
|
WHERE rootpage = 0
|
||||||
AND (
|
AND (
|
||||||
|
|
@ -907,8 +919,10 @@ class Table(Queryable):
|
||||||
AND sql LIKE '%VIRTUAL TABLE%USING FTS%'
|
AND sql LIKE '%VIRTUAL TABLE%USING FTS%'
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
""".format(
|
"""
|
||||||
table=self.name
|
)
|
||||||
|
.strip()
|
||||||
|
.format(table=self.name)
|
||||||
)
|
)
|
||||||
rows = self.db.execute(sql).fetchall()
|
rows = self.db.execute(sql).fetchall()
|
||||||
if len(rows) == 0:
|
if len(rows) == 0:
|
||||||
|
|
@ -922,15 +936,19 @@ class Table(Queryable):
|
||||||
self.db.execute(
|
self.db.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO [{table}] ([{table}]) VALUES ("optimize");
|
INSERT INTO [{table}] ([{table}]) VALUES ("optimize");
|
||||||
""".format(
|
""".strip().format(
|
||||||
table=fts_table
|
table=fts_table
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self.db.conn.execute(
|
self.db.conn.execute(
|
||||||
"""
|
textwrap.dedent(
|
||||||
|
"""
|
||||||
DELETE FROM [{table}_docsize] WHERE {column} NOT IN (
|
DELETE FROM [{table}_docsize] WHERE {column} NOT IN (
|
||||||
SELECT rowid FROM [{table}]);
|
SELECT rowid FROM [{table}]);
|
||||||
""".format(
|
"""
|
||||||
|
)
|
||||||
|
.strip()
|
||||||
|
.format(
|
||||||
# FTS5 uses 'id' but FTS4 uses 'docid'
|
# FTS5 uses 'id' but FTS4 uses 'docid'
|
||||||
column=self.db["{}_docsize".format(fts_table)].columns[0].name,
|
column=self.db["{}_docsize".format(fts_table)].columns[0].name,
|
||||||
table=fts_table,
|
table=fts_table,
|
||||||
|
|
@ -939,14 +957,18 @@ class Table(Queryable):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def search(self, q):
|
def search(self, q):
|
||||||
sql = """
|
sql = (
|
||||||
|
textwrap.dedent(
|
||||||
|
"""
|
||||||
select * from "{table}" where rowid in (
|
select * from "{table}" where rowid in (
|
||||||
select rowid from [{table}_fts]
|
select rowid from [{table}_fts]
|
||||||
where [{table}_fts] match :search
|
where [{table}_fts] match :search
|
||||||
)
|
)
|
||||||
order by rowid
|
order by rowid
|
||||||
""".format(
|
"""
|
||||||
table=self.name
|
)
|
||||||
|
.strip()
|
||||||
|
.format(table=self.name)
|
||||||
)
|
)
|
||||||
return self.db.execute(sql, (q,)).fetchall()
|
return self.db.execute(sql, (q,)).fetchall()
|
||||||
|
|
||||||
|
|
@ -1207,14 +1229,12 @@ class Table(Queryable):
|
||||||
or_what = "OR IGNORE "
|
or_what = "OR IGNORE "
|
||||||
sql = """
|
sql = """
|
||||||
INSERT {or_what}INTO [{table}] ({columns}) VALUES {rows};
|
INSERT {or_what}INTO [{table}] ({columns}) VALUES {rows};
|
||||||
""".format(
|
""".strip().format(
|
||||||
or_what=or_what,
|
or_what=or_what,
|
||||||
table=self.name,
|
table=self.name,
|
||||||
columns=", ".join("[{}]".format(c) for c in all_columns),
|
columns=", ".join("[{}]".format(c) for c in all_columns),
|
||||||
rows=", ".join(
|
rows=", ".join(
|
||||||
"""
|
"({placeholders})".format(
|
||||||
({placeholders})
|
|
||||||
""".format(
|
|
||||||
placeholders=", ".join(
|
placeholders=", ".join(
|
||||||
[conversions.get(col, "?") for col in all_columns]
|
[conversions.get(col, "?") for col in all_columns]
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue