From 5e4c967ded2c4146af261914dc30c7e03d575c2b Mon Sep 17 00:00:00 2001 From: Pjotr Prins Date: Thu, 23 Jun 2016 06:17:27 +0000 Subject: DB: created fetch1 function which can do both SQL and GN_SERVER --- wqflask/db/call.py | 17 ++++++++++++++++- wqflask/db/webqtlDatabaseFunction.py | 17 ++++++----------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/wqflask/db/call.py b/wqflask/db/call.py index 6b15abb3..ba6eb149 100644 --- a/wqflask/db/call.py +++ b/wqflask/db/call.py @@ -15,6 +15,21 @@ logger = getLogger(__name__ ) from inspect import stack +def fetch1(query, path=None, func=None): + """Fetch one result using either a SQL query or the URI path to +GN_SERVER (when USE_GN_SERVER is True). Apply func to GN_SERVER result +when set. + + """ + if USE_GN_SERVER and path: + result = gn_server(path) + if func != None: + return [func(result)] + else: + return [result] + else: + return fetchone(query) + def fetchone(query): """Return tuple containing one row by calling SQL directly @@ -23,7 +38,7 @@ def fetchone(query): def helper(query): res = g.db.execute(query) return res.fetchone() - callername = stack()[1][3] + callername = stack()[2][3] return logger.sql(callername, query, helper) def gn_server(path): diff --git a/wqflask/db/webqtlDatabaseFunction.py b/wqflask/db/webqtlDatabaseFunction.py index bdc3eeb2..2bc4745b 100644 --- a/wqflask/db/webqtlDatabaseFunction.py +++ b/wqflask/db/webqtlDatabaseFunction.py @@ -30,7 +30,7 @@ from base import webqtlConfig from utility.tools import USE_GN_SERVER, LOG_SQL from utility.benchmark import Bench -from db.call import fetchone, gn_server +from db.call import fetch1 from utility.logger import getLogger logger = getLogger(__name__ ) @@ -52,18 +52,13 @@ def retrieve_species(group): """Get the species of a group (e.g. returns string "mouse" on "BXD" """ - if USE_GN_SERVER: - result = gn_server("/cross/"+group+".json") - return result["species"] - else: - result = fetchone("select Species.Name from Species, InbredSet where InbredSet.Name = '%s' and InbredSet.SpeciesId = Species.Id" % (group)) - return result[0] + result = fetch1("select Species.Name from Species, InbredSet where InbredSet.Name = '%s' and InbredSet.SpeciesId = Species.Id" % (group),"/cross/"+group+".json",lambda r: r["species"])[0] + logger.debug("retrieve_species result:",result) + return result + def getMappingMethod(cursor=None, groupName=None): - if USE_GN_SERVER: - return gn_server("/cross/"+group+".json")["mapping_method_id"] - else: - return fetchone("select MappingMethodId from InbredSet where Name= '%s'" % groupName) + return fetch1("select MappingMethodId from InbredSet where Name= '%s'" % groupName, "/cross/"+group+".json", lambda r: r["mapping_method_id"])[0] ########################################################################### #input: cursor, inbredSetId (int), strainId (int) -- cgit v1.2.3