Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion piccolo/engine/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from dataclasses import dataclass
from decimal import Decimal
from functools import partial, wraps
from typing import TYPE_CHECKING, Any, Optional, Union
from typing import TYPE_CHECKING, Any, Literal, Optional, Union

from typing_extensions import Self

Expand Down Expand Up @@ -590,6 +590,7 @@ def __init__(
path: str = "piccolo.sqlite",
log_queries: bool = False,
log_responses: bool = False,
journal_mode: Literal['DELETE', 'TRUNCATE', 'PERSIST', 'MEMORY', 'WAL', 'OFF'] | None = None,
**connection_kwargs,
) -> None:
"""
Expand All @@ -602,6 +603,9 @@ def __init__(
:param log_responses:
If ``True``, the raw response from each query is printed out.
Useful for debugging.
:param journal_mode:
executes the PRAGMA for the given journal mode when returning the db connection.
e.g. executes `PRAGMA journal_mode = WAL` if `journal_mode="WAL"`.
:param connection_kwargs:
These are passed directly to the database adapter. We recommend
setting ``timeout`` if you expect your application to process a
Expand All @@ -618,6 +622,7 @@ def __init__(

self.log_queries = log_queries
self.log_responses = log_responses
self.journal_mode = journal_mode
self.connection_kwargs = {
**default_connection_kwargs,
**connection_kwargs,
Expand Down Expand Up @@ -696,6 +701,10 @@ async def get_connection(self) -> Connection:
connection = await aiosqlite.connect(**self.connection_kwargs)
connection.row_factory = dict_factory # type: ignore
await connection.execute("PRAGMA foreign_keys = 1")

if self.journal_mode is not None:
await connection.execute(f"PRAGMA journal_mode = {self.journal_mode}")

return connection

###########################################################################
Expand Down Expand Up @@ -724,6 +733,9 @@ async def _run_in_new_connection(
args = []
async with aiosqlite.connect(**self.connection_kwargs) as connection:
await connection.execute("PRAGMA foreign_keys = 1")

if self.journal_mode is not None:
await connection.execute(f"PRAGMA journal_mode = {self.journal_mode}")

connection.row_factory = dict_factory # type: ignore
async with connection.execute(query, args) as cursor:
Expand Down Expand Up @@ -753,6 +765,9 @@ async def _run_in_existing_connection(
args = []
await connection.execute("PRAGMA foreign_keys = 1")

if self.journal_mode is not None:
await connection.execute(f"PRAGMA journal_mode = {self.journal_mode}")

connection.row_factory = dict_factory
async with connection.execute(query, args) as cursor:
response = await cursor.fetchall()
Expand Down