Index: ext/sqlite3/sqlite3.vcproj =================================================================== diff -u -rcaa075123222d2b7ae40270c006f021218d96a72 -r3397fd021739bea537248415a7b4fc2712dd2320 --- ext/sqlite3/sqlite3.vcproj (.../sqlite3.vcproj) (revision caa075123222d2b7ae40270c006f021218d96a72) +++ ext/sqlite3/sqlite3.vcproj (.../sqlite3.vcproj) (revision 3397fd021739bea537248415a7b4fc2712dd2320) @@ -321,8 +321,8 @@ @@ -394,8 +394,8 @@ @@ -468,8 +468,8 @@ TSQLiteDatabasePtr; +} + +END_CHCORE_NAMESPACE + +#endif Index: src/libchcore/TSQLiteException.cpp =================================================================== diff -u --- src/libchcore/TSQLiteException.cpp (revision 0) +++ src/libchcore/TSQLiteException.cpp (revision 3397fd021739bea537248415a7b4fc2712dd2320) @@ -0,0 +1,39 @@ +// ============================================================================ +// Copyright (C) 2001-2013 by Jozef Starosczyk +// ixen@copyhandler.com +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU Library General Public License +// (version 2) as published by the Free Software Foundation; +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// ============================================================================ +#include "stdafx.h" +#include "TSQLiteException.h" + +BEGIN_CHCORE_NAMESPACE + +namespace sqlite +{ + TSQLiteException::TSQLiteException(EGeneralErrors eErrorCode, int iSQLiteError, const wchar_t* pszMsg, const wchar_t* pszFile, size_t stLineNumber, const wchar_t* pszFunction) : + TBaseException(eErrorCode, pszMsg, pszFile, stLineNumber, pszFunction), + m_iSQLiteError(iSQLiteError) + { + } + + TSQLiteException::TSQLiteException(EGeneralErrors eErrorCode, int iSQLiteError, const char* pszMsg, const wchar_t* pszFile, size_t stLineNumber, const wchar_t* pszFunction) : + TBaseException(eErrorCode, pszMsg, pszFile, stLineNumber, pszFunction), + m_iSQLiteError(iSQLiteError) + { + } +} + +END_CHCORE_NAMESPACE Index: src/libchcore/TSQLiteException.h =================================================================== diff -u --- src/libchcore/TSQLiteException.h (revision 0) +++ src/libchcore/TSQLiteException.h (revision 3397fd021739bea537248415a7b4fc2712dd2320) @@ -0,0 +1,45 @@ +// ============================================================================ +// Copyright (C) 2001-2013 by Jozef Starosczyk +// ixen@copyhandler.com +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU Library General Public License +// (version 2) as published by the Free Software Foundation; +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// ============================================================================ +#ifndef __TSQLITEEXCEPTION_H__ +#define __TSQLITEEXCEPTION_H__ + +#include "libchcore.h" +#include "TBaseException.h" + +#define THROW_SQLITE_EXCEPTION(error_code, sqlite_error_code, err_msg)\ + throw TSQLiteException(error_code, sqlite_error_code, err_msg, __FILEW__, __LINE__, __FUNCTIONW__) + +BEGIN_CHCORE_NAMESPACE + +namespace sqlite +{ + class TSQLiteException : public TBaseException + { + public: + TSQLiteException(EGeneralErrors eErrorCode, int iSQLiteError, const wchar_t* pszMsg, const wchar_t* pszFile, size_t stLineNumber, const wchar_t* pszFunction); + TSQLiteException(EGeneralErrors eErrorCode, int iSQLiteError, const char* pszMsg, const wchar_t* pszFile, size_t stLineNumber, const wchar_t* pszFunction); + + private: + int m_iSQLiteError; + }; +} + +END_CHCORE_NAMESPACE + +#endif Index: src/libchcore/TSQLiteStatement.cpp =================================================================== diff -u --- src/libchcore/TSQLiteStatement.cpp (revision 0) +++ src/libchcore/TSQLiteStatement.cpp (revision 3397fd021739bea537248415a7b4fc2712dd2320) @@ -0,0 +1,189 @@ +// ============================================================================ +// Copyright (C) 2001-2013 by Jozef Starosczyk +// ixen@copyhandler.com +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU Library General Public License +// (version 2) as published by the Free Software Foundation; +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// ============================================================================ +#include "stdafx.h" +#include "TSQLiteStatement.h" +#include "sqlite3/sqlite3.h" +#include "ErrorCodes.h" +#include "TSQLiteException.h" + +BEGIN_CHCORE_NAMESPACE + +namespace sqlite +{ + TSQLiteStatement::TSQLiteStatement(const TSQLiteDatabasePtr& spDatabase) : + m_pStatement(NULL), + m_spDatabase(spDatabase), + m_bHasRow(false) + { + if(!m_spDatabase) + THROW_SQLITE_EXCEPTION(eErr_InvalidArgument, 0, _T("Invalid database provided")); + } + + TSQLiteStatement::~TSQLiteStatement() + { + int iResult = sqlite3_finalize(m_pStatement); + _ASSERTE(iResult == SQLITE_OK); + } + + void TSQLiteStatement::Close() + { + if(m_pStatement != NULL) + { + int iResult = sqlite3_finalize(m_pStatement); + if(iResult != SQLITE_OK) + THROW_SQLITE_EXCEPTION(eErr_SQLiteFinalizeError, iResult, _T("Cannot finalize statement")); + m_pStatement = NULL; + } + m_bHasRow = false; + } + + void TSQLiteStatement::Prepare(PCWSTR pszQuery) + { + Close(); + + int iResult = sqlite3_prepare16_v2((sqlite3*)m_spDatabase->GetHandle(), pszQuery, -1, &m_pStatement, NULL); + if(iResult != SQLITE_OK) + THROW_SQLITE_EXCEPTION(eErr_SQLitePrepareError, iResult, (PCTSTR)sqlite3_errmsg16((sqlite3*)m_spDatabase->GetHandle())); + } + + TSQLiteStatement::EStepResult TSQLiteStatement::Step() + { + m_bHasRow = false; + + if(!m_pStatement) + THROW_SQLITE_EXCEPTION(eErr_SQLiteStatementNotPrepared, 0, _T("Tried to step on unprepared statement")); + + int iResult = sqlite3_step(m_pStatement); + switch(iResult) + { + case SQLITE_ROW: + m_bHasRow = true; + return eStep_HasRow; + case SQLITE_OK: + case SQLITE_DONE: + Reset(); + return eStep_Finished; + default: + THROW_SQLITE_EXCEPTION(eErr_SQLiteStepError, iResult, _T("Cannot perform step on the statement")); + } + } + + void TSQLiteStatement::BindValue(int iColumn, double dValue) + { + if(!m_pStatement) + THROW_SQLITE_EXCEPTION(eErr_SQLiteStatementNotPrepared, 0, _T("Tried to step on unprepared statement")); + + int iResult = sqlite3_bind_double(m_pStatement, iColumn, dValue); + if(iResult != SQLITE_OK) + THROW_SQLITE_EXCEPTION(eErr_SQLiteBindError, iResult, _T("Cannot bind a parameter")); + } + + void TSQLiteStatement::BindValue(int iColumn, int iValue) + { + if(!m_pStatement) + THROW_SQLITE_EXCEPTION(eErr_SQLiteStatementNotPrepared, 0, _T("Tried to step on unprepared statement")); + + int iResult = sqlite3_bind_int(m_pStatement, iColumn, iValue); + if(iResult != SQLITE_OK) + THROW_SQLITE_EXCEPTION(eErr_SQLiteBindError, iResult, _T("Cannot bind a parameter")); + } + + void TSQLiteStatement::BindValue(int iColumn, long long llValue) + { + if(!m_pStatement) + THROW_SQLITE_EXCEPTION(eErr_SQLiteStatementNotPrepared, 0, _T("Tried to step on unprepared statement")); + + int iResult = sqlite3_bind_int64(m_pStatement, iColumn, llValue); + if(iResult != SQLITE_OK) + THROW_SQLITE_EXCEPTION(eErr_SQLiteBindError, iResult, _T("Cannot bind a parameter")); + } + + void TSQLiteStatement::BindValue(int iColumn, PCTSTR pszText) + { + if(!m_pStatement) + THROW_SQLITE_EXCEPTION(eErr_SQLiteStatementNotPrepared, 0, _T("Tried to step on unprepared statement")); + + int iResult = sqlite3_bind_text16(m_pStatement, iColumn, pszText, -1, SQLITE_TRANSIENT); + if(iResult != SQLITE_OK) + THROW_SQLITE_EXCEPTION(eErr_SQLiteBindError, iResult, _T("Cannot bind a parameter")); + } + + double TSQLiteStatement::GetDouble(int iCol) + { + if(!m_pStatement) + THROW_SQLITE_EXCEPTION(eErr_SQLiteStatementNotPrepared, 0, _T("Tried to step on unprepared statement")); + if(!m_bHasRow) + THROW_SQLITE_EXCEPTION(eErr_SQLiteNoRowAvailable, 0, _T("No row available")); + + return sqlite3_column_double(m_pStatement, iCol); + } + + int TSQLiteStatement::GetInt(int iCol) + { + if(!m_pStatement) + THROW_SQLITE_EXCEPTION(eErr_SQLiteStatementNotPrepared, 0, _T("Tried to step on unprepared statement")); + if(!m_bHasRow) + THROW_SQLITE_EXCEPTION(eErr_SQLiteNoRowAvailable, 0, _T("No row available")); + + return sqlite3_column_int(m_pStatement, iCol); + } + + long long TSQLiteStatement::GetInt64(int iCol) + { + if(!m_pStatement) + THROW_SQLITE_EXCEPTION(eErr_SQLiteStatementNotPrepared, 0, _T("Tried to step on unprepared statement")); + if(!m_bHasRow) + THROW_SQLITE_EXCEPTION(eErr_SQLiteNoRowAvailable, 0, _T("No row available")); + + return sqlite3_column_int64(m_pStatement, iCol); + } + + TString TSQLiteStatement::GetText(int iCol) + { + if(!m_pStatement) + THROW_SQLITE_EXCEPTION(eErr_SQLiteStatementNotPrepared, 0, _T("Tried to step on unprepared statement")); + if(!m_bHasRow) + THROW_SQLITE_EXCEPTION(eErr_SQLiteNoRowAvailable, 0, _T("No row available")); + + return TString((const wchar_t*)sqlite3_column_text16(m_pStatement, iCol)); + } + + void TSQLiteStatement::ClearBindings() + { + if(!m_pStatement) + THROW_SQLITE_EXCEPTION(eErr_SQLiteStatementNotPrepared, 0, _T("Tried to step on unprepared statement")); + + int iResult = sqlite3_clear_bindings(m_pStatement); + if(iResult != SQLITE_OK) + THROW_SQLITE_EXCEPTION(eErr_SQLiteBindError, iResult, _T("Cannot clear bindings")); + } + + void TSQLiteStatement::Reset() + { + if(!m_pStatement) + THROW_SQLITE_EXCEPTION(eErr_SQLiteStatementNotPrepared, 0, _T("Tried to step on unprepared statement")); + + int iResult = sqlite3_reset(m_pStatement); + if(iResult != SQLITE_OK) + THROW_SQLITE_EXCEPTION(eErr_SQLiteBindError, iResult, _T("Cannot reset statement")); + } + +} + +END_CHCORE_NAMESPACE Index: src/libchcore/TSQLiteStatement.h =================================================================== diff -u --- src/libchcore/TSQLiteStatement.h (revision 0) +++ src/libchcore/TSQLiteStatement.h (revision 3397fd021739bea537248415a7b4fc2712dd2320) @@ -0,0 +1,77 @@ +// ============================================================================ +// Copyright (C) 2001-2013 by Jozef Starosczyk +// ixen@copyhandler.com +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU Library General Public License +// (version 2) as published by the Free Software Foundation; +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// ============================================================================ +#ifndef __TSQLITESTATEMENT_H__ +#define __TSQLITESTATEMENT_H__ + +#include "libchcore.h" +#include "TSQLiteDatabase.h" +#include "TString.h" + +struct sqlite3_stmt; + +BEGIN_CHCORE_NAMESPACE + +namespace sqlite +{ + typedef boost::shared_ptr SQLiteStatementHandle; + + class TSQLiteStatement + { + public: + enum EStepResult + { + eStep_Finished, + eStep_HasRow + }; + + public: + TSQLiteStatement(const TSQLiteDatabasePtr& spDatabase); + ~TSQLiteStatement(); + + void Close(); + + void Prepare(PCTSTR pszQuery); + + void BindValue(int iColumn, double dValue); + void BindValue(int iColumn, int iValue); + void BindValue(int iColumn, long long llValue); + void BindValue(int iColumn, PCTSTR pszText); + + void ClearBindings(); + + EStepResult Step(); + void Reset(); + + double GetDouble(int iCol); + int GetInt(int iCol); + long long GetInt64(int iCol); + TString GetText(int iCol); + + private: + sqlite3_stmt* m_pStatement; + TSQLiteDatabasePtr m_spDatabase; + bool m_bHasRow; + }; + + typedef boost::shared_ptr TSQLiteStatementPtr; +} + +END_CHCORE_NAMESPACE + +#endif Index: src/libchcore/TSQLiteTransaction.cpp =================================================================== diff -u --- src/libchcore/TSQLiteTransaction.cpp (revision 0) +++ src/libchcore/TSQLiteTransaction.cpp (revision 3397fd021739bea537248415a7b4fc2712dd2320) @@ -0,0 +1,100 @@ +// ============================================================================ +// Copyright (C) 2001-2013 by Jozef Starosczyk +// ixen@copyhandler.com +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU Library General Public License +// (version 2) as published by the Free Software Foundation; +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// ============================================================================ +#include "stdafx.h" +#include "TSQLiteTransaction.h" +#include "TSQLiteException.h" +#include "ErrorCodes.h" +#include "sqlite3/sqlite3.h" + +BEGIN_CHCORE_NAMESPACE + +namespace sqlite +{ + TSQLiteTransaction::TSQLiteTransaction(const TSQLiteDatabasePtr& spDatabase) : + m_spDatabase(spDatabase), + m_bTransactionStarted(false) + { + if(!m_spDatabase) + THROW_SQLITE_EXCEPTION(eErr_InvalidArgument, 0, _T("Invalid database provided")); + Begin(); + } + + TSQLiteTransaction::~TSQLiteTransaction() + { + // try to rollback the transaction; this is the last resort + if(m_bTransactionStarted && m_spDatabase->GetInTransaction()) + { + int iResult = sqlite3_exec((sqlite3*)m_spDatabase->GetHandle(), "ROLLBACK TRANSACTION;", NULL, NULL, NULL); + _ASSERTE(iResult == SQLITE_OK); + m_spDatabase->SetInTransaction(false); + } + } + + void TSQLiteTransaction::Begin() + { + if(m_spDatabase->GetInTransaction()) + return; + + if(m_bTransactionStarted) + THROW_SQLITE_EXCEPTION(eErr_SQLiteCannotBeginTransaction, 0, _T("Transaction already started")); + + int iResult = sqlite3_exec((sqlite3*)m_spDatabase->GetHandle(), "BEGIN TRANSACTION", NULL, NULL, NULL); + if(iResult != SQLITE_OK) + THROW_SQLITE_EXCEPTION(eErr_SQLiteCannotBeginTransaction, iResult, _T("Cannot begin transaction")); + + m_spDatabase->SetInTransaction(true); + m_bTransactionStarted = true; + } + + void TSQLiteTransaction::Rollback() + { + // no transactions whatsoever (even on database) + if(!m_bTransactionStarted && !m_spDatabase->GetInTransaction()) + THROW_SQLITE_EXCEPTION(eErr_SQLiteCannotRollbackTransaction, 0, _T("Transaction not started")); + + // database has transaction started, but not by this object + if(!m_bTransactionStarted) + return; + + int iResult = sqlite3_exec((sqlite3*)m_spDatabase->GetHandle(), "ROLLBACK TRANSACTION;", NULL, NULL, NULL); + if(iResult != SQLITE_OK) + THROW_SQLITE_EXCEPTION(eErr_SQLiteCannotRollbackTransaction, iResult, _T("Cannot rollback transaction")); + m_spDatabase->SetInTransaction(false); + m_bTransactionStarted = false; + } + + void TSQLiteTransaction::Commit() + { + // no transactions whatsoever (even on database) + if(!m_bTransactionStarted && !m_spDatabase->GetInTransaction()) + THROW_SQLITE_EXCEPTION(eErr_SQLiteCannotRollbackTransaction, 0, _T("Transaction not started")); + + // database has transaction started, but not by this object + if(!m_bTransactionStarted) + return; + + int iResult = sqlite3_exec((sqlite3*)m_spDatabase->GetHandle(), "COMMIT TRANSACTION;", NULL, NULL, NULL); + if(iResult != SQLITE_OK) + THROW_SQLITE_EXCEPTION(eErr_SQLiteCannotCommitTransaction, iResult, _T("Cannot commit transaction")); + m_spDatabase->SetInTransaction(false); + m_bTransactionStarted = false; + } +} + +END_CHCORE_NAMESPACE Index: src/libchcore/TSQLiteTransaction.h =================================================================== diff -u --- src/libchcore/TSQLiteTransaction.h (revision 0) +++ src/libchcore/TSQLiteTransaction.h (revision 3397fd021739bea537248415a7b4fc2712dd2320) @@ -0,0 +1,47 @@ +// ============================================================================ +// Copyright (C) 2001-2013 by Jozef Starosczyk +// ixen@copyhandler.com +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU Library General Public License +// (version 2) as published by the Free Software Foundation; +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// ============================================================================ +#ifndef __TSQLITETRANSACTION_H__ +#define __TSQLITETRANSACTION_H__ + +#include "libchcore.h" +#include "TSQLiteDatabase.h" + +BEGIN_CHCORE_NAMESPACE + +namespace sqlite +{ + class TSQLiteTransaction + { + public: + TSQLiteTransaction(const TSQLiteDatabasePtr& spDatabase); + ~TSQLiteTransaction(); + + void Begin(); + void Rollback(); + void Commit(); + + private: + TSQLiteDatabasePtr m_spDatabase; + bool m_bTransactionStarted; // states if transaction was started by this object + }; +} + +END_CHCORE_NAMESPACE + +#endif Fisheye: tag 548382442cbf7bed7f744b279ce3f66b54992724 is not in file src/libchcore/Tests/TestsTDataBufferManager.cpp Fisheye: Tag 3397fd021739bea537248415a7b4fc2712dd2320 refers to a dead (removed) revision in file `src/libchcore/Tests/TDataBufferManagerTest.cpp'. Fisheye: No comparison available. Pass `N' to diff? Index: src/libchcore/Tests/TestsTSQLiteDatabase.cpp =================================================================== diff -u --- src/libchcore/Tests/TestsTSQLiteDatabase.cpp (revision 0) +++ src/libchcore/Tests/TestsTSQLiteDatabase.cpp (revision 3397fd021739bea537248415a7b4fc2712dd2320) @@ -0,0 +1,13 @@ +#include "stdafx.h" +#include "gtest/gtest.h" +#include "gmock/gmock.h" +#include "../TSQLiteDatabase.h" + +using namespace chcore::sqlite; + +TEST(SQLiteDatabase, CreationWithVerification) +{ + TSQLiteDatabase db(_T(":memory:")); + EXPECT_TRUE(db.GetHandle() != NULL); + EXPECT_FALSE(db.GetInTransaction()); +} Index: src/libchcore/Tests/TestsTSQLiteStatement.cpp =================================================================== diff -u --- src/libchcore/Tests/TestsTSQLiteStatement.cpp (revision 0) +++ src/libchcore/Tests/TestsTSQLiteStatement.cpp (revision 3397fd021739bea537248415a7b4fc2712dd2320) @@ -0,0 +1,108 @@ +#include "stdafx.h" +#include "gtest/gtest.h" +#include "gmock/gmock.h" +#include "../TSQLiteDatabase.h" +#include "../TSQLiteStatement.h" +#include "../TSQLiteException.h" + +using namespace chcore::sqlite; + +TEST(SQLiteStatement, CorrectPrepare) +{ + TSQLiteDatabasePtr spDB(new TSQLiteDatabase(_T(":memory:"))); + TSQLiteStatement tStatement(spDB); + + tStatement.Prepare(_T("CREATE TABLE test(col1 INTEGER, col2 VARCHAR(40))")); + EXPECT_EQ(TSQLiteStatement::eStep_Finished, tStatement.Step()); +} + +TEST(SQLiteStatement, IncorrectPrepare) +{ + TSQLiteDatabasePtr spDB(new TSQLiteDatabase(_T(":memory:"))); + TSQLiteStatement tStatement(spDB); + + EXPECT_THROW(tStatement.Prepare(_T("CREATE incorrect TABLE test(col1 INTEGER, col2 VARCHAR(40))")), TSQLiteException); +} + +TEST(SQLiteStatement, PreparedStep) +{ + TSQLiteDatabasePtr spDB(new TSQLiteDatabase(_T(":memory:"))); + TSQLiteStatement tStatement(spDB); + + tStatement.Prepare(_T("CREATE TABLE test(col1 INTEGER, col2 VARCHAR(40))")); + EXPECT_EQ(tStatement.Step(), TSQLiteStatement::eStep_Finished); +} + +TEST(SQLiteStatement, UnpreparedStep) +{ + TSQLiteDatabasePtr spDB(new TSQLiteDatabase(_T(":memory:"))); + TSQLiteStatement tStatement(spDB); + + EXPECT_THROW(tStatement.Step(), TSQLiteException); +} + +TEST(SQLiteStatement, UnpreparedBind) +{ + TSQLiteDatabasePtr spDB(new TSQLiteDatabase(_T(":memory:"))); + TSQLiteStatement tStatement(spDB); + + // insert data + EXPECT_THROW(tStatement.BindValue(1, 54), TSQLiteException); +} + +TEST(SQLiteStatement, InsertAndRetrieveData) +{ + TSQLiteDatabasePtr spDB(new TSQLiteDatabase(_T(":memory:"))); + TSQLiteStatement tStatement(spDB); + + // create schema + tStatement.Prepare(_T("CREATE TABLE test(col1 INTEGER, col2 VARCHAR(40))")); + EXPECT_EQ(TSQLiteStatement::eStep_Finished, tStatement.Step()); + + // insert data + tStatement.Prepare(_T("INSERT INTO test(col1, col2) VALUES(?1, ?2)")); + tStatement.BindValue(1, 54); + tStatement.BindValue(2, _T("Some Value")); + EXPECT_EQ(TSQLiteStatement::eStep_Finished, tStatement.Step()); + + // retrieve data + tStatement.Prepare(_T("SELECT col2, col1 FROM test")); + EXPECT_EQ(TSQLiteStatement::eStep_HasRow, tStatement.Step()); + EXPECT_EQ(54, tStatement.GetInt(1)); + EXPECT_STREQ(_T("Some Value"), tStatement.GetText(0)); +} + +TEST(SQLiteStatement, ClearBindings) +{ + TSQLiteDatabasePtr spDB(new TSQLiteDatabase(_T(":memory:"))); + TSQLiteStatement tStatement(spDB); + + // create schema + tStatement.Prepare(_T("CREATE TABLE test(col1 INTEGER, col2 VARCHAR(40))")); + EXPECT_EQ(TSQLiteStatement::eStep_Finished, tStatement.Step()); + + // insert data + tStatement.Prepare(_T("INSERT INTO test(col1, col2) VALUES(?1, ?2)")); + + tStatement.BindValue(1, 54); + tStatement.BindValue(2, _T("Some Value")); + EXPECT_EQ(TSQLiteStatement::eStep_Finished, tStatement.Step()); + + tStatement.BindValue(1, 32); + tStatement.BindValue(2, _T("???")); + EXPECT_EQ(TSQLiteStatement::eStep_Finished, tStatement.Step()); + + // retrieve data + tStatement.Prepare(_T("SELECT col2, col1 FROM test ORDER BY col1")); + EXPECT_EQ(TSQLiteStatement::eStep_HasRow, tStatement.Step()); + + EXPECT_EQ(32, tStatement.GetInt(1)); + EXPECT_STREQ(_T("???"), tStatement.GetText(0)); + + EXPECT_EQ(TSQLiteStatement::eStep_HasRow, tStatement.Step()); + + EXPECT_EQ(54, tStatement.GetInt(1)); + EXPECT_STREQ(_T("Some Value"), tStatement.GetText(0)); + + EXPECT_EQ(TSQLiteStatement::eStep_Finished, tStatement.Step()); +} Index: src/libchcore/Tests/TestsTSQLiteTransaction.cpp =================================================================== diff -u --- src/libchcore/Tests/TestsTSQLiteTransaction.cpp (revision 0) +++ src/libchcore/Tests/TestsTSQLiteTransaction.cpp (revision 3397fd021739bea537248415a7b4fc2712dd2320) @@ -0,0 +1,81 @@ +#include "stdafx.h" +#include "gtest/gtest.h" +#include "gmock/gmock.h" +#include "../TSQLiteTransaction.h" +#include "../TSQLiteDatabase.h" +#include "../TSQLiteStatement.h" +#include "../TSQLiteException.h" + +using namespace chcore::sqlite; + +TEST(SQLiteTransaction, BeginTransactionWithDefaultRollback_Empty) +{ + TSQLiteDatabasePtr spDB(new TSQLiteDatabase(_T(":memory:"))); + + // separate scope for the transaction + { + TSQLiteTransaction tran(spDB); + EXPECT_TRUE(spDB->GetInTransaction()); + } + EXPECT_FALSE(spDB->GetInTransaction()); +} + +TEST(SQLiteTransaction, BeginTransactionWithDefaultRollback_WithData) +{ + TSQLiteDatabasePtr spDB(new TSQLiteDatabase(_T(":memory:"))); + TSQLiteStatement tStatement(spDB); + + // separate scope for the transaction + { + TSQLiteTransaction tran(spDB); + + tStatement.Prepare(_T("CREATE TABLE test(col1 INTEGER, col2 VARCHAR(40))")); + tStatement.Step(); + + tStatement.Prepare(_T("INSERT INTO test(col1, col2) VALUES(?1, ?2)")); + tStatement.BindValue(1, 54); + tStatement.BindValue(2, _T("Some Value")); + tStatement.Step(); + } + + // rollback seem to revert the schema changes, so this statement can't be processed due to missing table + EXPECT_THROW(tStatement.Prepare(_T("SELECT count(*) FROM test")), TSQLiteException); +} + +TEST(SQLiteTransaction, BeginTransactionWithCommit) +{ + TSQLiteDatabasePtr spDB(new TSQLiteDatabase(_T(":memory:"))); + + // separate scope for the transaction + TSQLiteTransaction tran(spDB); + + tran.Commit(); + EXPECT_FALSE(spDB->GetInTransaction()); +} + +TEST(SQLiteTransaction, BeginTransactionWithCommit_WithData) +{ + TSQLiteDatabasePtr spDB(new TSQLiteDatabase(_T(":memory:"))); + TSQLiteStatement tStatement(spDB); + + // separate scope for the transaction + { + TSQLiteTransaction tran(spDB); + + tStatement.Prepare(_T("CREATE TABLE test(col1 INTEGER, col2 VARCHAR(40))")); + tStatement.Step(); + + tStatement.Prepare(_T("INSERT INTO test(col1, col2) VALUES(?1, ?2)")); + tStatement.BindValue(1, 54); + tStatement.BindValue(2, _T("Some Value")); + tStatement.Step(); + + tran.Commit(); + } + + // rollback seem to revert the schema changes, so this statement can't be processed due to missing table + tStatement.Prepare(_T("SELECT count(*) FROM test")); + tStatement.Step(); + + EXPECT_EQ(1, tStatement.GetInt(0)); +} Index: src/libchcore/libchcore.vc90.vcproj =================================================================== diff -u -ra5f396da5ed5ffb3fcd9fdf22afb5a7fd07e1ab8 -r3397fd021739bea537248415a7b4fc2712dd2320 --- src/libchcore/libchcore.vc90.vcproj (.../libchcore.vc90.vcproj) (revision a5f396da5ed5ffb3fcd9fdf22afb5a7fd07e1ab8) +++ src/libchcore/libchcore.vc90.vcproj (.../libchcore.vc90.vcproj) (revision 3397fd021739bea537248415a7b4fc2712dd2320) @@ -1,7 +1,7 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +