-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_functions.py
116 lines (90 loc) · 3.72 KB
/
db_functions.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
import sys
import sqlite3
import datetime
import sys
"""
Class to handle basic functions with the sqlite db
TODO: error handling
"""
class db_wrapper:
def __init__(self, db_path):
self.conn_obj = sqlite3.connect(db_path)
self.cursor = self.conn_obj.cursor()
#things ran regularly
#---------------------------------------------------#
def get_songs(self):
sql = 'select * from songs'
self.cursor.execute(sql)
return self.cursor.fetchall()
def process_song_local(self, username, track):
'''Adds song to local db'''
if not self._song_exists(track.Name, track.Artist):
self._add_song(track, 'local')
def process_song_central(self, username, track):
'''
If this song does not exist at all in central, add it. Then
handle the user song record
'''
song_id = self._song_exists(track.Name, track.Artist)
if not song_id:
song_id = self._add_song(track, 'central')
if not self._user_song_exists(song_id, username):
self._add_user_song(song_id, username)
#things ran initially by the first setup script
#---------------------------------------------------#
def add_user(self, username):
params = (username,)
sql = """insert into users (name) values (?)"""
self.cursor.execute(sql, params)
self.conn_obj.commit()
def create_songs_table(self):
sql = """CREATE TABLE songs(
id INTEGER PRIMARY KEY NOT NULL,
title TEXT NOT NULL,
artist TEXT,
album TEXT,
creation_date DATE DEFAULT (datetime('now','localtime'))
)"""
self.cursor.execute(sql)
self.conn_obj.commit()
def get_total_changes(self):
return self.conn_obj.total_changes
#things used only by utilities and such.
#--------------------------------------------------#
def get_user(self, username):
params = (username,)
sql = """SELECT * FROM users WHERE name = ?"""
self.cursor.execute(sql, params)
return self.cursor.fetchone()
def _song_exists(self, title, artist):
'''
If the song name and singer are the same, consider it a dupe. This
returns None or the id of the song
'''
params = (title.lower(), artist.lower(), )
sql = """SELECT id FROM songs
WHERE LOWER(title) = ?
AND LOWER(artist) = ?"""
self.cursor.execute(sql, params)
song_id = self.cursor.fetchone()
return(song_id if song_id == None else song_id[0])
def _user_song_exists(self, song_id, user_id):
params = (song_id, user_id, )
sql = """SELECT 1 FROM user_songs
WHERE song_id = ?
AND user_id = ?"""
self.cursor.execute(sql, params)
return(self.cursor.fetchone())
def _add_song(self, track, db):
print("adding song *** %s *** from *** %s *** to %s db" % (track.Name, track.Artist, db))
params = (track.Name, track.Artist, track.Album)
sql = """INSERT INTO songs (title, artist, album, creation_date)
VALUES (?, ?, ?, datetime('NOW'))"""
self.cursor.execute(sql, params)
self.conn_obj.commit()
return(self.cursor.lastrowid)
def _add_user_song(self, song_id, user_id):
params = (song_id, user_id)
sql = """INSERT INTO user_songs (song_id, user_id) VALUES (?, ?)"""
self.cursor.execute(sql, params)
self.conn_obj.commit()