Skip to content

Commit

Permalink
Added move ctors and move assignment operators for session, statement…
Browse files Browse the repository at this point in the history
… and transaction classes
  • Loading branch information
pmed committed Sep 17, 2012
1 parent 5635ca7 commit d70b01f
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 15 deletions.
23 changes: 23 additions & 0 deletions sqlitepp/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "session.hpp"
#include "exception.hpp"
#include "transaction.hpp"

//////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -57,6 +58,28 @@ session::session(string_t const& file_name, int flags)
}
//----------------------------------------------------------------------------

session::session(session&& src)
{
*this = std::move(src);
}
//----------------------------------------------------------------------------

session& session::operator=(session&& src)
{
if ( &src != this )
{
impl_ = src.impl_;
src.impl_ = nullptr;
if ( src.active_txn_ )
{
*active_txn_ = std::move(*src.active_txn_);
}
last_exec_ = src.last_exec_;
}
return *this;
}
//----------------------------------------------------------------------------

session::~session()
{
try
Expand Down
5 changes: 4 additions & 1 deletion sqlitepp/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ class session
// Optional parameter flags for file open operations
// (see SQLite reference at http://sqlite.org/c3ref/c_open_autoproxy.html)
explicit session(string_t const& file_name, int flags = 0);


session(session&& src);
session& operator=(session&& src);

// Close session on destroy.
~session();

Expand Down
22 changes: 22 additions & 0 deletions sqlitepp/statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ statement::statement(session& s, string_t const& sql)
}
//----------------------------------------------------------------------------

statement::statement(statement&& src)
: s_(src.s_)
, q_(std::move(src.q_))
, impl_(std::move(src.impl_))
{
src.impl_ = nullptr;
}
//----------------------------------------------------------------------------

statement& statement::operator=(statement&& src)
{
if ( &src != this )
{
s_ = std::move(src.s_);
q_ = std::move(src.q_);
impl_ = std::move(src.impl_);
src.impl_ = nullptr;
}
return *this;
}
//----------------------------------------------------------------------------

statement::~statement()
{
finalize(false);
Expand Down
11 changes: 6 additions & 5 deletions sqlitepp/statement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class statement
// Create statement with SQL query text.
statement(session& s, string_t const& sql);

statement(statement&& src);
statement& operator=(statement&& src);

// Finalize statement on destroy.
~statement();

Expand Down Expand Up @@ -128,11 +131,9 @@ class statement
// Get use position by name in query.
int use_pos(string_t const& name) const;
private:
// Copy not allowed.
statement(statement const&);
// Assignment not allowed.
statement& operator=(statement const&);

statement(statement const&); // = delete;
statement& operator=(statement const&); // = delete;

session& s_;
query q_;
sqlite3_stmt* impl_;
Expand Down
63 changes: 55 additions & 8 deletions sqlitepp/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ namespace sqlitepp {
//////////////////////////////////////////////////////////////////////////////

transaction::transaction(session& s, type t)
: s_(s)
: s_(&s)
, do_rollback_(false)
{
if ( s_.active_txn() )
if ( s_->active_txn() )
{
throw nested_txn_not_supported();
}
Expand All @@ -44,25 +44,72 @@ transaction::transaction(session& s, type t)
break;
}

s_ << utf(begin_cmd);
s_.active_txn_ = this;
*s_ << utf(begin_cmd);
s_->active_txn_ = this;
do_rollback_ = true;
}
//----------------------------------------------------------------------------

transaction::transaction(transaction&& src)
: s_(src.s_)
, do_rollback_(src.do_rollback_)
{
*this = std::move(src);
}
//----------------------------------------------------------------------------

transaction& transaction::operator=(transaction&& src)
{
if ( &src != this )
{
s_ = src.s_;
src.s_ = nullptr;

do_rollback_ = src.do_rollback_;
src.do_rollback_ = false;

if ( s_ )
{
s_->active_txn_ = this;
}
}
return *this;
}
//----------------------------------------------------------------------------

transaction::~transaction()
{
if ( do_rollback_ )
try
{
s_ << utf("rollback");
rollback();
}
s_.active_txn_ = nullptr;
catch (...)
{
assert(false);
}
}
//----------------------------------------------------------------------------

void transaction::rollback()
{
if ( do_rollback_ && s_ )
{
*s_ << utf("rollback");
}
if ( s_ )
{
s_->active_txn_ = nullptr;
}
do_rollback_ = false;
}
//----------------------------------------------------------------------------

void transaction::commit()
{
s_ << utf("commit");
if ( s_ )
{
*s_ << utf("commit");
}
do_rollback_ = false;
}
//----------------------------------------------------------------------------
Expand Down
8 changes: 7 additions & 1 deletion sqlitepp/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,23 @@ class transaction
// Begin transaction in context of session.
explicit transaction(session& s, type t = deferred);

transaction(transaction&& src);
transaction& operator=(transaction&& src);

// End transaction with rollback if it is not commited.
~transaction();

// Rollback transaction
void rollback();

// Commit transaction.
void commit();

private:
transaction(transaction const&); // = delete
transaction& operator=(transaction const&); // = delete

session& s_;
session* s_;
bool do_rollback_;
};

Expand Down

0 comments on commit d70b01f

Please sign in to comment.