Commit 4ec3e958 authored by Lukas Appelhans's avatar Lukas Appelhans

Abstract insertDelta

parent daef1c3a
/* This file is part of the Chakra project
Copyright (C) 2010 Dario Freddi <drf@chakra-project.org>
Copyright (C) 2011 Lukas Appelhans <l.appelhans@gmx.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
......@@ -32,6 +33,7 @@
#include "md5.h"
#include "akabeihelpers_c.h"
#include "akabeihook.h"
#include "akabeidelta.h"
namespace Akabei
{
......@@ -1281,6 +1283,89 @@ int QueryPerformer::removeScriptlet(sqlite3* db, Package* p)
return SQLITE_OK;
}
int QueryPerformer::insertDelta(Database* db, Delta* delta, int packageId)
{
return insertDelta(QueryHelper::dbFromAkabeiDatabase(db), delta, packageId);
}
int QueryPerformer::insertDelta(sqlite3* db, Delta* delta, int packageId)
{
QString sqlQuery = "INSERT INTO deltas ('Package', 'Filename', 'VersionFrom', 'VersionTo', 'MD5SUM') "
"VALUES (:Package, :Filename, :VersionFrom, :VersionTo, :MD5SUM)";
// Now prepare
sqlite3_stmt *stmt = 0;
int result = sqlite3_prepare16_v2(db, sqlQuery.constData(), (sqlQuery.size() + 1) * sizeof(QChar),
&stmt, 0);
switch (result) {
case SQLITE_OK:
// Everything went fine and the handle is ready
break;
default:
// Errors. Take action here
return result;
break;
}
// Now go for all the bindings
if (!performBind(stmt, ":VersionFrom", delta->versionFrom())) return result;
if (!performBind(stmt, ":VersionTo", delta->versionTo())) return result;
if (!performIntBind(stmt, ":Package", packageId)) return result;
if (!performBind(stmt, ":Filename", delta->filename())) return result;
if (!performBind(stmt, ":MD5SUM", delta->md5sum())) return result;
// Ok, do the query
result = sqlite3_step(stmt);
if (result != SQLITE_DONE) {
// errors...
return result;
}
sqlite3_finalize(stmt);
return SQLITE_OK;
}
int QueryPerformer::removeDeltas(Database* db, Package *p)
{
return removeDeltas(QueryHelper::dbFromAkabeiDatabase(db), p);
}
int QueryPerformer::removeDeltas(sqlite3* db, Package *p)
{
// Query first.
QString sqlQuery =
"DELETE FROM hooks WHERE Package = :Package";
// Now prepare
sqlite3_stmt *stmt = 0;
int result = sqlite3_prepare16_v2(db, sqlQuery.constData(), (sqlQuery.size() + 1) * sizeof(QChar),
&stmt, 0);
switch (result) {
case SQLITE_OK:
// Everything went fine and the handle is ready
break;
default:
// Errors. Take action here
return result;
break;
}
// Perform bindings
if (!performIntBind(stmt, ":Package", p->databaseId())) finalizeAndReturn(stmt);
// Ok, do the query
result = sqlite3_step(stmt);
if (result != SQLITE_DONE) {
// errors...
return result;
}
sqlite3_finalize(stmt);
// Done!
return SQLITE_OK;
}
}
}
......
......@@ -26,6 +26,7 @@ class Package;
class Database;
class Group;
class Hook;
class Delta;
namespace Helpers
{
......@@ -77,8 +78,14 @@ namespace QueryPerformer
int AKABEICORESHARED_EXPORT removeScriptlet(Akabei::Database *db, Package *p);
int AKABEICORESHARED_EXPORT removeScriptlet(sqlite3 *db, Package *p);
int AKABEICORESHARED_EXPORT insertHook(Akabei::Database *db, Hook * hook);
int AKABEICORESHARED_EXPORT insertHook(sqlite3 *db, Hook * hook);
int AKABEICORESHARED_EXPORT insertHook(Akabei::Database *db, Hook *hook);
int AKABEICORESHARED_EXPORT insertHook(sqlite3 *db, Hook *hook);
int AKABEICORESHARED_EXPORT insertDelta(Akabei::Database *db, Delta *delta, int packageId);
int AKABEICORESHARED_EXPORT insertDelta(sqlite3 *db, Delta *delta, int packageId);
int AKABEICORESHARED_EXPORT removeDeltas(Akabei::Database *db, Package *p);
int AKABEICORESHARED_EXPORT removeDeltas(sqlite3 *db, Package *p);
}
class PackageEventLoop : public QEventLoop
......
......@@ -587,41 +587,25 @@ void CreateDb::start()
out.flush();
}
}
// Add deltas!
for (QHash< QString, Akabei::Delta* >::const_iterator i = deltas.constBegin(); i != deltas.constEnd(); ++i) {
QString sqlQuery = "INSERT INTO deltas ('Package', 'Filename', 'VersionFrom', 'VersionTo', 'MD5SUM') "
"VALUES (:Package, :Filename, :VersionFrom, :VersionTo, :MD5SUM)";
// Now prepare
sqlite3_stmt *stmt = 0;
int result = sqlite3_prepare16_v2(db, sqlQuery.constData(), (sqlQuery.size() + 1) * sizeof(QChar),
&stmt, 0);
switch (result) {
case SQLITE_OK:
// Everything went fine and the handle is ready
break;
default:
// Errors. Take action here
continue;
break;
}
// Now go for all the bindings
if (!performBind(stmt, ":VersionFrom", i.value()->versionFrom())) continue;
if (!performBind(stmt, ":VersionTo", i.value()->versionTo())) continue;
if (!performIntBind(stmt, ":Package", dbtargets[i.key()]->databaseId())) continue;
if (!performBind(stmt, ":Filename", i.value()->filename())) continue;
if (!performBind(stmt, ":MD5SUM", i.value()->md5sum())) continue;
// Ok, do the query
result = sqlite3_step(stmt);
if (result != SQLITE_DONE) {
// errors...
continue;
out << endl;
{
out << "Adding deltas to database... (0/" << deltas.size() << ")\r";
out.flush();
int idx = 0;
// Add deltas!
for (QHash< QString, Akabei::Delta* >::const_iterator i = deltas.constBegin(); i != deltas.constEnd(); ++i) {
++idx;
int ret = Akabei::Helpers::QueryPerformer::insertDelta(db, i.value(), dbtargets[i.key()]->databaseId());
if (ret != SQLITE_OK) {
out << "Failed! Could not perform the query. SQLite error code: " << ret << ". Skipping target." << endl;
++skipped;
continue;
}
out << "Adding deltas to database... (" << idx << '/' << deltas.size() << ")\r";
out.flush();
}
sqlite3_finalize(stmt);
}
out << endl;
......
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