#include "database.h"
#include "constants.h"
-#include <QDesktopServices>
+#include <QtDebug>
static const int DATABASE_VERSION = 1;
static const QString dbName = QLatin1String(Constants::UNIX_NAME) + ".db";
static Database *databaseInstance = 0;
Database::Database() {
+ QString dataLocation = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
- QString dataLocation = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
- QDir().mkpath(dataLocation);
+ if (!QDir().mkpath(dataLocation)) {
+ qCritical() << "Failed to create directory " << dataLocation;
+ }
dbLocation = dataLocation + "/" + dbName;
+ qDebug() << "dbLocation" << dbLocation;
QMutexLocker locker(&lock);
- if(QFile::exists(dbLocation)) {
+ if (QFile::exists(dbLocation)) {
// check db version
int databaseVersion = getAttribute("version").toInt();
- if (databaseVersion != DATABASE_VERSION)
+ if (databaseVersion > DATABASE_VERSION)
qWarning("Wrong database version: %d", databaseVersion);
+
+ if (!getAttribute("channelIdFix").toBool())
+ fixChannelIds();
+
} else createDatabase();
}
}
void Database::createDatabase() {
+ qDebug() << __PRETTY_FUNCTION__;
+
+#ifdef APP_LINUX
+ // Qt5 changed its "data" path. Try to move the old db to the new path
+ QString homeLocation = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
+ QString qt4DataLocation = homeLocation + "/.local/share/data/" + Constants::ORG_NAME + "/" + Constants::NAME;
+ QString oldDbLocation = qt4DataLocation + "/" + dbName;
+ qDebug() << oldDbLocation;
+ if (QFile::exists(oldDbLocation)) {
+ if (QFile::copy(oldDbLocation, dbLocation)) {
+ qDebug() << "Moved db from" << oldDbLocation << "to" << dbLocation;
+ return;
+ }
+ }
+#endif
qWarning() << "Creating the database";
QSqlQuery("create table subscriptions ("
"id integer primary key autoincrement,"
- "user_id varchar,"
- "user_name varchar,"
- "name varchar,"
+ "user_id varchar," // this is really channel_id
+ "user_name varchar," // obsolete yt2 username
+ "name varchar," // this is really channel_title
"description varchar,"
"thumb_url varchar,"
"country varchar,"
QSqlQuery("create table subscriptions_videos ("
"id integer primary key autoincrement,"
"video_id varchar,"
- "channel_id integer,"
+ "channel_id integer," // this is really subscription_id
"published integer,"
"added integer,"
"watched integer,"
"title varchar,"
- "author varchar,"
- "user_id varchar,"
+ "author varchar," // this is really channel_title
+ "user_id varchar," // this is really channel_id
"description varchar,"
"url varchar,"
"thumb_url varchar,"
+ QString::number(DATABASE_VERSION) + ")", db);
}
+// static
QString Database::getDbLocation() {
- static QString dataLocation = QDesktopServices::storageLocation(
- QDesktopServices::DataLocation);
- return dataLocation + "/" + dbName;
+ return QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/" + dbName;
}
+// static
bool Database::exists() {
static bool fileExists = false;
- if (!fileExists)
+ if (!fileExists) {
fileExists = QFile::exists(getDbLocation());
+#ifdef APP_LINUX
+ if (!fileExists) {
+ QString homeLocation = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
+ QString qt4DataLocation = homeLocation + "/.local/share/data/" + Constants::ORG_NAME + "/" + Constants::NAME;
+ QString oldDbLocation = qt4DataLocation + "/" + dbName;
+ fileExists = QFile::exists(oldDbLocation);
+ }
+#endif
+ }
return fileExists;
}
+// static
Database& Database::instance() {
static QMutex mutex;
QMutexLocker locker(&mutex);
}
}
-QVariant Database::getAttribute(QString name) {
+QVariant Database::getAttribute(const QString &name) {
QSqlQuery query("select value from attributes where name=?", getConnection());
query.bindValue(0, name);
return QVariant();
}
-void Database::setAttribute(QString name, QVariant value) {
+void Database::setAttribute(const QString &name, const QVariant &value) {
QSqlQuery query(getConnection());
- query.prepare("update attributes set value=? where name=?");
- query.bindValue(0, value);
- query.bindValue(1, name);
+ query.prepare("insert or replace into attributes (name, value) values (?,?)");
+ query.bindValue(0, name);
+ query.bindValue(1, value);
bool success = query.exec();
- if (!success) qDebug() << query.lastError().text();
+ if (!success) qWarning() << query.lastError().text();
+}
+
+void Database::fixChannelIds() {
+ if (!getConnection().transaction())
+ qWarning() << "Transaction failed" << __PRETTY_FUNCTION__;
+
+ qWarning() << "Fixing channel ids";
+
+ QSqlQuery query(getConnection());
+ bool success = query.exec("update subscriptions set user_id='UC' || user_id where user_id not like 'UC%'");
+ if (!success) qWarning() << query.lastError().text();
+
+ query = QSqlQuery(getConnection());
+ success = query.exec("update subscriptions_videos set user_id='UC' || user_id where user_id not like 'UC%'");
+ if (!success) qWarning() << query.lastError().text();
+
+ setAttribute("channelIdFix", 1);
+
+ if (!getConnection().commit())
+ qWarning() << "Commit failed" << __PRETTY_FUNCTION__;
}
/**
while (query.next()) {
QString tableName = query.value(0).toString();
- if (tableName.startsWith("sqlite_") || tableName == "attributes") continue;
+ if (tableName.startsWith("sqlite_") || tableName == QLatin1String("attributes")) continue;
QString dropSQL = "delete from " + tableName;
QSqlQuery query2(db);
if (!query2.exec(dropSQL))
}
void Database::closeConnections() {
- foreach(QSqlDatabase connection, connections.values()) {
+ foreach(QSqlDatabase connection, connections) {
// qDebug() << "Closing connection" << connection;
connection.close();
}