Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions students/RussellLarge/Final/Complete/DataModel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Desc: Python 210 Final DataModel
# ChangeLog: (When,Who,What)
# 2/28/19,RRoot,Created Script
# 3/17/2019, RLarge, Modified Script

import re as rex
from sqlite3 import Error as sqlErr


class Product(object):

def __init__(self, product_id: int, product_name: str):
self.product_id = product_id
self.product_name = product_name

@property
def product_id(self):
return self.__product_id

@product_id.setter
def product_id(self, product_id):
if type(product_id) is not int: raise TypeError("Requires integer!")
if product_id <= 0: raise ValueError("Requires value greater than zero!")
else: self.__product_id = product_id

@property
def product_name(self):
return self.__product_name

@product_name.setter
def product_name(self, product_name):
self.__product_name = str(product_name).strip()

def __str__(self):
return '{0} {1}'.format(self.product_id, self.product_name)


class InventoryCount(object):

def __init__(self, product_inventory_count: int, inventory_id: int, product_id: int):
self.inventory_id = inventory_id
self.__product_id = product_id
self.__count = product_inventory_count


@property
def inventory_id(self):
return self.__inventory_id

@inventory_id.setter
def inventory_id(self, inventory_id):
if type(inventory_id) is not int: raise TypeError("Requires integer!")
if inventory_id <= 0:
raise ValueError("Requires a value greater than zero!")
else:
self.__inventory_id = inventory_id

@property
def product_id(self):
return self.__product_id

@product_id.setter
def product_id(self, product_id: int):
self.__product = product_id

@property
def product_inventory_count(self):
return self.__count

@product_inventory_count.setter
def product_inventory_count(self, count: int):
__count = count

def __str__(self):
return "{0} {1} {2}".format(self.product_inventory_count, self.inventory_id, self.product_id)

class Inventory(object):

def __init__(self, inventory_id, inventory_date):
self.inventory_date = inventory_date
self.inventory_id = inventory_id

@property
def inventory_date(self):
return self.__inventory_date

@inventory_date.setter
def inventory_date(self, inventory_date):
if rex.match("\d\d\d\d-\d\d-\d\d", str(inventory_date)) is None:
raise sqlErr("Not a Date!")
else:
self.__inventory_date = inventory_date

@property
def inventory_id(self):
return self.__inventory_id

@inventory_id.setter
def inventory_id(self, inventory_id):
if type(inventory_id) is not int: raise TypeError("Requires integer!")
if inventory_id <= 0:
raise ValueError("Requires a value greater than zero!")
else:
self.__inventory_id = inventory_id

def __str__(self):
return '{0} {1}'.format(self.inventory_id, self.inventory_date)
204 changes: 204 additions & 0 deletions students/RussellLarge/Final/Complete/DataProcessor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
# Desc: Python 210 Final DataModel
# ChangeLog: (When,Who,What)
# 3/17/2019, RLarge, Created & Modified Script

import sqlite3
from sqlite3 import Error as sqlErr
import re as rex

class DBProcessor(object):

def __init__(self, db_name: str):
self.__db_name = db_name
self.__db_con = self.create_connection(self.db_name)

@property
def db_name(self): # Get DB Name
return self.__db_name

@property
def db_con(self): # Get Live Connection
return self.__db_con

# SQL Validators
@staticmethod
def check_for_extra_semicolon(sql_str):
"""Checks for an extra semicolon"""
try:
if len(sql_str.split(';')) > 2:
raise sqlErr("Extra Semi-Colon Detected!")
except Exception as e:
raise e

@staticmethod
def check_for_string(sql_str):
'''Checks for string value'''
try:
if type(sql_str) != str:
pass
except TypeError:
raise TypeError

@staticmethod
def check_for_or(sql_str):
"""Checks for an injected OR in tampered WHERE Clause"""
try:
if rex.search("WHERE", sql_str, rex.IGNORECASE):
if rex.search(' or ', sql_str.split('WHERE')[1], rex.IGNORECASE) is not None:
raise sqlErr("OR Detected!")
except Exception as e:
raise e

@staticmethod
def check_for_date(date_str):
"""Checks for an valid date string"""
try:
if rex.match("\d\d\d\d-\d\d-\d\d", str(date_str)) is None:
raise sqlErr("Not a Date!")
except Exception as e:
raise e

def create_connection(self, db_file: str):
""" Create or connect to a SQLite database """
try:
con = sqlite3.connect(db_file)
except sqlErr as se:
raise Exception('SQL Error in create_connection(): ' + se.__str__())
except Exception as e:
raise Exception('General Error in create_connection(): ' + e.__str__())
return con


