Commit c6f64a6c authored by Rinigus Saar's avatar Rinigus Saar
Browse files

use stringliteral in the database code

parent c70beef3
......@@ -50,24 +50,24 @@ void BookmarksHistoryModel::setFilter(const QString &f)
void BookmarksHistoryModel::onDatabaseChanged(const QString &table)
{
if ( (table == "bookmarks" && m_bookmarks) ||
(table == "history" && m_history) )
if ( (table == QLatin1String("bookmarks") && m_bookmarks) ||
(table == QLatin1String("history") && m_history) )
setQuery();
}
void BookmarksHistoryModel::setQuery()
{
QString command;
const char *b = "SELECT rowid AS id, url, title, icon, :now - lastVisited AS lastVisited, %1 AS bookmarked FROM %2 ";
QString filter = m_filter.isEmpty() ? QString() : "WHERE url LIKE '%' || :filter || '%' OR title LIKE '%' || :filter || '%'";
QString b = QStringLiteral("SELECT rowid AS id, url, title, icon, :now - lastVisited AS lastVisited, %1 AS bookmarked FROM %2 ");
QLatin1String filter = m_filter.isEmpty() ? QLatin1String() : QLatin1String("WHERE url LIKE '%' || :filter || '%' OR title LIKE '%' || :filter || '%'");
bool include_history = m_history && !(m_bookmarks && m_filter.isEmpty());
if (m_bookmarks)
command = QString(b).arg(1).arg("bookmarks") + filter;
command = b.arg(1).arg(QLatin1String("bookmarks")) + filter;
if (m_bookmarks && include_history)
command += "\n UNION \n";
command += QLatin1String("\n UNION \n");
if (include_history)
command += QString(b).arg(0).arg("history") + filter;
command += "\n ORDER BY bookmarked, lastVisited ASC";
command += b.arg(0).arg(QLatin1String("history")) + filter;
command += QLatin1String("\n ORDER BY bookmarked, lastVisited ASC");
if (include_history)
command += QStringLiteral("\n LIMIT %1").arg(QUERY_LIMIT);
......@@ -81,8 +81,8 @@ void BookmarksHistoryModel::setQuery()
}
if (!m_filter.isEmpty())
query.bindValue(":filter", m_filter);
query.bindValue(":now", ref);
query.bindValue(QStringLiteral(":filter"), m_filter);
query.bindValue(QStringLiteral(":now"), ref);
if (!query.exec()) {
qWarning() << Q_FUNC_INFO << "Failed to execute SQL statement";
......
......@@ -42,7 +42,7 @@ DBManager::DBManager(QObject *parent) : QObject(parent)
QString dbname = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation)
+ QStringLiteral("/angelfish/angelfish.sqlite");
QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");
QSqlDatabase database = QSqlDatabase::addDatabase(QLatin1String("QSQLITE"));
database.setDatabaseName(dbname);
if (!database.open()) {
throw std::runtime_error("Failed to open database " + dbname.toStdString());
......@@ -58,7 +58,7 @@ DBManager::DBManager(QObject *parent) : QObject(parent)
int DBManager::version()
{
QSqlQuery query("PRAGMA user_version");
QSqlQuery query(QLatin1String("PRAGMA user_version"));
if (query.next()) {
bool ok;
int value = query.value(0).toInt(&ok);
......@@ -117,12 +117,12 @@ bool DBManager::migrate()
bool DBManager::migrateTo1()
{
// Starting from empty database, let's create the tables.
QString bookmarks = "CREATE TABLE bookmarks (url TEXT UNIQUE, title TEXT, icon TEXT, lastVisited INT)";
QString history = "CREATE TABLE history (url TEXT UNIQUE, title TEXT, icon TEXT, lastVisited INT)";
QString icons = "CREATE TABLE icons (url TEXT UNIQUE, icon BLOB)";
QString idx_bookmarks = "CREATE UNIQUE INDEX idx_bookmarks_url ON bookmarks(url)";
QString idx_history = "CREATE UNIQUE INDEX idx_history_url ON history(url)";
QString idx_icons = "CREATE UNIQUE INDEX idx_icons_url ON icons(url)";
QString bookmarks = QStringLiteral("CREATE TABLE bookmarks (url TEXT UNIQUE, title TEXT, icon TEXT, lastVisited INT)");
QString history = QStringLiteral("CREATE TABLE history (url TEXT UNIQUE, title TEXT, icon TEXT, lastVisited INT)");
QString icons = QStringLiteral("CREATE TABLE icons (url TEXT UNIQUE, icon BLOB)");
QString idx_bookmarks = QStringLiteral("CREATE UNIQUE INDEX idx_bookmarks_url ON bookmarks(url)");
QString idx_history = QStringLiteral("CREATE UNIQUE INDEX idx_history_url ON history(url)");
QString idx_icons = QStringLiteral("CREATE UNIQUE INDEX idx_icons_url ON icons(url)");
if (!execute(bookmarks) || !execute(idx_bookmarks) ||
!execute(history) || !execute(idx_history) ||
!execute(icons) || !execute(idx_icons) )
......@@ -157,10 +157,10 @@ void DBManager::addRecord(const QString &table, const QVariantMap &pagedata)
QSqlQuery query;
query.prepare(QStringLiteral("INSERT OR REPLACE INTO %1 (url, title, icon, lastVisited) " \
"VALUES (:url, :title, :icon, :lastVisited)").arg(table));
query.bindValue(":url", url);
query.bindValue(":title", title);
query.bindValue(":icon", icon);
query.bindValue(":lastVisited", lastVisited);
query.bindValue(QStringLiteral(":url"), url);
query.bindValue(QStringLiteral(":title"), title);
query.bindValue(QStringLiteral(":icon"), icon);
query.bindValue(QStringLiteral(":lastVisited"), lastVisited);
execute(query);
emit databaseTableChanged(table);
......@@ -172,7 +172,7 @@ void DBManager::removeRecord(const QString &table, const QString &url)
QSqlQuery query;
query.prepare(QStringLiteral("DELETE FROM %1 WHERE url = :url").arg(table));
query.bindValue(":url", url);
query.bindValue(QStringLiteral(":url"), url);
execute(query);
emit databaseTableChanged(table);
......@@ -184,8 +184,8 @@ void DBManager::updateIconRecord(const QString &table, const QString &url, const
QSqlQuery query;
query.prepare(QStringLiteral("UPDATE %1 SET icon = :icon WHERE url = :url").arg(table));
query.bindValue(":url", url);
query.bindValue(":icon", iconSource);
query.bindValue(QStringLiteral(":url"), url);
query.bindValue(QStringLiteral(":icon"), iconSource);
execute(query);
emit databaseTableChanged(table);
......@@ -198,8 +198,8 @@ void DBManager::lastVisitedRecord(const QString &table, const QString &url)
qint64 lastVisited = QDateTime::currentSecsSinceEpoch();
QSqlQuery query;
query.prepare(QStringLiteral("UPDATE %1 SET lastVisited = :lv WHERE url = :url").arg(table));
query.bindValue(":url", url);
query.bindValue(":lv", lastVisited);
query.bindValue(QStringLiteral(":url"), url);
query.bindValue(QStringLiteral(":lv"), lastVisited);
execute(query);
emit databaseTableChanged(table);
......@@ -207,33 +207,33 @@ void DBManager::lastVisitedRecord(const QString &table, const QString &url)
void DBManager::addBookmark(const QVariantMap &bookmarkdata)
{
addRecord("bookmarks", bookmarkdata);
addRecord(QStringLiteral("bookmarks"), bookmarkdata);
}
void DBManager::removeBookmark(const QString &url)
{
removeRecord("bookmarks", url);
removeRecord(QStringLiteral("bookmarks"), url);
}
void DBManager::addToHistory(const QVariantMap &pagedata)
{
addRecord("history", pagedata);
addRecord(QStringLiteral("history"), pagedata);
}
void DBManager::removeFromHistory(const QString &url)
{
removeRecord("history", url);
removeRecord(QStringLiteral("history"), url);
}
void DBManager::lastVisited(const QString &url)
{
lastVisitedRecord("bookmarks", url);
lastVisitedRecord("history", url);
lastVisitedRecord(QStringLiteral("bookmarks"), url);
lastVisitedRecord(QStringLiteral("history"), url);
}
void DBManager::updateIcon(const QString &url, const QString &iconSource, const QImage &image)
{
QString updatedSource = IconImageProvider::storeImage(iconSource, image);
updateIconRecord("bookmarks", url, updatedSource);
updateIconRecord("history", url, updatedSource);
updateIconRecord(QStringLiteral("bookmarks"), url, updatedSource);
updateIconRecord(QStringLiteral("history"), url, updatedSource);
}
......@@ -20,19 +20,19 @@ QString IconImageProvider::providerId()
QString IconImageProvider::storeImage(const QString &iconSource, const QImage &image)
{
QString prefix_favicon = "image://favicon/";
QLatin1String prefix_favicon = QLatin1String("image://favicon/");
if (!iconSource.startsWith(prefix_favicon)) {
// don't know what to do with it, return as it is
return iconSource;
}
// new uri for image
QString url = QStringLiteral("image://%1/%2").arg(providerId()).arg(iconSource.mid(prefix_favicon.length()));
QString url = QStringLiteral("image://%1/%2").arg(providerId()).arg(iconSource.mid(prefix_favicon.size()));
// check if we have that image already
QSqlQuery query_check;
query_check.prepare("SELECT 1 FROM icons WHERE url = :url LIMIT 1");
query_check.bindValue(":url", url);
query_check.prepare(QStringLiteral("SELECT 1 FROM icons WHERE url = :url LIMIT 1"));
query_check.bindValue(QStringLiteral(":url"), url);
if (!query_check.exec()) {
qWarning() << Q_FUNC_INFO << "Failed to execute SQL statement";
qWarning() << query_check.lastQuery();
......@@ -58,9 +58,9 @@ QString IconImageProvider::storeImage(const QString &iconSource, const QImage &i
}
QSqlQuery query_write;
query_write.prepare("INSERT INTO icons(url, icon) VALUES (:url, :icon)");
query_write.bindValue(":url", url);
query_write.bindValue(":icon", data);
query_write.prepare(QStringLiteral("INSERT INTO icons(url, icon) VALUES (:url, :icon)"));
query_write.bindValue(QStringLiteral(":url"), url);
query_write.bindValue(QStringLiteral(":icon"), data);
if (!query_write.exec()) {
qWarning() << Q_FUNC_INFO << "Failed to execute SQL statement";
qWarning() << query_write.lastQuery();
......@@ -74,8 +74,8 @@ QString IconImageProvider::storeImage(const QString &iconSource, const QImage &i
QImage IconImageProvider::requestImage(const QString &id, QSize *size, const QSize &/*requestedSize*/)
{
QSqlQuery query;
query.prepare("SELECT icon FROM icons WHERE url LIKE :url LIMIT 1");
query.bindValue(":url", QStringLiteral("image://%1/%2%").arg(providerId()).arg(id));
query.prepare(QStringLiteral("SELECT icon FROM icons WHERE url LIKE :url LIMIT 1"));
query.bindValue(QStringLiteral(":url"), QStringLiteral("image://%1/%2%").arg(providerId()).arg(id));
if (!query.exec()) {
qWarning() << Q_FUNC_INFO << "Failed to execute SQL statement";
qWarning() << query.lastQuery();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment