summaryrefslogtreecommitdiff
path: root/python/database.py
blob: 06f5d61fb42f2196f7b2a42d2b0048d8aa10a47a (plain)
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
import mysql.connector


connector = mysql.connector.connect(
    host="localhost",
    user="root",
    database="balalaika",
)

cursor = connector.cursor()


def get_song(id, name):
    if id is None:
        id = "%"

    if name is None:
        name = "%"

    cursor.execute(
        """
        select * from song
        where id like %(id)s
        and name like %(name)s;
        """,
        {
            'id': id,
            'name': name,
        }
    )

    return cursor.fetchall()


def get_filtered_songs(song, album, artist):
    if artist is None:
        artist = "%"

    if album is None:
        album = "%"

    cursor.execute(
        """
        select song.name, album.name, artist.name
        from song
        inner join album on song.album_id=album.id
        inner join artist on album.artist_id=artist.id
        where artist.name like %(artist)s
        and album.name like %(album)s;
        """,
        {
            'artist': artist,
            'album': album,
            'song': song,
        }
    )

    return cursor.fetchall()