summaryrefslogtreecommitdiff
path: root/plugins/database/basedb.hpp
blob: 4d6a1296907de94e3d9f9ed1c32db5f75722615d (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * timedate - Displays time and date and daily events
 * Copyright (c) <2009>, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 2.1, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#ifndef _BASEDB_H_
#define _BASEDB_H_

#include "sqlitedatabase.h"
#include "sqlitequery.h"
#include "debugout.h"
#include <string>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;

template<typename T>
class NameValuePair
{
	public:
		NameValuePair(){ }
		NameValuePair(std::string n, T v){name = n; value = v;}
		std::string name;
		T value;
};

template<typename T>
class DictionaryList: public std::vector<NameValuePair<T> >
{

};

class BaseDB
{
public:
	BaseDB():db(NULL),q(NULL)
	{

	}


	virtual ~BaseDB()
	{
		DebugOut()<<"BaseDB: Destroying db object. Table: "<<table<<endl;
		delete q;
		delete db;
	}

	void setTable(string tablename)
	{
		if(tablename == "") return;
		table = tablename;

		if(!tableExists())
			reloadTable();
	}

	virtual void
	init(string dbname, string tablename, string tablestring)
	{
		DebugOut()<<"BaseDB: Initializing db object. Table: "<<tablename.c_str()<<endl;
		tableString = tablestring;

		db = new sqlitedatabase();

		db->init(dbname);

		DebugOut()<<"BaseDB: Using db/db-file: "<<dbname.c_str()<<endl;

		if(! db->Connected())
		{
			DebugOut(0)<<"BaseDB: database not found "<<dbname<<endl;
			return;
		}
		q = new sqlitequery();

		q->init(db);

		setTable(tablename);
	}

	virtual void
	reloadTable()
	{
		DebugOut()<<"BaseDB: reloading table "<<table<<endl;
		dropTable();
		createTable();
	}

	virtual bool tableExists()
	{
		bool exists=false;
		string query = "SELECT * FROM "+table+" LIMIT 0,1";
		DebugOut()<<"BaseDB: checking for existing table with "<<query.c_str()<<endl;
		q->getResult(query);
		int numrows = q->numRows();
		if(numrows <= 0 )
			exists = false;
		else exists = true;

		DebugOut()<<"BaseDB: Table '"<<table<<"' exists? "<<exists<<" because "<<numrows<<" rows where found."<<endl;
		q->freeResult();
		return exists;
	}

	virtual void
	renameTable(string newname)
	{
		dropTable(newname);
		string query = "ALTER TABLE "+table+" RENAME TO "+newname;
		q->execute(query);
	}

	template<typename T>
	void insert(DictionaryList<T> params)
	{
		string query = "INSERT INTO "+table+" (";
		ostringstream endquery;
		endquery<<" VALUES ( ";
		for(size_t i=0; i< params.size(); i++)
		{
			query+=" `"+fixInvalids(params[i].name)+"`";
			ostringstream tempval;
			tempval<<params[i].value;
			endquery<<"'"<<fixInvalids(tempval.str())<<"'";
			if(i < params.size()-1)
			{
				query+=",";
				endquery<<",";
			}
		}
		endquery<<" )";
		query+=" )"+endquery.str();
		DebugOut()<<"BaseDB: "<<query<<endl;
		q->execute(query);
	}

	template<typename T>
	void
	insert(NameValuePair<T> param)
	{
		string query = "INSERT INTO "+table+" (";
		ostringstream endquery;
		endquery<<" VALUES ( ";
		query+=" `"+fixInvalids(param.name)+"`";
		ostringstream tempval;
		tempval<<param.value;
		endquery<<"'"<<fixInvalids(tempval.str())<<"'";
		endquery<<" )";
		query+=" )"+endquery.str();
		DebugOut()<<"BaseDB: "<<query<<endl;
		q->execute(query);
	}

	virtual void
	insert(DictionaryList<string> params)
	{
		insert<string>(params);
	}

	template<typename T, typename TT, typename T3>
	void
	update(T col, TT colval, NameValuePair<T3> qualifier)
	{
		ostringstream query;
		ostringstream tempval;
		ostringstream tempcolval;
		tempval<<qualifier.value;
		tempcolval<<colval;
		query << "UPDATE "<< table <<
			" SET `"<<col<<"` = '"<<fixInvalids(tempcolval.str())<<
			"' WHERE `"<<fixInvalids(qualifier.name)<<"` = '"<<fixInvalids(tempval.str())<<"'";
		DebugOut() << "BaseDB: Update: " << query.str() << endl;
		q->execute(query.str());
	}

	template<typename T, typename TT>
	void
	update(NameValuePair<T> param, NameValuePair<TT> qualifier)
	{
		update<string,T,TT>(param.name, param.value, qualifier);
	}

	template<typename T, typename TT>
	void
	update(DictionaryList<T> params, NameValuePair<TT> qualifier)
	{
		for(size_t i=0;i<params.size();i++)
		{
			update<T,TT>(params[i],qualifier);
		}
	}

	virtual void update(NameValuePair<string> param, NameValuePair<string> qualifier)
	{
		update<string,string>(param,qualifier);
	}

	template<typename T>
	void deleteRow(NameValuePair<T> qualifier)
	{
		ostringstream query;
		ostringstream tempval;
		tempval<<qualifier.value;
		query << "DELETE FROM "<< table<<
				" WHERE `"<<qualifier.name<<"` = '"<<fixInvalids(tempval.str())<<"'";
		DebugOut() << "BaseDB: " << __FUNCTION__ << " : " << query.str() << endl;
		q->execute(query.str());
	}

	virtual void
	deleteRow(NameValuePair<string> qualifier)
	{
		deleteRow<string>(qualifier);
	}

	virtual void
	dropTable()
	{
		dropTable(table);
	}

	virtual void
	dropTable(string tablename)
	{
		string query="DROP TABLE IF EXISTS "+tablename;
		DebugOut() << "BaseDB: Dropping Table " << tablename <<" with query: " << query << endl;
		q->execute(query);
	}

	virtual void
	createTable()
	{
		string t = tableString;
		string query;
		string::size_type i=t.find("%s",0);
		if(i!=string::npos) query=t.replace(i, 2, table);
		else query = t;
		DebugOut() << "BaseDB: Creating Table" << table << " with query: " << query << endl;
		q->execute(query);
	}



	string
	fixInvalids(string filename)
	{
		return filename;
	}

	vector<vector<string> > select(string query)
	{
		DebugOut()<<query<<endl;

		vector<vector<string>> dataMap;

		q->getResult(query);

		if(q->numRows() <= 0)
		{
			q->freeResult();
			return dataMap;
		}

		int i=0;

		while(q->fetchRow())
		{
			string v;
			dataMap.push_back(vector<string>());

			while((v = q->getStr()) != "")
			{
				dataMap[i].push_back(v);
			}
			i++;
		}

		q->freeResult();

		return dataMap;

	}

	void exec(string query)
	{
		q->execute(query);
	}

protected:

	void
	fixFilename(string* filename)
	{
		std::string::size_type i=0;
		while(1)
		{
			i = filename->find(" ",i);
			if(i == string::npos)
				break;
			filename->replace(i,1,"\\ ");
			i+=2;
		}
	}

	void
	unfixFilename(string* filename)
	{
		std::string::size_type i=0;
		i=filename->find("\\",0);
		if(i == string::npos)
			return;
		else
		{
			filename->replace(i,1,"");
			unfixFilename(filename);
		}

	}

	sqlitedatabase *db;
	sqlitequery *q;
	string table;
	string tableString;

}; //BaseDB class

#endif