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