# Path: datapackage_pipelines/utilities/extended_json.py
# class json(object):
#     dumps = _dumps
#     loads = _loads
#     dumpl = _dumpl
#     loadl = _loadl
#     dump = _dump
#     load = _load
#     JSONDecodeError = _json.JSONDecodeError


import os
import sqlite3
from datapackage_pipelines.utilities.extended_json import json

DPP_DB_FILENAME = os.environ.get('DPP_DB_FILENAME', '.dpp.db')


class Sqlite3Dict(object):
    def __init__(self, filename):
        self.filename = filename
        conn = sqlite3.connect(self.filename)
        cursor = conn.cursor()
        cursor.execute(
            '''CREATE TABLE IF NOT EXISTS d (_key text, _value text)'''
        )
        conn.commit()
        conn.close()

    def __getitem__(self, key):
        conn = sqlite3.connect(self.filename)
        cursor = conn.cursor()
        result = cursor.execute(
            'SELECT _value from d where _key=?',
            (key,)
        ).fetchone()
        conn.close()
        if result is not None:

# Path: datapackage_pipelines/utilities/extended_json.py
# class json(object):
#     dumps = _dumps
#     loads = _loads
#     dumpl = _dumpl
#     loadl = _loadl
#     dump = _dump
#     load = _load
#     JSONDecodeError = _json.JSONDecodeError


# Path: datapackage_pipelines/status/backend_sqlite.py
import os
import sqlite3
from datapackage_pipelines.utilities.extended_json import json


DPP_DB_FILENAME = os.environ.get('DPP_DB_FILENAME', '.dpp.db')


class Sqlite3Dict(object):
    def __init__(self, filename):
        self.filename = filename
        conn = sqlite3.connect(self.filename)
        cursor = conn.cursor()
        cursor.execute(
            '''CREATE TABLE IF NOT EXISTS d (_key text, _value text)'''
        )
        conn.commit()
        conn.close()

    def __getitem__(self, key):
        conn = sqlite3.connect(self.filename)
        cursor = conn.cursor()
        result = cursor.execute(
            'SELECT _value from d where _key=?',
            (key,)
        ).fetchone()
        conn.close()
        if result is not None:

            return json.loads(result[0])