// ============================================================================ // 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. // ============================================================================ #include "stdafx.h" #include "TSQLiteSerializerFactory.h" #include #include #include #include #include "TSQLiteTaskSchema.h" #include "TSQLiteSerializer.h" #include "TSQLiteTaskManagerSchema.h" #include "TCoreWin32Exception.h" #include "ErrorCodes.h" namespace chcore { TSQLiteSerializerFactory::TSQLiteSerializerFactory(const TSmartPath& pathSerializeDir) : m_pathSerializeDir(pathSerializeDir) { } TSQLiteSerializerFactory::~TSQLiteSerializerFactory() { } ISerializerPtr TSQLiteSerializerFactory::CreateTaskManagerSerializer(bool bForceRecreate) { TSmartPath pathTaskManager = m_pathSerializeDir + PathFromString(_T("tasks.sqlite")); if (bForceRecreate) { if (!DeleteFile(pathTaskManager.ToString())) { DWORD dwLastError = GetLastError(); if (dwLastError != ERROR_FILE_NOT_FOUND) throw TCoreWin32Exception(eErr_CannotDeleteFile, dwLastError, L"Cannot delete tasks.sqlite file", LOCATION); } } TSQLiteSerializerPtr spSerializer(std::make_shared( pathTaskManager, std::make_shared())); return spSerializer; } ISerializerPtr TSQLiteSerializerFactory::CreateTaskSerializer(const TString& strNameHint, bool bForceRecreate) { TString strName(strNameHint); if (strName.IsEmpty()) { boost::uuids::random_generator gen; boost::uuids::uuid u = gen(); strName = boost::lexical_cast(u).c_str(); } TSmartPath pathTask = PathFromWString(strName); if (!pathTask.HasFileRoot()) { if (!strName.EndsWithNoCase(_T(".sqlite"))) strName += _T(".sqlite"); pathTask = m_pathSerializeDir; pathTask += PathFromWString(strName); } if (bForceRecreate) { if (!DeleteFile(pathTask.ToString())) { DWORD dwLastError = GetLastError(); if (dwLastError != ERROR_FILE_NOT_FOUND) throw TCoreWin32Exception(eErr_CannotDeleteFile, dwLastError, L"Cannot delete task database file", LOCATION); } } TSQLiteSerializerPtr spSerializer(std::make_shared(pathTask, std::make_shared())); return spSerializer; } }