#!/usr/bin/env python3

'''
MODIFIED to CONVERT the legacy JSOB File into SQLIte3
========
Generated by Soft9000/PyDAO, Ver. 2.0 (Alpha)
Generated @ Sun Apr 11 10:50:13 2021
'''

from collections import OrderedDict

import sqlite3

class AllQuests:

    def __init__(self):
        self.db = '../AllQuests01.sqlt3'
        self.conn = None
        self.curs = None
        self.bOpen = False
        self.fields = OrderedDict([
            ('ID', 'integer'), ('KID', 'text'), ('GID', 'text'),
            ('QUESTION', 'text'), ('ANSWER', 'text'), ('DIFFICULTY', 'text'),
            ('ASSOCIATION', 'text'), ('STATUS', 'text'), ('LANGUAGE', 'text'),
            ('CODE1', 'integer'), ('CODE2', 'integer'), ('VERSION', 'real')])
        self.table_name = 'Questions'
        
    @classmethod
    def get_fields(cls, value):
        if isinstance(value, cls):
            return list(value.fields.values())[1:]
        return value
        
    def open(self):
        if self.bOpen is False:
            self.conn = sqlite3.connect(self.db)
            self.conn.row_factory = sqlite3.Row
            self.curs = self.conn.cursor()
            self.bOpen = True
        return True
        
    def close(self):
        if self.bOpen:
            self.conn.commit()
            self.bOpen = False
        return True
        
    def count(self):
        if self.bOpen:
            res = self.curs.execute("SELECT count(*) FROM Questions;")
            return res.fetchone()[0]
        return -1
        
    def drop_table(self):
        if self.bOpen:
            self.curs.execute("DrOp TaBLe IF EXISTS Questions;")
            return True
        return False
        
    def create_table(self):
        if self.bOpen:
            self.curs.execute("CREATE TABLE IF NOT EXISTS Questions(ID INTEGER PRIMARY KEY AUTOINCREMENT, KID text, GID text, QUESTION text, ANSWER text, DIFFICULTY text, ASSOCIATION text, STATUS text, LANGUAGE text, CODE1 integer, CODE2 integer, VERSION real);")
            return True
        return False
        
    def insert(self, fields):
        fields = AllQuests.get_fields(fields)
        if self.bOpen:
            self.curs.execute("INSERT INTO Questions ( KID, GID, QUESTION, ANSWER, DIFFICULTY, ASSOCIATION, STATUS, LANGUAGE, CODE1, CODE2, VERSION) VALUES (?,?,?,?,?,?,?,?,?,?,?);", fields)
            return True
        return False
        
    def update(self, id_, fields):
        fields = AllQuests.get_fields(fields)
        if self.bOpen:
            self.curs.execute("UPDATE Questions SET KID=?, GID=?, QUESTION=?, ANSWER=?, DIFFICULTY=?, ASSOCIATION=?, STATUS=?, LANGUAGE=?, CODE1=?, CODE2=?, VERSION=? WHERE ID = {};".format(id_), fields)
            return True
        return False
        
    def delete(self, primary_key):
        if self.bOpen:
            self.curs.execute("DELETE from Questions WHERE ID = ?;", [primary_key])
            return True
        return False
        
    def select(self, sql_select):
        if self.bOpen:
            self.curs.execute(sql_select)
            zlist = self.curs.fetchall()
            for ref in zlist:
                yield OrderedDict(ref)
        return None
        
    @staticmethod
    def Import():
        try:
            dao = AllQuests()
            dao.open()
            dao.create_table()
            rows = Quest.Load('/d_drive/a5/2020_01_03_TEC/2021_01_01_Python_Related/9000_Python_QnA_2020_12_29/2021_01_01_9000_Python_QnA/AllQuestions.json')
            for obj in rows:
                row = obj.__dict__
                newrow = AllQuests()
                record = newrow.fields
                record['KID'] = row['KID']
                record['GID'] = row['GID']
                record['QUESTION'] = row['question']
                record['ANSWER'] = row['answer']
                record['DIFFICULTY'] = row['difficulty']
                record['ASSOCIATION'] = row['association']
                record['STATUS'] = row['status']
                record['LANGUAGE'] = 'PYTHON3'
                record['CODE1'] = 0
                record['CODE2'] = 0
                record['VERSION'] = 0.0
                dao.insert(newrow)
            dao.close()
            return True
        except Exception as ex:
            print(ex)
        return False
        
    
if __name__ == '__main__':
    import sys
    sys.path.insert(0, '.')
    sys.path.insert(0, '../')
    from QuestJSOB.Questions import Quest as Quest  
    AllQuests.Import()

#!/usr/bin/env python3

