Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] add basic read only mode to connect #4783

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
15 changes: 12 additions & 3 deletions qcodes/dataset/sqlite/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ def _adapt_complex(value: complex | np.complexfloating) -> sqlite3.Binary:
return sqlite3.Binary(out.read())


def connect(name: str | Path, debug: bool = False, version: int = -1) -> ConnectionPlus:
def connect(
name: str | Path, debug: bool = False, version: int = -1, read_only: bool = False
) -> ConnectionPlus:
"""
Connect or create database. If debug the queries will be echoed back.
This function takes care of registering the numpy/sqlite type
Expand All @@ -126,6 +128,7 @@ def connect(name: str | Path, debug: bool = False, version: int = -1) -> Connect
debug: should tracing be turned on.
version: which version to create. We count from 0. -1 means 'latest'.
Should always be left at -1 except when testing.
read_only: Should the database be opened in read only mode.

Returns:
connection object to the database (note, it is
Expand All @@ -137,8 +140,14 @@ def connect(name: str | Path, debug: bool = False, version: int = -1) -> Connect
# register binary(TEXT) -> numpy converter
sqlite3.register_converter("array", _convert_array)

sqlite3_conn = sqlite3.connect(name, detect_types=sqlite3.PARSE_DECLTYPES,
check_same_thread=True)
path = f"file:{str(name)}"

if read_only:
path = path + "?ro"

sqlite3_conn = sqlite3.connect(
path, detect_types=sqlite3.PARSE_DECLTYPES, check_same_thread=True, uri=True
)
conn = ConnectionPlus(sqlite3_conn)

latest_supported_version = _latest_available_version()
Expand Down