diff options
Diffstat (limited to 'gn3/db/calls.py')
-rw-r--r-- | gn3/db/calls.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/gn3/db/calls.py b/gn3/db/calls.py new file mode 100644 index 0000000..547bccf --- /dev/null +++ b/gn3/db/calls.py @@ -0,0 +1,51 @@ +"""module contains calls method for db""" +import json +import urllib +from flask import g +from gn3.utility.logger import getLogger +logger = getLogger(__name__) +# should probably put this is env +USE_GN_SERVER = False +LOG_SQL = False + +GN_SERVER_URL = None + + +def fetch1(query, path=None, func=None): + """fetch1 method""" + if USE_GN_SERVER and path: + result = gn_server(path) + if func is not None: + res2 = func(result) + + else: + res2 = result + + if LOG_SQL: + pass + # should probably and logger + # logger.debug("Replaced SQL call", query) + + # logger.debug(path,res2) + return res2 + + return fetchone(query) + + +def gn_server(path): + """Return JSON record by calling GN_SERVER + + """ + res = urllib.request.urlopen(GN_SERVER_URL+path) + rest = res.read() + res2 = json.loads(rest) + return res2 + + +def fetchone(query): + """method to fetchone item from db""" + def helper(query): + res = g.db.execute(query) + return res.fetchone() + + return logger.sql(query, helper) |