File: playlist.cc

package info (click to toggle)
imms 3.0.2-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 920 kB
  • ctags: 1,195
  • sloc: cpp: 7,224; ansic: 1,872; sh: 186; makefile: 127
file content (190 lines) | stat: -rw-r--r-- 4,281 bytes parent folder | download | duplicates (2)
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
#include <iostream>

#include "playlist.h"
#include "strmanip.h"
#include "immsutil.h" 

using std::endl;
using std::cerr;

void PlaylistDb::sql_create_tables()
{
    RuntimeErrorBlocker reb;
    try {
        Q("CREATE TABLE DiskPlaylist ("
                "'uid' INTEGER NOT NULL);").execute();

        Q("CREATE TABLE DiskMatches "
                "('uid' INTEGER UNIQUE NOT NULL);").execute();

        Q("CREATE TEMPORARY TABLE Playlist ("
                "'pos' INTEGER PRIMARY KEY, "
                "'path' VARCHAR(4096) NOT NULL, "
                "'uid' INTEGER DEFAULT -1);").execute();

        Q("CREATE TEMPORARY TABLE Matches "
                "('uid' INTEGER UNIQUE NOT NULL);").execute();

        Q("CREATE TEMPORARY VIEW Filter AS "
                "SELECT * FROM Playlist WHERE uid IN Matches "
                "OR NOT EXISTS (SELECT * FROM Matches LIMIT 1);").execute();
    }
    WARNIFFAILED();
}

int PlaylistDb::get_unknown_playlist_item()
{
    try {
        Q q("SELECT pos FROM Playlist WHERE uid = -1 LIMIT 1;");

        if (q.next())
        {
            int result;
            q >> result;
            return result;
        }
    }
    WARNIFFAILED();

    return -1;
}

Song PlaylistDb::playlist_id_from_item(int pos)
{
    try {
        Q q("SELECT L.uid, L.sid, P.path FROM Library L "
                "INNER JOIN Playlist P USING(uid) WHERE P.pos = ?;");
        q << pos;

        if (!q.next())
            return Song();

        int uid, sid;
        string path;
        q >> uid >> sid >> path;
        return Song(path, uid, sid);
    }
    WARNIFFAILED();
    return Song();
}

void PlaylistDb::playlist_update_identity(int pos, int uid)
{
    try {
        Q q("UPDATE Playlist SET uid = ? WHERE pos = ?;");
        q << uid << pos;
        q.execute();
    }
    WARNIFFAILED();
}

void PlaylistDb::playlist_insert_item(int pos, const string &path)
{
    try {
        Q q("INSERT OR REPLACE INTO Playlist ('pos', 'path', 'uid') "
                "VALUES (?, ?, coalesce((SELECT uid FROM Identify "
                    "WHERE path = ?), -1));");
        q << pos << path << path;
        q.execute();
    }
    WARNIFFAILED();
}

int PlaylistDb::get_real_playlist_length()
{
    int result = 0;
    try {
        Q q("SELECT count(1) FROM Playlist;");
        if (q.next())
            q >> result;
    }
    WARNIFFAILED();

    return result;
}

int PlaylistDb::get_effective_playlist_length()
{
    if (effective_length_cache != -1)
        return effective_length_cache;

    try {
        Q q("SELECT count(1) FROM Filter WHERE uid != -2;");
        if (q.next())
            q >> effective_length_cache;
    }
    WARNIFFAILED();

    return effective_length_cache;
}

void PlaylistDb::get_random_sample(vector<int> &metacandidates, int size)
{
    try {
        int total = get_effective_playlist_length();

        Q q("SELECT pos FROM Filter "
                "WHERE uid != -2 AND (abs(random()) % ?) < ?;");
        q << total << (size + 5);

        int result;
        while (q.next())
        {
            q >> result;
            metacandidates.push_back(result);
        }

    }
    WARNIFFAILED();
}

void PlaylistDb::clear_matches()
{
    try {
        AutoTransaction a(AppName != IMMSD_APP);
        Q("DELETE FROM DiskMatches;").execute();
        a.commit();
    }
    IGNOREFAILURE();
}

string PlaylistDb::get_item_from_playlist(int pos)
{
    string path;

    try {
        Q q("SELECT path FROM Playlist WHERE pos = ?;");
        q << pos;
        if (q.next())
            q >> path;
    }
    WARNIFFAILED();

    return path;
}

void PlaylistDb::playlist_clear()
{
    try {
        Q("DELETE FROM Playlist;").execute();
        Q("DELETE FROM Matches;").execute();
        Q("DELETE FROM DiskPlaylist;").execute();
        Q("DELETE FROM DiskMatches;").execute();
    }
    WARNIFFAILED();
}

void PlaylistDb::sync()
{
    effective_length_cache = -1;
    try {
        AutoTransaction a;
        Q("DELETE FROM DiskPlaylist;").execute();
        Q("INSERT INTO DiskPlaylist "
                "SELECT uid FROM Playlist;").execute();
        Q("DELETE FROM Matches;").execute();
        Q("INSERT INTO Matches SELECT uid FROM DiskMatches;").execute();
        a.commit();
    }
    WARNIFFAILED();
}