def execute_sql_code(self, sql_code: str = ''):
""" Execute SQL code on a open connection """
db_con = self.db_con
try:
if db_con is not None and sql_code != '':
# Validate
self.check_for_extra_semicolon(sql_code);
self.check_for_or(sql_code);
# Connect and Run
with db_con:
csr = db_con.cursor()
csr.execute(sql_code)
else:
raise Exception('SQL Code or Connection is missing!')
except sqlErr as se:
raise Exception('SQL Error in execute_sql_code(): ' + se.__str__())
except Exception as e:
raise Exception('General Error in execute_sql_code(): ' + e.__str__())
return csr

def build_ins_code(self):
# Validate Input
sql = str.format("INSERT Not Implemented Yet")
return sql

def build_upd_code(self):
# Validate Input
sql = str.format("UPDATE Not Implemented Yet")
return sql

def build_del_code(self):
# Validate Input
# Validate Input
sql = str.format("DELETE Not Implemented Yet")
return sql

def build_sel_code(self):
# Validate Input
sql = str.format("SELECT Not Implemented Yet")
return sql


class InventoryProcessor(DBProcessor):

def build_ins_code(self, inventory_id: int, inventory_date: str):
DBProcessor.check_for_date(inventory_date)
sql = str.format("INSERT INTO Inventories (InventoryID, InventoryDate) "
"VALUES ({id},'{date}');", id=inventory_id, date=inventory_date)
return sql

def build_upd_code(self, inventory_id: int, inventory_date: str ):
DBProcessor.check_for_date(inventory_date)
sql = str.format("UPDATE Inventories SET InventoryDate = '{date}' "
"WHERE InventoryID = {id};", id=inventory_id, date=inventory_date)
return sql

def build_del_code(self, inventory_id: int):
sql = str.format("DELETE FROM Inventories "
"WHERE InventoryID = {id};", id=inventory_id)
return sql

def build_sel_code(self, inventory_id: int = None):
if inventory_id is not None:
w = ' WHERE InventoryID = ' + str(inventory_id)
else:
w = ''
sql = str.format("SELECT InventoryID, InventoryDate "
"FROM Inventories{WHERE};", WHERE=w)
return sql

class ProductsProcessor(DBProcessor):

def build_ins_code(self, product_id: int, product_name: str):

sql = str.format("INSERT INTO Products (ProductID, ProductName) "
"VALUES ({id},'{name}');", id=product_id, name=product_name)
return sql

def build_upd_code(self, product_id: int, product_name: str):
sql = str.format("UPDATE Products SET ProductName = '{name}' "
"WHERE ProductID = {id};", name=product_name, id=product_id)
return sql

def build_del_code(self, product_id: str):
sql = str.format("DELETE FROM Products "
"WHERE ProductID = '{id}';", id=product_id)
return sql

def build_sel_code(self, product_id: int = None):
if product_id is not None:
w = ' WHERE ProductID = ' + str(product_id)
else:
w = ''
sql = str.format("SELECT ProductID, ProductName "
"FROM Products{WHERE};", WHERE=w)
return sql

class InventoryCountProcessor(DBProcessor):

# inventory ID, Product ID, Count

def build_ins_code(self, product_inventory_count: int, inventory_id: int, product_id: int):

sql = str.format("INSERT INTO InventoryCounts (InventoryID, ProductID, Count) "
"VALUES ({inv},{prod},{ct});", inv=inventory_id, prod=product_id, ct=product_inventory_count)
return sql


def build_upd_code(self, product_inventory_count: int, inventory_id: int, product_id: int):
sql = str.format("UPDATE InventoryCounts SET Count = {ct} "
"WHERE InventoryID = {inv} "
"AND ProductID = {prod};", inv=inventory_id, prod=product_id, ct=product_inventory_count)
return sql


def build_sel_code(self, inventory_id: int = None, product_id: int = None):
if inventory_id is not None and product_id is None:
w = ' WHERE InventoryID = ' + str(inventory_id)
elif inventory_id is None and product_id is not None:
w = ' WHERE ProductID = ' + str(product_id)
elif inventory_id is not None and product_id is not None:
w = ' WHERE InventoryID = ' + str(inventory_id) + ' AND ProductID = ' + str(product_id)
else:
w = ''
sql = str.format("SELECT InventoryId, ProductID, Count "
"FROM InventoryCounts{WHERE};", WHERE=w)
return sql

def build_del_code(self, inventory_id: int):
sql = "DELETE FROM InventoryCounts WHERE InventoryID = {inv};".format(inv=inventory_id)

return sql

Loading