-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
171 lines (123 loc) · 4.51 KB
/
database.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
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
'''
-----------------------------------------
| Scraper Database Module |
| Author : Damian Lallement |
| Database Author : Damian Lallement |
----------------------------------------
'''
from abc import ABC, abstractmethod
import mysql.connector
from pojo import Organization
from pojo import Person
db = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="projet_tutore_s3"
)
cursor = db.cursor()
class DB(ABC):
@abstractmethod
def insert(self, obj):
pass
@abstractmethod
def update(self, obj):
pass
@abstractmethod
def delete(self, obj):
pass
@abstractmethod
def get_all(self):
pass
class DBCompany(DB):
def insert(self, organization):
sql = "INSERT INTO entreprise(nom_ent, pays, ville) VALUES(%s, %s, %s)"
values = (organization.org_name, organization.org_country, organization.org_city)
cursor.execute(sql, values)
db.commit()
organization.org_id = cursor.lastrowid
return True
def update(self, organization):
sql = "UPDATE entreprise SET nom_ent = %s, pays = %s, ville = %s WHERE no_ent = %s"
values = (organization.org_name, organization.org_country, organization.org_city, organization.org_id)
try:
cursor.execute(sql, values)
db.commit()
except mysql.connector.Error as err:
print("Something went wrong: {}".format(err))
return True
def delete(self, organization):
sql = "DELETE FROM entreprise WHERE no_ent = %s"
values = (organization.org_id,)
cursor.execute(sql, values)
db.commit()
return True
def get_all(self):
org_list = []
sql = "SELECT * FROM entreprise"
cursor.execute(sql)
row = cursor.fetchone()
while row is not None:
org = Organization(row[1], row[3], row[2], row[0])
org_list.append(org)
row = cursor.fetchone()
return org_list
def get_by_name(self, org_name):
org_list = []
sql = "SELECT * FROM entreprise WHERE nom_ent = %s"
values = (org_name,)
cursor.execute(sql, values)
row = cursor.fetchone()
while row is not None:
org = Organization(row[1], row[3], row[2], row[0])
org_list.append(org)
row = cursor.fetchone()
return org_list
class DBPerson(ABC):
def insert(self, person):
sql = "INSERT INTO personne(nom, prenom, nom_poste, no_tel, addr, email, linkedin_url) VALUES(%s, %s, %s, %s, %s, %s, %s)"
values = (person.last_name, person.first_name, person.job_title, person.phone, person.address, person.mail, person.linkedin_url)
cursor.execute(sql, values)
db.commit()
person.id = cursor.lastrowid
return True
def update(self, person):
sql = "UPDATE personne SET nom = %s, prenom = %s, nom_poste = %s, no_tel = %s, addr = %s, email = %s, linkedin_url = %s WHERE id_pers = %s"
values = (person.last_name, person.first_name, person.job_title, person.phone, person.address, person.mail, person.linkedin_url, person.id)
cursor.execute(sql, values)
db.commit()
return True
def delete(self, person):
sql = "DELETE FROM personne WHERE id_pers = %s"
values = (person.id,)
cursor.execute(sql, values)
db.commit()
return True
def get_all(self):
person_list = []
sql = "SELECT * FROM personne"
cursor.execute(sql)
row = cursor.fetchone()
while row is not None:
person = Person(row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[0])
person_list.append(person)
row = cursor.fetchone()
return person_list
def get_by_name(self, first_name, last_name):
person_list = []
sql = "SELECT * FROM personne WHERE nom = %s AND prenom = %s"
values = (last_name, first_name)
cursor.execute(sql, values)
row = cursor.fetchone()
while row is not None:
person = Person(row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[0])
person_list.append(person)
row = cursor.fetchone()
return person_list
class DBEmployment:
def insert(self, person_id, org_id):
sql = "INSERT INTO emploi(id_pers, no_ent) VALUES(%s, %s)"
values = (person_id, org_id)
cursor.execute(sql, values)
db.commit()
return True