Praxis_Bot/db.py
2021-04-05 14:12:47 -04:00

124 lines
3.9 KiB
Python

import credentials
import config as config
import pandas as pd
import sqlite3
from sqlalchemy import create_engine
class db_module():
def __init__(self):
super().__init__()
self.dbCredential: credentials.DB_Credential
self.currentDBStrategy: config.DBStrategy = config.dbStrategy.SQLite
self.Strategy = self.Strategy_SQLite()
self.currentWorkingDB: str
self.engine = None
class Strategy_SQLite():
def __init__(self):
self.dbName:str = ""
self.connection = sqlite3.connect(":memory:")
self.cursor = self.connection.cursor()
self.dirtyMode:bool = False
self.dirtyModeSetup()
def dirtyModeSetup(self):
self.create_table('testing')
self.get_data(key = "testerinoz")
def setup_connection(self, newName:str):
self.dbName = newName
self.connection = sqlite3.connect(newName)
# Table Stuff
def create_table(self, tableName: str = ""):
print("making stuff....")
command = 'CREATE TABLE ' + tableName + ' (key text, data text, bonusdata text)'
self.cursor.execute(command)
self.cursor.execute("INSERT INTO testing VALUES ('testing','testerino','extra stuff 1')")
self.cursor.execute("INSERT INTO testing VALUES ('testing','testerinoz','extra stuff 2')")
self.cursor.execute("INSERT INTO testing VALUES ('testing','testerinozy','extra stuff 3')")
self.connection.commit()
def create_json_obj_table():
pass
def does_table_exist(self, tableName: str = ""):
pass
def delete_table(self, tableName: str = ""):
pass
# Data Stuff
def get_data(self, tableName: str = "", key: str = ""):
print("fetching...")
t = ('testing',)
self.cursor.execute('SELECT * FROM testing WHERE key=?', t)
resultz = self.cursor.fetchall()
print(resultz)
print("loop")
for thing in resultz:
if thing[1] == key:
print("found it")
print(thing[2])
def insert_data(self, tableName: str = "", param: str = ""):
pass
def edit_data(self, tableName: str = "", key: str = "", param: str = ""):
pass
def delete_data(self, tableName: str = "", key: str = ""):
pass
class Strategy_MySQL():
def __init__(self):
pass
def setup_engine(self, credential: credentials.DB_Credential = None):
createEngine = True
if credential is None:
if self.dbCredential is None:
createEngine = False
else:
credential = self.dbCredential
if createEngine:
self.engine = create_engine(credential.engine_url)
self.currentWorkingDB = credential.databaseName
print("SQL Engine Created")
# This was a old function used prior to the creation of this class. I need to remake it.
# def get_data_old(self, tableName: str = "", key: str = ""):
# table = '_channel_commands'
# table = tableName
#
# df = pd.read_sql_query('SELECT * FROM ' + table, engine)
# stmt = "trigger == '" + key + "'"
# temp = df.query(stmt)
# result = temp.get("response")
#
# # print(result)
# i = len(temp.index.values)
#
# if i == 1:
# output = result[temp.index.values[0]]
# pass
# else:
# output = "$$None$$"
# return output
if __name__ == "__main__":
testModule = db_module()
credentials_manager = credentials.Credentials_Module()
credentials_manager.load_credentials()
testModule.dbCredential = credentials_manager.find_DB_Credential(config.credentialsNickname)
testModule.setup_engine()