Commit dc6ae109 authored by Lukas Appelhans's avatar Lukas Appelhans
Browse files

move to d-pointer

parent eaf9fdd5
......@@ -11,18 +11,42 @@
#include <QDebug>
#include "SQLiteConnection.h"
class SQLiteConnection::Private
{
public:
Private(const QString &dbName, bool readonly) : statement(0) {
int flags(readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE);
std::string tmp = dbName.toStdString();
const char* name(tmp.c_str());
int retvalue(sqlite3_open_v2(name, &dbHandle, flags, NULL));
checkForErrors(retvalue);
}
~Private() {
int retvalue(sqlite3_close(dbHandle));
if (retvalue != SQLITE_OK) {
qDebug() << "Error during destruction of SQLiteConnection object";
}
}
void checkForErrors(int);
Row getRowFromStatement(sqlite3_stmt*);
sqlite3 *dbHandle;
sqlite3_stmt *statement;
QList<Row> rows;
QMap<QString, int> intBindings;
QMap<QString, QString> stringBindings;
};
/*
* Constructor
* Establish a connection with dbName
*/
SQLiteConnection::SQLiteConnection(QString const& dbName, bool readonly)
: d(new Private(dbName, readonly))
{
int flags(readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE);
std::string tmp = dbName.toStdString();
const char* name(tmp.c_str());
int retvalue(sqlite3_open_v2(name, &dbHandle, flags, NULL));
checkForErrors(retvalue);
}
/*
......@@ -31,11 +55,7 @@ SQLiteConnection::SQLiteConnection(QString const& dbName, bool readonly)
*/
SQLiteConnection::~SQLiteConnection()
{
int retvalue(sqlite3_close(dbHandle));
if (retvalue != SQLITE_OK) {
qDebug() << "Error during destruction of SQLiteConnection object";
}
delete d;
}
//FIXME: If a query fails somewhere we should wipe intBindings and stringBindings... else we run into problems with the new one
......@@ -44,7 +64,7 @@ SQLiteConnection::~SQLiteConnection()
*/
SQLiteResource SQLiteConnection::query(const QString &q)
{
rows.clear();
d->rows.clear();
/*
* Prepares the statement. _v2 assures us a better error checking
......@@ -53,46 +73,46 @@ SQLiteResource SQLiteConnection::query(const QString &q)
*/
std::string tmp = q.toStdString();
const char* query(tmp.c_str());
int retvalue = sqlite3_prepare_v2(dbHandle, query, -1, &statement, NULL);
checkForErrors(retvalue);
int retvalue = sqlite3_prepare_v2(d->dbHandle, query, -1, &(d->statement), NULL);
d->checkForErrors(retvalue);
if (!intBindings.isEmpty()) {
QMap<QString, int>::iterator it = intBindings.begin();
QMap<QString, int>::iterator end = intBindings.end();
if (!d->intBindings.isEmpty()) {
QMap<QString, int>::iterator it = d->intBindings.begin();
QMap<QString, int>::iterator end = d->intBindings.end();
for (; it != end; it++) {
std::string key = it.key().toStdString();
int ret = sqlite3_bind_int64(statement, sqlite3_bind_parameter_index(statement, key.c_str()), it.value());
checkForErrors(ret);
int ret = sqlite3_bind_int64(d->statement, sqlite3_bind_parameter_index(d->statement, key.c_str()), it.value());
d->checkForErrors(ret);
}
intBindings.clear();
d->intBindings.clear();
}
if (!stringBindings.isEmpty()) {
QMap<QString, QString>::iterator it = stringBindings.begin();
QMap<QString, QString>::iterator end = stringBindings.end();
if (!d->stringBindings.isEmpty()) {
QMap<QString, QString>::iterator it = d->stringBindings.begin();
QMap<QString, QString>::iterator end = d->stringBindings.end();
for (; it != end; it++) {
std::string key = it.key().toStdString();
int ret = sqlite3_bind_text16(statement, sqlite3_bind_parameter_index(statement, key.c_str()), it.value().utf16(),
int ret = sqlite3_bind_text16(d->statement, sqlite3_bind_parameter_index(d->statement, key.c_str()), it.value().utf16(),
(it.value().size() + 1) * sizeof(QChar), SQLITE_TRANSIENT);
checkForErrors(ret);
d->checkForErrors(ret);
}
stringBindings.clear();
d->stringBindings.clear();
}
while ((retvalue = sqlite3_step(statement)) == SQLITE_ROW) {
rows.append(getRowFromStatement(statement));
while ((retvalue = sqlite3_step(d->statement)) == SQLITE_ROW) {
d->rows.append(d->getRowFromStatement(d->statement));
}
if (retvalue != SQLITE_DONE && retvalue != SQLITE_OK) {
throw SQLiteException(sqlite3_errmsg(dbHandle));
throw SQLiteException(sqlite3_errmsg(d->dbHandle));
}
SQLiteResource table(rows);
checkForErrors(sqlite3_finalize(statement));
SQLiteResource table(d->rows);
d->checkForErrors(sqlite3_finalize(d->statement));
return table;
}
SQLiteConnection::Row SQLiteConnection::getRowFromStatement(sqlite3_stmt* statement)
SQLiteConnection::Row SQLiteConnection::Private::getRowFromStatement(sqlite3_stmt* statement)
{
SQLiteConnection::Row row;
......@@ -143,7 +163,7 @@ SQLiteConnection::Row SQLiteConnection::getRowFromStatement(sqlite3_stmt* statem
*/
qint64 SQLiteConnection::getLastRowId()
{
sqlite3_int64 id = sqlite3_last_insert_rowid(dbHandle);
sqlite3_int64 id = sqlite3_last_insert_rowid(d->dbHandle);
return static_cast<qint64>(id);
}
......@@ -152,32 +172,32 @@ qint64 SQLiteConnection::getLastRowId()
*/
void SQLiteConnection::bind(const QString& name, int value)
{
if (intBindings.contains(name) || stringBindings.contains(name)) {
if (d->intBindings.contains(name) || d->stringBindings.contains(name)) {
return;
}
intBindings.insert(name, value);
d->intBindings.insert(name, value);
}
void SQLiteConnection::bind(const QString& name, QString value)
{
if (intBindings.contains(name) || stringBindings.contains(name)) {
if (d->intBindings.contains(name) || d->stringBindings.contains(name)) {
return;
}
stringBindings.insert(name, value);
d->stringBindings.insert(name, value);
}
void SQLiteConnection::bind(const QString& name, QByteArray value)
{
if (intBindings.contains(name) || stringBindings.contains(name)) {
if (d->intBindings.contains(name) || d->stringBindings.contains(name)) {
return;
}
stringBindings.insert(name, value);
d->stringBindings.insert(name, value);
}
void SQLiteConnection::checkForErrors(int r)
void SQLiteConnection::Private::checkForErrors(int r)
{
if (r != SQLITE_OK) {
const char *mess = sqlite3_errmsg(dbHandle);
......
......@@ -68,14 +68,8 @@ private:
// Disable copy constructor and default constructor
SQLiteConnection(SQLiteConnection const&);
void checkForErrors(int);
Row getRowFromStatement(sqlite3_stmt*);
sqlite3 *dbHandle;
sqlite3_stmt *statement;
QList<Row> rows;
QMap<QString, int> intBindings;
QMap<QString, QString> stringBindings;
class Private;
Private *d;
};
class SQLiteException : public std::exception
......
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