database.py
3.11 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
import sqlite3
import ipdb
from enums import Table, States, Services, Datatypes
class DBconnection:
tables = ["msg","history"]
structure = {
Table.id : "integer PRIMARY KEY",
Table.path : "text",
Table.serv : "text",
Table.dest : "text",
Table.type : "text",
Table.state : "text"
}
def __init__(self,db):
self.db = db
query = "CREATE TABLE IF NOT EXISTS msg("
for column in DBconnection.structure:
query += column + " " + DBconnection.structure[column] + ","
query = query.strip(",") + ")"
# main table
self.query(query)
# delivered messages that were informed
self.query(query.replace("msg","history",1))
def query(self,query,*args):
if query.upper().find("INSERT") == 0:
return "Use la funcion insert() para insertar"
entities = None
for ar in args:
entities = ar
con = sqlite3.connect(self.db)
cursor = con.cursor()
if entities:
cursor.execute(query,entities)
else:
cursor.execute(query)
rows = cursor.fetchall()
con.commit()
con.close()
return rows
def insert(self,table,insertions):
if not table in DBconnection.tables:
return "La tabla " + table + " no existe o no está contemplada"
query = "INSERT INTO " + table + "("
values = " VALUES("
for column in insertions:
if not self.check(column,insertions[column]):
return "El dato '" + insertions[column] + "' no es valido"
query += column + ","
values += "'" + insertions[column] + "',"
query = query.strip(",") + ")"
values = values.strip(",") + ")"
con = sqlite3.connect(self.db)
cursor = con.cursor()
cursor.execute(query + values)
cursor.execute("SELECT last_insert_rowid()")
id = cursor.fetchall()[0][0]
con.commit()
con.close()
return id
def update(self,table,comparator,alterations):
if not table in DBconnection.tables:
return "La tabla " + table + " no existe o no está contemplada"
if not Table.validate(comparator[0]):
return "El comparador no es una columna valida"
query = "UPDATE " + table + " SET "
where = " WHERE " + comparator[0] + "=" + comparator[1]
for column in alterations:
if not self.check(column,alterations[column]):
return "El dato '" + alterations[column] + "' no es valido"
query += column + "='" + alterations[column] + "',"
query = query.strip(",")
ipdb.set_trace()
con = sqlite3.connect(self.db)
cursor = con.cursor()
cursor.execute(query + where)
con.commit()
con.close()
def check(self,column,data):
if column == Table.path:
return True
if column == Table.serv:
return Services.validate(data)
if column == Table.dest:
return True
if column == Table.type:
return Datatypes.validate(data)
if column == Table.state:
return States.validate(data)
return False
@staticmethod
def parseToTable(rows):
dictarray = []
for row in rows:
dictrow = {}
i = 0
for column in DBconnection.structure:
dictrow[column] = row[i]
i += 1
dictarray.append(dictrow)
return dictarray