-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.cpp
633 lines (578 loc) · 13.4 KB
/
database.cpp
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/**
* @(#)database.cpp 1.0.0
*
* Copyright 2004 Vijay Mathew Pandyalakal. All rights reserved.
*
* Change history:
* Created on July 26, 2004
* Last Modified on January 11, 2007
*
*/
#include <algorithm>
#include <iterator>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include "database.h"
/**
* This file defines C++ abtractions to provide a JDBC style interface to SQLite.
* @author Vijay Mathew Pandyalakal
* @since 1.0.0
*/
// class SqlException
/**
* Creates an exception with the given code
* @param code error code
* @since 1.0.0
*/
SQLException::SQLException(int code)
: m_nCode(code)
{
setMessageFromCode();
}
/**
* Creates an exception with the given message
* @param msg Error message
* @since 1.0.0
*/
SQLException::SQLException(std::string msg)
: m_nCode(0), m_strMsg(msg)
{
}
SQLException::~SQLException() throw () { }
const char*
SQLException::what() const throw()
{
std::ostringstream oss;
oss << m_nCode << ": " << m_strMsg;
return oss.str().c_str();
}
/**
* Returns the error code
* @return int
* @since 1.0.0
*/
int
SQLException::getCode()
{
return m_nCode;
}
/**
* Returns the error message
* @return string
* @since 1.0.0
*/
std::string
SQLException::getMessage()
{
return m_strMsg;
}
/**
* Creates a message from the current error code
* @since 1.0.0
*/
void
SQLException::setMessageFromCode()
{
if (m_nCode == SQLITE_ERROR) m_strMsg = "SQL error or missing database";
else if(m_nCode == SQLITE_INTERNAL) m_strMsg = "An internal logic error in the database";
else if(m_nCode == SQLITE_PERM) m_strMsg = "Access permission denied";
else if(m_nCode == SQLITE_ABORT) m_strMsg = "Callback routine requested an abort";
else if(m_nCode == SQLITE_BUSY) m_strMsg = "The database file is locked";
else if(m_nCode == SQLITE_LOCKED) m_strMsg = "A table in the database is locked";
else if(m_nCode == SQLITE_NOMEM) m_strMsg = "A memory allocation failed in the database";
else if(m_nCode == SQLITE_READONLY) m_strMsg = "Attempt to write a readonly database";
else if(m_nCode == SQLITE_INTERRUPT) m_strMsg = "Operation terminated by a database interrupt";
else if(m_nCode == SQLITE_IOERR) m_strMsg = "Some kind of disk I/O error occurred";
else if(m_nCode == SQLITE_CORRUPT) m_strMsg = "The database disk image is malformed";
else if(m_nCode == SQLITE_NOTFOUND) m_strMsg = "Table or record not found";
else if(m_nCode == SQLITE_FULL) m_strMsg = "Insertion failed because database is full";
else if(m_nCode == SQLITE_CANTOPEN) m_strMsg = "Unable to open the database file";
else if(m_nCode == SQLITE_PROTOCOL) m_strMsg = "Database lock protocol error";
else if(m_nCode == SQLITE_EMPTY) m_strMsg = "Database table is empty";
else if(m_nCode == SQLITE_SCHEMA) m_strMsg = "The database schema changed";
else if(m_nCode == SQLITE_TOOBIG) m_strMsg = "Too much data for one row of a table";
else if(m_nCode == SQLITE_CONSTRAINT) m_strMsg = "Abort due to contraint violation";
else if(m_nCode == SQLITE_MISMATCH) m_strMsg = "Data type mismatch";
else if(m_nCode == SQLITE_MISUSE) m_strMsg = "Library used incorrectly";
else if(m_nCode == SQLITE_NOLFS) m_strMsg = "Uses OS features not supported on host";
else if(m_nCode == SQLITE_AUTH) m_strMsg = "Authorization denied";
else m_strMsg = "Unknown database error";
}
// class Connection
/**
* Constructor
* @since 1.0.0
*/
Connection::Connection()
: m_sqlite(0), m_stmt(0), m_dbMtdt(0)
{
}
/**
* Destructor
* @since 1.0.0
*/
Connection::~Connection()
{
close();
}
/**
* Opens a database. The database is created, if not found.
* @param db_name database name
* @since 1.0.0
*/
void
Connection::open(const char* db_name)
throw (SQLException)
{
close();
int ret = sqlite3_open(db_name,&m_sqlite);
if (ret != SQLITE_OK)
{
close();
throw SQLException(ret);
}
m_dbMtdt = NULL;
// fills in the DatabaseMetaData
refreshMetaData();
}
/**
* Creates and returns a statement object
* @return Statement*
* @since 1.0.0
*/
Statement*
Connection::createStatement()
{
delete m_stmt;
m_stmt = new Statement(m_sqlite);
return m_stmt;
}
/**
* Returns meta data of the entire database
* @return DatabaseMetaData*
* @since 1.0.0
*/
DatabaseMetaData*
Connection::getDatabaseMetaData()
{
return m_dbMtdt;
}
/**
* Refreshes the meta data
* @since 1.0.0
*/
void
Connection::refreshMetaData() throw (SQLException)
{
sqlite3_stmt *stmt;
std::string sql = "select type,name,sql from sqlite_master";
int ret = sqlite3_prepare(m_sqlite,sql.c_str(),sql.length(),&stmt,NULL);
if (ret != SQLITE_OK)
throw SQLException(ret);
int res = sqlite3_step(stmt);
std::vector<DatabaseObject> vct;
while (true)
{
if (res != SQLITE_ROW && res != SQLITE_OK && res != SQLITE_DONE)
throw SQLException(res);
if (res != SQLITE_ROW) break;
DatabaseObject dobj;
dobj.type_ = (const char*)sqlite3_column_text(stmt,0);
dobj.name_ = (const char*)sqlite3_column_text(stmt,1);
dobj.sql_ = (const char*)sqlite3_column_text(stmt,2);
vct.push_back(dobj);
res = sqlite3_step(stmt);
}
sqlite3_finalize(stmt);
delete m_dbMtdt;
m_dbMtdt = new DatabaseMetaData(vct);
}
/**
* Closes the database and releases all resources
* @since 1.0.0
*/
void
Connection::close()
{
delete m_stmt;
m_stmt = NULL;
delete m_dbMtdt;
m_dbMtdt = NULL;
if (m_sqlite != NULL)
{
sqlite3_close(m_sqlite);
m_sqlite = NULL;
}
}
// class Statement
/**
* Constructor
* @param pS Handle to the core database
* @since 1.0.0
*/
Statement::Statement(sqlite3* pS)
: m_sqlite(pS), m_rslt(0)
{
}
/**
* Destructor
* @since 1.0.0
*/
Statement::~Statement()
{
delete m_rslt;
}
/**
* Executes an sql and returns the number of rows affected
* @param sql SQL to execute
* @since 1.0.0
*/
int
Statement::execute(const char* sql) throw (SQLException)
{
int ret = sqlite3_exec(m_sqlite,sql,0,0,0);
if (ret != SQLITE_OK)
throw SQLException(ret);
return sqlite3_changes(m_sqlite);
}
/**
* Executes an sql and returns a pointer to the resultset
* @param sql SQL query
* @since 1.0.0
*/
ResultSet*
Statement::executeQuery(const char* sql)
throw (SQLException)
{
sqlite3_stmt *stmt;
int ret = sqlite3_prepare(m_sqlite,sql,strlen(sql),&stmt,NULL);
if (ret != SQLITE_OK) throw SQLException(ret);
delete m_rslt;
m_rslt = new ResultSet(stmt);
return m_rslt;
}
// class ResultSet
/**
* Constructor
* @param ps Pointer to sqlite statement object
* since 1.0.0
*/
ResultSet::ResultSet(sqlite3_stmt* ps)
throw (SQLException)
{
if (ps == NULL) throw SQLException("NULL statement object");
m_stmt = ps;
m_nRes = sqlite3_step(m_stmt);
if (m_nRes != SQLITE_OK && m_nRes != SQLITE_ROW && m_nRes != SQLITE_DONE)
throw SQLException(m_nRes);
m_bSecondStep = true;
std::vector<Column> vct;
int col_count = sqlite3_column_count(m_stmt);
for (int i=0;i<col_count;i++)
{
Column col;
col.name_ = sqlite3_column_name(m_stmt,i);
try
{
const char* type = sqlite3_column_decltype(m_stmt,i);
if (type)
col.type_ = type;
else
col.type_ = "VARIANT"; // is this selection good?
}
catch(...)
{
col.type_ = "VARIANT";
}
vct.push_back(col);
}
m_rsltMtdt = new ResultSetMetaData(vct);
}
/**
* Destructor
* @since 1.0.0
*/
ResultSet::~ResultSet()
{
if (m_stmt != NULL)
{
sqlite3_finalize(m_stmt);
m_stmt = NULL;
}
delete m_rsltMtdt;
}
/**
* Moves to the next row of the resultset
* @return bool
* @since 1.0.0
*/
bool
ResultSet::next() throw (SQLException)
{
if (m_nRes != SQLITE_ROW) return false;
if (m_bSecondStep)
{
m_bSecondStep = false;
return true;
}
m_nRes = sqlite3_step(m_stmt);
if (m_nRes != SQLITE_OK && m_nRes != SQLITE_ROW && m_nRes != SQLITE_DONE)
throw SQLException(m_nRes);
if (m_nRes == SQLITE_DONE) return false;
return true;
}
/**
* Returns true if the given column has a null value
* @return bool
* @since 1.0.0
*/
bool
ResultSet::isNull(int colNum) throw (SQLException)
{
// as of now we don't support null
return false;
}
/**
* Returns data of column as a string
* @param colNum Column number, starting at 1
* @return const char*
* @since 1.0.0
*/
std::string
ResultSet::getString(int colNum) throw (SQLException)
{
int i = colNum-1;
if (i < 0) throw SQLException("Invalid column number");
std::string ret = (const char*)sqlite3_column_text(m_stmt,(i));
return ret;
}
/**
* Returns data of column as an int
* @param colNum Column number, starting at 1
* @return int
* @since 1.0.0
*/
int
ResultSet::getInt(int colNum) throw (SQLException)
{
return atoi(getString(colNum).c_str());
}
/**
* Returns data of column as a Long
* @param colNum Column number, starting at 1
* @return long
* @since 1.0.0
*/
long
ResultSet::getLong(int colNum) throw (SQLException)
{
return atol(getString(colNum).c_str());
}
/**
* Returns data of column as an unsiged int
* @param colNum Column number, starting at 1
* @return unsigned int
* @since 1.0.0
*/
unsigned int
ResultSet::getUInt(int colNum) throw (SQLException)
{
return (unsigned int)atoi(getString(colNum).c_str());
}
/**
* Returns data of column as a unsigned long
* @param colNum Column number, starting at 1
* @return unsigned long
* @since 1.0.0
*/
unsigned long
ResultSet::getULong(int colNum) throw (SQLException)
{
return (unsigned long)atol(getString(colNum).c_str());
}
/**
* Returns data of column as a float
* @param colNum Column number, starting at 1
* @return float
* @since 1.0.0
*/
float
ResultSet::getFloat(int colNum) throw (SQLException)
{
return atof(getString(colNum).c_str());
}
/**
* Returns data of column as a double
* @param colNum Column number, starting at 1
* @return double
* @since 1.0.0
*/
double
ResultSet::getDouble(int colNum) throw (SQLException)
{
return (double)atof(getString(colNum).c_str());
}
namespace
{
const std::string to_lower(const std::string& s)
{
std::string res;
std::transform(s.begin(), s.end(), std::back_inserter(res), tolower);
return res;
}
}
/**
* Returns data of column as a bool
* @param colNum Column number, starting at 1
* @return double
* @since 1.0.0
*/
bool
ResultSet::getBoolean(int colNum) throw (SQLException)
{
std::string str = to_lower(getString(colNum));
return (str == "true" || str == "1");
}
/**
* Returns meta data of the resultset
* @return ResultSetMetaData*
* @since 1.0.0
*/
ResultSetMetaData*
ResultSet::getMetaData() throw (SQLException)
{
return m_rsltMtdt;
}
// class ResultSetMetaData
/**
* Constructor
* @param vct Vector of columns
* @since 1.0.0
*/
ResultSetMetaData::ResultSetMetaData(std::vector<Column> vct)
: m_vctColumns(vct)
{
}
/**
* Returns number of columns in the resultset
* @return int
* @since 1.0.0
*/
int
ResultSetMetaData::getColumnCount()
{
return m_vctColumns.size();
}
/**
* Returns the name of the column
* @param colNum Number of column, starting at 1
* @return string
* @since 1.0.0
*/
std::string
ResultSetMetaData::getColumnName(int colNum)
throw (SQLException)
{
int i = colNum-1;
if (i < 0 || i >= m_vctColumns.size())
throw SQLException("Invalid column number");
return m_vctColumns[i].name_;
}
/**
* Returns the datatype of the column
* @param colNum Number of column, starting at 1
* @return string
* @since 1.0.0
*/
std::string
ResultSetMetaData::getColumnType(int colNum)
throw (SQLException)
{
int i = colNum-1;
if (i < 0 || i >= m_vctColumns.size())
throw SQLException("Invalid column number");
return m_vctColumns[i].type_;
}
// class DatabaseMetaData
/**
* Constructor
* @param Vector of database objects
* @since 1.0.0
*/
DatabaseMetaData::DatabaseMetaData(std::vector<DatabaseObject> vct)
: m_vctObjects(vct)
{
}
/**
* Returns the number of objects in the database
* @return int
* @since 1.0.0
*/
int
DatabaseMetaData::getNumObjects()
{
return m_vctObjects.size();
}
/**
* Returns the name of the object at the given position
* @param colNum Column number,starts at 1
* @return string
* @since 1.0.0
*/
std::string
DatabaseMetaData::getName(int colNum) throw (SQLException)
{
int i = colNum - 1;
if (i < 0 || i >= m_vctObjects.size())
throw SQLException("Invalid column number");
return m_vctObjects[i].name_;
}
/**
* Returns the type of the object at the given position
* @param colNum Column number,starts at 1
* @return string
* @since 1.0.0
*/
std::string
DatabaseMetaData::getType(int colNum) throw (SQLException)
{
int i = colNum - 1;
if (i < 0 || i >= m_vctObjects.size())
throw SQLException("Invalid column number");
return m_vctObjects[i].type_;
}
/**
* Returns the sql that created the object at the given position
* @param colNum Column number,starts at 1
* @return string
* @since 1.0.0
*/
std::string
DatabaseMetaData::getSql(int colNum) throw (SQLException)
{
int i = colNum - 1;
if (i < 0 || i >= m_vctObjects.size())
throw SQLException("Invalid column number");
return m_vctObjects[i].sql_;
}
/**
* Checks if the given object exists in the database
* @param name_ Name of the object
* @param type_ Type of the object (table,index)
* @return bool
* @since 1.0.0
*/
bool
DatabaseMetaData::doesObjectExist(const char* name_,
const char* type_)
{
int sz = m_vctObjects.size();
for (int i=0;i<sz;i++)
{
if ((to_lower(m_vctObjects[i].name_) == to_lower(name_))
&& (to_lower(m_vctObjects[i].type_) == to_lower(type_)))
return true;
}
return false;
}