Index: src/libchcore/TSQLiteSerializerRowReader.cpp =================================================================== diff -u -N -re8f31b0f922b402878356e130c866c4f3682a7f5 -ra4635addad389b9e117679437a3e1b64a739ea96 --- src/libchcore/TSQLiteSerializerRowReader.cpp (.../TSQLiteSerializerRowReader.cpp) (revision e8f31b0f922b402878356e130c866c4f3682a7f5) +++ src/libchcore/TSQLiteSerializerRowReader.cpp (.../TSQLiteSerializerRowReader.cpp) (revision a4635addad389b9e117679437a3e1b64a739ea96) @@ -1,167 +1,167 @@ -// ============================================================================ -// 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 "TSQLiteSerializerRowReader.h" -#include -#include "TCoreException.h" -#include "ErrorCodes.h" -#include -#include "TSQLiteStatement.h" -#include "SerializerTrace.h" - -namespace chcore -{ - TSQLiteSerializerRowReader::TSQLiteSerializerRowReader(const sqlite::TSQLiteDatabasePtr& spDatabase, TSQLiteColumnsDefinition& rColumns, const TString& strContainerName) : - m_spStatement(new sqlite::TSQLiteStatement(spDatabase)), - m_rColumns(rColumns), - m_bInitialized(false), - m_strContainerName(strContainerName) - { - if (m_strContainerName.IsEmpty()) - throw TCoreException(eErr_InvalidArgument, L"m_strContainerName", LOCATION); - } - - TSQLiteSerializerRowReader::~TSQLiteSerializerRowReader() - { - } - - bool TSQLiteSerializerRowReader::Next() - { - if (m_rColumns.IsEmpty()) - throw TCoreException(eErr_SerializeLoadError, L"m_rColumns is empty", LOCATION); - - if (!m_bInitialized) - { - // generate query to retrieve data from db - TString strQuery; - strQuery = boost::str(boost::wformat(L"SELECT %1% FROM %2% ORDER BY id") % m_rColumns.GetCommaSeparatedColumns().c_str() % m_strContainerName.c_str()).c_str(); - - DBTRACE1_D(_T("Executing query: %s\n"), strQuery.c_str()); - m_spStatement->Prepare(strQuery.c_str()); - m_bInitialized = true; - } - - return m_spStatement->Step() == sqlite::TSQLiteStatement::eStep_HasRow; - } - - void TSQLiteSerializerRowReader::GetValue(const TString& strColName, bool& bValue) - { - if (!m_bInitialized) - throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); - - m_spStatement->GetValue(GetColumnIndex(strColName), bValue); - } - - void TSQLiteSerializerRowReader::GetValue(const TString& strColName, short& iValue) - { - if (!m_bInitialized) - throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); - - m_spStatement->GetValue(GetColumnIndex(strColName), iValue); - } - - void TSQLiteSerializerRowReader::GetValue(const TString& strColName, unsigned short& uiValue) - { - if (!m_bInitialized) - throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); - - m_spStatement->GetValue(GetColumnIndex(strColName), uiValue); - } - - void TSQLiteSerializerRowReader::GetValue(const TString& strColName, int& iValue) - { - if (!m_bInitialized) - throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); - - m_spStatement->GetValue(GetColumnIndex(strColName), iValue); - } - - void TSQLiteSerializerRowReader::GetValue(const TString& strColName, unsigned int& uiValue) - { - if (!m_bInitialized) - throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); - - m_spStatement->GetValue(GetColumnIndex(strColName), uiValue); - } - - void TSQLiteSerializerRowReader::GetValue(const TString& strColName, long& lValue) - { - if (!m_bInitialized) - throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); - - m_spStatement->GetValue(GetColumnIndex(strColName), lValue); - } - - void TSQLiteSerializerRowReader::GetValue(const TString& strColName, unsigned long& ulValue) - { - if (!m_bInitialized) - throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); - - m_spStatement->GetValue(GetColumnIndex(strColName), ulValue); - } - - void TSQLiteSerializerRowReader::GetValue(const TString& strColName, long long& llValue) - { - if (!m_bInitialized) - throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); - - m_spStatement->GetValue(GetColumnIndex(strColName), llValue); - } - - void TSQLiteSerializerRowReader::GetValue(const TString& strColName, unsigned long long& ullValue) - { - if (!m_bInitialized) - throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); - - m_spStatement->GetValue(GetColumnIndex(strColName), ullValue); - } - - void TSQLiteSerializerRowReader::GetValue(const TString& strColName, double& dValue) - { - if (!m_bInitialized) - throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); - - m_spStatement->GetValue(GetColumnIndex(strColName), dValue); - } - - void TSQLiteSerializerRowReader::GetValue(const TString& strColName, TString& strValue) - { - if (!m_bInitialized) - throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); - - m_spStatement->GetValue(GetColumnIndex(strColName), strValue); - } - - void TSQLiteSerializerRowReader::GetValue(const TString& strColName, TSmartPath& pathValue) - { - if (!m_bInitialized) - throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); - - m_spStatement->GetValue(GetColumnIndex(strColName), pathValue); - } - - int TSQLiteSerializerRowReader::GetColumnIndex(const TString& strColName) const - { - if (!m_bInitialized) - throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); - - size_t stColumn = m_rColumns.GetColumnIndex(strColName.c_str()); - return boost::numeric_cast(stColumn); - } -} +// ============================================================================ +// 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 "TSQLiteSerializerRowReader.h" +#include +#include "TCoreException.h" +#include "ErrorCodes.h" +#include +#include "TSQLiteStatement.h" +#include "SerializerTrace.h" + +namespace chcore +{ + TSQLiteSerializerRowReader::TSQLiteSerializerRowReader(const sqlite::TSQLiteDatabasePtr& spDatabase, TSQLiteColumnsDefinition& rColumns, const TString& strContainerName) : + m_spStatement(new sqlite::TSQLiteStatement(spDatabase)), + m_rColumns(rColumns), + m_bInitialized(false), + m_strContainerName(strContainerName) + { + if (m_strContainerName.IsEmpty()) + throw TCoreException(eErr_InvalidArgument, L"m_strContainerName", LOCATION); + } + + TSQLiteSerializerRowReader::~TSQLiteSerializerRowReader() + { + } + + bool TSQLiteSerializerRowReader::Next() + { + if (m_rColumns.IsEmpty()) + throw TCoreException(eErr_SerializeLoadError, L"m_rColumns is empty", LOCATION); + + if (!m_bInitialized) + { + // generate query to retrieve data from db + TString strQuery; + strQuery = boost::str(boost::wformat(L"SELECT %1% FROM %2% ORDER BY id") % m_rColumns.GetCommaSeparatedColumns().c_str() % m_strContainerName.c_str()).c_str(); + + DBTRACE1_D(_T("Executing query: %s\n"), strQuery.c_str()); + m_spStatement->Prepare(strQuery.c_str()); + m_bInitialized = true; + } + + return m_spStatement->Step() == sqlite::TSQLiteStatement::eStep_HasRow; + } + + void TSQLiteSerializerRowReader::GetValue(const TString& strColName, bool& bValue) + { + if (!m_bInitialized) + throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); + + m_spStatement->GetValue(GetColumnIndex(strColName), bValue); + } + + void TSQLiteSerializerRowReader::GetValue(const TString& strColName, short& iValue) + { + if (!m_bInitialized) + throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); + + m_spStatement->GetValue(GetColumnIndex(strColName), iValue); + } + + void TSQLiteSerializerRowReader::GetValue(const TString& strColName, unsigned short& uiValue) + { + if (!m_bInitialized) + throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); + + m_spStatement->GetValue(GetColumnIndex(strColName), uiValue); + } + + void TSQLiteSerializerRowReader::GetValue(const TString& strColName, int& iValue) + { + if (!m_bInitialized) + throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); + + m_spStatement->GetValue(GetColumnIndex(strColName), iValue); + } + + void TSQLiteSerializerRowReader::GetValue(const TString& strColName, unsigned int& uiValue) + { + if (!m_bInitialized) + throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); + + m_spStatement->GetValue(GetColumnIndex(strColName), uiValue); + } + + void TSQLiteSerializerRowReader::GetValue(const TString& strColName, long& lValue) + { + if (!m_bInitialized) + throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); + + m_spStatement->GetValue(GetColumnIndex(strColName), lValue); + } + + void TSQLiteSerializerRowReader::GetValue(const TString& strColName, unsigned long& ulValue) + { + if (!m_bInitialized) + throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); + + m_spStatement->GetValue(GetColumnIndex(strColName), ulValue); + } + + void TSQLiteSerializerRowReader::GetValue(const TString& strColName, long long& llValue) + { + if (!m_bInitialized) + throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); + + m_spStatement->GetValue(GetColumnIndex(strColName), llValue); + } + + void TSQLiteSerializerRowReader::GetValue(const TString& strColName, unsigned long long& ullValue) + { + if (!m_bInitialized) + throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); + + m_spStatement->GetValue(GetColumnIndex(strColName), ullValue); + } + + void TSQLiteSerializerRowReader::GetValue(const TString& strColName, double& dValue) + { + if (!m_bInitialized) + throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); + + m_spStatement->GetValue(GetColumnIndex(strColName), dValue); + } + + void TSQLiteSerializerRowReader::GetValue(const TString& strColName, TString& strValue) + { + if (!m_bInitialized) + throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); + + m_spStatement->GetValue(GetColumnIndex(strColName), strValue); + } + + void TSQLiteSerializerRowReader::GetValue(const TString& strColName, TSmartPath& pathValue) + { + if (!m_bInitialized) + throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); + + m_spStatement->GetValue(GetColumnIndex(strColName), pathValue); + } + + int TSQLiteSerializerRowReader::GetColumnIndex(const TString& strColName) const + { + if (!m_bInitialized) + throw TCoreException(eErr_SerializeLoadError, L"Serializer not initialized", LOCATION); + + size_t stColumn = m_rColumns.GetColumnIndex(strColName.c_str()); + return boost::numeric_cast(stColumn); + } +}