OpenStructure
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  }
124  RowSet<R>& operator++()
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