CYCLUS
Loading...
Searching...
No Matches
sqlite_db.h
Go to the documentation of this file.
1#ifndef CYCLUS_SRC_SQLITE_DB_H_
2#define CYCLUS_SRC_SQLITE_DB_H_
3
4#include <vector>
5#include <string>
6#include <boost/shared_ptr.hpp>
7
8#include "error.h"
9
10class sqlite3;
11class sqlite3_stmt;
12
13namespace cyclus {
14
15typedef std::vector<std::string> StrList;
16
17class SqliteDb;
18
19/// Thin wrapper class over sqlite3 prepared statements. See
20/// http://sqlite.org/cintro.html for an overview of how prepared statements
21/// work.
22class SqlStatement {
23 friend class SqliteDb;
24
25 public:
26 typedef boost::shared_ptr<SqlStatement> Ptr;
27
29
30 /// Executes the prepared statement.
31 void Exec();
32
33 /// Executes the prepared statement.
34 void Reset();
35
36 /// Step to next row of previously executed query. Returns false when there
37 /// are no more rows. Previously retrieved text or blob column data memory
38 /// is deallocated.
39 bool Step();
40
41 /// Returns an int value for the specified column of the current query row.
42 int GetInt(int col);
43
44 /// Returns a double value for the specified column of the current query row.
45 double GetDouble(int col);
46
47 /// Returns a byte array value for the specified column of the current query
48 /// row. This can be used for retrieving TEXT and BLOB column data.
49 char* GetText(int col, int* n);
50
51 /// Binds the templated sql parameter at index i to val.
52 void BindInt(int i, int val);
53
54 /// Binds the templated sql parameter at index i to val.
55 void BindDouble(int i, double val);
56
57 /// Binds the templated sql parameter at index i to val.
58 void BindText(int i, const char* val);
59
60 /// Binds the templated sql parameter at index i to the value pointed to by
61 /// val. n is the length in bytes of the value.
62 void BindBlob(int i, const void* val, int n);
63
64 private:
65 SqlStatement(sqlite3* db, std::string zSql);
66
67 /// throws an exception if status (the return value of an sqlite function)
68 /// does not represent success.
69 void Must(int status);
70
71 sqlite3* db_;
72 std::string zSql_;
73 sqlite3_stmt* stmt_;
74};
75
76/// An abstraction over the Sqlite native C interface to simplify database
77/// creation and data insertion.
78class SqliteDb {
79 public:
80 /// Creates a new Sqlite database to be stored at the specified path.
81 ///
82 /// @param path the path+name for the sqlite database file
83 /// @param readonly a boolean indicating true if db is readonly
84 SqliteDb(std::string path, bool readonly = false);
85
86 virtual ~SqliteDb();
87
88 /// Finishes any incomplete operations and closes the database.
89 /// @throw IOError if failed to close the database properly
90 void close();
91
92 /// Opens the sqlite database by either opening/creating a file (default) or
93 /// creating/overwriting a file (see the overwrite method).
94 ///
95 /// @throw IOError if failed to open existing database
96 void open();
97
98 /// Instead of opening a file of the specified name (if it already exists),
99 /// overwrite it with a new empty database.
100 void Overwrite();
101
102 /// Creates a sqlite prepared statement for the given sql. See
103 /// http://sqlite.org/cintro.html for an overview of how prepared statements
104 /// work.
105 SqlStatement::Ptr Prepare(std::string sql);
106
107 /// Execute an SQL command.
108 ///
109 /// @param cmd an Sqlite compatible SQL command
110 /// @throw IOError SQL command execution failed (e.g. invalid SQL)
111 void Execute(std::string cmd);
112
113 /// Execute an SQL query and return its results
114 ///
115 /// @param cmd an Sqlite compatible SQL query
116 /// @return a list of row entries
117 /// @throw IOError SQL command execution failed (e.g. invalid SQL)
118 std::vector<StrList> Query(std::string cmd);
119
120 private:
121 sqlite3* db_;
122
123 bool isOpen_;
124
125 std::string path_;
126
127 /// indicates if open() will overwrite a file at 'path'.
128 bool overwrite_;
129
130 /// indicates true if the db is readonly false otherwise
131 bool readonly_;
132};
133
134} // namespace cyclus
135
136#endif // CYCLUS_SRC_SQLITE_DB_H_
double GetDouble(int col)
Returns a double value for the specified column of the current query row.
Definition sqlite_db.cc:39
boost::shared_ptr< SqlStatement > Ptr
Definition sqlite_db.h:26
int GetInt(int col)
Returns an int value for the specified column of the current query row.
Definition sqlite_db.cc:35
friend class SqliteDb
Definition sqlite_db.h:23
char * GetText(int col, int *n)
Returns a byte array value for the specified column of the current query row.
Definition sqlite_db.cc:43
void BindDouble(int i, double val)
Binds the templated sql parameter at index i to val.
Definition sqlite_db.cc:56
void Reset()
Executes the prepared statement.
Definition sqlite_db.cc:21
bool Step()
Step to next row of previously executed query.
Definition sqlite_db.cc:26
void BindText(int i, const char *val)
Binds the templated sql parameter at index i to val.
Definition sqlite_db.cc:60
void BindInt(int i, int val)
Binds the templated sql parameter at index i to val.
Definition sqlite_db.cc:52
void Exec()
Executes the prepared statement.
Definition sqlite_db.cc:16
void BindBlob(int i, const void *val, int n)
Binds the templated sql parameter at index i to the value pointed to by val.
Definition sqlite_db.cc:64
An abstraction over the Sqlite native C interface to simplify database creation and data insertion.
Definition sqlite_db.h:78
void Overwrite()
Instead of opening a file of the specified name (if it already exists), overwrite it with a new empty...
Definition sqlite_db.cc:92
void open()
Opens the sqlite database by either opening/creating a file (default) or creating/overwriting a file ...
Definition sqlite_db.cc:106
void close()
Finishes any incomplete operations and closes the database.
Definition sqlite_db.cc:97
void Execute(std::string cmd)
Execute an SQL command.
Definition sqlite_db.cc:137
SqlStatement::Ptr Prepare(std::string sql)
Creates a sqlite prepared statement for the given sql.
Definition sqlite_db.cc:131
SqliteDb(std::string path, bool readonly=false)
Creates a new Sqlite database to be stored at the specified path.
Definition sqlite_db.cc:81
virtual ~SqliteDb()
Definition sqlite_db.cc:89
std::vector< StrList > Query(std::string cmd)
Execute an SQL query and return its results.
Definition sqlite_db.cc:157
const char * sqlite3()
Definition version.cc:46
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
std::vector< std::string > StrList
Definition sqlite_db.h:15