sqlitepp
A small sqlite3 C++ library
 All Classes Functions Pages
statement.cpp
1 /* Copyright (C)
2  * 2012 - Paul Weingardt
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU General Public License
5  * as published by the Free Software Foundation; either version 2
6  * of the License, or (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16  *
17  */
18 
19 #include "sqlitepp.h"
20 
21 #include <iostream>
22 
23 namespace sqlitepp {
24 
25  SQLiteException StatementNotPrepared("The statement has not been prepared or it has been finalized.");
26 
27  inline void Statement::checkPrepared() const {
28  if(this->finalized) {
29  throw StatementNotPrepared;
30  }
31  }
32 
33  Statement::Statement(Database& database) : db(database) {
34  if(!db.isOpen()) {
35  throw DatabaseNotOpened;
36  }
37 
38  this->finalized = true;
39  this->statement = NULL;
40  }
41 
42  void Statement::exec() {
43  this->checkPrepared();
44 
45  this->step();
46  this->finalize();
47  }
48 
49  void Statement::prepare(const std::string& str) {
50  if(!this->finalized && this->statement) {
51  std::cout << "The statement has not been finalized! Memory leak!" << std::endl;
52  }
53 
54  this->lastResult = sqlite3_prepare_v2(this->db.database, str.c_str(), str.size(),
55  &this->statement, NULL);
56 
57 
58  if(this->lastResult == SQLITE_OK) {
59  this->finalized = false;
60  } else {
61  throw SQLiteException(this->db.database);
62  }
63  }
64 
65  StepValue Statement::step() {
66  this->checkPrepared();
67 
68  this->lastResult = sqlite3_step(this->statement);
69 
70  switch(this->lastResult) {
71  case SQLITE_DONE:
72  return DONE;
73 
74  case SQLITE_ROW:
75  if(this->columns.size() == 0) {
76  for(int index = 0;
77  index < sqlite3_column_count(this->statement);
78  ++index) {
79  const char*p = (const char*)
80  sqlite3_column_name(this->statement, index);
81  std::string str(p);
82  this->columns.insert(this->columns.begin(),
83  std::pair<std::string, int>(p, index));
84  }
85  }
86  return ROW;
87 
88  default:
89  return UNKNOWN;
90  }
91  }
92 
93  bool Statement::fetchRow(void) {
94  this->checkPrepared();
95 
96  return (this->step() == ROW);
97  }
98 
99  int Statement::getInt(const std::string& name) const {
100  this->checkPrepared();
101 
102  if(this->columns.find(name) == this->columns.end()) {
103  std::cout << "Unknown column " << name << std::endl;
104  }
105  return this->getInt(this->columns.find(name)->second);
106  }
107 
108  int Statement::getInt(const int index) const {
109  this->checkPrepared();
110 
111  return sqlite3_column_int(this->statement, index);
112  }
113 
114  void Statement::getInt(const int index, int& out) const {
115  this->checkPrepared();
116 
117  out = this->getInt(index);
118  }
119 
120  std::string Statement::getString(const int index) const {
121  this->checkPrepared();
122 
123  const char* p = (const char*)sqlite3_column_text(this->statement, index);
124  if(p) {
125  return std::string(p);
126  } else {
127  return "NULL";
128  }
129  }
130 
131  std::string Statement::getString(const std::string& name) const {
132  this->checkPrepared();
133 
134  if(this->columns.find(name) == this->columns.end()) {
135  std::cout << "Unknown column " << name << std::endl;
136  }
137  return this->getString(this->columns.find(name)->second);
138  }
139 
140  void Statement::getString(const int index, std::string& out) const {
141  this->checkPrepared();
142 
143  out = this->getString(index);
144  }
145 
146  double Statement::getDouble(const std::string& name) const {
147  this->checkPrepared();
148 
149  if(this->columns.find(name) == this->columns.end()) {
150  std::cout << "Unknown column " << name << std::endl;
151  }
152  return this->getDouble(this->columns.find(name)->second);
153  }
154 
155  double Statement::getDouble(const int index) const {
156  this->checkPrepared();
157 
158  return sqlite3_column_double(this->statement, index);
159  }
160 
161  void Statement::getDouble(const int index, double& out) const {
162  this->checkPrepared();
163 
164  out = this->getDouble(index);
165  }
166 
167  void Statement::bindInt(const int index, const int value) {
168  this->checkPrepared();
169 
170  this->lastResult = sqlite3_bind_int(this->statement, index, value);
171  }
172 
173  void Statement::bindString(const int index, const std::string& value) {
174  this->checkPrepared();
175 
176  this->lastResult = sqlite3_bind_text(this->statement, index, value.c_str(), value.size(), NULL);
177  }
178 
179  void Statement::bindDouble(const int index, const double value) {
180  this->checkPrepared();
181 
182  this->lastResult = sqlite3_bind_double(this->statement, index, value);
183  }
184 
185  void Statement::bindNull(const int index) {
186  this->checkPrepared();
187 
188  this->lastResult = sqlite3_bind_null(this->statement, index);
189  }
190 
191  void Statement::finalize(void) {
192  if(this->statement && !this->finalized) {
193  sqlite3_finalize(this->statement);
194  this->columns.clear();
195  this->statement = NULL;
196  this->finalized = true;
197  }
198  }
199 
201  this->finalize();
202  }
203 
204 }
void bindNull(const int n)
Binds the nth parameter with the null value.
Definition: statement.cpp:185
bool fetchRow(void)
fetches the next row
Definition: statement.cpp:93
bool isOpen(void)
returns true, when a database has been opened, false otherwise
Definition: database.cpp:90
std::string getString(const std::string &column) const
gets a column as string
Definition: statement.cpp:131
int getInt(const std::string &column) const
gets a column as integer
Definition: statement.cpp:99
void bindString(const int n, const std::string &value)
Binds the nth parameter with the passed value as string.
Definition: statement.cpp:173
void exec(void)
executes the prepared statement
Definition: statement.cpp:42
Statement(Database &db)
Default constructor.
Definition: statement.cpp:33
~Statement()
Destructor.
Definition: statement.cpp:200
void bindDouble(const int n, const double value)
Binds the nth parameter with the passed value as integer.
Definition: statement.cpp:179
void prepare(const std::string &sql)
prepares a string as statement
Definition: statement.cpp:49
double getDouble(const std::string &column) const
gets a column as double
Definition: statement.cpp:146
void finalize(void)
releases any allocated resources. Use it, when you want to reuse a statement object.
Definition: statement.cpp:191
void bindInt(const int n, const int value)
Binds the nth parameter with the passed value as integer.
Definition: statement.cpp:167
The main database class.
Definition: sqlitepp.h:73