EasyQtSql
Easy SQL data access helper for QtSql
EasyQtSql::Database Class Reference

QSqlDatabase wrapper. More...

#include <EasyQtSql.h>

Inheritance diagram for EasyQtSql::Database:
EasyQtSql::Transaction

Public Member Functions

 Database (const QSqlDatabase &db=QSqlDatabase())
 
 Database (Database &&other)
 
Databaseoperator= (Database &&other)
 
QSqlError lastError () const
 Returns information about the last error that occurred on the underlying database. More...
 
NonQueryResult execNonQuery (const QString &sql) const
 Executes non-query SQL statement (DELETE, INSERT, UPDATE, CREATE, ALTER, etc.) More...
 
QueryResult execQuery (const QString &sql) const
 Executes SELECT query. More...
 
InsertQuery insertInto (const QString &table) const
 Creates INSERT query wrapper. More...
 
DeleteQuery deleteFrom (const QString &table) const
 Creates DELETE query wrapper. More...
 
UpdateQuery update (const QString &table) const
 Creates UPDATE query wrapper. More...
 
PreparedQuery prepare (const QString &sql, bool forwardOnly=true) const
 Prepares SQL statement. More...
 
QSqlDatabase & qSqlDatabase ()
 Returns a reference to the wrapped QSqlDatabase object. More...
 
template<typename Func >
int each (const QString &query, Func &&f) const
 Executes query and applies function f to each result row. More...
 
template<typename Func >
int first (const QString &query, Func &&f) const
 Executes query and applies function f to the first result row. More...
 
template<typename Func >
int range (const QString &query, int start, int count, Func &&f) const
 Executes query and applies function f to count result rows starting from index start. More...
 
template<typename Func >
int top (const QString &query, int topCount, Func &&f) const
 Executes query and applies function f to topCount result rows. More...
 
template<typename T >
scalar (const QString &query) const
 Executes query and returns scalar value converted to T. More...
 
QVariant scalar (const QString &query) const
 Executes query and returns scalar value. More...
 

Protected Attributes

QSqlDatabase m_db
 

Detailed Description

QSqlDatabase wrapper.

void test()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
try
{
Database sdb(db);
sdb.execNonQuery("CREATE TABLE table (a int, b int, c int, d text)");
}
catch (const DBException &e)
{
//you can handle all the errors at one point
qDebug() << e.lastError << e.lastQuery;
}
}

Constructor & Destructor Documentation

◆ Database() [1/2]

EasyQtSql::Database::Database ( const QSqlDatabase &  db = QSqlDatabase())
inlineexplicit
Parameters
dbQSqlDatabase to use

Creates an Database object, tries to open db connection if not opened.

Exceptions
DBException

◆ Database() [2/2]

EasyQtSql::Database::Database ( Database &&  other)
inline

Member Function Documentation

◆ deleteFrom()

DeleteQuery EasyQtSql::Database::deleteFrom ( const QString &  table) const
inline

Creates DELETE query wrapper.

Parameters
tableTable to delete from

◆ each()

template<typename Func >
int EasyQtSql::Database::each ( const QString &  query,
Func &&  f 
) const
inline

Executes query and applies function f to each result row.

Parameters
querySQL query string (SELECT statement)
fFunction (lambda) to apply to
Returns
num rows handled with function f
db.each("SELECT * FROM table", [](const QueryResult &res)
{
qDebug() << res.toMap();
});

◆ execNonQuery()

NonQueryResult EasyQtSql::Database::execNonQuery ( const QString &  sql) const
inline

Executes non-query SQL statement (DELETE, INSERT, UPDATE, CREATE, ALTER, etc.)

Parameters
querySQL statement string
Exceptions
DBException

◆ execQuery()

QueryResult EasyQtSql::Database::execQuery ( const QString &  sql) const
inline

Executes SELECT query.

Parameters
querySQL statement string
Exceptions
DBException

◆ first()

template<typename Func >
int EasyQtSql::Database::first ( const QString &  query,
Func &&  f 
) const
inline

Executes query and applies function f to the first result row.

Parameters
querySQL query string (SELECT statement)
fFunction (lambda) to apply to
Returns
num rows handled with function f
db.first("SELECT * FROM table", [](const QueryResult &res)
{
qDebug() << res.toMap();
});

◆ insertInto()

InsertQuery EasyQtSql::Database::insertInto ( const QString &  table) const
inline

Creates INSERT query wrapper.

Parameters
tableTable to insert into with list of columns

◆ lastError()

QSqlError EasyQtSql::Database::lastError ( ) const
inline

Returns information about the last error that occurred on the underlying database.

◆ operator=()

Database& EasyQtSql::Database::operator= ( Database &&  other)
inline

◆ prepare()

PreparedQuery EasyQtSql::Database::prepare ( const QString &  sql,
bool  forwardOnly = true 
) const
inline

Prepares SQL statement.

Parameters
sqlSQL statement string
forwardOnlyConfigure underlying QSqlQuery as forwardOnly

◆ qSqlDatabase()

QSqlDatabase& EasyQtSql::Database::qSqlDatabase ( )
inline

Returns a reference to the wrapped QSqlDatabase object.

◆ range()

template<typename Func >
int EasyQtSql::Database::range ( const QString &  query,
int  start,
int  count,
Func &&  f 
) const
inline

Executes query and applies function f to count result rows starting from index start.

Parameters
querySQL query string (SELECT statement)
startStart index
countRow count to handle
fFunction (lambda) to apply to
Returns
num rows handled with function f
db.range("SELECT * FROM table", 3, 10, [](const QueryResult &res)
{
qDebug() << res.toMap();
});

◆ scalar() [1/2]

template<typename T >
T EasyQtSql::Database::scalar ( const QString &  query) const
inline

Executes query and returns scalar value converted to T.

Parameters
querySQL query string (SELECT statement)
See also
QueryResult::scalar

◆ scalar() [2/2]

QVariant EasyQtSql::Database::scalar ( const QString &  query) const
inline

Executes query and returns scalar value.

Parameters
querySQL query string (SELECT statement)
See also
QueryResult::scalar

◆ top()

template<typename Func >
int EasyQtSql::Database::top ( const QString &  query,
int  topCount,
Func &&  f 
) const
inline

Executes query and applies function f to topCount result rows.

Parameters
querySQL query string (SELECT statement)
topCountRow count to handle
fFunction (lambda) to apply to
Returns
num rows handled with function f
db.top("SELECT * FROM table", 10, [](const QueryResult &res)
{
qDebug() << res.toMap();
});

◆ update()

UpdateQuery EasyQtSql::Database::update ( const QString &  table) const
inline

Creates UPDATE query wrapper.

Parameters
tableTable to update

Member Data Documentation

◆ m_db

QSqlDatabase EasyQtSql::Database::m_db
protected

The documentation for this class was generated from the following file: