#!/usr/bin/env python3

'''
Generated by PyDAO 9000, Ver. 1.2.1 
Generated @ Tue Aug 15 09:59:54 2023
'''
import os.path

from collections import OrderedDict
import sqlite3


class Maxims:

    def __init__(self):
        pdir = os.path.abspath(os.path.dirname(__file__))
        self.db = os.path.join(pdir, 'Maxims.sqlt3')
        self.conn = None
        self.curs = None
        self.bOpen = False
        self.fields = OrderedDict([('ID', 'integer'), ('Author', 'text'), ('Quote', 'text'), ('Meta', 'text'), ('MightyID', 'integer'), ('Status', 'integer'), ('Keywords', 'text')])
        self.table_name = 'Maxims'
        
    @classmethod
    def get_fields(cls, value):
        if isinstance(value, dict) and 'ID' in value.keys():
            value = OrderedDict(value)
            del value['ID']
            return list(value.values())
        if isinstance(value, cls):
            return cls.get_fields(value.fields)
        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 Maxims;")
            return res.fetchone()[0]
        return -1
        
    def drop_table(self):
        if self.bOpen:
            self.curs.execute("DrOp TaBLe IF EXISTS Maxims;")
            return True
        return False
        
    def create_table(self):
        if self.bOpen:
            self.curs.execute("CREATE TABLE IF NOT EXISTS Maxims(ID INTEGER PRIMARY KEY AUTOINCREMENT, Author text, Quote text, Meta text, MightyID integer, Status integer, Keywords text);")
            return True
        return False
        
    def insert(self, fields):
        fields = Maxims.get_fields(fields)
        if self.bOpen:
            self.curs.execute("INSERT INTO Maxims ( Author, Quote, Meta, MightyID, Status, Keywords) VALUES (?,?,?,?,?,?);", fields)
            return True
        return False
        
    def update(self, id_, fields):
        fields = Maxims.get_fields(fields)
        if self.bOpen:
            self.curs.execute("UPDATE Maxims SET Author=?, Quote=?, Meta=?, MightyID=?, Status=?, Keywords=? WHERE ID = {};".format(id_), fields)
            return True
        return False
        
    def delete(self, primary_key):
        if self.bOpen:
            self.curs.execute("DELETE from Maxims 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:
                try:
                    yield ref
                except:
                    pass
            return None
            
    @staticmethod
    def Import(dao, encoding=None, text_file='Maxims.sqlt3.txt', hasHeader=True, sep='|'):
        try:
            # dao.open()
            with open(text_file, encoding=encoding) as fh:
                line = fh.readline().strip()
                if hasHeader is True:
                    line = fh.readline().strip()
                while len(line) != 0:
                    if dao.insert(line.split(sep)) is False:
                        return False
                    line = fh.readline().strip()
            # dao.close()
            return True
        except:
            pass
        return False
        
    

