1 #ifndef EASYQTSQL_TRANSACTION_H 2 #define EASYQTSQL_TRANSACTION_H 29 #ifndef EASY_QT_SQL_MAIN 80 explicit Database (
const QSqlDatabase &db = QSqlDatabase())
82 m_db = db.isValid() ? db : QSqlDatabase::database();
88 #ifdef DB_EXCEPTIONS_ENABLED 98 other.m_db = QSqlDatabase();
103 if (
this == &other)
return *
this;
106 other.m_db = QSqlDatabase();
116 return m_db.lastError();
126 QSqlQuery q =
m_db.exec(sql);
128 #ifdef DB_EXCEPTIONS_ENABLED 147 QSqlQuery q =
m_db.exec(sql);
149 #ifdef DB_EXCEPTIONS_ENABLED 228 template<
typename Func>
229 int each (
const QString &query, Func&& f)
const 250 template<
typename Func>
251 int first (
const QString &query, Func&& f)
const 274 template<
typename Func>
275 int range(
const QString &query,
int start,
int count, Func&& f)
const 297 template<
typename Func>
298 int top(
const QString &query,
int topCount, Func&& f)
const 325 QVariant
scalar(
const QString &query)
const 409 m_started =
m_db.transaction();
411 #ifdef DB_EXCEPTIONS_ENABLED 422 m_commited = other.m_commited;
423 m_started = other.m_started;
425 other.m_commited =
false;
426 other.m_started =
false;
431 m_started = other.m_started;
432 m_commited = other.m_commited;
434 other.m_commited =
false;
435 other.m_started =
false;
442 if (
m_db.isValid() && !m_commited)
457 if (
m_db.isValid() && !m_commited)
459 m_commited =
m_db.commit();
461 #ifdef DB_EXCEPTIONS_ENABLED 480 if (
m_db.isValid() && !m_commited)
482 res =
m_db.rollback();
507 bool m_commited =
false;
508 bool m_started =
false;
511 #endif // EASYQTSQL_TRANSACTION_H QVariant scalar(const QString &query) const
Executes query and returns scalar value.
Definition: EasyQtSql_Transaction.h:325
int first(const QString &query, Func &&f) const
Executes query and applies function f to the first result row.
Definition: EasyQtSql_Transaction.h:251
InsertQuery insertInto(const QString &table) const
Creates INSERT query wrapper.
Definition: EasyQtSql_Transaction.h:165
Transaction & operator=(Transaction &&other)
Definition: EasyQtSql_Transaction.h:429
QueryResult execQuery(const QString &sql) const
Executes SELECT query.
Definition: EasyQtSql_Transaction.h:145
bool commited() const
Returns true if the transaction has been commited successfully. Otherwise it returns false...
Definition: EasyQtSql_Transaction.h:501
QSqlDatabase m_db
Definition: EasyQtSql_Transaction.h:335
Class for query preparation and execution.
Definition: EasyQtSql_PreparedQuery.h:63
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.
Definition: EasyQtSql_Transaction.h:275
T scalar() const
Returns scalar value converted to type T (the value of the first column of the current row) ...
Definition: EasyQtSql_QueryResult.h:424
bool rollback()
Rolls back transaction.
Definition: EasyQtSql_Transaction.h:476
static int range(QueryResult &res, int start, int count, Func &&f)
Applies function (lambda) f to count rows starting from start index.
Definition: EasyQtSql_Util.h:88
~Transaction()
Definition: EasyQtSql_Transaction.h:440
bool commit()
Commits transaction.
Definition: EasyQtSql_Transaction.h:455
UpdateQuery update(const QString &table) const
Creates UPDATE query wrapper.
Definition: EasyQtSql_Transaction.h:187
QSqlQuery wrapper for non-select query results reading.
Definition: EasyQtSql_NonQueryResult.h:38
bool started() const
Returns true if the transaction has been started successfully. Otherwise it returns false...
Definition: EasyQtSql_Transaction.h:493
static int top(QueryResult &res, int topCount, Func &&f)
Applies function (lambda) f to topCount rows from res.
Definition: EasyQtSql_Util.h:122
QSqlError lastError() const
Returns information about the last error that occurred on the underlying database.
Definition: EasyQtSql_Transaction.h:114
T scalar(const QString &query) const
Executes query and returns scalar value converted to T.
Definition: EasyQtSql_Transaction.h:311
QSqlDatabase wrapper.
Definition: EasyQtSql_Transaction.h:67
Database & operator=(Database &&other)
Definition: EasyQtSql_Transaction.h:101
QSqlDatabase & qSqlDatabase()
Returns a reference to the wrapped QSqlDatabase object.
Definition: EasyQtSql_Transaction.h:209
QSqlQuery wrapper for DELETE FROM ... WHERE .. query execution.
Definition: EasyQtSql_DeleteQuery.h:39
Transaction(Transaction &&other)
Definition: EasyQtSql_Transaction.h:419
Database(Database &&other)
Definition: EasyQtSql_Transaction.h:95
NonQueryResult execNonQuery(const QString &sql) const
Executes non-query SQL statement (DELETE, INSERT, UPDATE, CREATE, ALTER, etc.)
Definition: EasyQtSql_Transaction.h:124
int each(const QString &query, Func &&f) const
Executes query and applies function f to each result row.
Definition: EasyQtSql_Transaction.h:229
DeleteQuery deleteFrom(const QString &table) const
Creates DELETE query wrapper.
Definition: EasyQtSql_Transaction.h:176
QSqlQuery wrapper with handy data fetch methods.
Definition: EasyQtSql_QueryResult.h:39
PreparedQuery prepare(const QString &sql, bool forwardOnly=true) const
Prepares SQL statement.
Definition: EasyQtSql_Transaction.h:199
QSqlDatabase transaction wrapper.
Definition: EasyQtSql_Transaction.h:398
static int first(QueryResult &res, Func &&f)
Applies function (lambda) f to first result row.
Definition: EasyQtSql_Util.h:133
bool next()
Retrieves the next record in the result, if available, and positions the query on the retrieved recor...
Definition: EasyQtSql_QueryResult.h:60
static int each(QueryResult &res, Func &&f)
Applies function (lambda) f to each row in res.
Definition: EasyQtSql_Util.h:64
Transaction(const QSqlDatabase &db=QSqlDatabase())
Definition: EasyQtSql_Transaction.h:404
QSqlQuery wrapper for INSERT INTO table query execution.
Definition: EasyQtSql_InsertQuery.h:39
int top(const QString &query, int topCount, Func &&f) const
Executes query and applies function f to topCount result rows.
Definition: EasyQtSql_Transaction.h:298
Database(const QSqlDatabase &db=QSqlDatabase())
Definition: EasyQtSql_Transaction.h:80
QSqlQuery wrapper for UPDATE ... SET ... WHERE ... query execution.
Definition: EasyQtSql_UpdateQuery.h:39
Exception class for SQL errors handling.
Definition: EasyQtSql_DBException.h:38