mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
db.view_exists() method, needed by #1896
This commit is contained in:
parent
b29ccb59c7
commit
c588a89f26
3 changed files with 22 additions and 0 deletions
|
|
@ -338,6 +338,12 @@ class Database:
|
||||||
)
|
)
|
||||||
return bool(results.rows)
|
return bool(results.rows)
|
||||||
|
|
||||||
|
async def view_exists(self, table):
|
||||||
|
results = await self.execute(
|
||||||
|
"select 1 from sqlite_master where type='view' and name=?", params=(table,)
|
||||||
|
)
|
||||||
|
return bool(results.rows)
|
||||||
|
|
||||||
async def table_names(self):
|
async def table_names(self):
|
||||||
results = await self.execute(
|
results = await self.execute(
|
||||||
"select name from sqlite_master where type='table'"
|
"select name from sqlite_master where type='table'"
|
||||||
|
|
|
||||||
|
|
@ -909,6 +909,9 @@ The ``Database`` class also provides properties and methods for introspecting th
|
||||||
``await db.table_exists(table)`` - boolean
|
``await db.table_exists(table)`` - boolean
|
||||||
Check if a table called ``table`` exists.
|
Check if a table called ``table`` exists.
|
||||||
|
|
||||||
|
``await db.view_exists(view)`` - boolean
|
||||||
|
Check if a view called ``view`` exists.
|
||||||
|
|
||||||
``await db.table_names()`` - list of strings
|
``await db.table_names()`` - list of strings
|
||||||
List of names of tables in the database.
|
List of names of tables in the database.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -78,6 +78,19 @@ async def test_table_exists(db, tables, exists):
|
||||||
assert exists == actual
|
assert exists == actual
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"view,expected",
|
||||||
|
(
|
||||||
|
("not_a_view", False),
|
||||||
|
("paginated_view", True),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_view_exists(db, view, expected):
|
||||||
|
actual = await db.view_exists(view)
|
||||||
|
assert actual == expected
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"table,expected",
|
"table,expected",
|
||||||
(
|
(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue