Index: src/libchcore/TSQLiteSerializerRowData.cpp =================================================================== diff -u -r31c4b1fc46687ed2cf35dd9fa0acec2543ae1886 -r1875711000138ff9d4185c2e3e74d455533de8a8 --- src/libchcore/TSQLiteSerializerRowData.cpp (.../TSQLiteSerializerRowData.cpp) (revision 31c4b1fc46687ed2cf35dd9fa0acec2543ae1886) +++ src/libchcore/TSQLiteSerializerRowData.cpp (.../TSQLiteSerializerRowData.cpp) (revision 1875711000138ff9d4185c2e3e74d455533de8a8) @@ -58,6 +58,78 @@ return *this; } +struct BindingVisitor : public boost::static_visitor<> +{ +private: + BindingVisitor& operator=(const BindingVisitor&); + +public: + BindingVisitor(sqlite::TSQLiteStatement& rStatement, int& rColumn) : m_rStatement(rStatement), m_rColumn(rColumn) {} + + void operator()(bool value) const + { + m_rStatement.BindValue(m_rColumn++, value); + } + + void operator()(short value) const + { + m_rStatement.BindValue(m_rColumn++, value); + } + + void operator()(unsigned short value) const + { + m_rStatement.BindValue(m_rColumn++, value); + } + + void operator()(int value) const + { + m_rStatement.BindValue(m_rColumn++, value); + } + + void operator()(unsigned int value) const + { + m_rStatement.BindValue(m_rColumn++, value); + } + + void operator()(long value) const + { + m_rStatement.BindValue(m_rColumn++, value); + } + + void operator()(unsigned long value) const + { + m_rStatement.BindValue(m_rColumn++, value); + } + + void operator()(long long value) const + { + m_rStatement.BindValue(m_rColumn++, value); + } + + void operator()(unsigned long long value) const + { + m_rStatement.BindValue(m_rColumn++, value); + } + + void operator()(double value) const + { + m_rStatement.BindValue(m_rColumn++, value); + } + + void operator()(const TString& value) const + { + m_rStatement.BindValue(m_rColumn++, value); + } + + void operator()(const TSmartPath& value) const + { + m_rStatement.BindValue(m_rColumn++, value); + } + + int& m_rColumn; + sqlite::TSQLiteStatement& m_rStatement; +}; + void TSQLiteSerializerRowData::Flush(const sqlite::TSQLiteDatabasePtr& spDatabase, const TString& strContainerName) { using namespace sqlite; @@ -72,14 +144,21 @@ { // update container_name set col1=val1, col2=val2 where id=? TString strPairs = boost::str(boost::wformat(L"UPDATE %1% SET ") % strContainerName).c_str(); + + int iColumn = 0; for(MapVariants::iterator iterVariant = m_mapValues.begin(); iterVariant != m_mapValues.end(); ++iterVariant) { strPairs += boost::str(boost::wformat(_T("%1%=?,")) % m_spColumns->GetColumnName(iterVariant->first)).c_str(); - //tStatement.BindValue(); + + boost::apply_visitor(BindingVisitor(tStatement, iColumn), iterVariant->second); } strPairs.TrimRightSelf(_T(",")); + strPairs += _T(" WHERE id=?"); + tStatement.BindValue(iColumn++, m_stRowID); + + } } Index: src/libchcore/TSQLiteSerializerRowReader.cpp =================================================================== diff -u -r9479911a096555a7504c5c8a8eaee83ecb63440c -r1875711000138ff9d4185c2e3e74d455533de8a8 --- src/libchcore/TSQLiteSerializerRowReader.cpp (.../TSQLiteSerializerRowReader.cpp) (revision 9479911a096555a7504c5c8a8eaee83ecb63440c) +++ src/libchcore/TSQLiteSerializerRowReader.cpp (.../TSQLiteSerializerRowReader.cpp) (revision 1875711000138ff9d4185c2e3e74d455533de8a8) @@ -63,79 +63,79 @@ if(!m_bInitialized) THROW_CORE_EXCEPTION(eErr_SerializeLoadError); - bValue = m_spStatement->GetInt(GetColumnIndex(strColName)) != 0; + m_spStatement->GetValue(GetColumnIndex(strColName), bValue); } void TSQLiteSerializerRowReader::GetValue(const TString& strColName, short& iValue) { if(!m_bInitialized) THROW_CORE_EXCEPTION(eErr_SerializeLoadError); - iValue = boost::numeric_cast(m_spStatement->GetInt(GetColumnIndex(strColName))); + m_spStatement->GetValue(GetColumnIndex(strColName), iValue); } void TSQLiteSerializerRowReader::GetValue(const TString& strColName, unsigned short& uiValue) { if(!m_bInitialized) THROW_CORE_EXCEPTION(eErr_SerializeLoadError); - uiValue = boost::numeric_cast(m_spStatement->GetUInt(GetColumnIndex(strColName))); + m_spStatement->GetValue(GetColumnIndex(strColName), uiValue); } void TSQLiteSerializerRowReader::GetValue(const TString& strColName, int& iValue) { if(!m_bInitialized) THROW_CORE_EXCEPTION(eErr_SerializeLoadError); - iValue = m_spStatement->GetInt(GetColumnIndex(strColName)); + m_spStatement->GetValue(GetColumnIndex(strColName), iValue); } void TSQLiteSerializerRowReader::GetValue(const TString& strColName, unsigned int& uiValue) { if(!m_bInitialized) THROW_CORE_EXCEPTION(eErr_SerializeLoadError); - uiValue = m_spStatement->GetUInt(GetColumnIndex(strColName)); + m_spStatement->GetValue(GetColumnIndex(strColName), uiValue); } void TSQLiteSerializerRowReader::GetValue(const TString& strColName, long long& llValue) { if(!m_bInitialized) THROW_CORE_EXCEPTION(eErr_SerializeLoadError); - llValue = m_spStatement->GetInt64(GetColumnIndex(strColName)); + m_spStatement->GetValue(GetColumnIndex(strColName), llValue); } void TSQLiteSerializerRowReader::GetValue(const TString& strColName, unsigned long long& ullValue) { if(!m_bInitialized) THROW_CORE_EXCEPTION(eErr_SerializeLoadError); - ullValue = m_spStatement->GetUInt64(GetColumnIndex(strColName)); + m_spStatement->GetValue(GetColumnIndex(strColName), ullValue); } void TSQLiteSerializerRowReader::GetValue(const TString& strColName, double& dValue) { if(!m_bInitialized) THROW_CORE_EXCEPTION(eErr_SerializeLoadError); - dValue = m_spStatement->GetDouble(GetColumnIndex(strColName)); + m_spStatement->GetValue(GetColumnIndex(strColName), dValue); } void TSQLiteSerializerRowReader::GetValue(const TString& strColName, TString& strValue) { if(!m_bInitialized) THROW_CORE_EXCEPTION(eErr_SerializeLoadError); - strValue = m_spStatement->GetText(GetColumnIndex(strColName)); + m_spStatement->GetValue(GetColumnIndex(strColName), strValue); } void TSQLiteSerializerRowReader::GetValue(const TString& strColName, TSmartPath& pathValue) { if(!m_bInitialized) THROW_CORE_EXCEPTION(eErr_SerializeLoadError); - pathValue = PathFromString(m_spStatement->GetText(GetColumnIndex(strColName))); + m_spStatement->GetValue(GetColumnIndex(strColName), pathValue); } int TSQLiteSerializerRowReader::GetColumnIndex(const TString& strColName) const Index: src/libchcore/TSQLiteStatement.cpp =================================================================== diff -u -rb1ecc12ba4c1f2a7b4acd6e82fc4193535e55ff0 -r1875711000138ff9d4185c2e3e74d455533de8a8 --- src/libchcore/TSQLiteStatement.cpp (.../TSQLiteStatement.cpp) (revision b1ecc12ba4c1f2a7b4acd6e82fc4193535e55ff0) +++ src/libchcore/TSQLiteStatement.cpp (.../TSQLiteStatement.cpp) (revision 1875711000138ff9d4185c2e3e74d455533de8a8) @@ -21,6 +21,7 @@ #include "sqlite3/sqlite3.h" #include "ErrorCodes.h" #include "TSQLiteException.h" +#include BEGIN_CHCORE_NAMESPACE @@ -85,16 +86,21 @@ } } - void TSQLiteStatement::BindValue(int iColumn, double dValue) + void TSQLiteStatement::BindValue(int iColumn, bool bValue) { - if(!m_pStatement) - THROW_SQLITE_EXCEPTION(eErr_SQLiteStatementNotPrepared, 0, _T("Tried to step on unprepared statement")); + BindValue(iColumn, bValue ? 1 : 0); + } - 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, short siValue) + { + BindValue(iColumn, (int)siValue); } + void TSQLiteStatement::BindValue(int iColumn, unsigned short usiValue) + { + BindValue(iColumn, (unsigned int)usiValue); + } + void TSQLiteStatement::BindValue(int iColumn, int iValue) { if(!m_pStatement) @@ -105,6 +111,21 @@ 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, 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, long long llValue) { if(!m_pStatement) @@ -115,14 +136,19 @@ THROW_SQLITE_EXCEPTION(eErr_SQLiteBindError, iResult, _T("Cannot bind a parameter")); } - void TSQLiteStatement::BindValue(int iColumn, unsigned int uiValue) + void TSQLiteStatement::BindValue(int iColumn, unsigned long long ullValue) { - BindValue(iColumn, *(int*)&uiValue); + BindValue(iColumn, *(long long*)&ullValue); } - void TSQLiteStatement::BindValue(int iColumn, unsigned long long ullValue) + void TSQLiteStatement::BindValue(int iColumn, double dValue) { - BindValue(iColumn, *(long long*)&ullValue); + 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, PCTSTR pszText) @@ -135,16 +161,32 @@ THROW_SQLITE_EXCEPTION(eErr_SQLiteBindError, iResult, _T("Cannot bind a parameter")); } - double TSQLiteStatement::GetDouble(int iCol) + void TSQLiteStatement::BindValue(int iColumn, const TString& strText) { - 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")); + BindValue(iColumn, (PCTSTR)strText); + } - return sqlite3_column_double(m_pStatement, iCol); + void TSQLiteStatement::BindValue(int iColumn, const TSmartPath& path) + { + BindValue(iColumn, path.ToString()); } + + bool TSQLiteStatement::GetBool(int iCol) + { + return GetInt(iCol) != 0; + } + + short TSQLiteStatement::GetShort(int iCol) + { + return boost::numeric_cast(GetInt(iCol)); + } + + unsigned short TSQLiteStatement::GetUShort(int iCol) + { + return boost::numeric_cast(GetUInt(iCol)); + } + int TSQLiteStatement::GetInt(int iCol) { if(!m_pStatement) @@ -155,6 +197,22 @@ return sqlite3_column_int(m_pStatement, iCol); } + 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)); + } + + unsigned long TSQLiteStatement::GetULong(int iCol) + { + return boost::numeric_cast(GetUInt(iCol)); + } + long long TSQLiteStatement::GetInt64(int iCol) { if(!m_pStatement) @@ -165,6 +223,22 @@ return sqlite3_column_int64(m_pStatement, iCol); } + 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")); + + return sqlite3_column_double(m_pStatement, iCol); + } + TString TSQLiteStatement::GetText(int iCol) { if(!m_pStatement) @@ -175,6 +249,11 @@ return TString((const wchar_t*)sqlite3_column_text16(m_pStatement, iCol)); } + TSmartPath TSQLiteStatement::GetPath(int iCol) + { + return PathFromWString(GetText(iCol)); + } + void TSQLiteStatement::ClearBindings() { if(!m_pStatement) @@ -195,17 +274,55 @@ THROW_SQLITE_EXCEPTION(eErr_SQLiteBindError, iResult, _T("Cannot reset statement")); } - unsigned int TSQLiteStatement::GetUInt(int iCol) + void TSQLiteStatement::GetValue(int iCol, bool& bValue) { - int iVal = GetInt(iCol); - return *(unsigned int*)&iVal; + bValue = GetBool(iCol); } - unsigned long long TSQLiteStatement::GetUInt64(int iCol) + void TSQLiteStatement::GetValue(int iCol, short& iValue) { - long long llVal = GetInt64(iCol); - return *(unsigned long long*)&llVal; + iValue = GetShort(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, unsigned int& uiValue) + { + uiValue = GetUInt(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, double& dValue) + { + dValue = GetDouble(iCol); + } + + void TSQLiteStatement::GetValue(int iCol, TString& strValue) + { + strValue = GetText(iCol); + } + + void TSQLiteStatement::GetValue(int iCol, TSmartPath& pathValue) + { + pathValue = GetPath(iCol); + } } END_CHCORE_NAMESPACE Index: src/libchcore/TSQLiteStatement.h =================================================================== diff -u -rb1ecc12ba4c1f2a7b4acd6e82fc4193535e55ff0 -r1875711000138ff9d4185c2e3e74d455533de8a8 --- src/libchcore/TSQLiteStatement.h (.../TSQLiteStatement.h) (revision b1ecc12ba4c1f2a7b4acd6e82fc4193535e55ff0) +++ src/libchcore/TSQLiteStatement.h (.../TSQLiteStatement.h) (revision 1875711000138ff9d4185c2e3e74d455533de8a8) @@ -48,25 +48,49 @@ void Prepare(PCTSTR pszQuery); - void BindValue(int iColumn, double dValue); + void BindValue(int iColumn, bool bValue); + void BindValue(int iColumn, short siValue); + void BindValue(int iColumn, unsigned short usiValue); void BindValue(int iColumn, int iValue); void BindValue(int iColumn, unsigned int uiValue); + void BindValue(int iColumn, long lValue); + void BindValue(int iColumn, unsigned long ulValue); void BindValue(int iColumn, long long llValue); void BindValue(int iColumn, unsigned long long ullValue); + void BindValue(int iColumn, double dValue); void BindValue(int iColumn, PCTSTR pszText); + void BindValue(int iColumn, const TString& strText); + void BindValue(int iColumn, const TSmartPath& path); void ClearBindings(); EStepResult Step(); void Reset(); - double GetDouble(int iCol); + bool GetBool(int iCol); + short GetShort(int iCol); + unsigned short GetUShort(int iCol); int GetInt(int iCol); unsigned int GetUInt(int iCol); + long GetLong(int iCol); + unsigned long GetULong(int iCol); long long GetInt64(int iCol); unsigned long long GetUInt64(int iCol); + double GetDouble(int iCol); TString GetText(int iCol); + TSmartPath GetPath(int iCol); + void GetValue(int iCol, bool& bValue); + void GetValue(int iCol, short& iValue); + void GetValue(int iCol, unsigned short& uiValue); + void GetValue(int iCol, int& iValue); + void GetValue(int iCol, unsigned int& uiValue); + void GetValue(int iCol, long long& llValue); + void GetValue(int iCol, unsigned long long& ullValue); + void GetValue(int iCol, double& dValue); + void GetValue(int iCol, TString& strValue); + void GetValue(int iCol, TSmartPath& pathValue); + private: sqlite3_stmt* m_pStatement; TSQLiteDatabasePtr m_spDatabase;