/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#pragma once

#include "storagebackend.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <util/sll/util.h>

namespace LeechCraft
{
namespace Poshuku
{
	class SQLStorageBackend : public StorageBackend
	{
		Q_OBJECT

		const Type Type_;

		QSqlDatabase DB_;
		const Util::DefaultScopeGuard DBGuard_;

				/** Returns:
					* - title
					* - date
					* - url
					*/
		mutable QSqlQuery HistoryLoader_,
				/** Binds:
					* - titlebase
					* - urlbase
					*
					* Returns:
					* - title
					* - url
					*/
				HistoryRatedLoader_,
				/** Binds:
					* - date
					* - title
					* - url
					*/
				HistoryAdder_,
				/** Binds:
					* - age
					*/
				HistoryEraser_,
				/** Binds:
					* - items
					*/
				HistoryTruncater_,
				/** Returns:
					* - title
					* - url
					* - tags
					*/
				FavoritesLoader_,
				/** Binds:
					* - title
					* - url
					* - tags
					*/
				FavoritesAdder_,
				/** Binds:
					* - title
					* - url
					* - tags
					*/
				FavoritesUpdater_,
				/** Binds:
					* - url
					*/
				FavoritesRemover_,
				/** Binds:
					* - url
					*/
				FormsIgnoreSetter_,
				/** Binds:
					* - url
					*/
				FormsIgnoreGetter_,
				/** Binds:
					* - url
					*/
				FormsIgnoreClearer_;
	public:
		SQLStorageBackend (Type);
		virtual ~SQLStorageBackend ();

		void Prepare ();

		virtual void LoadHistory (history_items_t&) const;
		virtual void LoadResemblingHistory (const QString&,
				history_items_t&) const;
		virtual void AddToHistory (const HistoryItem&);
		virtual void ClearOldHistory (int, int);
		virtual void LoadFavorites (FavoritesModel::items_t&) const;
		virtual void AddToFavorites (const FavoritesModel::FavoritesItem&);
		virtual void RemoveFromFavorites (const FavoritesModel::FavoritesItem&);
		virtual void UpdateFavorites (const FavoritesModel::FavoritesItem&);
		virtual void SetFormsIgnored (const QString&, bool);
		virtual bool GetFormsIgnored (const QString&) const;
	private:
		void InitializeTables ();
		void CheckVersions ();
		QString GetSetting (const QString&) const;
		void SetSetting (const QString&, const QString&);
	};
}
}
