-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataProcessor.py
More file actions
204 lines (165 loc) · 7.29 KB
/
DataProcessor.py
File metadata and controls
204 lines (165 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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