OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
sqlite_wrap.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef OST_IO_SQLITE_WRAP_HH
20 #define OST_IO_SQLITE_WRAP_HH
21 /*
22  Author: Marco Biasini
23 
24  lightweight sqlite wrapper
25 */
26 #include <ost/message.hh>
27 #include <boost/shared_ptr.hpp>
28 
29 #include <ost/base.hh>
30 #include <ost/message.hh>
31 
32 #include <sqlite3.h>
33 #include "sqlite_conv.hh"
34 
35 namespace ost { namespace db {
36 
38 class Database;
39 typedef boost::shared_ptr<Database> DatabasePtr;
40 
42 typedef boost::shared_ptr<PreparedStatement> PreparedStatementPtr;
43 
45  friend class Database;
46 public:
49 
50  sqlite3_stmt* Handle()
51  {
52  return statement_;
53  }
55  bool Submit()
56  {
57  return sqlite3_step(statement_)==SQLITE_DONE;
58  }
60  void BindInt(int col, int value)
61  {
62  sqlite3_bind_int(statement_, col, value);
63  }
65  void BindInt64(int col, sqlite3_int64 value)
66  {
67  sqlite3_bind_int64(statement_, col, value);
68  }
70  void BindText(int col, const String& text)
71  {
72  sqlite3_bind_text(statement_, col, text.c_str(),
73  static_cast<int>(text.size()), NULL);
74  }
76  void BindBlob(int col, const char* start, size_t size)
77  {
78  sqlite3_bind_blob(statement_, col, start, size, NULL);
79  }
80 private:
81  sqlite3_stmt* statement_;
82 };
83 
84 class DLLEXPORT DatabaseError : public Error {
85 public:
86  DatabaseError(const String& msg):
87  Error(msg)
88  {
89 
90  }
91 };
92 
110 template <typename R>
112 public:
114  stmt_(stmt), value_()
115  {
116  this->Step();
117  }
119  stmt_(), value_(), cur_val_(SQLITE_DONE)
120  { }
121  R& operator*() {
122  return value_;
123  }
125  {
126  this->Step();
127  return *this;
128  }
129 
130  bool AtEnd()
131  {
132  return cur_val_!=SQLITE_ROW;
133  }
134 private:
135  void Step()
136  {
137  cur_val_=sqlite3_step(stmt_->Handle());
138  if (cur_val_==SQLITE_ROW) {
139  SqlConvert(stmt_->Handle(), value_);
140  }
141  }
142  PreparedStatementPtr stmt_;
143  R value_;
144  int cur_val_;
145 };
146 
149 public:
154  static DatabasePtr Open(const String& database);
155 
156  ~Database();
160  PreparedStatementPtr Prepare(const String& query);
161 
163  sqlite3* Handle()
164  {
165  return conn_;
166  }
167 
168  sqlite3_int64 LastRowID();
172  DatabasePtr Copy(const String& filename);
173 private:
174  sqlite3* conn_;
175 };
176 
177 }}
178 
179 #endif
RowSet< R > & operator++()
Definition: sqlite_wrap.hh:124
std::string String
Definition: base.hh:54
Row set iterator.
Definition: sqlite_wrap.hh:111
boost::shared_ptr< PreparedStatement > PreparedStatementPtr
Definition: sqlite_wrap.hh:41
void BindText(int col, const String &text)
bind String value to placeholder
Definition: sqlite_wrap.hh:70
void BindBlob(int col, const char *start, size_t size)
bind binary blob to placeholder
Definition: sqlite_wrap.hh:76
SQLite database handle.
Definition: sqlite_wrap.hh:148
bool Submit()
submit query after binding all a parameters.
Definition: sqlite_wrap.hh:55
void BindInt(int col, int value)
bind integer value to placeholder
Definition: sqlite_wrap.hh:60
DatabaseError(const String &msg)
Definition: sqlite_wrap.hh:86
void SqlConvert(sqlite3_stmt *stmt, int &value, int col=0)
Definition: sqlite_conv.hh:28
sqlite3 * Handle()
Get SQLite handle.
Definition: sqlite_wrap.hh:163
void BindInt64(int col, sqlite3_int64 value)
bind 64 bit integer value to placeholder
Definition: sqlite_wrap.hh:65
RowSet(PreparedStatementPtr stmt)
Definition: sqlite_wrap.hh:113
#define DLLEXPORT_OST_DB
sqlite3_stmt * Handle()
Definition: sqlite_wrap.hh:50
boost::shared_ptr< Database > DatabasePtr
Definition: sqlite_wrap.hh:38