Commit 8289fbd5 authored by Daniele Cocca's avatar Daniele Cocca
Browse files

Fixed the CMakeLists.txt for the sqlite3++ wrapper. Added some "FIXME" tags...

Fixed the CMakeLists.txt for the sqlite3++ wrapper. Added some "FIXME" tags here and there. Code is still not compiling.
parent 75289eed
add_subdirectory(benchmarks)
add_subdirectory(sqlite3++)
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
)
${AKABEI_SQLITE3_INCLUDE})
set(AKABEI_CORE_SRCS
akabeibackend.cpp
......@@ -23,7 +25,11 @@ md5.c
)
set(AKABEI_OPERATION_SRCS
<<<<<<< HEAD
operations/akabeiplainhookoperation.cpp
=======
#operations/akabeiplainhookoperation.cpp
>>>>>>> Fixed the CMakeLists.txt for the sqlite3++ wrapper. Added some "FIXME" tags here and there. Code is still not compiling.
operations/akabeiplainreinstalloperation.cpp
operations/akabeiplaininstalloperation.cpp
operations/akabeiplainremoveoperation.cpp
......
......@@ -21,16 +21,20 @@ namespace Akabei {
DatabasePrivate::DatabasePrivate(Database *db, const QString &pathToDatabase)
: q_ptr(db)
, pathToDb(pathToDatabase)
, dbConnection(pathToDatabase, false)
, valid(false)
, mutex(new QMutex())
{
}
DatabasePrivate::~DatabasePrivate() {}
// FIXME: what do we need this method for?
void DatabasePrivate::init()
{
try {
dbConnection(pathToDb, false);
// FIXME: this is not correct. :)
// dbConnection(pathToDb, false);
valid = true;
populateGroupCache(); /* populate group cache on startup */
}
......@@ -48,7 +52,7 @@ void DatabasePrivate::populateGroupCache()
groupCache.clear();
dbConnection.query("SELECT * FROM GROUPS");
for (int i = 0; i < dbConnection.getRowsNumber(); i++)
for (int i = 0; i < dbConnection.getRowsCount(); i++)
{
Group *group = groupFromRow(i);
groupCache[group->name()] = group;
......@@ -191,7 +195,7 @@ Package *DatabasePrivate::packageFromRow(int row)
.arg(id);
dbConnection.query(sql);
if (dbConnection.getRowsNumber() > 0)
if (dbConnection.getRowsCount() > 0)
{
package->d_func()->hasScriptlet = true;
}
......@@ -202,7 +206,7 @@ Package *DatabasePrivate::packageFromRow(int row)
.arg(id);
dbConnection.query(sql);
if (dbConnection.getRowsNumber() > 0)
if (dbConnection.getRowsCount() > 0)
{
// There is one
package->d_func()->hasHooks = true;
......@@ -324,7 +328,7 @@ QList<Package*> Database::queryPackages(const QString &sql)
try
{
d->dbConnection.query(sql);
for (int i = 0; i < d->dbConnection.getRowsNumber(); i++)
for (int i = 0; i < d->dbConnection.getRowsCount(); i++)
{
retlist << d->packageFromRow(i);
}
......@@ -361,7 +365,7 @@ QList< Group* > Database::queryGroups(const QString& sql)
try
{
d->dbConnection.query(sql);
for (int i = 0; i < d->dbConnection.getRowsNumber(); i++)
for (int i = 0; i < d->dbConnection.getRowsCount(); i++)
{
retlist << d->groupFromRow(i);
}
......@@ -384,7 +388,7 @@ QList<Delta*> Database::queryDeltas(const QString &sql)
try
{
d->dbConnection.query(sql);
for (int i = 0; i < d->dbConnection.getRowsNumber(); i++)
for (int i = 0; i < d->dbConnection.getRowsCount(); i++)
{
retlist << d->deltaFromRow(i);
}
......
......@@ -41,7 +41,7 @@ public:
QString pathToDb;
SQLiteConnection dbConnection;
bool valid = false;
bool valid;
QHash<int, Package*> packageCache;
QHash<QString, Group*> groupCache;
......
......@@ -14,10 +14,7 @@
#include "akabeihelpers.h"
#include <archive.h>
#include "SQLiteConnection.h"
struct sqlite3_stmt;
#include <SQLiteConnection.h>
namespace Akabei
{
......
......@@ -2,17 +2,15 @@ cmake_minimum_required(VERSION 2.6)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${KDE4_ENABLE_EXCEPTIONS}")
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
set(AKABEI_SQLITE3_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR} PARENT_SCOPE)
set(AKABEICORE_SQLITE3_SRCS
set(AKABEI_SQLITE3_SRCS
SQLiteConnection.cpp
)
qt4_wrap_cpp(AKABEICORE_SQLITE3_MOC_SRCS
qt4_wrap_cpp(AKABEI_SQLITE3_MOC_SRCS
databasebenchmark.h)
add_executable(sqlite3++ ${AKABEICORE_SQLITE3_SRCS} ${AKABEICORE_SQLITE3_MOC_SRCS})
add_executable(sqlite3++ ${AKABEI_SQLITE3_SRCS} ${AKABEI_SQLITE3_MOC_SRCS})
target_link_libraries(sqlite3++ akabeicore ${QT_QTTEST_LIBRARY})
\ No newline at end of file
......@@ -19,16 +19,8 @@ using namespace std;
*/
SQLiteConnection::SQLiteConnection(QString const& dbName, bool readonly)
{
int retvalue;
if (readonly)
{
retvalue = sqlite3_open_v2(dbName.toStdString().c_str(), &dbHandle, SQLITE_OPEN_READONLY, NULL);
}
else
{
retvalue = sqlite3_open(dbName.toStdString().c_str(), &dbHandle);
}
int flags(readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE);
int retvalue(sqlite3_open_v2(dbName.constData(), &dbHandle, flags, NULL));
checkForErrors(retvalue);
}
......@@ -38,8 +30,11 @@ SQLiteConnection::SQLiteConnection(QString const& dbName, bool readonly)
*/
SQLiteConnection::~SQLiteConnection()
{
int retvalue = sqlite3_close(dbHandle);
checkForErrors(retvalue);
int retvalue(sqlite3_close(dbHandle));
// FIXME: this might throw an exception, which is mostly asking for
// problems when done in a destructor!
// checkForErrors(retvalue);
}
/*
......@@ -47,88 +42,77 @@ SQLiteConnection::~SQLiteConnection()
*/
void SQLiteConnection::query(QString q)
{
int retvalue;
rows.clear();
QString query = translateBindings(q);
/*
* Prepares the statement. _v2 assures us a better error checking
*/
string tmp = query.toStdString();
retvalue = sqlite3_prepare_v2(dbHandle, tmp.c_str(), -1, &statement, NULL);
int retvalue = sqlite3_prepare_v2(dbHandle, query.constData(), -1, &statement, NULL);
checkForErrors(retvalue);
while ((retvalue = sqlite3_step(statement)) == SQLITE_ROW)
{
QVariantMap row;
for (int i = 0; i < sqlite3_column_count(statement); i++)
{
const char *name = sqlite3_column_name(statement, i);
QString cName(name);
QVariant record;
switch(sqlite3_column_type(statement, i))
{
case SQLITE_TEXT:
{
const unsigned char *text = sqlite3_column_text(statement, i);
QString textValue((const char *)text);
record.setValue(textValue);
break;
}
case SQLITE_INTEGER:
{
int integer = sqlite3_column_int(statement, i);
record.setValue(integer);
break;
}
case SQLITE_FLOAT:
{
double floating = sqlite3_column_double(statement, i);
record.setValue(floating);
break;
}
/* I don't use _column_blob because _text adds a NULL terminator at the end */
case SQLITE_BLOB:
{
const unsigned char *blob = sqlite3_column_text(statement, i);
QByteArray byte = QByteArray::fromRawData((const char *)blob, sqlite3_column_bytes(statement, i) + 1);
record.setValue(byte);
break;
}
/* SQLITE_NULL */
default:
throw SQLiteException("Null data in record");
break;
}
row[cName] = record;
}
rows.append(row);
while ((retvalue = sqlite3_step(statement)) == SQLITE_ROW) {
rows.append(getRowFromStatement(statement));
}
if (retvalue != SQLITE_DONE && retvalue != SQLITE_OK)
{
if (retvalue != SQLITE_DONE && retvalue != SQLITE_OK) {
throw SQLiteException(sqlite3_errmsg(dbHandle));
}
retvalue = sqlite3_finalize(statement); /* destroys the statement */
checkForErrors(retvalue);
checkForErrors(sqlite3_finalize(statement));
bindings.clear();
}
Row SQLiteConnection::getRowFromStatement(sqlite3_stmt* statement)
{
Row row;
for (int i = 0; i < sqlite3_column_count(statement); i++) {
QString name(sqlite3_column_name(statement, i));
QVariant record;
switch (sqlite3_column_type(statement, i)) {
case SQLITE_TEXT:
const unsigned char* text = sqlite3_column_text(statement, i);
record.setValue(QString::fromAscii(static_cast<const char *>( text )));
break;
case SQLITE_INTEGER:
record.setValue(sqlite3_column_int(statement, i));
break;
case SQLITE_FLOAT:
record.setValue(sqlite3_column_double(statement, i));
break;
/* I don't use _column_blob because _text adds a NULL terminator at the end */
case SQLITE_BLOB:
const unsigned char* blob = sqlite3_column_text(statement, i);
int bytes(sqlite3_column_bytes(statement, i) + 1);
record.setValue(QByteArray::fromRawData(static_cast<const char *>( blob ), bytes));
break;
/* SQLITE_NULL */
default:
// FIXME: do we really need to throw an exception?
// NULL values are acceptable in a database record.
// throw SQLiteException("NULL data in record");
break;
}
row[name] = record;
}
return row;
}
QVariant SQLiteConnection::getDataAt(int rowNumber, QString const& columnName)
{
if (rows.isEmpty() || rowNumber > rows.length() || rows[rowNumber].count(columnName) == 0)
{
throw SQLiteException("Access to non-existent data");
}
QVariantMap map = rows[rowNumber];
return map[columnName];
}
......@@ -136,19 +120,19 @@ QVariant SQLiteConnection::getDataAt(int rowNumber, QString const& columnName)
QList<QVariant> SQLiteConnection::getColumn(const QString& columnName)
{
QList<QVariant> data;
for (QList<QVariantMap>::iterator it = rows.begin(); it < rows.end(); it++)
{
{
QVariantMap map = (*it);
if (map.count(columnName) == 0)
{
throw SQLiteException("Unexistent column in table");
}
data.append(map[columnName]);
}
return data;
}
......@@ -158,7 +142,7 @@ void SQLiteConnection::bind(const QString& name, int value)
{
throw SQLiteException("Bind error: key already present");
}
QVariant v(value);
bindings.insert(name, v);
}
......@@ -169,51 +153,55 @@ void SQLiteConnection::bind(const QString& name, QString value)
{
throw SQLiteException("Bind error: key already present");
}
QVariant v("'" + value + "'");
bindings.insert(name, v);
}
void SQLiteConnection::bind (const QString& name, QByteArray value)
void SQLiteConnection::bind(const QString& name, QByteArray value)
{
if (bindings.contains(name))
{
throw SQLiteException("Bind error: key already present");
}
QVariant v("'" + value + "'");
bindings.insert(name, v);
}
int SQLiteConnection::getRowsNumber()
int SQLiteConnection::getRowsCount()
{
return data.size();
}
qint32 SQLiteConnection::getLastRowId()
qint64 SQLiteConnection::getLastRowId()
{
sqlite3_int64 id = sqlite3_last_insert_rowid(dbHandle);
return (qint32)id;
return static_cast<qint64>( id );
}
sqlite3_stmt* SQLiteConnection::getRowAsStatement(int i)
{
return rows[i];
}
QString SQLiteConnection::translateBindings(QString query)
{
QList<QString> keys = bindings.keys();
for (QList<QString>::iterator it = keys.begin(); it < keys.end(); it++)
{
QString key = (*it);
query.replace(key, bindings[key]);
}
return query;
}
void SQLiteConnection::checkForErrors(int r)
{
if (r != SQLITE_OK)
{
if (r != SQLITE_OK) {
const char *mess = sqlite3_errmsg(dbHandle);
sqlite3_finalize(statement);
throw SQLiteException(mess);
......
......@@ -33,27 +33,35 @@
*/
class SQLiteConnection
{
private:
typedef QVariant Value;
typedef QVariantMap Row;
sqlite3 *dbHandle;
sqlite3_stmt *statement;
QList<QVariantMap> rows;
QList<Row> rows;
QVariantMap bindings;
bool prepared;
void checkForErrors(int);
QString translateBindings(QString);
// Disable copy constructor and default constructor
SQLiteConnection();
SQLiteConnection(SQLiteConnection const&);
public:
SQLiteConnection(QString const& , bool);
SQLiteConnection(QString const&, bool);
~SQLiteConnection();
void query(QString);
QVariant getDataAt(int, QString const&);
QList<QVariant> getColumn(QString const&);
qint32 getLastRowId();
int getRowsNumber();
qint64 getLastRowId();
int getRowsCount();
Row getRowFromStatement(sqlite3_stmt*);
void bind(QString const&, int);
void bind(QString const&, QString);
void bind(QString const&, QByteArray);
......@@ -63,10 +71,10 @@ class SQLiteException : public std::exception
{
private:
const char *message;
public:
SQLiteException(const char *m) : message(m) {}
virtual const char *what() const throw()
{
return message;
......
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