mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
db.execute_write_script() and db.execute_write_many(), closes #1570
Refs #1555
This commit is contained in:
parent
2e4ba71b53
commit
5cadc24489
4 changed files with 58 additions and 23 deletions
|
|
@ -94,22 +94,33 @@ class Database:
|
|||
f"file:{self.path}{qs}", uri=True, check_same_thread=False
|
||||
)
|
||||
|
||||
async def execute_write(self, sql, params=None, executescript=False, block=False):
|
||||
assert not (
|
||||
executescript and params
|
||||
), "Cannot use params with executescript=True"
|
||||
|
||||
async def execute_write(self, sql, params=None, block=False):
|
||||
def _inner(conn):
|
||||
with conn:
|
||||
if executescript:
|
||||
return conn.executescript(sql)
|
||||
else:
|
||||
return conn.execute(sql, params or [])
|
||||
return conn.execute(sql, params or [])
|
||||
|
||||
with trace("sql", database=self.name, sql=sql.strip(), params=params):
|
||||
results = await self.execute_write_fn(_inner, block=block)
|
||||
return results
|
||||
|
||||
async def execute_write_script(self, sql, block=False):
|
||||
def _inner(conn):
|
||||
with conn:
|
||||
return conn.executescript(sql)
|
||||
|
||||
with trace("sql", database=self.name, sql=sql.strip(), executescript=True):
|
||||
results = await self.execute_write_fn(_inner, block=block)
|
||||
return results
|
||||
|
||||
async def execute_write_many(self, sql, params_seq, block=False):
|
||||
def _inner(conn):
|
||||
with conn:
|
||||
return conn.executemany(sql, params_seq)
|
||||
|
||||
with trace("sql", database=self.name, sql=sql.strip(), executemany=True):
|
||||
results = await self.execute_write_fn(_inner, block=block)
|
||||
return results
|
||||
|
||||
async def execute_write_fn(self, fn, block=False):
|
||||
task_id = uuid.uuid5(uuid.NAMESPACE_DNS, "datasette.io")
|
||||
if self._write_queue is None:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue