Commit d2294bd9 authored by Lisa's avatar Lisa Committed by Daniele Cocca
Browse files

Bindings and new SQL functions added to the library

parent 9ac5194f
......@@ -30,6 +30,26 @@ which returns a QList<QVariant>, if you want an entire column in one passage.
3. Closing the connection
The destructor does this for you.
4. Other features
4.1 last_insert_rowid() function
This function:
connection.getLastRowId();
returns the last key inserted in the database.
4.2 Number of rows
You can now know the number of rows provided from the last query, calling
connection.getRowsNumber()
4.3 Bindings
Bindings are now supported for integers, strings and QByteArrays. Call:
connection.bind(our name, value);
The binding is valid until the execution of the next query.
4. Error checking
An exception of type SQLiteException is thrown whatever any error occurs.
For database opening, closing or for queries, the sqlite3 error message is
......
......@@ -45,14 +45,16 @@ SQLiteConnection::~SQLiteConnection()
/*
* Executes a query and saves any result for further interrogation
*/
void SQLiteConnection::query(QString query)
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);
checkForErrors(retvalue);
......@@ -117,6 +119,7 @@ void SQLiteConnection::query(QString query)
retvalue = sqlite3_finalize(statement); /* destroys the statement */
checkForErrors(retvalue);
bindings.clear();
}
QVariant SQLiteConnection::getDataAt(int rowNumber, QString const& columnName)
......@@ -149,10 +152,70 @@ QList<QVariant> SQLiteConnection::getColumn(const QString& columnName)
return data;
}
void SQLiteConnection::bind(const QString& name, int value)
{
if (bindings.contains(name))
{
throw SQLiteException("Bind error: key already present");
}
QVariant v(value);
bindings.insert(name, v);
}
void SQLiteConnection::bind(const QString& name, QString value)
{
if (bindings.contains(name))
{
throw SQLiteException("Bind error: key already present");
}
QVariant v("'" + value + "'");
bindings.insert(name, v);
}
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()
{
return data.size();
}
qint32 SQLiteConnection::getLastRowId()
{
sqlite3_int64 id = sqlite3_last_insert_rowid(dbHandle);
return (qint32)id;
}
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)
{
throw SQLiteException(sqlite3_errmsg(dbHandle));
const char *mess = sqlite3_errmsg(dbHandle);
sqlite3_finalize(statement);
throw SQLiteException(mess);
}
}
......@@ -21,7 +21,6 @@
* Instead, these are rather good people indeed
*/
#include <QString>
#include <QObject>
#include <QMap>
#include <QList>
#include <QVariant>
......@@ -32,17 +31,17 @@
/*
* Public interface for SQLite3 main functions and utilities
*/
class SQLiteConnection : QObject
class SQLiteConnection
{
Q_OBJECT
private:
sqlite3 *dbHandle;
sqlite3_stmt *statement;
QList<QVariantMap> rows;
QVariantMap bindings;
bool prepared;
const char *getCharArray(QString);
void checkForErrors(int);
QString translateBindings(QString);
public:
SQLiteConnection(QString const& , bool);
......@@ -52,9 +51,12 @@ class SQLiteConnection : QObject
QVariant getDataAt(int, QString const&);
QList<QVariant> getColumn(QString const&);
qint32 getLastRowId();
int getRowsNumber();
signals:
void sqlite3Error(QString);
void bind(QString const&, int);
void bind(QString const&, QString);
void bind(QString const&, QByteArray);
};
class SQLiteException : public std::exception
......@@ -63,10 +65,7 @@ class SQLiteException : public std::exception
const char *message;
public:
SQLiteException(const char *m)
{
message = m;
}
SQLiteException(const char *m) : message(m) {}
virtual const char *what() const throw()
{
......
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