Skip to content

Commit

Permalink
Make query under DatabaseBackend.get() reusable in a private method
Browse files Browse the repository at this point in the history
  • Loading branch information
khaeru committed Apr 6, 2021
1 parent 9486865 commit d5c5371
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions ixmp/backend/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@ def init(self, ts, annotation):

self.conn.commit()

def get(self, ts):
args = [ts.model, ts.scenario]
if ts.version:
def _get(self, model, scenario, version):
args = [model, scenario]
if version:
query = """
SELECT ts.id, ts.version FROM timeseries AS ts WHERE model_name = ?
AND scenario_name = ? AND version = ?
"""
args.append(ts.version)
args.append(version)
else:
query = """
SELECT ts.id, ts.version FROM timeseries AS ts JOIN annotation AS a
Expand All @@ -173,9 +173,16 @@ def get(self, ts):
"""

cur = self.conn.cursor()
cur.execute(query, (ts.model, ts.scenario))
cur.execute(query, args)

id, version = cur.fetchone()
result = cur.fetchone()
if result is None:
raise ValueError(f"model={model}, scenario={scenario}")
else:
return result

def get(self, ts):
id, version = self._get(ts.model, ts.scenario, ts.version)

ts.version = ts.version or version
assert ts.version == version # Sanity check
Expand Down

0 comments on commit d5c5371

Please sign in to comment.