@@ -41,6 +41,74 @@ def _create_row(self, row_class, keys, row):
41
41
return new_row
42
42
43
43
44
+ class Delete :
45
+ def __init__ (self , parent ):
46
+ self .parent = parent
47
+ self ._table_names = [list (table ["table" ].keys ())[0 ] for table in parent .table_info ]
48
+
49
+ def __getattribute__ (self , name ):
50
+ table_names = super ().__getattribute__ ('_table_names' )
51
+ if name in table_names :
52
+ _delete_rows = super ().__getattribute__ ('_delete_rows' )
53
+ return lambda ** kwargs : _delete_rows (name , ** kwargs )
54
+ return super ().__getattribute__ (name )
55
+
56
+ def _delete_rows (self , table_name , ** kwargs ):
57
+ if not kwargs :
58
+ raise ValueError ("You must provide at least one argument to delete a row." )
59
+ else :
60
+ query = f"DELETE FROM { table_name } WHERE "
61
+ query += " AND " .join ([f"{ key } ='{ value } '" for key , value in kwargs .items ()])
62
+ self .parent .cur .execute (query )
63
+ self .parent .cxn .commit ()
64
+
65
+
66
+ class Update :
67
+ def __init__ (self , parent ):
68
+ self .parent = parent
69
+ self ._table_names = [list (table ["table" ].keys ())[0 ] for table in parent .table_info ]
70
+
71
+ def __getattribute__ (self , name ):
72
+ table_names = super ().__getattribute__ ('_table_names' )
73
+ if name in table_names :
74
+ _update_rows = super ().__getattribute__ ('_update_rows' )
75
+ return lambda ** kwargs : _update_rows (name , ** kwargs )
76
+ return super ().__getattribute__ (name )
77
+
78
+ def _update_rows (self , table_name , where , set_data ):
79
+ query = f"UPDATE { table_name } SET "
80
+ query += "," .join ([f"{ key } ='{ value } '" for key , value in set_data .items ()])
81
+ query += f" WHERE { where } "
82
+ self .parent .cur .execute (query )
83
+ self .parent .cxn .commit ()
84
+
85
+
86
+ class Insert :
87
+ def __init__ (self , parent ):
88
+ self .parent = parent
89
+ self ._table_names = [list (table ["table" ].keys ())[0 ] for table in parent .table_info ]
90
+
91
+ def __getattribute__ (self , name ):
92
+ table_names = super ().__getattribute__ ('_table_names' )
93
+ if name in table_names :
94
+ _insert_row = super ().__getattribute__ ('_insert_row' )
95
+ return lambda ** kwargs : _insert_row (name , ** kwargs )
96
+ return super ().__getattribute__ (name )
97
+
98
+ def _insert_row (self , table_name , ** kwargs ):
99
+ not_null_keys = [table ["constraints" ].get ("not_null" , []) for table in self .parent .table_info if list (table ["table" ].keys ())[0 ] == table_name ][0 ]
100
+ for key in not_null_keys :
101
+ if key not in kwargs :
102
+ raise ValueError (f"Missing not null key '{ key } '" )
103
+ elif kwargs [key ] is None :
104
+ raise ValueError (f"Key '{ key } ' is None" )
105
+ keys = [key for key in kwargs .keys ()]
106
+ values = [kwargs [key ] for key in keys ]
107
+ query = f"INSERT INTO { table_name } ({ ',' .join (keys )} ) VALUES ({ ', ' .join (['?' for _ in range (len (keys ))])} )"
108
+ self .parent .cur .execute (query , tuple (values ))
109
+ self .parent .cxn .commit ()
110
+
111
+
44
112
class DB :
45
113
DB_PATH = "./data/database.db"
46
114
cxn : Connection
@@ -125,6 +193,10 @@ def __init__(self):
125
193
self .cxn = connect (self .DB_PATH , check_same_thread = False )
126
194
self .cur = self .cxn .cursor ()
127
195
self .get : Get = Get (self )
196
+ self .delete : Delete = Delete (self )
197
+ self .update : Update = Update (self )
198
+ self .insert : Insert = Insert (self )
199
+ self .insert .guilds (id = 1 , level_id = 1 )
128
200
129
201
def update_table (self , table : str , data : str ):
130
202
try :
0 commit comments