-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhappy_models.py
717 lines (608 loc) · 24.1 KB
/
happy_models.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
# Python
from pydantic import BaseModel, EmailStr, Field, PositiveInt
from typing import List, Optional
from datetime import date
import mysql.connector
from mysql.connector import Error
from dotenv import load_dotenv
import os
# MySQLDB() manages the connection to the database server
class MySQLDB:
# this class handles the connection to the DB server, the connection details must be stored in the .env file
# the database name must be set at object instatiation
# it's possible to work with several databases hosted on the same server by instantiating an object for each database
def __init__(self, database: str, env_file='.env'):
"""
Initialize the MySQLDB instance with the specified database and environment file.
Args:
database (str): Name of the database to connect to.
env_file (str): Path to the environment file containing DB credentials.
"""
# Load the environment variables from the .env file
load_dotenv(env_file)
self.host = os.getenv('DB_HOST')
self.user = os.getenv('DB_USER')
self.password = os.getenv('DB_PASSWORD')
self.port = os.getenv('DB_PORT')
self.database = database
def test_connection(self):
"""
Test the connection to the database server.
Returns:
bool: True if connection is successful, False otherwise.
"""
# Establish the connection to the server, doesn't connect to any database
conn = None
try:
conn = mysql.connector.connect(
host=self.host,
port=self.port,
user=self.user,
password=self.password
)
if conn.is_connected():
print("Connection to the DB server was successful.")
return True
else:
print("Failed to connect to the DB server.")
return False
except Error as e:
print(f"Error: {e}")
return False
finally:
if conn and conn.is_connected():
conn.close()
print("DB server connection closed.")
def sql_execute(self, sql: str, val=()):
"""
Execute a SQL command (INSERT, UPDATE, DELETE).
Args:
sql (str): SQL query to execute.
val (tuple): Values to use in the SQL query.
Returns:
tuple: Number of affected rows and last inserted row ID.
"""
# execute a 'sql' command, use variables from the 'val' tuple
# use for executing statements like INSERT, UPDATE, or DELETE
# returns the number of affected rows and the last inserted row ID
conn = None
cursor = None
try:
# Establish the connection
conn = mysql.connector.connect(
host=self.host,
port=self.port,
user=self.user,
password=self.password,
database=self.database,
autocommit=True
)
cursor = conn.cursor()
cursor.execute(sql, val)
conn.commit()
rowcount = cursor.rowcount
lastrowid = cursor.lastrowid
return rowcount, lastrowid
except Error as e:
print(f"Error: {e}")
return None, None
finally:
if cursor:
cursor.close()
if conn and conn.is_connected():
conn.close()
print("Database connection closed.")
def sql_query(self, sql: str, val=()):
"""
Execute a SELECT query and return the results.
Args:
sql (str): SQL query to execute.
val (tuple): Values to use in the SQL query.
Returns:
list: Fetched results as a list of dictionaries.
"""
# for executing SELECT queries and returning results
# returns the fetched results as a list of dictionaries
conn = None
cursor = None
try:
# Establish the connection to the specified database
conn = mysql.connector.connect(
host=self.host,
port=self.port,
user=self.user,
password=self.password,
database=self.database
)
# creates a cursor that returns results as dictionaries instead of tuples.
# allows access to columns by their names instead of their positional indexes.
cursor = conn.cursor(dictionary=True)
# Execute the SELECT query with the provided condition
cursor.execute(sql, val)
results = cursor.fetchall()
return results
except Error as e:
print(f"Error: {e}")
return None
finally:
if cursor:
cursor.close()
if conn and conn.is_connected():
conn.close()
print("Database connection closed.")
def create_database(self, overwrite: bool = False):
"""
Create a new database or check if it exists.
Args:
overwrite (bool): If True, overwrite an existing database.
Returns:
bool: True if database is created or exists, False otherwise.
"""
# this method can create or test if a database exists
# create_database(False) --> do not overwrite an existing database
# create_database(True) --> overwrite an existing database
conn = None
cursor = None
try:
# Establish the connection
conn = mysql.connector.connect(
host=self.host,
port=self.port,
user=self.user,
password=self.password,
autocommit=True
)
cursor = conn.cursor()
# Check if the database exists
cursor.execute(f"SHOW DATABASES LIKE '{self.database}'")
result = cursor.fetchone()
if result:
if overwrite:
# Drop the existing database
cursor.execute(f"DROP DATABASE {self.database}")
print(f"Database {self.database} dropped.")
else:
print(f"Database {self.database} already exists.")
return True
# Create the new database
cursor.execute(f"CREATE DATABASE {self.database}")
print(f"Database {self.database} created.")
return True
except Error as e:
print(f"Error: {e}")
return False
finally:
if cursor:
cursor.close()
if conn and conn.is_connected():
conn.close()
print("Database connection closed.")
def create_table(self, table_name: str, sql_code: str, overwrite: bool = False):
"""
Create a new table in the database or check if it exists.
Args:
table_name (str): Name of the table to create.
sql_code (str): SQL code to create the table.
overwrite (bool): If True, overwrite an existing table.
Returns:
bool: True if table is created or exists, False otherwise.
"""
# checks if a table exists before executing the 'sql' code to create it
# if overwrite == True, then overwrite an existing table
conn = None
cursor = None
try:
# Establish the connection to the specified database
conn = mysql.connector.connect(
host=self.host,
port=self.port,
user=self.user,
password=self.password,
database=self.database,
autocommit=True
)
cursor = conn.cursor()
# Check if the table exists
cursor.execute(f"SHOW TABLES LIKE '{table_name}'")
result = cursor.fetchone()
if result:
if overwrite:
# Drop the existing table
cursor.execute(f"DROP TABLE {table_name}")
print(f"Table {table_name} dropped.")
else:
print(f"Table {table_name} already exists in database {self.database}.")
return
# Create the new table
cursor.execute(sql_code)
print(f"Table {table_name} created in database {self.database}.")
return True
except Error as e:
print(f"Error: {e}")
return False
finally:
if cursor:
cursor.close()
if conn and conn.is_connected():
conn.close()
print("Database connection closed.")
# All the following classes are Pydantic V2 classes.
# They leverage Pydantic's type annotations and validations,
# this way, we avoid writing getters and setters for each attribute,
# and we get Pydantic to check the data types automatically.
class Quote(BaseModel):
id_quote: Optional[PositiveInt] = Field(None, alias='id_quote')
content: str = Field(..., alias='content')
author_id: Optional[PositiveInt] = Field(None, alias='author_id')
tags: List[str] = Field(..., alias='tags')
def db_save(self, db: MySQLDB):
"""
Save the quote to the database.
Args:
db (MySQLDB): Database connection instance.
Returns:
tuple: Number of affected rows and last inserted row ID.
"""
sql = """
INSERT INTO quote (content, author_id, tags)
VALUES (%s, %s, %s)
"""
# the list 'tags' is transformed into a string before inserting into the database
val = (self.content, self.author_id, ', '.join(self.tags))
rowcount, lastrowid = db.sql_execute(sql, val)
if lastrowid:
self.id_quote = lastrowid # assign id to object
return rowcount, lastrowid
def fetch_all(db: MySQLDB):
"""
Fetch all quotes from the database.
Args:
db (MySQLDB): Database connection instance.
Returns:
list: List of dictionaries containing all quotes.
"""
sql_query = """
SELECT
q.content AS content,
a.name AS author_name,
q.tags AS tags
FROM quote q
JOIN author a ON q.author_id = a.id_author
"""
results = db.sql_query(sql_query, ())
return results # returns a list of dicts, else the db returns []
def fetch_by_id(db: MySQLDB, quote_id: int):
"""
Fetch a quote by its ID from the database.
Args:
db (MySQLDB): Database connection instance.
quote_id (int): ID of the quote to fetch.
Returns:
list: List containing one dictionary with quote details, or an empty list if not found.
"""
sql_query = """
SELECT
q.content AS content,
a.name AS author_name,
q.tags AS tags
FROM quote q
JOIN author a ON q.author_id = a.id_author
WHERE q.id_quote = %s
"""
val = (quote_id,)
result = db.sql_query(sql_query, val)
return result # if quote_id exists, then returns list with one dict, else the db returns []
def fetch_by_tag(db: MySQLDB, tag: str):
"""
Fetch quotes by tag from the database.
Args:
db (MySQLDB): Database connection instance.
tag (str): Tag to filter quotes.
Returns:
list: List of dictionaries containing quotes with the specified tag.
"""
sql_query = """
SELECT
q.content AS content,
a.name AS author_name,
q.tags AS tags
FROM quote q
JOIN author a ON q.author_id = a.id_author
WHERE q.tags LIKE %s
"""
val = ("%" + tag + "%",)
result = db.sql_query(sql_query, val)
return result # if tag exists, then returns list of dicts, else the db returns []
def fetch_by_author(db: MySQLDB, author_name: str):
"""
Fetch quotes by author from the database.
Args:
db (MySQLDB): Database connection instance.
author_name (str): Author name to filter quotes.
Returns:
list: List of dictionaries containing quotes by the specified author.
"""
sql_query = """
SELECT
q.content AS content,
a.name AS author_name,
q.tags AS tags
FROM quote q
JOIN author a ON q.author_id = a.id_author
WHERE a.name LIKE %s
"""
val = (author_name,)
results = db.sql_query(sql_query, val)
return results # if author_name exists, then returns list of dicts, else the db returns []
class Author(BaseModel):
id_author: Optional[PositiveInt] = Field(None, alias='id_author')
name: str = Field(..., alias='name')
birth_date: date = Field(..., alias='birth_date')
birth_city: Optional[str] = Field(..., alias='birth_city')
birth_state: Optional[str] = Field(..., alias='birth_state')
birth_country: Optional[str] = Field(..., alias='birth_country')
description: str = Field(..., alias='description')
def db_save(self, db: MySQLDB):
"""
Save the author to the database.
Args:
db (MySQLDB): Database connection instance.
Returns:
tuple: Number of affected rows and last inserted row ID.
"""
sql = """
INSERT INTO author (name, birth_date, birth_city, birth_state, birth_country, description)
VALUES (%s, %s, %s, %s, %s, %s)
"""
val = (self.name, self.birth_date, self.birth_city, self.birth_state, self.birth_country, self.description)
rowcount, lastrowid = db.sql_execute(sql, val)
if lastrowid:
self.id_author = lastrowid # assign id to object
return rowcount, lastrowid
def fetch_all(db: MySQLDB):
"""
Fetch all authors from the database.
Args:
db (MySQLDB): Database connection instance.
Returns:
list: List of dictionaries containing all authors.
"""
sql_query = """
SELECT name, birth_date, birth_city, birth_state, birth_country, description
FROM author
"""
results = db.sql_query(sql_query, ())
return results # returns a list of dicts, else the db returns []
def fetch_by_id(db: MySQLDB, author_id: int):
"""
Fetch an author by their ID from the database.
Args:
db (MySQLDB): Database connection instance.
author_id (int): ID of the author to fetch.
Returns:
list: List containing one dictionary with author details, or an empty list if not found.
"""
sql_query = """
SELECT name, birth_date, birth_city, birth_state, birth_country, description
FROM author
WHERE id_author = %s"""
val = (author_id,)
result = db.sql_query(sql_query, val)
return result # if author_id exists, then returns list with one dict, else the db returns []
def fetch_by_name(db: MySQLDB, author_name: str):
"""
Fetch an author by their name from the database.
Args:
db (MySQLDB): Database connection instance.
author_name (str): Name of the author to fetch.
Returns:
list: List containing one dictionary with author details, or an empty list if not found.
"""
sql_query = """
SELECT name, birth_date, birth_city, birth_state, birth_country, description
FROM author
WHERE name LIKE %s"""
val = (author_name,)
result = db.sql_query(sql_query, val)
return result # if author_name exists, then returns list with one dict, else the db returns []
class Comment(BaseModel):
id_comment: Optional[PositiveInt] = Field(None, alias='id_comment')
quote_id: PositiveInt = Field(..., alias='quote_id')
title: str = Field(..., alias='title')
details: str = Field(..., alias='details')
user_email: EmailStr = Field(..., alias='user_email')
def db_save(self, db: MySQLDB):
"""
Save the comment to the database.
Args:
db (MySQLDB): Database connection instance.
Returns:
tuple: Number of affected rows and last inserted row ID.
"""
sql = """
INSERT INTO comment (quote_id, title, details, user_email)
VALUES (%s, %s, %s, %s)
"""
val = (self.quote_id, self.title, self.details, self.user_email)
rowcount, lastrowid = db.sql_execute(sql, val)
if lastrowid:
self.id_comment = lastrowid # assign id to object
return rowcount, lastrowid
def fetch_by_quote_id(db: MySQLDB, quote_id: int):
"""
Fetch comments by quote ID from the database.
Args:
db (MySQLDB): Database connection instance.
quote_id (int): ID of the quote to filter comments.
Returns:
list: List of dictionaries containing comments for the specified quote.
"""
sql_query = """
SELECT title, details, user_email
FROM comment
WHERE quote_id = %s"""
val = (quote_id,)
result = db.sql_query(sql_query, val)
return result # if quote_id exists, then returns list of dicts, else the db returns []
class Metadata(BaseModel):
id_key: Optional[PositiveInt] = Field(None, alias='id_key')
key_name: str = Field(..., alias='key_name')
key_value: List[str] = Field(..., alias='key_value')
def db_save(self, db: MySQLDB):
"""
Save the metadata to the database.
Args:
db (MySQLDB): Database connection instance.
Returns:
tuple: Number of affected rows and last inserted row ID.
"""
sql = """
INSERT INTO metadata (key_name, key_value)
VALUES (%s, %s)
"""
# transform the list of key_values into a string before inserting into the database
val = (self.key_name, ', '.join(self.key_value))
rowcount, lastrowid = db.sql_execute(sql, val)
if lastrowid:
self.id_key = lastrowid # assign id to object
return rowcount, lastrowid
def fetch_by_key_name(db: MySQLDB, key_name: str):
"""
Fetch metadata by key name from the database.
Args:
db (MySQLDB): Database connection instance.
key_name (str): Key name to filter metadata.
Returns:
list: List containing one dictionary with metadata details, or an empty list if not found.
"""
sql_query = """
SELECT key_value
FROM metadata
WHERE key_name = %s"""
val = (key_name,)
result = db.sql_query(sql_query, val)
return result # returns list with one dict, else the db returns []
# the ModelQueries() class encapsulates all the SQL queries that will be run by the user
# this class is meant to be the Model that is used by the TKinter View-Controller
# it provides an API to retrieve data from the database,
# without the TKinter View-Controller having to know the details of the tables
class ModelQueries:
def __init__(self, database: str):
"""
Initialize the ModelQueries instance with the specified database.
This class implements the Model in the MVP/MVC pattern.
Args:
database (str): Name of the database to connect to.
"""
self.db = MySQLDB(database)
def run_model(self, query_case, value=None):
"""
Run the specified query based on the query case.
Args:
query_case (str): The case specifying which query to run.
value (str, optional): The value to use in the query.
Returns:
list or tuple: Results of the query.
"""
# These are all the queries available to run in the GUI
match query_case:
case ('quotes_by_author'):
return Quote.fetch_by_author(self.db, value) # list of dicts
case ('x_quotes'):
# return a list of 'value' random quotes
results = self.db.sql_query("""
SELECT
q.content,
a.name AS author_name,
q.tags
FROM
quote q
JOIN
author a ON q.author_id = a.id_author
ORDER BY
RAND()
LIMIT
%s
""", (value,))
return results # list of dicts
case ('random_quote'):
# return one random quote
results = self.db.sql_query("""
SELECT
q.content,
a.name AS author_name,
q.tags
FROM
quote q
JOIN
author a ON q.author_id = a.id_author
ORDER BY
RAND()
LIMIT
1
""", ())
return results # list of dicts
case ('total_quotes'):
# return count of total quotes
results = self.db.sql_query("""
SELECT
COUNT(*) AS total_quotes
FROM
quote;
""", ())
return results # list of dicts
case ('quotes_by_tag'):
return Quote.fetch_by_tag(self.db, value) # list of dicts
case ('top5_authors'):
# return TOP 5 Authors by total quotes
results = self.db.sql_query("""
SELECT
a.name,
COUNT(q.id_quote) AS quote_count
FROM
author a
JOIN
quote q ON a.id_author = q.author_id
GROUP BY
a.name
ORDER BY
quote_count DESC
LIMIT
5;
""", ())
return results # list of dicts
case ('comments_random_quote'):
# note: works better if queries are split in 2
# return id for one random quote
rand_id = self.db.sql_query("""
SELECT
id_quote
FROM
quote
ORDER BY
RAND()
LIMIT 1
""", ())
# return the random quote
value = rand_id[0]['id_quote']
random_quote = Quote.fetch_by_id(self.db, value)
# return all comments from one random quote
results = self.db.sql_query("""
SELECT
title,
details,
user_email
FROM
comment
WHERE
quote_id = %s;
""", (value, ))
return results, random_quote # 2 x list of dicts
case ('author_bio'):
return Author.fetch_by_name(self.db, value) # list of dicts
case ('all_quotes'):
return Quote.fetch_all(self.db) # list of dicts
case ('metadata'):
return Metadata.fetch_by_key_name(self.db, value)
case _:
return []