Commit 631cc48d authored by Lukas Appelhans's avatar Lukas Appelhans
Browse files

Astyle

parent 07547f24
...@@ -17,12 +17,12 @@ ...@@ -17,12 +17,12 @@
*/ */
SQLiteConnection::SQLiteConnection(QString const& dbName, bool readonly) SQLiteConnection::SQLiteConnection(QString const& dbName, bool readonly)
{ {
int flags(readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE); int flags(readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE);
std::string tmp = dbName.toStdString(); std::string tmp = dbName.toStdString();
const char* name(tmp.c_str()); const char* name(tmp.c_str());
int retvalue(sqlite3_open_v2(name, &dbHandle, flags, NULL)); int retvalue(sqlite3_open_v2(name, &dbHandle, flags, NULL));
checkForErrors(retvalue); checkForErrors(retvalue);
} }
/* /*
...@@ -31,11 +31,11 @@ SQLiteConnection::SQLiteConnection(QString const& dbName, bool readonly) ...@@ -31,11 +31,11 @@ SQLiteConnection::SQLiteConnection(QString const& dbName, bool readonly)
*/ */
SQLiteConnection::~SQLiteConnection() SQLiteConnection::~SQLiteConnection()
{ {
int retvalue(sqlite3_close(dbHandle)); int retvalue(sqlite3_close(dbHandle));
if (retvalue != SQLITE_OK) { if (retvalue != SQLITE_OK) {
qDebug() << "Error during destruction of SQLiteConnection object"; qDebug() << "Error during destruction of SQLiteConnection object";
} }
} }
//FIXME: If a query fails somewhere we should wipe intBindings and stringBindings... else we run into problems with the new one //FIXME: If a query fails somewhere we should wipe intBindings and stringBindings... else we run into problems with the new one
...@@ -43,53 +43,53 @@ SQLiteConnection::~SQLiteConnection() ...@@ -43,53 +43,53 @@ SQLiteConnection::~SQLiteConnection()
* Executes a query * Executes a query
*/ */
SQLiteResource SQLiteConnection::query(QString q) SQLiteResource SQLiteConnection::query(QString q)
{ {
rows.clear(); rows.clear();
/* /*
* Prepares the statement. _v2 assures us a better error checking * Prepares the statement. _v2 assures us a better error checking
* To future developers: don't fall in the temptation of calling bound.toStdString().c_str() to make things shorter. * To future developers: don't fall in the temptation of calling bound.toStdString().c_str() to make things shorter.
* If you do that, then good luck finding out why akabei is suddenly full of syntax errors :D * If you do that, then good luck finding out why akabei is suddenly full of syntax errors :D
*/ */
std::string tmp = q.toStdString(); std::string tmp = q.toStdString();
const char* query(tmp.c_str()); const char* query(tmp.c_str());
int retvalue = sqlite3_prepare_v2(dbHandle, query, -1, &statement, NULL); int retvalue = sqlite3_prepare_v2(dbHandle, query, -1, &statement, NULL);
checkForErrors(retvalue); checkForErrors(retvalue);
if (!intBindings.isEmpty()) { if (!intBindings.isEmpty()) {
QMap<QString, int>::iterator it = intBindings.begin(); QMap<QString, int>::iterator it = intBindings.begin();
QMap<QString, int>::iterator end = intBindings.end(); QMap<QString, int>::iterator end = intBindings.end();
for ( ; it != end; it++) { for (; it != end; it++) {
std::string key = it.key().toStdString(); std::string key = it.key().toStdString();
int ret = sqlite3_bind_int64(statement, sqlite3_bind_parameter_index(statement, key.c_str()), it.value()); int ret = sqlite3_bind_int64(statement, sqlite3_bind_parameter_index(statement, key.c_str()), it.value());
checkForErrors(ret); checkForErrors(ret);
}
intBindings.clear();
} }
intBindings.clear();
} if (!stringBindings.isEmpty()) {
QMap<QString, QString>::iterator it = stringBindings.begin();
if (!stringBindings.isEmpty()) { QMap<QString, QString>::iterator end = stringBindings.end();
QMap<QString, QString>::iterator it = stringBindings.begin(); for (; it != end; it++) {
QMap<QString, QString>::iterator end = stringBindings.end(); std::string key = it.key().toStdString();
for ( ; it != end; it++) { int ret = sqlite3_bind_text16(statement, sqlite3_bind_parameter_index(statement, key.c_str()), it.value().utf16(),
std::string key = it.key().toStdString(); (it.value().size() + 1) * sizeof(QChar), SQLITE_TRANSIENT);
int ret = sqlite3_bind_text16(statement, sqlite3_bind_parameter_index(statement, key.c_str()), it.value().utf16(), checkForErrors(ret);
(it.value().size() + 1) * sizeof(QChar), SQLITE_TRANSIENT); }
checkForErrors(ret); stringBindings.clear();
} }
stringBindings.clear();
}
while ((retvalue = sqlite3_step(statement)) == SQLITE_ROW) { while ((retvalue = sqlite3_step(statement)) == SQLITE_ROW) {
rows.append(getRowFromStatement(statement)); rows.append(getRowFromStatement(statement));
} }
if (retvalue != SQLITE_DONE && retvalue != SQLITE_OK) { if (retvalue != SQLITE_DONE && retvalue != SQLITE_OK) {
throw SQLiteException(sqlite3_errmsg(dbHandle)); throw SQLiteException(sqlite3_errmsg(dbHandle));
} }
SQLiteResource table(rows); SQLiteResource table(rows);
checkForErrors(sqlite3_finalize(statement)); checkForErrors(sqlite3_finalize(statement));
return table; return table;
} }
SQLiteConnection::Row SQLiteConnection::getRowFromStatement(sqlite3_stmt* statement) SQLiteConnection::Row SQLiteConnection::getRowFromStatement(sqlite3_stmt* statement)
...@@ -106,7 +106,7 @@ SQLiteConnection::Row SQLiteConnection::getRowFromStatement(sqlite3_stmt* statem ...@@ -106,7 +106,7 @@ SQLiteConnection::Row SQLiteConnection::getRowFromStatement(sqlite3_stmt* statem
switch (sqlite3_column_type(statement, i)) { switch (sqlite3_column_type(statement, i)) {
case SQLITE_TEXT: case SQLITE_TEXT:
ptr = sqlite3_column_text(statement, i); ptr = sqlite3_column_text(statement, i);
record.setValue(QString::fromAscii(reinterpret_cast<const char *>( ptr ))); record.setValue(QString::fromAscii(reinterpret_cast<const char *>(ptr)));
break; break;
case SQLITE_INTEGER: case SQLITE_INTEGER:
...@@ -117,14 +117,14 @@ SQLiteConnection::Row SQLiteConnection::getRowFromStatement(sqlite3_stmt* statem ...@@ -117,14 +117,14 @@ SQLiteConnection::Row SQLiteConnection::getRowFromStatement(sqlite3_stmt* statem
record.setValue(sqlite3_column_double(statement, i)); record.setValue(sqlite3_column_double(statement, i));
break; break;
/* I don't use _column_blob because _text adds a NULL terminator at the end */ /* I don't use _column_blob because _text adds a NULL terminator at the end */
case SQLITE_BLOB: case SQLITE_BLOB:
ptr = sqlite3_column_text(statement, i); ptr = sqlite3_column_text(statement, i);
bytes = sqlite3_column_bytes(statement, i) + 1; bytes = sqlite3_column_bytes(statement, i) + 1;
record.setValue(QByteArray::fromRawData(reinterpret_cast<const char *>( ptr ), bytes)); record.setValue(QByteArray::fromRawData(reinterpret_cast<const char *>(ptr), bytes));
break; break;
/* SQLITE_NULL */ /* SQLITE_NULL */
default: default:
// FIXME: do we really need to throw an exception? // FIXME: do we really need to throw an exception?
// NULL values are acceptable in a database record. // NULL values are acceptable in a database record.
...@@ -143,8 +143,8 @@ SQLiteConnection::Row SQLiteConnection::getRowFromStatement(sqlite3_stmt* statem ...@@ -143,8 +143,8 @@ SQLiteConnection::Row SQLiteConnection::getRowFromStatement(sqlite3_stmt* statem
*/ */
qint64 SQLiteConnection::getLastRowId() qint64 SQLiteConnection::getLastRowId()
{ {
sqlite3_int64 id = sqlite3_last_insert_rowid(dbHandle); sqlite3_int64 id = sqlite3_last_insert_rowid(dbHandle);
return static_cast<qint64>( id ); return static_cast<qint64>(id);
} }
/* /*
...@@ -152,41 +152,38 @@ qint64 SQLiteConnection::getLastRowId() ...@@ -152,41 +152,38 @@ qint64 SQLiteConnection::getLastRowId()
*/ */
void SQLiteConnection::bind(const QString& name, int value) void SQLiteConnection::bind(const QString& name, int value)
{ {
if (intBindings.contains(name) || stringBindings.contains(name)) if (intBindings.contains(name) || stringBindings.contains(name)) {
{ return;
return; }
}
intBindings.insert(name, value); intBindings.insert(name, value);
} }
void SQLiteConnection::bind(const QString& name, QString value) void SQLiteConnection::bind(const QString& name, QString value)
{ {
if (intBindings.contains(name) || stringBindings.contains(name)) if (intBindings.contains(name) || stringBindings.contains(name)) {
{ return;
return; }
}
stringBindings.insert(name, value); stringBindings.insert(name, value);
} }
void SQLiteConnection::bind(const QString& name, QByteArray value) void SQLiteConnection::bind(const QString& name, QByteArray value)
{ {
if (intBindings.contains(name) || stringBindings.contains(name)) if (intBindings.contains(name) || stringBindings.contains(name)) {
{ return;
return; }
}
stringBindings.insert(name, value); stringBindings.insert(name, value);
} }
void SQLiteConnection::checkForErrors(int r) void SQLiteConnection::checkForErrors(int r)
{ {
if (r != SQLITE_OK) { if (r != SQLITE_OK) {
const char *mess = sqlite3_errmsg(dbHandle); const char *mess = sqlite3_errmsg(dbHandle);
sqlite3_finalize(statement); sqlite3_finalize(statement);
throw SQLiteException(mess); throw SQLiteException(mess);
} }
} }
...@@ -195,35 +192,32 @@ SQLiteResource::SQLiteResource(QList<Row> t) : table(t) {} ...@@ -195,35 +192,32 @@ SQLiteResource::SQLiteResource(QList<Row> t) : table(t) {}
QList<QVariant> SQLiteResource::getColumn(const QString& columnName) QList<QVariant> SQLiteResource::getColumn(const QString& columnName)
{ {
QList<QVariant> data; QList<QVariant> data;
for (QList<QVariantMap>::iterator it = table.begin(); it < table.end(); it++) for (QList<QVariantMap>::iterator it = table.begin(); it < table.end(); it++) {
{ QVariantMap map = (*it);
QVariantMap map = (*it);
if (map.count(columnName) == 0) if (map.count(columnName) == 0) {
{ throw SQLiteException("Unexistent column in table");
throw SQLiteException("Unexistent column in table"); }
}
data.append(map[columnName]); data.append(map[columnName]);
} }
return data; return data;
} }
QVariant SQLiteResource::getDataAt(int rowNumber, const QString& columnName) QVariant SQLiteResource::getDataAt(int rowNumber, const QString& columnName)
{ {
if (table.isEmpty() || rowNumber >= table.size() || table[rowNumber].count(columnName) == 0) if (table.isEmpty() || rowNumber >= table.size() || table[rowNumber].count(columnName) == 0) {
{ throw SQLiteException("Access to non-existent data");
throw SQLiteException("Access to non-existent data"); }
}
QVariantMap map = table[rowNumber]; QVariantMap map = table[rowNumber];
return map[columnName]; return map[columnName];
} }
int SQLiteResource::getRowsCount() int SQLiteResource::getRowsCount()
{ {
return table.size(); return table.size();
} }
...@@ -35,14 +35,14 @@ class SQLiteResource ...@@ -35,14 +35,14 @@ class SQLiteResource
{ {
typedef QVariant Value; typedef QVariant Value;
typedef QVariantMap Row; typedef QVariantMap Row;
public:
QList<Row> table;
public:
SQLiteResource(QList<Row>); SQLiteResource(QList<Row>);
QVariant getDataAt(int, QString const&); QVariant getDataAt(int, QString const&);
QList<QVariant> getColumn(QString const&); QList<QVariant> getColumn(QString const&);
int getRowsCount(); int getRowsCount();
private:
QList<Row> table;
}; };
/* /*
...@@ -52,21 +52,8 @@ class SQLiteConnection ...@@ -52,21 +52,8 @@ class SQLiteConnection
{ {
typedef QVariant Value; typedef QVariant Value;
typedef QVariantMap Row; typedef QVariantMap Row;
sqlite3 *dbHandle;
sqlite3_stmt *statement;
QList<Row> rows;
QMap<QString, int> intBindings;
QMap<QString, QString> stringBindings;
void checkForErrors(int); public:
Row getRowFromStatement(sqlite3_stmt*);
// Disable copy constructor and default constructor
SQLiteConnection();
SQLiteConnection(SQLiteConnection const&);
public:
SQLiteConnection(QString const&, bool); SQLiteConnection(QString const&, bool);
~SQLiteConnection(); ~SQLiteConnection();
...@@ -76,20 +63,32 @@ class SQLiteConnection ...@@ -76,20 +63,32 @@ class SQLiteConnection
void bind(QString const&, int); void bind(QString const&, int);
void bind(QString const&, QString); void bind(QString const&, QString);
void bind(QString const&, QByteArray); void bind(QString const&, QByteArray);
};
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 SQLiteException : public std::exception class SQLiteException : public std::exception
{ {
private: public:
const char *message;
public:
SQLiteException(const char *m) : message(m) {} SQLiteException(const char *m) : message(m) {}
virtual const char *what() const throw() virtual const char *what() const throw() {
{ return message;
return message; }
}
private:
const char *message;
}; };
#endif #endif
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