File: SQLiteBase.cpp

package info (click to toggle)
pinot 0.95-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 6,860 kB
  • ctags: 4,511
  • sloc: cpp: 39,387; sh: 9,973; ansic: 4,174; makefile: 657; xml: 372; python: 260
file content (601 lines) | stat: -rw-r--r-- 11,819 bytes parent folder | download
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/*
 *  Copyright 2005-2009 Fabrice Colin
 *
 *  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; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <unistd.h>
#include <algorithm>
#include <utility>
#include <iostream>

#include "SQLiteBase.h"

using std::clog;
using std::endl;
using std::string;
using std::vector;
using std::map;
using std::pair;

static int busyHandler(void *pData, int lockNum)
{
	// Try again
	return 1;
}

// A function object to delete statements with for_each()
struct DeleteStatementsFunc
{
	public:
		void operator()(map<string, sqlite3_stmt*>::value_type &p)
		{
			if (p.second != NULL)
			{
				sqlite3_finalize(p.second);
			}
		}
};

SQLiteRow::SQLiteRow(const vector<string> &rowColumns, unsigned int nColumns) :
	SQLRow(nColumns),
	m_pStatement(NULL)
{
	if (rowColumns.empty() == false)
	{
		m_columns.reserve(rowColumns.size());
#if 0
		// FIXME: why does this segfault in string::assign() ?
		copy(rowColumns.begin(), rowColumns.end(), m_columns.begin());
#else
		for (vector<string>::const_iterator colIter = rowColumns.begin(); colIter != rowColumns.end(); ++colIter)
		{
			m_columns.push_back(*colIter);
		}
#endif
	}
}

SQLiteRow::SQLiteRow(sqlite3_stmt *pStatement, unsigned int nColumns) :
	SQLRow(nColumns),
	m_pStatement(pStatement)
{
}

SQLiteRow::~SQLiteRow()
{
}

string SQLiteRow::getColumn(unsigned int nColumn) const
{
	if (m_pStatement != NULL)
	{
		const unsigned char *pTextColumn = sqlite3_column_text(m_pStatement, nColumn);
		if (pTextColumn != NULL)
		{
			return (const char*)pTextColumn;
		}
#ifdef DEBUG
		clog << "SQLiteRow::getColumn: couldn't get column " << nColumn << endl;
#endif

		return "";
	}

	if (nColumn < m_nColumns)
	{
		vector<string>::const_iterator colIter = m_columns.begin();
		for (int i = 0; (i < m_nColumns) && (colIter != m_columns.end()); ++i)
		{
			if (i == nColumn)
			{
				string column(*colIter);

				return column;
			}
			++colIter;
		}
	}

	return "";
}

SQLiteResults::SQLiteResults(char **results, unsigned long nRows, unsigned int nColumns) :
	SQLResults(nRows, nColumns),
	m_results(results),
	m_pStatement(NULL)
{
	// Check we actually have results
	if ((m_results == NULL) ||
		(m_nRows <= 0))
	{
		m_nRows = m_nCurrentRow = 0;
		m_nColumns = 0;
	}
}

SQLiteResults::SQLiteResults(sqlite3_stmt *pStatement) :
	SQLResults(0, sqlite3_column_count(pStatement)),
	m_results(NULL),
	m_pStatement(pStatement)
{
}

SQLiteResults::~SQLiteResults()
{
	if (m_results != NULL)
	{
		sqlite3_free_table(m_results);
	}
	if (m_pStatement != NULL)
	{
		rewind();
	}
}

bool SQLiteResults::hasMoreRows(void) const
{
	if (m_pStatement != NULL)
	{
		// There's no way to tell
		return true;
	}

	return SQLResults::hasMoreRows();
}

string SQLiteResults::getColumnName(unsigned int nColumn) const
{
	if (m_pStatement != NULL)
	{
		return sqlite3_column_name(m_pStatement, (int)nColumn);
	}

	if (nColumn < m_nColumns)
	{
		return m_results[nColumn];
	}

	return "";
}

SQLRow *SQLiteResults::nextRow(void)
{
	if (m_pStatement != NULL)
	{
		if (sqlite3_step(m_pStatement) == SQLITE_ROW)
		{
			++m_nCurrentRow;
			return new SQLiteRow(m_pStatement, m_nColumns);
		}
#ifdef DEBUG
		clog << "SQLiteResults::nextRow: no more row" << endl;
#endif

		return NULL;
	}

	if ((m_nCurrentRow < 0) ||
		(m_nCurrentRow >= m_nRows))
	{
		return NULL;
	}

	// The very first row holds the column names
	unsigned long firstIndex = (m_nCurrentRow  + 1) * m_nColumns;
	unsigned long lastIndex = firstIndex + m_nColumns - 1;
	vector<string> rowColumns;

	for (unsigned long i = firstIndex; i <= lastIndex; ++i)
	{
		if (m_results[i] == NULL)
		{
			rowColumns.push_back("");
		}
		else
		{
			rowColumns.push_back(m_results[i]);
		}
	}
	++m_nCurrentRow;

	return new SQLiteRow(rowColumns, m_nColumns);
}

bool SQLiteResults::rewind(void)
{
	SQLResults::rewind();
	if (m_pStatement != NULL)
	{
		sqlite3_reset(m_pStatement);
	}

	return true;
}

SQLiteBase::SQLiteBase(const string &databaseName,
	bool readOnly, bool onDemand) :
	SQLDB(databaseName, readOnly),
	m_onDemand(onDemand),
	m_pDatabase(NULL)
{
	if (m_onDemand == false)
	{
		open();
	}
}

SQLiteBase::~SQLiteBase()
{
	if (m_onDemand == false)
	{
		close();
	}
}

bool SQLiteBase::check(const string &databaseName)
{
	struct stat dbStat;

	// The specified path must be a file
	if ((stat(databaseName.c_str(), &dbStat) != -1) &&
		(!S_ISREG(dbStat.st_mode)))
	{
		// It exists, but it's not a file as expected
		clog << databaseName << " is not a file" << endl;
		return false;
	}

	return true;
}

void SQLiteBase::open(void)
{
	int openFlags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;

	if (m_readOnly == true)
	{
		openFlags = SQLITE_OPEN_READONLY;
	}

	// Open the new database
	if (sqlite3_open_v2(m_databaseName.c_str(), &m_pDatabase,
		openFlags, NULL) != SQLITE_OK)
	{
		// An handle is returned even when an error occurs !
		if (m_pDatabase != NULL)
		{
			clog << sqlite3_errmsg(m_pDatabase) << endl;
			close();
			m_pDatabase = NULL;
		}
	}
	else if (m_pDatabase == NULL)
	{
		clog << "Couldn't open " << m_databaseName << endl;
	}
	else
	{
		// Set up a busy handler
		sqlite3_busy_handler(m_pDatabase, busyHandler, NULL);
	}
}

void SQLiteBase::close(void)
{
	if (m_pDatabase != NULL)
	{
		for_each(m_statements.begin(), m_statements.end(),
			DeleteStatementsFunc());

		sqlite3_close(m_pDatabase);
	}
}

bool SQLiteBase::isOpen(void) const
{
	if (m_pDatabase == NULL)
	{
		return false;
	}

	return true;
}

bool SQLiteBase::alterTable(const string &tableName,
	const string &columns, const string &newDefinition)
{
	if ((tableName.empty() == true) ||
		(columns.empty() == true) ||
		(newDefinition.empty() == true))
	{
		return false;
	}

	string sql("BEGIN TRANSACTION; CREATE TEMPORARY TABLE ");
	sql += tableName;
	sql += "_backup (";
	sql += columns;
	sql += "); INSERT INTO ";
	sql += tableName;
	sql += "_backup SELECT ";
	sql += columns;
	sql += " FROM ";
	sql += tableName;
	sql += "; DROP TABLE ";
	sql += tableName;
	sql += "; CREATE TABLE ";
	sql += tableName;
	sql += " (";
	sql += newDefinition;
	sql += "); INSERT INTO ";
	sql += tableName;
	sql += " SELECT ";
	sql += columns;
	sql += " FROM ";
	sql += tableName;
	sql += "_backup; DROP TABLE ";
	sql += tableName;
	sql += "_backup; COMMIT;";
#ifdef DEBUG
	clog << "SQLiteBase::alterTable: " << sql << endl;
#endif

	return executeSimpleStatement(sql);
}

bool SQLiteBase::beginTransaction(void)
{
	if (m_onDemand == true)
	{
		// Not applicable
		return true;
	}

	return executeSimpleStatement("BEGIN TRANSACTION;");
}

bool SQLiteBase::endTransaction(void)
{
	if (m_onDemand == true)
	{
		// Not applicable
		return true;
	}

	return executeSimpleStatement("END TRANSACTION;");
}

bool SQLiteBase::executeSimpleStatement(const string &sql)
{
	char *errMsg = NULL;
	bool success = true;

	if (sql.empty() == true)
	{
		return false;
	}

	if (m_onDemand == true)
	{
		open();
	}
	if (m_pDatabase == NULL)
	{
		return false;
	}

	if (sqlite3_exec(m_pDatabase,
		sql.c_str(), 
		NULL, NULL, // No callback
		&errMsg) != SQLITE_OK)
	{
		if (errMsg != NULL)
		{
			clog << m_databaseName << ": statement <" << sql << "> failed: " << errMsg << endl;

			sqlite3_free(errMsg);
		}

		success = false;
	}
	if (m_onDemand == true)
	{
		close();
	}

	return success;
}

SQLResults *SQLiteBase::executeStatement(const char *sqlFormat, ...)
{
	SQLiteResults *pResults = NULL;
#ifdef _USE_VSNPRINTF
	char stringBuff[2048];
#endif
	va_list ap;

	if (sqlFormat == NULL)
	{
		return NULL;
	}

	if (m_onDemand == true)
	{
		open();
	}
	if (m_pDatabase == NULL)
	{
		return NULL;
	}

	va_start(ap, sqlFormat);
#ifdef _USE_VSNPRINTF
	int numChars = vsnprintf(stringBuff, 2048, sqlFormat, ap);
	if (numChars <= 0)
	{
#ifdef DEBUG
		clog << "SQLiteBase::executeStatement: couldn't format statement" << endl;
#endif
		if (m_onDemand == true)
		{
			close();
		}
		return NULL;
	}
	if (numChars >= 2048)
	{
		// Not enough space
#ifdef DEBUG
		clog << "SQLiteBase::executeStatement: not enough space (" << numChars << ")" << endl;
#endif
		if (m_onDemand == true)
		{
			close();
		}
		return NULL;
	}
	stringBuff[numChars] = '\0';
#else
	char *stringBuff = sqlite3_vmprintf(sqlFormat, ap);
	if (stringBuff == NULL)
	{
#ifdef DEBUG
		clog << "SQLiteBase::executeStatement: couldn't format statement" << endl;
#endif
		if (m_onDemand == true)
		{
			close();
		}
		return NULL;
	}
#endif

	char **results;
	char *errMsg;
	int nRows, nColumns;
	if (sqlite3_get_table(m_pDatabase,
		stringBuff,
		&results,
		&nRows,
		&nColumns,
		&errMsg) != SQLITE_OK)
	{
		if (errMsg != NULL)
		{
			clog << m_databaseName << ": statement <" << stringBuff << "> failed: " << errMsg << endl;

			sqlite3_free(errMsg);
		}
	}
	else
	{
		pResults = new SQLiteResults(results, (unsigned long)nRows, (unsigned int)nColumns);
	}
	va_end(ap);
#ifndef _USE_VSNPRINTF
	sqlite3_free(stringBuff);
#endif
	if (m_onDemand == true)
	{
		close();
	}

	return pResults;
}

bool SQLiteBase::prepareStatement(const string &statementId,
	const string &sqlFormat)
{
	if ((sqlFormat.empty() == true) ||
		(m_onDemand == true) ||
		(m_pDatabase == NULL))
	{
		return false;
	}

	map<string, sqlite3_stmt*>::iterator statIter = m_statements.find(statementId);
	if (statIter != m_statements.end())
	{
#ifdef DEBUG
		clog << "SQLiteBase::prepareStatement: invalid statement ID " << statementId << endl;
#endif
		return false;
	}

	sqlite3_stmt *pStatement = NULL;
	if (sqlite3_prepare_v2(m_pDatabase,
		sqlFormat.c_str(),
		(int)sqlFormat.length(),
		&pStatement,
		NULL) == SQLITE_OK)
	{
		if (pStatement != NULL)
		{
#ifdef DEBUG
			clog << "SQLiteBase::prepareStatement: compiled statement ID " << statementId << endl;
#endif
			m_statements.insert(pair<string, sqlite3_stmt*>(statementId, pStatement));
			return true;
		}
	}

	clog << m_databaseName << ": failed to compile statement <" << sqlFormat << ">" << endl;

	return false;
}

SQLResults * SQLiteBase::executePreparedStatement(const string &statementId,
	const vector<string> &values)
{
	int paramIndex = 1;

	map<string, sqlite3_stmt*>::iterator statIter = m_statements.find(statementId);
	if (statIter == m_statements.end())
	{
#ifdef DEBUG
		clog << "SQLiteBase::executePreparedStatement: invalid statement ID " << statementId << endl;
#endif
		return NULL;
	}

	// Bind values
	// The left-most parameter's index is 1
	for (vector<string>::const_iterator valueIter = values.begin();
		valueIter != values.end(); ++valueIter, ++paramIndex)
	{
		int errorCode = sqlite3_bind_text(statIter->second, paramIndex,
			valueIter->c_str(), -1, SQLITE_TRANSIENT);
		if (errorCode != SQLITE_OK)
		{
			clog << m_databaseName << ": failed to bind parameter " << paramIndex
				<< " to statement ID " << statementId << ": error " << errorCode << endl;
			return NULL;
		}
#ifdef DEBUG
		clog << "SQLiteBase::executePreparedStatement: bound parameter " << paramIndex
			<< " size " << valueIter->length() << " to statement ID " << statementId << endl;
#endif
	}

#ifdef DEBUG
	clog << "SQLiteBase::executePreparedStatement: statement ID " << statementId << endl;
#endif
	return new SQLiteResults(statIter->second);
}