Skip to content

Commit

Permalink
Get rid of sqlite3.Row and adjust SQL queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Blanchet committed Aug 2, 2022
1 parent f517e43 commit 8af5eef
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 175 deletions.
9 changes: 6 additions & 3 deletions qcodes/dataset/data_set.py
Expand Up @@ -1178,11 +1178,14 @@ def unsubscribe_all(self) -> None:
"""
Remove all subscribers
"""
sql = "select * from sqlite_master where type = 'trigger';"
sql = """
SELECT name FROM sqlite_master
WHERE type = 'trigger'
"""
triggers = atomic_transaction(self.conn, sql).fetchall()
with atomic(self.conn) as conn:
for trigger in triggers:
remove_trigger(conn, trigger['name'])
for (trigger,) in triggers:
remove_trigger(conn, trigger)
for sub in self.subscribers.values():
sub.schedule_stop()
sub.join()
Expand Down
9 changes: 5 additions & 4 deletions qcodes/dataset/experiment_container.py
Expand Up @@ -161,8 +161,10 @@ def data_set(self, counter: int) -> DataSet:

def data_sets(self) -> List[DataSetProtocol]:
"""Get all the datasets of this experiment"""
runs = get_runs(self.conn, self.exp_id)
return [load_by_id(run['run_id'], conn=self.conn) for run in runs]
return [
load_by_id(run_id, conn=self.conn)
for run_id, in get_runs(self.conn, self.exp_id)
]

def last_data_set(self) -> DataSetProtocol:
"""Get the last dataset of this experiment"""
Expand Down Expand Up @@ -208,8 +210,7 @@ def experiments(conn: Optional[ConnectionPlus] = None) -> List[Experiment]:
"""
conn = conn_from_dbpath_or_conn(conn=conn, path_to_db=None)
log.info(f"loading experiments from {conn.path_to_dbfile}")
rows = get_experiments(conn)
return [load_experiment(row['exp_id'], conn) for row in rows]
return [load_experiment(exp_id, conn) for exp_id, in get_experiments(conn)]


def new_experiment(name: str,
Expand Down
3 changes: 0 additions & 3 deletions qcodes/dataset/sqlite/database.py
Expand Up @@ -180,9 +180,6 @@ def connect(name: Union[str, Path], debug: bool = False,
f"version of QCoDeS supports up to "
f"version {latest_supported_version}")

# sqlite3 options
conn.row_factory = sqlite3.Row

# Make sure numpy ints and floats types are inserted properly
for numpy_int in numpy_ints:
sqlite3.register_adapter(numpy_int, int)
Expand Down
34 changes: 12 additions & 22 deletions qcodes/dataset/sqlite/db_upgrades/upgrade_2_to_3.py
Expand Up @@ -14,7 +14,7 @@
atomic_transaction,
transaction,
)
from qcodes.dataset.sqlite.query_helpers import one
from qcodes.dataset.sqlite.query_helpers import get_description_map, one

log = logging.getLogger(__name__)

Expand All @@ -27,8 +27,8 @@ def _2to3_get_result_tables(conn: ConnectionPlus) -> Dict[int, str]:
data = cur.fetchall()
cur.close()
results = {}
for row in data:
results[row['run_id']] = row['result_table_name']
for run_id, result_table_name in data:
results[run_id] = result_table_name
return results


Expand All @@ -45,9 +45,7 @@ def _2to3_get_layout_ids(conn: ConnectionPlus) -> DefaultDict[int, List[int]]:

results: DefaultDict[int, List[int]] = defaultdict(list)

for row in data:
run_id = row['run_id']
layout_id = row['layout_id']
for run_id, layout_id in data:
results[run_id].append(layout_id)

return results
Expand All @@ -66,9 +64,7 @@ def _2to3_get_indeps(conn: ConnectionPlus) -> DefaultDict[int, List[int]]:
cur.close()
results: DefaultDict[int, List[int]] = defaultdict(list)

for row in data:
run_id = row['run_id']
layout_id = row['layout_id']
for run_id, layout_id in data:
results[run_id].append(layout_id)

return results
Expand All @@ -87,9 +83,7 @@ def _2to3_get_deps(conn: ConnectionPlus) -> DefaultDict[int, List[int]]:
cur.close()
results: DefaultDict[int, List[int]] = defaultdict(list)

for row in data:
run_id = row['run_id']
layout_id = row['layout_id']
for run_id, layout_id in data:
results[run_id].append(layout_id)

return results
Expand All @@ -111,9 +105,7 @@ def _2to3_get_dependencies(conn: ConnectionPlus
if len(data) == 0:
return results

for row in data:
dep = row['dependent']
indep = row['independent']
for dep, indep in data:
results[dep].append(indep)

return results
Expand All @@ -129,11 +121,8 @@ def _2to3_get_layouts(conn: ConnectionPlus) -> Dict[int,
cur.execute(query)

results: Dict[int, Tuple[str, str, str, str]] = {}
for row in cur.fetchall():
results[row['layout_id']] = (row['parameter'],
row['label'],
row['unit'],
row['inferred_from'])
for layout_id, parameter, label, unit, inferred_from in cur.fetchall():
results[layout_id] = (parameter, label, unit, inferred_from)
return results


Expand All @@ -159,10 +148,11 @@ def _2to3_get_paramspecs(
# get the data type
sql = f'PRAGMA TABLE_INFO("{result_table_name}")'
c = transaction(conn, sql)
description = get_description_map(c)
paramtype = None
for row in c.fetchall():
if row['name'] == name:
paramtype = row['type']
if row[description["name"]] == name:
paramtype = row[description["type"]]
break
if paramtype is None:
raise TypeError(f"Could not determine type of {name} during the"
Expand Down

0 comments on commit 8af5eef

Please sign in to comment.