'''
MODIFIED to CONVERT the legacy JSOB File into SQLIte3
========
Generated by Soft9000/PyDAO, Ver. 2.0 (Alpha)
Generated @ Sun Apr 11 10:50:13 2021
'''

from collections import OrderedDict

import sqlite3

class AllQuests:

    def __init__(self):
        self.db = '../AllQuests01.sqlt3'
        self.conn = None
        self.curs = None
        self.bOpen = False
        self.fields = OrderedDict([
            ('ID', 'integer'), ('KID', 'text'), ('GID', 'text'),
            ('QUESTION', 'text'), ('ANSWER', 'text'), ('DIFFICULTY', 'text'),
            ('ASSOCIATION', 'text'), ('STATUS', 'text'), ('LANGUAGE', 'text'),
            ('CODE1', 'integer'), ('CODE2', 'integer'), ('VERSION', 'real')])
        self.table_name = 'Questions'
        
    @classmethod
    def get_fields(cls, value):
        if isinstance(value, cls):
            return list(value.fields.values())[1:]
        return value
        
    def open(self):
        if self.bOpen is False:
            self.conn = sqlite3.connect(self.db)
            self.conn.row_factory = sqlite3.Row
            self.curs = self.conn.cursor()
            self.bOpen = True
        return True
        
    def close(self):
        if self.bOpen:
            self.conn.commit()
            self.bOpen = False
        return True
        
    def count(self):
        if self.bOpen:
            res = self.curs.execute("SELECT count(*) FROM Questions;")
            return res.fetchone()[0]
        return -1
        
    def drop_table(self):
        if self.bOpen:
            self.curs.execute("DrOp TaBLe IF EXISTS Questions;")
            return True
        return False
        
    def create_table(self):
        if self.bOpen:
            self.curs.execute("CREATE TABLE IF NOT EXISTS Questions(ID INTEGER PRIMARY KEY AUTOINCREMENT, KID text, GID text, QUESTION text, ANSWER text, DIFFICULTY text, ASSOCIATION text, STATUS text, LANGUAGE text, CODE1 integer, CODE2 integer, VERSION real);")
            return True
        return False
        
    def insert(self, fields):
        fields = AllQuests.get_fields(fields)
        if self.bOpen:
            self.curs.execute("INSERT INTO Questions ( KID, GID, QUESTION, ANSWER, DIFFICULTY, ASSOCIATION, STATUS, LANGUAGE, CODE1, CODE2, VERSION) VALUES (?,?,?,?,?,?,?,?,?,?,?);", fields)
            return True
        return False
        
    def update(self, id_, fields):
        fields = AllQuests.get_fields(fields)
        if self.bOpen:
            self.curs.execute("UPDATE Questions SET KID=?, GID=?, QUESTION=?, ANSWER=?, DIFFICULTY=?, ASSOCIATION=?, STATUS=?, LANGUAGE=?, CODE1=?, CODE2=?, VERSION=? WHERE ID = {};".format(id_), fields)
            return True
        return False
        
    def delete(self, primary_key):
        if self.bOpen:
            self.curs.execute("DELETE from Questions WHERE ID = ?;", [primary_key])
            return True
        return False
        
    def select(self, sql_select):
        if self.bOpen:
            self.curs.execute(sql_select)
            zlist = self.curs.fetchall()
            for ref in zlist:
                yield OrderedDict(ref)
        return None
        
    @staticmethod
    def Import():
        try:
            dao = AllQuests()
            dao.open()
            dao.create_table()
            rows = Quest.Load('/d_drive/a5/2020_01_03_TEC/2021_01_01_Python_Related/9000_Python_QnA_2020_12_29/2021_01_01_9000_Python_QnA/AllQuestions.json')
            for obj in rows:
                row = obj.__dict__
                newrow = AllQuests()
                record = newrow.fields
                record['KID'] = row['KID']
                record['GID'] = row['GID']
                record['QUESTION'] = row['question']
                record['ANSWER'] = row['answer']
                record['DIFFICULTY'] = row['difficulty']
                record['ASSOCIATION'] = row['association']
                record['STATUS'] = row['status']
                record['LANGUAGE'] = 'PYTHON3'
                record['CODE1'] = 0
                record['CODE2'] = 0
                record['VERSION'] = 0.0
                dao.insert(newrow)
            dao.close()
            return True
        except Exception as ex:
            print(ex)
        return False
        
    
if __name__ == '__main__':
    import sys
    sys.path.insert(0, '.')
    sys.path.insert(0, '../')
    from QuestJSOB.Questions import Quest as Quest  
    AllQuests.Import()

#!/usr/bin/env python3 MODIFIED to CONVERT the legacy JSOB File into SQLIte3 ======== Generated by Soft9000/PyDAO, Ver. 2.0 (Alpha) Generated @ Sun Apr 11 10:50:13 2021