-
Notifications
You must be signed in to change notification settings - Fork 2
/
AmarokCollection.cpp
317 lines (282 loc) · 11.3 KB
/
AmarokCollection.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* This file is part of Neposync program.
* Copyright (C) 2010 Eric Pignet <eric at erixpage dot com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Unfortunately QtSql cannot connect to an embedded Mysql file (mysqle).
* A solution could be to write a QtSql driver for Mysqle.
* But for neposync we'll use directly mysql API.
* Part of this file is extracted from MySqlEmbeddedStorage.cpp (Amarok sources, GPL licence).
*/
#include <mysql/mysql.h>
#include "AmarokCollection.h"
#include <iostream>
#include <kstandarddirs.h>
#include <kglobal.h>
#include <QtCore/QString>
#include <QtCore/QDir>
bool AmarokCollection::connect()
{
QString defaultsFile;
// TODO improve amarok directory detection (mimic amarok itself ?)
QString storageLocation = KGlobal::dirs()->localkdedir() + "/share/apps/amarok/";
QString databaseDir;
QDir dir( storageLocation);
dir.mkpath( "." ); //ensure directory exists
defaultsFile = QDir::cleanPath( dir.absoluteFilePath( "my.cnf" ) );
databaseDir = dir.absolutePath() + QDir::separator() + "mysqle";
if (m_isVerbose)
{
std::cout << "Path of Amarok mysqle database: " << databaseDir.toStdString() << std::endl;
}
char* defaultsLine = qstrdup( QString( "--defaults-file=%1" ).arg( defaultsFile ).toAscii().data() );
char* databaseLine = qstrdup( QString( "--datadir=%1" ).arg( databaseDir ).toAscii().data() );
if( !QFile::exists( defaultsFile ) )
{
QFile df( defaultsFile );
if ( !df.open( QIODevice::WriteOnly ) )
{
std::cout << "Error: Unable to open " << defaultsFile.toStdString() << " for writing." << std::endl;
return false;
}
}
if( !QFile::exists( databaseDir ) )
{
std::cout << "Error: mysql database does not exist: " << databaseDir.toStdString() << std::endl;
return false;
}
static const int num_elements = 9;
char **server_options = new char* [ num_elements + 1 ];
server_options[0] = const_cast<char*>( "amarokmysqld" );
server_options[1] = defaultsLine;
server_options[2] = databaseLine;
// CAUTION: if we ever change the table type we will need to fix a number of MYISAM specific
// functions, such as FULLTEXT indexing.
server_options[3] = const_cast<char*>( "--default-storage-engine=MYISAM" );
server_options[4] = const_cast<char*>( "--loose-skip-innodb" );
server_options[5] = const_cast<char*>( "--skip-grant-tables" );
server_options[6] = const_cast<char*>( "--myisam-recover=FORCE" );
server_options[7] = const_cast<char*>( "--character-set-server=utf8" );
server_options[8] = const_cast<char*>( "--collation-server=utf8_bin" );
server_options[num_elements] = 0;
char **server_groups = new char* [ 3 ];
server_groups[0] = const_cast<char*>( "amarokserver" );
server_groups[1] = const_cast<char*>( "amarokclient" );
server_groups[2] = 0;
if( mysql_library_init(num_elements, server_options, server_groups) != 0 )
{
std::cout << "MySQL library initialization failed." << std::endl;
return false;
}
m_db = mysql_init( NULL );
delete [] server_options;
delete [] server_groups;
delete [] defaultsLine;
delete [] databaseLine;
if( !m_db )
{
std::cout << "Error: MySQLe initialization failed" << std::endl;
return false;
}
if( mysql_options( m_db, MYSQL_READ_DEFAULT_GROUP, "amarokclient" ) )
std::cout << "Error setting options for READ_DEFAULT_GROUP" << std::endl;
if( mysql_options( m_db, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL ) )
std::cout << "Error setting option to use embedded connection" << std::endl;
if( !mysql_real_connect( m_db, NULL,NULL,NULL, "amarok", 0,NULL, 0 ) )
{
std::cout << "Could not connect to mysql!" << std::endl;
std::cout << "Error: " << mysql_error(m_db) << std::endl;
mysql_close( m_db );
m_db = 0;
return false;
}
if (m_isVerbose)
{
std::cout << "Connected to MySQLe server" << mysql_get_server_info( m_db ) << std::endl;
}
return true;
}
bool AmarokCollection::getRating(QString iUrl, bool &oUrlPresent, int &oRating)
{
MYSQL_RES *result;
MYSQL_ROW row;
oUrlPresent = false;
oRating = 0;
std::string utf8Url(iUrl.toLocal8Bit());
char escapedUrl[utf8Url.length() *2 +1];
mysql_real_escape_string(m_db, escapedUrl, utf8Url.c_str(), utf8Url.length());
std::string query("SELECT s.rating FROM devices d, urls u LEFT OUTER JOIN statistics s ON s.url=u.id WHERE u.deviceid=d.id AND CONCAT(TRIM(TRAILING '/' FROM d.lastmountpoint), SUBSTRING(u.rpath, 2))='" + std::string(escapedUrl) + "'");
if (mysql_query(m_db, query.c_str()) != 0)
{
std::cout << "Error in Mysqle query to retrieve rating from url" << std::endl;
std::cout << "Error: " << mysql_error(m_db) << std::endl;
return false;
}
if (!(result = mysql_store_result(m_db)))
{
std::cout << "Error in storing results of Mysqle query to retrieve rating from url" << std::endl;
std::cout << "Error: " << mysql_error(m_db) << std::endl;
return false;
}
if ((row = mysql_fetch_row(result)) != 0)
{
oUrlPresent = true;
if (row[0] != NULL)
{
QString s_rating(row[0]);
oRating = s_rating.toInt();
}
// else, no rating for this URL
}
// else, no URL
mysql_free_result(result);
return true;
}
bool AmarokCollection::getAllRating(QString iUrl, QMap<QString, int> &oRatings)
{
MYSQL_RES *result;
MYSQL_ROW row;
std::string utf8Url(iUrl.toLocal8Bit());
char escapedUrl[utf8Url.length() *2 +1];
mysql_real_escape_string(m_db, escapedUrl, utf8Url.c_str(), utf8Url.length());
std::string query("SELECT CONCAT(TRIM(TRAILING '/' FROM d.lastmountpoint), SUBSTRING(u.rpath, 2)), rating FROM statistics s, urls u, devices d WHERE s.url=u.id AND u.deviceid=d.id AND CONCAT(TRIM(TRAILING '/' FROM d.lastmountpoint), SUBSTRING(u.rpath, 2)) LIKE '" + std::string(escapedUrl) + "%'");
if (mysql_query(m_db, query.c_str()) != 0)
{
std::cout << "Error in Mysqle query to retrieve rating from url" << std::endl;
std::cout << "Error: " << mysql_error(m_db) << std::endl;
return false;
}
if (!(result = mysql_store_result(m_db)))
{
std::cout << "Error in storing results of Mysqle query to retrieve rating from url" << std::endl;
std::cout << "Error: " << mysql_error(m_db) << std::endl;
return false;
}
while ((row = mysql_fetch_row(result)) != 0)
{
int rating = QString(row[1]).toInt();
if (rating > 0)
{
oRatings[QString::fromLocal8Bit(row[0])] = rating;
}
}
// else, no rating for this URL
mysql_free_result(result);
return true;
}
// Prerequisite to call this method: iUrl is present in Amarok collection
// (please test with getRating before)
bool AmarokCollection::setRating(QString iUrl, int iRating)
{
MYSQL_RES *result;
MYSQL_ROW row;
std::string utf8Url(iUrl.toLocal8Bit());
char escapedUrl[utf8Url.length() *2 +1];
mysql_real_escape_string(m_db, escapedUrl, utf8Url.c_str(), utf8Url.length());
std::string query("SELECT u.id, s.id from devices d, urls u LEFT OUTER JOIN statistics s ON s.url=u.id WHERE u.deviceid=d.id AND CONCAT(TRIM(TRAILING '/' FROM d.lastmountpoint), SUBSTRING(u.rpath, 2))='" + std::string(escapedUrl) + "'");
if (mysql_query(m_db, query.c_str()) != 0)
{
std::cout << "Error in Mysqle query to retrieve rating from url" << std::endl;
std::cout << "Error: " << mysql_error(m_db) << std::endl;
return false;
}
if (!(result = mysql_store_result(m_db)))
{
std::cout << "Error in storing results of Mysqle query to retrieve rating from url" << std::endl;
std::cout << "Error: " << mysql_error(m_db) << std::endl;
return false;
}
if ((row = mysql_fetch_row(result)) == 0)
{
std::cout << "Error: method setRating should be called only if Url is present in Amarok collection" << std::endl;
return false;
}
else
{
if (row[1] == NULL)
{
//---------------------------------------
// There is no statistic row in database
QString queryInsert("INSERT INTO statistics(url, rating) VALUES('%1', '%2')");
queryInsert = queryInsert.arg(QString(row[0]), QString::number(iRating));
//std::cout << queryInsert.toStdString() << std::endl;
if (mysql_query(m_db, queryInsert.toStdString().c_str()) != 0)
{
std::cout << "Error in Mysqle query to insert rating" << std::endl;
std::cout << "Error: " << mysql_error(m_db) << std::endl;
return false;
}
}
else
{
//----------------------------------------------
// There is already a statistic row in database
QString queryUpdate("UPDATE statistics SET rating='%1' WHERE url='%2'");
queryUpdate = queryUpdate.arg(QString::number(iRating), QString(row[0]));
//std::cout << queryUpdate.toStdString() << std::endl;
if (mysql_query(m_db, queryUpdate.toStdString().c_str()) != 0)
{
std::cout << "Error in Mysqle query to update rating" << std::endl;
std::cout << "Error: " << mysql_error(m_db) << std::endl;
return false;
}
}
}
return true;
}
bool AmarokCollection::query(QString iQuery, QList<QString> &oResult)
{
MYSQL_RES *result;
MYSQL_FIELD *fields;
MYSQL_ROW row;
unsigned int num_fields = 0;
if (mysql_query(m_db, iQuery.toLocal8Bit()) != 0)
{
std::cout << "Error in Mysqle query" << std::endl;
std::cout << "Error: " << mysql_error(m_db) << std::endl;
return false;
}
if (!(result = mysql_store_result(m_db)))
{
std::cout << "Error in storing results of Mysqle query" << std::endl;
std::cout << "Error: " << mysql_error(m_db) << std::endl;
return false;
}
num_fields = mysql_num_fields(result);
fields = mysql_fetch_fields(result);
QString headers;
for (unsigned int i=0; i<num_fields; i++)
{
if (i>0)
headers += ",";
headers += fields[i].name;
}
oResult.push_back(headers);
while ((row = mysql_fetch_row(result)) != 0)
{
QString row_s;
for (unsigned int i=0; i<num_fields; i++)
{
if (i>0)
row_s += ",";
row_s += row[i];
}
oResult.push_back(row_s);
}
mysql_free_result(result);
return true;
}