Ability to list just FTS4 or FTS5 table names

This commit is contained in:
Simon Willison 2019-01-24 19:57:04 -08:00
commit 66fd63b119
8 changed files with 53 additions and 22 deletions

View file

@ -15,10 +15,16 @@ def cli():
type=click.Path(exists=True, file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
def table_names(path):
@click.option(
"--fts4", help="Just show FTS4 enabled tables", default=False, is_flag=True
)
@click.option(
"--fts5", help="Just show FTS5 enabled tables", default=False, is_flag=True
)
def table_names(path, fts4, fts5):
"""List the tables in the database"""
db = sqlite_utils.Database(path)
for name in db.table_names:
for name in db.table_names(fts4=fts4, fts5=fts5):
print(name)

View file

@ -25,18 +25,18 @@ class Database:
def __repr__(self):
return "<Database {}>".format(self.conn)
@property
def table_names(self):
return [
r[0]
for r in self.conn.execute(
"select name from sqlite_master where type = 'table'"
).fetchall()
]
def table_names(self, fts4=False, fts5=False):
where = ["type = 'table'"]
if fts4:
where.append("sql like '%FTS4%'")
if fts5:
where.append("sql like '%FTS5%'")
sql = "select name from sqlite_master where {}".format(" AND ".join(where))
return [r[0] for r in self.conn.execute(sql).fetchall()]
@property
def tables(self):
return [self[name] for name in self.table_names]
return [self[name] for name in self.table_names()]
def execute_returning_dicts(self, sql, params=None):
cursor = self.conn.execute(sql, params or tuple())
@ -106,7 +106,7 @@ class Table:
def __init__(self, db, name):
self.db = db
self.name = name
self.exists = self.name in self.db.table_names
self.exists = self.name in self.db.table_names()
def __repr__(self):
return "<Table {}{}>".format(