aboutsummaryrefslogtreecommitdiffstats
path: root/src/base/database.hpp
blob: 2a320f4594f229c5265f6654e3bef304a139c5ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * UltraStar Deluxe - Karaoke Game
 *
 * UltraStar Deluxe is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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 General Public License
 * along with this program; see the file COPYING. If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 */

#ifndef DATABASE_HPP
#define DATABASE_HPP

#include <string>
#include <sqlite3.h>
#include <log4cxx/logger.h>

namespace usdx
{
	/**
	 * Abstract base class for all sqlite databases.
	 */
	class Database
	{
	private:
		static log4cxx::LoggerPtr log;

	protected:
		/**
		 * Internal reference to the sqlite database handle of the open
		 * sqlite database.
		 */
		sqlite3 *database;

		Database(std::string filename);
		virtual ~Database(void);

	public:
		/**
		 * Wrapper around the sqlite_prepare_v2 function with propper
		 * logging and exception throwing on error.
		 *
		 * @param sqlStatement SQL Statement for preparing to
		 * 	sqlite3_stmt
		 * @return Pointer to a sqlite3_stmt used for binding
		 * 	parameters and executing the statement. Need to be freed
		 * 	with sqlite3_finalize.
		 */
		sqlite3_stmt *sqlite_prepare(const std::wstring sqlStatement);

		/**
		 * Just a quick alias for sqlite_prepare, sqlite3_step and
		 * sqlite3_finalize.
		 */
		void sqlite_exec(const std::wstring sqlStatement);

		/**
		 * Check if the given table exists in the database.
		 *
		 * @param table Name to check if exists
		 * @return true, if table exists, false if not
		 */
		const bool sqlite_table_exists(const std::wstring table);

		/**
		 * Check if the given table has the given column by name.
		 *
		 * @param table Table to examine
		 * @param column Name of the column to check if exists
		 * @return true, if column exists in that table, false if not
		 */
		const bool sqlite_table_contains_column(const std::wstring table, const std::wstring column);

		/**
		 * Queries the user version from the sqlite database. This is a
		 * free settable additional field to identify the version of the
		 * schemata in the database.
		 *
		 * @see set_version(const int version)
		 * @return Value of the user_version setting of the sqlite
		 * 	database
		 */
		const int get_version(void);

		/**
		 * Set the user version of the database.
		 *
		 * @see get_version(void)
		 * @param version Current scheme version.
		 */
		void set_version(const int version);
	};
};

#endif