Index: src/libchcore/ISerializerContainer.h =================================================================== diff -u -r9479911a096555a7504c5c8a8eaee83ecb63440c -r31c4b1fc46687ed2cf35dd9fa0acec2543ae1886 --- src/libchcore/ISerializerContainer.h (.../ISerializerContainer.h) (revision 9479911a096555a7504c5c8a8eaee83ecb63440c) +++ src/libchcore/ISerializerContainer.h (.../ISerializerContainer.h) (revision 31c4b1fc46687ed2cf35dd9fa0acec2543ae1886) @@ -25,8 +25,8 @@ BEGIN_CHCORE_NAMESPACE -class ISerializerRowWriter; -typedef boost::shared_ptr ISerializerRowWriterPtr; +class ISerializerRowData; +typedef boost::shared_ptr ISerializerRowDataPtr; class LIBCHCORE_API ISerializerContainer { @@ -37,8 +37,8 @@ virtual IColumnsDefinitionPtr GetColumnsDefinition() const = 0; // prepare data to be stored - virtual ISerializerRowWriterPtr AddRow(size_t stRowID) = 0; - virtual ISerializerRowWriterPtr GetRow(size_t stRowID) = 0; + virtual ISerializerRowDataPtr AddRow(size_t stRowID) = 0; + virtual ISerializerRowDataPtr GetRow(size_t stRowID) = 0; virtual void DeleteRow(size_t stRowID) = 0; // getting data from the serialized archive Fisheye: tag 9479911a096555a7504c5c8a8eaee83ecb63440c is not in file src/libchcore/ISerializerRowData.cpp Fisheye: Tag 31c4b1fc46687ed2cf35dd9fa0acec2543ae1886 refers to a dead (removed) revision in file `src/libchcore/ISerializerRowWriter.cpp'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: tag 9479911a096555a7504c5c8a8eaee83ecb63440c is not in file src/libchcore/ISerializerRowData.h Fisheye: Tag 31c4b1fc46687ed2cf35dd9fa0acec2543ae1886 refers to a dead (removed) revision in file `src/libchcore/ISerializerRowWriter.h'. Fisheye: No comparison available. Pass `N' to diff? Index: src/libchcore/TRowData.h =================================================================== diff -u -r9479911a096555a7504c5c8a8eaee83ecb63440c -r31c4b1fc46687ed2cf35dd9fa0acec2543ae1886 --- src/libchcore/TRowData.h (.../TRowData.h) (revision 9479911a096555a7504c5c8a8eaee83ecb63440c) +++ src/libchcore/TRowData.h (.../TRowData.h) (revision 31c4b1fc46687ed2cf35dd9fa0acec2543ae1886) @@ -69,7 +69,7 @@ InternalVariant m_varValue; #pragma warning(pop) - friend class TSQLiteSerializerRowWriter; + friend class TSQLiteSerializerRowData; }; typedef boost::shared_ptr TRowDataPtr; Index: src/libchcore/TSQLiteSerializer.cpp =================================================================== diff -u -r0c5027d5173ab0daeba6aa6b735a2b11d4cd2164 -r31c4b1fc46687ed2cf35dd9fa0acec2543ae1886 --- src/libchcore/TSQLiteSerializer.cpp (.../TSQLiteSerializer.cpp) (revision 0c5027d5173ab0daeba6aa6b735a2b11d4cd2164) +++ src/libchcore/TSQLiteSerializer.cpp (.../TSQLiteSerializer.cpp) (revision 31c4b1fc46687ed2cf35dd9fa0acec2543ae1886) @@ -21,6 +21,7 @@ #include "TSQLiteSerializerContainer.h" #include "TCoreException.h" #include "ErrorCodes.h" +#include "TSQLiteTransaction.h" BEGIN_CHCORE_NAMESPACE @@ -54,7 +55,15 @@ void TSQLiteSerializer::Flush() { - // TODO: generate the necessary queries and execute them + TSQLiteTransaction tran(m_spDatabase); + + + for(ContainerMap::iterator iterContainer = m_mapContainers.begin(); iterContainer != m_mapContainers.end(); ++iterContainer) + { + iterContainer->second->Flush(); + } + + tran.Commit(); } END_CHCORE_NAMESPACE Index: src/libchcore/TSQLiteSerializerContainer.cpp =================================================================== diff -u -r0c5027d5173ab0daeba6aa6b735a2b11d4cd2164 -r31c4b1fc46687ed2cf35dd9fa0acec2543ae1886 --- src/libchcore/TSQLiteSerializerContainer.cpp (.../TSQLiteSerializerContainer.cpp) (revision 0c5027d5173ab0daeba6aa6b735a2b11d4cd2164) +++ src/libchcore/TSQLiteSerializerContainer.cpp (.../TSQLiteSerializerContainer.cpp) (revision 31c4b1fc46687ed2cf35dd9fa0acec2543ae1886) @@ -18,7 +18,7 @@ // ============================================================================ #include "stdafx.h" #include "TSQLiteSerializerContainer.h" -#include "TSQLiteSerializerRowWriter.h" +#include "TSQLiteSerializerRowData.h" #include "ErrorCodes.h" #include "TCoreException.h" #include @@ -27,6 +27,8 @@ BEGIN_CHCORE_NAMESPACE +using namespace sqlite; + TSQLiteSerializerContainer::TSQLiteSerializerContainer(const TString& strName, const sqlite::TSQLiteDatabasePtr& spDB) : m_spColumns(new TSQLiteColumnsDefinition), m_strName(strName), @@ -46,19 +48,19 @@ { } -chcore::ISerializerRowWriterPtr TSQLiteSerializerContainer::AddRow(size_t stRowID) +chcore::ISerializerRowDataPtr TSQLiteSerializerContainer::AddRow(size_t stRowID) { RowMap::iterator iterInsert = m_mapRows.insert( - std::make_pair(stRowID, TSQLiteSerializerRowWriterPtr(new TSQLiteSerializerRowWriter(stRowID, m_spColumns, true))) + std::make_pair(stRowID, TSQLiteSerializerRowDataPtr(new TSQLiteSerializerRowData(stRowID, m_spColumns, true))) ).first; return (*iterInsert).second; } -ISerializerRowWriterPtr TSQLiteSerializerContainer::GetRow(size_t stRowID) +ISerializerRowDataPtr TSQLiteSerializerContainer::GetRow(size_t stRowID) { RowMap::iterator iterFnd = m_mapRows.find(stRowID); if(iterFnd == m_mapRows.end()) - iterFnd = m_mapRows.insert(std::make_pair(stRowID, TSQLiteSerializerRowWriterPtr(new TSQLiteSerializerRowWriter(stRowID, m_spColumns, false)))).first; + iterFnd = m_mapRows.insert(std::make_pair(stRowID, TSQLiteSerializerRowDataPtr(new TSQLiteSerializerRowData(stRowID, m_spColumns, false)))).first; return (*iterFnd).second; } @@ -86,4 +88,39 @@ return m_spColumns; } +void TSQLiteSerializerContainer::Flush() +{ + FlushDeletions(); + + for(RowMap::iterator iterRows = m_mapRows.begin(); iterRows != m_mapRows.end(); ++iterRows) + { + iterRows->second->Flush(m_spDB, m_strName); + } +} + +void TSQLiteSerializerContainer::FlushDeletions() +{ + // delete from m_strName WHERE id IN (???) + TSQLiteStatement tStatement(m_spDB); + + const size_t stMaxToRemoveAtOnce = 10; + + // delete items in chunks + std::set::const_iterator iterToDelete = m_setDeleteItems.begin(); + while(iterToDelete != m_setDeleteItems.end()) + { + TString strItemsToRemove; + size_t stToRemove = stMaxToRemoveAtOnce; + while(iterToDelete != m_setDeleteItems.end() && (--stToRemove) != 0) + { + strItemsToRemove += boost::str(boost::wformat(L"%1%,") % *iterToDelete).c_str(); + } + strItemsToRemove.TrimRightSelf(_T(",")); + + TString strQuery = boost::str(boost::wformat(L"DELETE FROM %1% WHERE id IN (%2%)") % m_strName % strItemsToRemove).c_str(); + tStatement.Prepare(strQuery); + tStatement.Step(); + } +} + END_CHCORE_NAMESPACE Index: src/libchcore/TSQLiteSerializerContainer.h =================================================================== diff -u -r0c5027d5173ab0daeba6aa6b735a2b11d4cd2164 -r31c4b1fc46687ed2cf35dd9fa0acec2543ae1886 --- src/libchcore/TSQLiteSerializerContainer.h (.../TSQLiteSerializerContainer.h) (revision 0c5027d5173ab0daeba6aa6b735a2b11d4cd2164) +++ src/libchcore/TSQLiteSerializerContainer.h (.../TSQLiteSerializerContainer.h) (revision 31c4b1fc46687ed2cf35dd9fa0acec2543ae1886) @@ -26,7 +26,7 @@ #include #include "TSQLiteColumnDefinition.h" #include "TSQLiteDatabase.h" -#include "TSQLiteSerializerRowWriter.h" +#include "TSQLiteSerializerRowData.h" BEGIN_CHCORE_NAMESPACE @@ -40,18 +40,23 @@ virtual IColumnsDefinitionPtr GetColumnsDefinition() const; - virtual ISerializerRowWriterPtr AddRow(size_t stRowID); - virtual ISerializerRowWriterPtr GetRow(size_t stRowID); + virtual ISerializerRowDataPtr AddRow(size_t stRowID); + virtual ISerializerRowDataPtr GetRow(size_t stRowID); virtual void DeleteRow(size_t stRowID); virtual ISerializerRowReaderPtr GetRowReader(); + void Flush(); + private: + void FlushDeletions(); + +private: #pragma warning(push) #pragma warning(disable: 4251) boost::optional m_stParentID; - typedef std::map RowMap; // maps row id to row data + typedef std::map RowMap; // maps row id to row data RowMap m_mapRows; TSQLiteColumnDefinitionPtr m_spColumns; Index: src/libchcore/TSQLiteSerializerRowData.cpp =================================================================== diff -u --- src/libchcore/TSQLiteSerializerRowData.cpp (revision 0) +++ src/libchcore/TSQLiteSerializerRowData.cpp (revision 31c4b1fc46687ed2cf35dd9fa0acec2543ae1886) @@ -0,0 +1,86 @@ +// ============================================================================ +// 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 "TSQLiteSerializerRowData.h" +#include "TSQLiteStatement.h" +#include + +BEGIN_CHCORE_NAMESPACE + +TSQLiteSerializerRowData::TSQLiteSerializerRowData(size_t stRowID, const TSQLiteColumnDefinitionPtr& spColumnDefinition, bool bAdded) : + m_stRowID(stRowID), + m_spColumns(spColumnDefinition), + m_bAdded(bAdded) +{ +} + +TSQLiteSerializerRowData::~TSQLiteSerializerRowData() +{ +} + +ISerializerRowData& TSQLiteSerializerRowData::operator%(const TRowData& rData) +{ + size_t stColumn = m_spColumns->GetColumnIndex(rData.m_strColName); + std::map::iterator iterFnd = m_mapValues.find(stColumn); + if(iterFnd == m_mapValues.end()) + m_mapValues.insert(std::make_pair(stColumn, rData.m_varValue)); + else + (*iterFnd).second = rData.m_varValue; + + return *this; +} + +ISerializerRowData& TSQLiteSerializerRowData::SetValue(const TRowData& rData) +{ + size_t stColumn = m_spColumns->GetColumnIndex(rData.m_strColName); + std::map::iterator iterFnd = m_mapValues.find(stColumn); + if(iterFnd == m_mapValues.end()) + m_mapValues.insert(std::make_pair(stColumn, rData.m_varValue)); + else + (*iterFnd).second = rData.m_varValue; + + return *this; +} + +void TSQLiteSerializerRowData::Flush(const sqlite::TSQLiteDatabasePtr& spDatabase, const TString& strContainerName) +{ + using namespace sqlite; + + TSQLiteStatement tStatement(spDatabase); + + if(m_bAdded) + { + // insert into container_name(col1, col2) values(val1, val2) + } + else + { + // update container_name set col1=val1, col2=val2 where id=? + TString strPairs = boost::str(boost::wformat(L"UPDATE %1% SET ") % strContainerName).c_str(); + 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(); + } + + strPairs.TrimRightSelf(_T(",")); + } +} + +END_CHCORE_NAMESPACE Index: src/libchcore/TSQLiteSerializerRowData.h =================================================================== diff -u --- src/libchcore/TSQLiteSerializerRowData.h (revision 0) +++ src/libchcore/TSQLiteSerializerRowData.h (revision 31c4b1fc46687ed2cf35dd9fa0acec2543ae1886) @@ -0,0 +1,58 @@ +// ============================================================================ +// Copyright (C) 2001-2014 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 __TSQLITESERIALIZERROWWRITER_H__ +#define __TSQLITESERIALIZERROWWRITER_H__ + +#include "libchcore.h" +#include "ISerializerRowData.h" +#include "TSQLiteColumnDefinition.h" +#include "ISerializerContainer.h" +#include "TRowData.h" +#include "TSQLiteDatabase.h" + +BEGIN_CHCORE_NAMESPACE + +class LIBCHCORE_API TSQLiteSerializerRowData : public ISerializerRowData +{ +public: + TSQLiteSerializerRowData(size_t stRowID, const TSQLiteColumnDefinitionPtr& spColumnDefinition, bool bAdded); + virtual ~TSQLiteSerializerRowData(); + + virtual ISerializerRowData& operator%(const TRowData& rData); + virtual ISerializerRowData& SetValue(const TRowData& rData); + + void Flush(const sqlite::TSQLiteDatabasePtr& spDatabase, const TString& strContainerName); + +private: + size_t m_stRowID; + bool m_bAdded; +#pragma warning(push) +#pragma warning(disable: 4251) + TSQLiteColumnDefinitionPtr m_spColumns; + + typedef std::map MapVariants; // column id -> variant data + MapVariants m_mapValues; +#pragma warning(pop) +}; + +typedef boost::shared_ptr TSQLiteSerializerRowDataPtr; + +END_CHCORE_NAMESPACE + +#endif Index: src/libchcore/TTaskInfo.cpp =================================================================== diff -u -r0c5027d5173ab0daeba6aa6b735a2b11d4cd2164 -r31c4b1fc46687ed2cf35dd9fa0acec2543ae1886 --- src/libchcore/TTaskInfo.cpp (.../TTaskInfo.cpp) (revision 0c5027d5173ab0daeba6aa6b735a2b11d4cd2164) +++ src/libchcore/TTaskInfo.cpp (.../TTaskInfo.cpp) (revision 31c4b1fc46687ed2cf35dd9fa0acec2543ae1886) @@ -20,7 +20,7 @@ #include "TTaskInfo.h" #include "TCoreException.h" #include "TRowData.h" -#include "ISerializerRowWriter.h" +#include "ISerializerRowData.h" #include "ISerializerRowReader.h" BEGIN_CHCORE_NAMESPACE @@ -78,14 +78,14 @@ if(IsAdded()) { - ISerializerRowWriterPtr spRow = spContainer->AddRow(GetObjectID()); + ISerializerRowDataPtr spRow = spContainer->AddRow(GetObjectID()); *spRow % TRowData(_T("path"), m_pathSerializeLocation) % TRowData(_T("task_order"), m_iOrder); } else { - ISerializerRowWriterPtr spRow = spContainer->GetRow(GetObjectID()); + ISerializerRowDataPtr spRow = spContainer->GetRow(GetObjectID()); if(GetModifications() & eMod_TaskPath) *spRow % TRowData(_T("path"), m_pathSerializeLocation); else if(GetModifications() & eMod_Order) Index: src/libchcore/libchcore.vc90.vcproj =================================================================== diff -u -r0c5027d5173ab0daeba6aa6b735a2b11d4cd2164 -r31c4b1fc46687ed2cf35dd9fa0acec2543ae1886 --- src/libchcore/libchcore.vc90.vcproj (.../libchcore.vc90.vcproj) (revision 0c5027d5173ab0daeba6aa6b735a2b11d4cd2164) +++ src/libchcore/libchcore.vc90.vcproj (.../libchcore.vc90.vcproj) (revision 31c4b1fc46687ed2cf35dd9fa0acec2543ae1886) @@ -1308,19 +1308,19 @@ >