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

Astyle

parent 07547f24
......@@ -17,12 +17,12 @@
*/
SQLiteConnection::SQLiteConnection(QString const& dbName, bool 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);
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 +31,11 @@ SQLiteConnection::SQLiteConnection(QString const& dbName, bool readonly)
*/
SQLiteConnection::~SQLiteConnection()
{
int retvalue(sqlite3_close(dbHandle));
int retvalue(sqlite3_close(dbHandle));
if (retvalue != SQLITE_OK) {
qDebug() << "Error during destruction of SQLiteConnection object";
}
if (retvalue != SQLITE_OK) {
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
......@@ -43,53 +43,53 @@ SQLiteConnection::~SQLiteConnection()
* Executes a query
*/
SQLiteResource SQLiteConnection::query(QString q)
{
rows.clear();
/*
* 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.
* If you do that, then good luck finding out why akabei is suddenly full of syntax errors :D
*/
std::string tmp = q.toStdString();
const char* query(tmp.c_str());
int retvalue = sqlite3_prepare_v2(dbHandle, query, -1, &statement, NULL);
checkForErrors(retvalue);
if (!intBindings.isEmpty()) {
QMap<QString, int>::iterator it = intBindings.begin();
QMap<QString, int>::iterator end = 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);
{
rows.clear();
/*
* 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.
* If you do that, then good luck finding out why akabei is suddenly full of syntax errors :D
*/
std::string tmp = q.toStdString();
const char* query(tmp.c_str());
int retvalue = sqlite3_prepare_v2(dbHandle, query, -1, &statement, NULL);
checkForErrors(retvalue);
if (!intBindings.isEmpty()) {
QMap<QString, int>::iterator it = intBindings.begin();
QMap<QString, int>::iterator end = 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);
}
intBindings.clear();
}
intBindings.clear();
}
if (!stringBindings.isEmpty()) {
QMap<QString, QString>::iterator it = stringBindings.begin();
QMap<QString, QString>::iterator end = 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(),
(it.value().size() + 1) * sizeof(QChar), SQLITE_TRANSIENT);
checkForErrors(ret);
if (!stringBindings.isEmpty()) {
QMap<QString, QString>::iterator it = stringBindings.begin();
QMap<QString, QString>::iterator end = 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(),
(it.value().size() + 1) * sizeof(QChar), SQLITE_TRANSIENT);
checkForErrors(ret);
}
stringBindings.clear();
}
stringBindings.clear();
}
while ((retvalue = sqlite3_step(statement)) == SQLITE_ROW) {
rows.append(getRowFromStatement(statement));
}
while ((retvalue = sqlite3_step(statement)) == SQLITE_ROW) {
rows.append(getRowFromStatement(statement));
}
if (retvalue != SQLITE_DONE && retvalue != SQLITE_OK) {
throw SQLiteException(sqlite3_errmsg(dbHandle));
}
if (retvalue != SQLITE_DONE && retvalue != SQLITE_OK) {
throw SQLiteException(sqlite3_errmsg(dbHandle));
}
SQLiteResource table(rows);
checkForErrors(sqlite3_finalize(statement));
return table;
SQLiteResource table(rows);
checkForErrors(sqlite3_finalize(statement));
return table;
}
SQLiteConnection::Row SQLiteConnection::getRowFromStatement(sqlite3_stmt* statement)
......@@ -106,7 +106,7 @@ SQLiteConnection::Row SQLiteConnection::getRowFromStatement(sqlite3_stmt* statem
switch (sqlite3_column_type(statement, i)) {
case SQLITE_TEXT:
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;
case SQLITE_INTEGER:
......@@ -117,14 +117,14 @@ SQLiteConnection::Row SQLiteConnection::getRowFromStatement(sqlite3_stmt* statem
record.setValue(sqlite3_column_double(statement, i));
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:
ptr = sqlite3_column_text(statement, i);
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;
/* SQLITE_NULL */
/* SQLITE_NULL */
default:
// FIXME: do we really need to throw an exception?
// NULL values are acceptable in a database record.
......@@ -143,8 +143,8 @@ SQLiteConnection::Row SQLiteConnection::getRowFromStatement(sqlite3_stmt* statem
*/
qint64 SQLiteConnection::getLastRowId()
{
sqlite3_int64 id = sqlite3_last_insert_rowid(dbHandle);
return static_cast<qint64>( id );
sqlite3_int64 id = sqlite3_last_insert_rowid(dbHandle);
return static_cast<qint64>(id);
}
/*
......@@ -152,41 +152,38 @@ qint64 SQLiteConnection::getLastRowId()
*/
void SQLiteConnection::bind(const QString& name, int value)
{
if (intBindings.contains(name) || stringBindings.contains(name))
{
return;
}
if (intBindings.contains(name) || stringBindings.contains(name)) {
return;
}
intBindings.insert(name, value);
intBindings.insert(name, value);
}
void SQLiteConnection::bind(const QString& name, QString value)
{
if (intBindings.contains(name) || stringBindings.contains(name))
{
return;
}
if (intBindings.contains(name) || stringBindings.contains(name)) {
return;
}
stringBindings.insert(name, value);
stringBindings.insert(name, value);
}
void SQLiteConnection::bind(const QString& name, QByteArray value)
{
if (intBindings.contains(name) || stringBindings.contains(name))
{
return;
}
if (intBindings.contains(name) || stringBindings.contains(name)) {
return;
}
stringBindings.insert(name, value);
stringBindings.insert(name, value);
}
void SQLiteConnection::checkForErrors(int r)
{
if (r != SQLITE_OK) {
const char *mess = sqlite3_errmsg(dbHandle);
sqlite3_finalize(statement);
throw SQLiteException(mess);
}
if (r != SQLITE_OK) {
const char *mess = sqlite3_errmsg(dbHandle);
sqlite3_finalize(statement);
throw SQLiteException(mess);
}
}
......@@ -195,35 +192,32 @@ SQLiteResource::SQLiteResource(QList<Row> t) : table(t) {}
QList<QVariant> SQLiteResource::getColumn(const QString& columnName)
{
QList<QVariant> data;
QList<QVariant> data;
for (QList<QVariantMap>::iterator it = table.begin(); it < table.end(); it++)
{
QVariantMap map = (*it);
for (QList<QVariantMap>::iterator it = table.begin(); it < table.end(); it++) {
QVariantMap map = (*it);
if (map.count(columnName) == 0)
{
throw SQLiteException("Unexistent column in table");
}
if (map.count(columnName) == 0) {
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)
{
if (table.isEmpty() || rowNumber >= table.size() || table[rowNumber].count(columnName) == 0)
{
throw SQLiteException("Access to non-existent data");
}
if (table.isEmpty() || rowNumber >= table.size() || table[rowNumber].count(columnName) == 0) {
throw SQLiteException("Access to non-existent data");
}
QVariantMap map = table[rowNumber];
return map[columnName];
QVariantMap map = table[rowNumber];
return map[columnName];
}
int SQLiteResource::getRowsCount()
{
return table.size();
return table.size();
}
......@@ -35,14 +35,14 @@ class SQLiteResource
{
typedef QVariant Value;
typedef QVariantMap Row;
QList<Row> table;
public:
public:
SQLiteResource(QList<Row>);
QVariant getDataAt(int, QString const&);
QList<QVariant> getColumn(QString const&);
int getRowsCount();
private:
QList<Row> table;
};
/*
......@@ -52,21 +52,8 @@ class SQLiteConnection
{
typedef QVariant Value;
typedef QVariantMap Row;
sqlite3 *dbHandle;
sqlite3_stmt *statement;
QList<Row> rows;
QMap<QString, int> intBindings;
QMap<QString, QString> stringBindings;
void checkForErrors(int);
Row getRowFromStatement(sqlite3_stmt*);
// Disable copy constructor and default constructor
SQLiteConnection();
SQLiteConnection(SQLiteConnection const&);
public:
public:
SQLiteConnection(QString const&, bool);
~SQLiteConnection();
......@@ -76,20 +63,32 @@ class SQLiteConnection
void bind(QString const&, int);
void bind(QString const&, QString);
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
{
private:
const char *message;
public:
public:
SQLiteException(const char *m) : message(m) {}
virtual const char *what() const throw()
{
return message;
}
virtual const char *what() const throw() {
return message;
}
private:
const char *message;
};
#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