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
|
#include <QtSql>
#include <QDir>
#include <QtDebug>
#include <QRegExp>
#include <QStringBuilder>
#include "library/dao/directorydao.h"
#include "library/queryutil.h"
#include "util/db/sqllikewildcards.h"
#include "util/db/sqllikewildcardescaper.h"
int DirectoryDAO::addDirectory(const QString& newDir) {
// Do nothing if the dir to add is a child of a directory that is already in
// the db.
QStringList dirs = getDirs();
QString childDir;
QString parentDir;
foreach (const QString& dir, dirs) {
if (isChildDir(newDir, dir)) {
childDir = dir;
}
if (isChildDir(dir, newDir)) {
parentDir = dir;
}
}
if (!childDir.isEmpty()) {
qDebug() << "return already watching";
return ALREADY_WATCHING;
}
if (!parentDir.isEmpty()) {
// removing the old directory won't harm because we are adding the
// parent later in this function
removeDirectory(parentDir);
}
QSqlQuery query(m_database);
query.prepare("INSERT INTO " % DIRECTORYDAO_TABLE %
" (" % DIRECTORYDAO_DIR % ") VALUES (:dir)");
query.bindValue(":dir", newDir);
if (!query.exec()) {
LOG_FAILED_QUERY(query) << "Adding new dir (" % newDir % ") failed.";
return SQL_ERROR;
}
return ALL_FINE;
}
bool DirectoryDAO::isChildDir(QString testDir, QString dirStr) {
QDir test = QDir(testDir);
QDir dir = QDir(dirStr);
bool child = dir == test;
while (test.cdUp()) {
if (dir == test) {
child = true;
}
}
// qDebug() << "--- test related function ---";
// qDebug() << "testDir " << testDir;
// qDebug() << "dir" << dirStr;
// qDebug() << "child = " << child;
// qDebug() << "-----------------------------";
return child;
}
int DirectoryDAO::removeDirectory(const QString& dir) {
QSqlQuery query(m_database);
query.prepare("DELETE FROM " % DIRECTORYDAO_TABLE % " WHERE "
% DIRECTORYDAO_DIR % "= :dir");
query.bindValue(":dir", dir);
if (!query.exec()) {
LOG_FAILED_QUERY(query) << "purging dir (" % dir % ") failed";
return SQL_ERROR;
}
return ALL_FINE;
}
QList<TrackRef> DirectoryDAO::relocateDirectory(const QString& oldFolder,
const QString& newFolder) {
// TODO(rryan): This method could use error reporting. It can fail in
// mysterious ways for example if a track in the oldFolder also has a zombie
// track location in newFolder then the replace query will fail because the
// location column becomes non-unique.
QSqlQuery query(m_database);
query.prepare("UPDATE " % DIRECTORYDAO_TABLE % " SET " % DIRECTORYDAO_DIR %
"=:newFolder WHERE " % DIRECTORYDAO_DIR % " = :oldFolder");
query.bindValue(":newFolder", newFolder);
query.bindValue(":oldFolder", oldFolder);
if (!query.exec()) {
LOG_FAILED_QUERY(query) << "could not relocate directory"
<< oldFolder << "to" << newFolder;
return {};
}
// on Windows the absolute path starts with the drive name
// we also need to check for that
QString startsWithOldFolder = SqlLikeWildcardEscaper::apply(
QDir(oldFolder).absolutePath() + "/", kSqlLikeMatchAll) + kSqlLikeMatchAll;
// Also update information in the track_locations table. This is where mixxx
// gets the location information for a track. Put marks around %1 so that
// this also works on windows
query.prepare(QString("SELECT library.id, track_locations.id, track_locations.location "
"FROM library INNER JOIN track_locations ON "
"track_locations.id = library.location WHERE "
"track_locations.location LIKE '%1' ESCAPE '%2'")
.arg(startsWithOldFolder, kSqlLikeMatchAll));
if (!query.exec()) {
LOG_FAILED_QUERY(query) << "could not relocate path of tracks";
return {};
}
QList<DbId> loc_ids;
QList<TrackRef> trackRefs;
while (query.next()) {
loc_ids.append(DbId(query.value(1).toInt()));
trackRefs.append(TrackRef::fromFileInfo(query.value(2).toString(), TrackId(query.value(0))));
}
QString replacement = "UPDATE track_locations SET location = :newloc "
"WHERE id = :id";
query.prepare(replacement);
for (int i = 0; i < loc_ids.size(); ++i) {
QString newloc = trackRefs.at(i).getLocation();
newloc.replace(0, oldFolder.size(), newFolder);
query.bindValue("newloc", newloc);
query.bindValue("id", loc_ids.at(i).toVariant());
if (!query.exec()) {
LOG_FAILED_QUERY(query) << "could not relocate path of tracks";
return {};
}
}
qDebug() << "Relocated tracks:" << trackRefs.size();
return trackRefs;
}
QStringList DirectoryDAO::getDirs() {
QSqlQuery query(m_database);
query.prepare("SELECT " % DIRECTORYDAO_DIR % " FROM " % DIRECTORYDAO_TABLE);
if (!query.exec()) {
LOG_FAILED_QUERY(query) << "could not retrieve directory list from database";
}
QStringList dirs;
const int dirColumn = query.record().indexOf(DIRECTORYDAO_DIR);
while (query.next()) {
dirs << query.value(dirColumn).toString();
}
return dirs;
}
|