Index: src/libchcore/TSQLiteStatement.cpp =================================================================== diff -u -N -rd76d3ce6c8c55fa23009dbb03b8bc06f482c5b72 -re96806b7f8ff7ca7e9f4afbea603e6351a3dc3e3 --- src/libchcore/TSQLiteStatement.cpp (.../TSQLiteStatement.cpp) (revision d76d3ce6c8c55fa23009dbb03b8bc06f482c5b72) +++ src/libchcore/TSQLiteStatement.cpp (.../TSQLiteStatement.cpp) (revision e96806b7f8ff7ca7e9f4afbea603e6351a3dc3e3) @@ -23,326 +23,325 @@ #include "TSQLiteException.h" #include -BEGIN_CHCORE_NAMESPACE - -namespace sqlite +namespace chcore { - TSQLiteStatement::TSQLiteStatement(const TSQLiteDatabasePtr& spDatabase) : - m_pStatement(NULL), - m_spDatabase(spDatabase), - m_bHasRow(false) + namespace sqlite { - if(!m_spDatabase) - THROW_SQLITE_EXCEPTION(eErr_InvalidArgument, 0, _T("Invalid database provided")); - } + 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); - iResult; - _ASSERTE(iResult == SQLITE_OK); - } - - void TSQLiteStatement::Close() - { - if(m_pStatement != NULL) + TSQLiteStatement::~TSQLiteStatement() { int iResult = sqlite3_finalize(m_pStatement); - if(iResult != SQLITE_OK) - THROW_SQLITE_EXCEPTION(eErr_SQLiteFinalizeError, iResult, _T("Cannot finalize statement")); - m_pStatement = NULL; + iResult; + _ASSERTE(iResult == SQLITE_OK); } - m_bHasRow = false; - } - void TSQLiteStatement::Prepare(PCWSTR pszQuery) - { - Close(); + 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; + } - 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())); - } + void TSQLiteStatement::Prepare(PCWSTR pszQuery) + { + Close(); - TSQLiteStatement::EStepResult TSQLiteStatement::Step() - { - m_bHasRow = false; + 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())); + } - if(!m_pStatement) - THROW_SQLITE_EXCEPTION(eErr_SQLiteStatementNotPrepared, 0, _T("Tried to step on unprepared statement")); - - int iResult = sqlite3_step(m_pStatement); - switch(iResult) + TSQLiteStatement::EStepResult TSQLiteStatement::Step() { - case SQLITE_ROW: - m_bHasRow = true; - return eStep_HasRow; - case SQLITE_OK: - case SQLITE_DONE: - Reset(); - return eStep_Finished; - default: + 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: + { const wchar_t* pszErrMsg = (const wchar_t*)sqlite3_errmsg16((sqlite3*)m_spDatabase->GetHandle()); const size_t stMaxSize = 1024; wchar_t szText[stMaxSize]; _snwprintf_s(szText, stMaxSize, _TRUNCATE, L"Cannot perform step on the statement. SQLite reported error: %s", pszErrMsg); THROW_SQLITE_EXCEPTION(eErr_SQLiteStepError, iResult, szText); } + } } - } - int TSQLiteStatement::Changes() - { - return sqlite3_changes((sqlite3*)m_spDatabase->GetHandle()); - } + int TSQLiteStatement::Changes() + { + return sqlite3_changes((sqlite3*)m_spDatabase->GetHandle()); + } - void TSQLiteStatement::BindValue(int iColumn, bool bValue) - { - BindValue(iColumn, bValue ? 1 : 0); - } + void TSQLiteStatement::BindValue(int iColumn, bool bValue) + { + BindValue(iColumn, bValue ? 1 : 0); + } - void TSQLiteStatement::BindValue(int iColumn, short siValue) - { - BindValue(iColumn, (int)siValue); - } + void TSQLiteStatement::BindValue(int iColumn, short siValue) + { + BindValue(iColumn, (int)siValue); + } - void TSQLiteStatement::BindValue(int iColumn, unsigned short usiValue) - { - BindValue(iColumn, (unsigned int)usiValue); - } + void TSQLiteStatement::BindValue(int iColumn, unsigned short usiValue) + { + BindValue(iColumn, (unsigned int)usiValue); + } - void TSQLiteStatement::BindValue(int iColumn, int iValue) - { - if(!m_pStatement) - THROW_SQLITE_EXCEPTION(eErr_SQLiteStatementNotPrepared, 0, _T("Tried to step on unprepared statement")); + 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")); - } + 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, unsigned int uiValue) - { - BindValue(iColumn, *(int*)&uiValue); - } + void TSQLiteStatement::BindValue(int iColumn, unsigned int uiValue) + { + BindValue(iColumn, *(int*)&uiValue); + } - void TSQLiteStatement::BindValue(int iColumn, long lValue) - { - BindValue(iColumn, boost::numeric_cast(lValue)); - } + void TSQLiteStatement::BindValue(int iColumn, long lValue) + { + BindValue(iColumn, boost::numeric_cast(lValue)); + } - void TSQLiteStatement::BindValue(int iColumn, unsigned long ulValue) - { - BindValue(iColumn, boost::numeric_cast(ulValue)); - } + void TSQLiteStatement::BindValue(int iColumn, unsigned long ulValue) + { + BindValue(iColumn, boost::numeric_cast(ulValue)); + } - void TSQLiteStatement::BindValue(int iColumn, long long llValue) - { - if(!m_pStatement) - THROW_SQLITE_EXCEPTION(eErr_SQLiteStatementNotPrepared, 0, _T("Tried to step on unprepared statement")); + 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")); - } + 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, unsigned long long ullValue) - { - BindValue(iColumn, *(long long*)&ullValue); - } + void TSQLiteStatement::BindValue(int iColumn, unsigned long long ullValue) + { + BindValue(iColumn, *(long long*)&ullValue); + } - void TSQLiteStatement::BindValue(int iColumn, double dValue) - { - if(!m_pStatement) - THROW_SQLITE_EXCEPTION(eErr_SQLiteStatementNotPrepared, 0, _T("Tried to step on unprepared 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")); - } + 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, PCTSTR pszText) - { - if(!m_pStatement) - THROW_SQLITE_EXCEPTION(eErr_SQLiteStatementNotPrepared, 0, _T("Tried to step on unprepared statement")); + 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")); - } + 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")); + } - void TSQLiteStatement::BindValue(int iColumn, const TString& strText) - { - BindValue(iColumn, strText.c_str()); - } + void TSQLiteStatement::BindValue(int iColumn, const TString& strText) + { + BindValue(iColumn, strText.c_str()); + } - void TSQLiteStatement::BindValue(int iColumn, const TSmartPath& path) - { - BindValue(iColumn, path.ToString()); - } + void TSQLiteStatement::BindValue(int iColumn, const TSmartPath& path) + { + BindValue(iColumn, path.ToString()); + } - bool TSQLiteStatement::GetBool(int iCol) - { - return GetInt(iCol) != 0; - } + bool TSQLiteStatement::GetBool(int iCol) + { + return GetInt(iCol) != 0; + } - short TSQLiteStatement::GetShort(int iCol) - { - return boost::numeric_cast(GetInt(iCol)); - } + short TSQLiteStatement::GetShort(int iCol) + { + return boost::numeric_cast(GetInt(iCol)); + } - unsigned short TSQLiteStatement::GetUShort(int iCol) - { - return boost::numeric_cast(GetUInt(iCol)); - } + unsigned short TSQLiteStatement::GetUShort(int iCol) + { + return boost::numeric_cast(GetUInt(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")); + 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); - } + return sqlite3_column_int(m_pStatement, iCol); + } - unsigned int TSQLiteStatement::GetUInt(int iCol) - { - int iVal = GetInt(iCol); - return *(unsigned int*)&iVal; - } + unsigned int TSQLiteStatement::GetUInt(int iCol) + { + int iVal = GetInt(iCol); + return *(unsigned int*)&iVal; + } - long TSQLiteStatement::GetLong(int iCol) - { - return boost::numeric_cast(GetInt(iCol)); - } + long TSQLiteStatement::GetLong(int iCol) + { + return boost::numeric_cast(GetInt(iCol)); + } - unsigned long TSQLiteStatement::GetULong(int iCol) - { - return boost::numeric_cast(GetUInt(iCol)); - } + unsigned long TSQLiteStatement::GetULong(int iCol) + { + return boost::numeric_cast(GetUInt(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")); + 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); - } + return sqlite3_column_int64(m_pStatement, iCol); + } - unsigned long long TSQLiteStatement::GetUInt64(int iCol) - { - long long llVal = GetInt64(iCol); - return *(unsigned long long*)&llVal; - } + unsigned long long TSQLiteStatement::GetUInt64(int iCol) + { + long long llVal = GetInt64(iCol); + return *(unsigned long long*)&llVal; + } - 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")); + 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); - } + return sqlite3_column_double(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")); + 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)); - } + return TString((const wchar_t*)sqlite3_column_text16(m_pStatement, iCol)); + } - TSmartPath TSQLiteStatement::GetPath(int iCol) - { - return PathFromWString(GetText(iCol)); - } + TSmartPath TSQLiteStatement::GetPath(int iCol) + { + return PathFromWString(GetText(iCol)); + } - void TSQLiteStatement::ClearBindings() - { - if(!m_pStatement) - THROW_SQLITE_EXCEPTION(eErr_SQLiteStatementNotPrepared, 0, _T("Tried to step on unprepared statement")); + 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")); - } + 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")); + 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")); - } + int iResult = sqlite3_reset(m_pStatement); + if (iResult != SQLITE_OK) + THROW_SQLITE_EXCEPTION(eErr_SQLiteBindError, iResult, _T("Cannot reset statement")); + } - void TSQLiteStatement::GetValue(int iCol, bool& bValue) - { - bValue = GetBool(iCol); - } + void TSQLiteStatement::GetValue(int iCol, bool& bValue) + { + bValue = GetBool(iCol); + } - void TSQLiteStatement::GetValue(int iCol, short& iValue) - { - iValue = GetShort(iCol); - } + void TSQLiteStatement::GetValue(int iCol, short& iValue) + { + iValue = GetShort(iCol); + } - void TSQLiteStatement::GetValue(int iCol, unsigned short& uiValue) - { - uiValue = GetUShort(iCol); - } + void TSQLiteStatement::GetValue(int iCol, unsigned short& uiValue) + { + uiValue = GetUShort(iCol); + } - void TSQLiteStatement::GetValue(int iCol, int& iValue) - { - iValue = GetInt(iCol); - } + void TSQLiteStatement::GetValue(int iCol, int& iValue) + { + iValue = GetInt(iCol); + } - void TSQLiteStatement::GetValue(int iCol, unsigned int& uiValue) - { - uiValue = GetUInt(iCol); - } + void TSQLiteStatement::GetValue(int iCol, unsigned int& uiValue) + { + uiValue = GetUInt(iCol); + } - void TSQLiteStatement::GetValue(int iCol, long& lValue) - { - lValue = GetLong(iCol); - } + void TSQLiteStatement::GetValue(int iCol, long& lValue) + { + lValue = GetLong(iCol); + } - void TSQLiteStatement::GetValue(int iCol, unsigned long& ulValue) - { - ulValue = GetULong(iCol); - } + void TSQLiteStatement::GetValue(int iCol, unsigned long& ulValue) + { + ulValue = GetULong(iCol); + } - void TSQLiteStatement::GetValue(int iCol, long long& llValue) - { - llValue = GetInt64(iCol); - } + void TSQLiteStatement::GetValue(int iCol, long long& llValue) + { + llValue = GetInt64(iCol); + } - void TSQLiteStatement::GetValue(int iCol, unsigned long long& ullValue) - { - ullValue = GetUInt64(iCol); - } + void TSQLiteStatement::GetValue(int iCol, unsigned long long& ullValue) + { + ullValue = GetUInt64(iCol); + } - void TSQLiteStatement::GetValue(int iCol, double& dValue) - { - dValue = GetDouble(iCol); - } + void TSQLiteStatement::GetValue(int iCol, double& dValue) + { + dValue = GetDouble(iCol); + } - void TSQLiteStatement::GetValue(int iCol, TString& strValue) - { - strValue = GetText(iCol); - } + void TSQLiteStatement::GetValue(int iCol, TString& strValue) + { + strValue = GetText(iCol); + } - void TSQLiteStatement::GetValue(int iCol, TSmartPath& pathValue) - { - pathValue = GetPath(iCol); + void TSQLiteStatement::GetValue(int iCol, TSmartPath& pathValue) + { + pathValue = GetPath(iCol); + } } } - -END_CHCORE_NAMESPACE