1 /*
2  *  Copyright 2005-2014 Fabrice Colin
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18 
19 #ifndef _SQLITE_BASE_H
20 #define _SQLITE_BASE_H
21 
22 #include <string>
23 #include <map>
24 #include <vector>
25 #include <utility>
26 
27 #include <sqlite3.h>
28 
29 #include "SQLDB.h"
30 
31 /// A row of results.
32 class SQLiteRow : public SQLRow
33 {
34 	public:
35 		SQLiteRow(const std::vector<std::string> &rowColumns, unsigned int nColumns);
36 		SQLiteRow(sqlite3_stmt *pStatement, unsigned int nColumns);
37 		virtual ~SQLiteRow();
38 
39 		virtual std::string getColumn(unsigned int nColumn) const;
40 
41 	protected:
42 		std::vector<std::string> m_columns;
43 		sqlite3_stmt *m_pStatement;
44 
45 	private:
46 		SQLiteRow(const SQLiteRow &other);
47 		SQLiteRow &operator=(const SQLiteRow &other);
48 
49 };
50 
51 /// Results extracted from a SQLite database.
52 class SQLiteResults : public SQLResults
53 {
54 	public:
55 		SQLiteResults(char **results, unsigned long nRows, unsigned int nColumns);
56 		SQLiteResults(sqlite3_stmt *pStatement);
57 		virtual ~SQLiteResults();
58 
59 		int getStepCode(void) const;
60 
61 		virtual bool hasMoreRows(void) const;
62 
63 		virtual std::string getColumnName(unsigned int nColumn) const;
64 
65 		virtual SQLRow *nextRow(void);
66 
67 		virtual bool rewind(void);
68 
69 	protected:
70 		char **m_results;
71 		sqlite3_stmt *m_pStatement;
72 		bool m_done;
73 		bool m_firstStep;
74 		int m_stepCode;
75 
76 		void step(void);
77 
78 	private:
79 		SQLiteResults(const SQLiteResults &other);
80 		SQLiteResults &operator=(const SQLiteResults &other);
81 
82 };
83 
84 /// Simple C++ wrapper around the SQLite API.
85 class SQLiteBase : public SQLDB
86 {
87 	public:
88 		SQLiteBase(const std::string &databaseName,
89 			bool readOnly = false, bool onDemand = true);
90 		virtual ~SQLiteBase();
91 
92 		static bool check(const std::string &databaseName);
93 
94 		bool backup(const std::string &destDatabaseName,
95 			int pagesCount = 5, bool retryOnLock = true);
96 
97 		virtual bool isOpen(void) const;
98 
99 		virtual bool reopen(const std::string &databaseName);
100 
101 		virtual bool alterTable(const std::string &tableName,
102 			const std::string &columns,
103 			const std::string &newDefinition);
104 
105 		virtual bool beginTransaction(void);
106 
107 		virtual bool rollbackTransaction(void);
108 
109 		virtual bool endTransaction(void);
110 
111 		virtual bool executeSimpleStatement(const std::string &sql);
112 
113 		virtual SQLResults *executeStatement(const char *sqlFormat, ...);
114 
115 		virtual bool prepareStatement(const std::string &statementId,
116 			const std::string &sqlFormat);
117 
118 		virtual SQLResults *executePreparedStatement(const std::string &statementId,
119 			const std::vector<std::string> &values);
120 
121 		virtual SQLResults *executePreparedStatement(const std::string &statementId,
122 			const std::vector<std::pair<std::string, SQLRow::SQLType> > &values);
123 
124 	protected:
125 		bool m_onDemand;
126 		bool m_inTransaction;
127 		sqlite3 *m_pDatabase;
128 		std::map<std::string, sqlite3_stmt*> m_statements;
129 
130 		void executeSimpleStatement(const std::string &sql, int &execError);
131 
132 		void open(void);
133 
134 		void close(void);
135 
136 	private:
137 		SQLiteBase(const SQLiteBase &other);
138 		SQLiteBase &operator=(const SQLiteBase &other);
139 
140 };
141 
142 #endif // _SQLITE_BASE_H
143