Skip to content

Commit

Permalink
add basic read only mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshnielsen committed Nov 1, 2022
1 parent 7da102b commit 653d700
Showing 1 changed file with 12 additions and 3 deletions.
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

0 comments on commit 653d700

Please sign in to comment.