-
Notifications
You must be signed in to change notification settings - Fork 10
/
base.py
71 lines (50 loc) · 1.25 KB
/
base.py
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
from models import session
from models import Base as BaseObj
from models import engine as BaseEngine
from sqlalchemy import select
class DataBase:
def select_all(self,Model,**filter_s):
query = session.query(Model)
if len(filter_s) > 0:
query = query.filter_by(**filter_s)
return query.all()
def get_one(self,Model,**filter_s):
query = session.query(Model)
if len(filter_s) > 0:
query = query.filter_by(**filter_s)
return query.first()
def test(self,Model,**filter_s):
if self.get_one(Model,**filter_s):
return True
else:
return False
def new(self,Model,*args):
tmp_new = Model(*args)
session.add(tmp_new)
session.commit()
return tmp_new
def delete(self,Model,**filter_s):
obj = self.select_all(Model,**filter_s)
if obj:
for i in obj:
session.delete(i)
session.commit()
return True
else:
return False
def update(self,Model,set,**filter_s):
query = session.query(Model)
if len(filter_s) > 0:
query = query.filter_by(**filter_s)
query.update(set)
session.commit()
return True
def set_state(self, Model, *args):
to_byte = Model(*args)
session.add(to_byte)
session.commit()
def base_init(self):
BaseObj.metadata.create_all(BaseEngine)
d = DataBase()
d.base_init()
print('ok')