db
1import sqlite3 2import uuid 3from job import Job 4 5 6def does_exist(job: Job) -> bool: 7 """Returns True if job exists in database, False otherwise""" 8 9 conn: sqlite3.Connection = sqlite3.connect("jobs.db") 10 c: sqlite3.Cursor = conn.cursor() 11 12 c.execute( 13 "SELECT * FROM jobs WHERE title = ? AND company = ? AND location = ?", 14 (job.title, job.company, job.location), 15 ) 16 result = c.fetchone() 17 18 conn.close() 19 20 return result is not None 21 22 23def edit_job( 24 job: Job, 25 field: str, 26 value: str, 27) -> None: 28 """Edits job with specified title""" 29 30 conn: sqlite3.Connection = sqlite3.connect("jobs.db") 31 c: sqlite3.Cursor = conn.cursor() 32 33 # Check if job exists in database 34 if not does_exist(job): 35 raise ValueError("Job does not exist") 36 37 # Check if field is valid 38 if field not in ("title", "company", "link", "location", "date"): 39 raise ValueError("Invalid field") 40 41 c.execute(f"UPDATE jobs SET {field} = ? WHERE title = ?", (value, job.title)) 42 43 # Modify job object 44 match field: 45 case "title": 46 job.title = value 47 case "company": 48 job.company = value 49 case "link": 50 job.link = value 51 case "location": 52 job.location = value 53 case "date": 54 job.date = value 55 56 conn.commit() 57 conn.close() 58 59 60def delete_job(job: Job) -> None: 61 """Deletes job with specified title""" 62 63 conn: sqlite3.Connection = sqlite3.connect("jobs.db") 64 c: sqlite3.Cursor = conn.cursor() 65 66 # Check if job exists in database 67 if not does_exist(job): 68 raise ValueError("Job does not exist") 69 70 try: 71 c.execute("DELETE FROM jobs WHERE title = ?", (job.title,)) 72 except sqlite3.Error as e: 73 raise e 74 75 conn.commit() 76 conn.close() 77 78 79def get_job(title: str) -> Job: 80 """Returns job with specified title""" 81 82 conn = sqlite3.connect("jobs.db") 83 c = conn.cursor() 84 85 c.execute("SELECT * FROM jobs WHERE title = ?", (title)) 86 result = c.fetchone() 87 88 conn.close() 89 90 if result is None: 91 raise ValueError("No job found with that title") 92 93 return Job(result[1], result[2], result[3], result[4], result[5], result[6]) 94 95 96def get_jobs() -> list[Job]: 97 """Returns all jobs in database""" 98 99 conn: sqlite3.Connection = sqlite3.connect("jobs.db") 100 c: sqlite3.Cursor = conn.cursor() 101 102 c.execute("SELECT * FROM jobs") 103 jobs: list[tuple[int, str, int, str, str, int]] = c.fetchall() 104 105 conn.close() 106 107 return [Job(job[1], job[2], job[3], job[4], job[5], job[6]) for job in jobs] 108 109 110def insert_job(job: Job) -> None: 111 """Inserts job into database""" 112 113 conn: sqlite3.Connection = sqlite3.connect("jobs.db") 114 c: sqlite3.Cursor = conn.cursor() 115 116 # Check if job already exists in database 117 if does_exist(job): 118 return 119 120 try: 121 c.execute( 122 "INSERT INTO jobs VALUES (?, ?, ?, ?, ?, ?, ?)", 123 ( 124 (str(uuid.uuid4())), 125 job.title, 126 job.company, 127 job.location, 128 job.link, 129 job.date, 130 job.date_added, 131 ), 132 ) 133 134 conn.commit() 135 conn.close() 136 except Exception as e: 137 print(f"Error: {e}") 138 139 140def initialize_database() -> None: 141 """Creates/initializes database""" 142 143 conn: sqlite3.Connection = sqlite3.connect("jobs.db") 144 c: sqlite3.Cursor = conn.cursor() 145 146 # Create table if it does not exist 147 try: 148 c.execute( 149 """CREATE TABLE jobs ( 150 unique_id text primary key, 151 title text not null, 152 company text not null, 153 location text not null, 154 link text, 155 date text, 156 date_added text default current_timestamp 157 )""" 158 ) 159 except sqlite3.OperationalError: 160 # Table already exists 161 pass 162 163 conn.commit() 164 conn.close()
def
does_exist(job: job.Job) -> bool:
7def does_exist(job: Job) -> bool: 8 """Returns True if job exists in database, False otherwise""" 9 10 conn: sqlite3.Connection = sqlite3.connect("jobs.db") 11 c: sqlite3.Cursor = conn.cursor() 12 13 c.execute( 14 "SELECT * FROM jobs WHERE title = ? AND company = ? AND location = ?", 15 (job.title, job.company, job.location), 16 ) 17 result = c.fetchone() 18 19 conn.close() 20 21 return result is not None
Returns True if job exists in database, False otherwise
def
edit_job(job: job.Job, field: str, value: str) -> None:
24def edit_job( 25 job: Job, 26 field: str, 27 value: str, 28) -> None: 29 """Edits job with specified title""" 30 31 conn: sqlite3.Connection = sqlite3.connect("jobs.db") 32 c: sqlite3.Cursor = conn.cursor() 33 34 # Check if job exists in database 35 if not does_exist(job): 36 raise ValueError("Job does not exist") 37 38 # Check if field is valid 39 if field not in ("title", "company", "link", "location", "date"): 40 raise ValueError("Invalid field") 41 42 c.execute(f"UPDATE jobs SET {field} = ? WHERE title = ?", (value, job.title)) 43 44 # Modify job object 45 match field: 46 case "title": 47 job.title = value 48 case "company": 49 job.company = value 50 case "link": 51 job.link = value 52 case "location": 53 job.location = value 54 case "date": 55 job.date = value 56 57 conn.commit() 58 conn.close()
Edits job with specified title
def
delete_job(job: job.Job) -> None:
61def delete_job(job: Job) -> None: 62 """Deletes job with specified title""" 63 64 conn: sqlite3.Connection = sqlite3.connect("jobs.db") 65 c: sqlite3.Cursor = conn.cursor() 66 67 # Check if job exists in database 68 if not does_exist(job): 69 raise ValueError("Job does not exist") 70 71 try: 72 c.execute("DELETE FROM jobs WHERE title = ?", (job.title,)) 73 except sqlite3.Error as e: 74 raise e 75 76 conn.commit() 77 conn.close()
Deletes job with specified title
def
get_job(title: str) -> job.Job:
80def get_job(title: str) -> Job: 81 """Returns job with specified title""" 82 83 conn = sqlite3.connect("jobs.db") 84 c = conn.cursor() 85 86 c.execute("SELECT * FROM jobs WHERE title = ?", (title)) 87 result = c.fetchone() 88 89 conn.close() 90 91 if result is None: 92 raise ValueError("No job found with that title") 93 94 return Job(result[1], result[2], result[3], result[4], result[5], result[6])
Returns job with specified title
def
get_jobs() -> list[job.Job]:
97def get_jobs() -> list[Job]: 98 """Returns all jobs in database""" 99 100 conn: sqlite3.Connection = sqlite3.connect("jobs.db") 101 c: sqlite3.Cursor = conn.cursor() 102 103 c.execute("SELECT * FROM jobs") 104 jobs: list[tuple[int, str, int, str, str, int]] = c.fetchall() 105 106 conn.close() 107 108 return [Job(job[1], job[2], job[3], job[4], job[5], job[6]) for job in jobs]
Returns all jobs in database
def
insert_job(job: job.Job) -> None:
111def insert_job(job: Job) -> None: 112 """Inserts job into database""" 113 114 conn: sqlite3.Connection = sqlite3.connect("jobs.db") 115 c: sqlite3.Cursor = conn.cursor() 116 117 # Check if job already exists in database 118 if does_exist(job): 119 return 120 121 try: 122 c.execute( 123 "INSERT INTO jobs VALUES (?, ?, ?, ?, ?, ?, ?)", 124 ( 125 (str(uuid.uuid4())), 126 job.title, 127 job.company, 128 job.location, 129 job.link, 130 job.date, 131 job.date_added, 132 ), 133 ) 134 135 conn.commit() 136 conn.close() 137 except Exception as e: 138 print(f"Error: {e}")
Inserts job into database
def
initialize_database() -> None:
141def initialize_database() -> None: 142 """Creates/initializes database""" 143 144 conn: sqlite3.Connection = sqlite3.connect("jobs.db") 145 c: sqlite3.Cursor = conn.cursor() 146 147 # Create table if it does not exist 148 try: 149 c.execute( 150 """CREATE TABLE jobs ( 151 unique_id text primary key, 152 title text not null, 153 company text not null, 154 location text not null, 155 link text, 156 date text, 157 date_added text default current_timestamp 158 )""" 159 ) 160 except sqlite3.OperationalError: 161 # Table already exists 162 pass 163 164 conn.commit() 165 conn.close()
Creates/initializes database