aboutsummaryrefslogtreecommitdiff
path: root/gn3/db/calls.py
blob: 547bccf3b0d69500cc9147529bba3f87396e82bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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)