-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsqlitequery.cpp
More file actions
95 lines (79 loc) · 2.53 KB
/
Copy pathsqlitequery.cpp
File metadata and controls
95 lines (79 loc) · 2.53 KB
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
#include <QDebug>
#include "sqlitequery.h"
namespace sqliteWrapper {
SqliteQuery::SqliteQuery(sqlite3_stmt *preparedStatement, bool eof, sqlite3 *db)
: preparedStatement_(preparedStatement),
db_ (db),
eof_(eof) {
cols_ = sqlite3_column_count(preparedStatement_);
}
SqliteQuery::~SqliteQuery() {
sqlite3_finalize(preparedStatement_);
}
const std::string SqliteQuery::getStringField(int fieldIndex, const std::string &nullValue) {
if (fieldIndex < 0) {
return nullValue;
}
if (getFieldDataType(fieldIndex) == SQLITE_NULL) {
return nullValue;
} else {
return std::string( reinterpret_cast<const char*>(
sqlite3_column_text(preparedStatement_, fieldIndex)
));
}
}
const std::string SqliteQuery::getStringField(const std::string &fieldName, const std::string &nullValue)
{
int fieldIndex = getFieldIndexByName(fieldName);
return getStringField(fieldIndex, nullValue);
}
int SqliteQuery::getIntField(int fieldIndex, const int nullValue) {
if (fieldIndex < 0) {
return nullValue;
}
if (getFieldDataType(fieldIndex) == SQLITE_NULL) {
return nullValue;
} else {
return sqlite3_column_int(preparedStatement_, fieldIndex);
}
}
int SqliteQuery::getIntField(const std::string &fieldName, const int nullValue)
{
int fieldIndex = getFieldIndexByName(fieldName);
return getIntField(fieldIndex, nullValue);
}
bool SqliteQuery::eof() {
return eof_;
}
void SqliteQuery::nextRow() {
int ret = sqlite3_step(preparedStatement_);
if (ret == SQLITE_DONE) {
eof_ = true;
} else if (ret == SQLITE_ROW) {
// more rows, nothing to do
} else {
// Unknown error occured!
sqlite3_finalize(preparedStatement_);
preparedStatement_ = 0;
const char* temp = sqlite3_errmsg(db_);
qDebug() << "ERROR SqliteQuery::nextRow: " << temp;
}
}
int SqliteQuery::getFieldIndexByName(const std::string &name)
{
for (int index = 0; index < cols_; index++) {
const char* temp = sqlite3_column_name(preparedStatement_, index);
if (name.compare(temp) == 0) {
return index;
}
}
qDebug() << "ERROR SqliteQuery: Field " << name.c_str() << " not found";
return -1;
}
int SqliteQuery::getFieldDataType(int col) {
if (col < 0 || col > cols_ -1) {
return -1;
}
return sqlite3_column_type(preparedStatement_, col);
}
}