CYCLUS
sqlite_back.h
Go to the documentation of this file.
1 #ifndef CYCLUS_SRC_SQLITE_BACK_H_
2 #define CYCLUS_SRC_SQLITE_BACK_H_
3 
4 #include <string>
5 #include <map>
6 #include <set>
7 
8 #include "query_backend.h"
9 #include "sqlite_db.h"
10 
11 namespace cyclus {
12 
13 /// An Recorder backend that writes data to an sqlite database. Identically
14 /// named Datum objects have their data placed as rows in a single table. Handles the
15 /// following datum value types: int, float, double, std::string, cyclus::Blob.
16 /// Unsupported value types are stored as an empty string.
17 class SqliteBack: public FullBackend {
18  public:
19  /// Creates a new sqlite backend that will write to the database file
20  /// specified by path. If the file doesn't exist, a new one is created.
21  /// @param path the filepath (including name) to write the sqlite file.
22  SqliteBack(std::string path);
23 
24  virtual ~SqliteBack();
25 
26  /// Writes Datum objects immediately to the database as a single transaction.
27  /// @param data group of Datum objects to write to the database together.
28  virtual void Notify(DatumList data);
29 
30  /// Returns a unique name for this backend.
31  std::string Name();
32 
33  /// Executes all pending commands.
34  void Flush();
35 
36  /// Closes the backend, if approriate.
37  void Close() {};
38 
39  virtual QueryResult Query(std::string table, std::vector<Cond>* conds);
40 
41  virtual std::map<std::string, DbTypes> ColumnTypes(std::string table);
42 
43  virtual std::set<std::string> Tables();
44 
45  /// Returns the underlying sqlite database. Only use this if you really know
46  /// what you are doing.
47  SqliteDb& db();
48 
49  private:
50  void Bind(boost::spirit::hold_any v, DbTypes type, SqlStatement::Ptr stmt, int index);
51 
52  QueryResult GetTableInfo(std::string table);
53 
54  /// returns a valid sql data type name for v (e.g. INTEGER, REAL, TEXT, etc).
55  std::string SqlType(boost::spirit::hold_any v);
56 
57  /// returns a canonical string name for the type in v
59 
60  /// converts the string value in s to a c++ value corresponding the the
61  /// supported sqlite datatype type in a hold_any object.
62  boost::spirit::hold_any ColAsVal(SqlStatement::Ptr stmt, int col, DbTypes type);
63 
64  /// Queue up a table-create command for d.
65  void CreateTable(Datum* d);
66 
67  void BuildStmt(Datum* d);
68 
69  /// constructs an SQL INSERT command for d and queues it for db insertion.
70  void WriteDatum(Datum* d);
71 
72  /// An interface to a sqlite db managed by the SqliteBack class.
73  SqliteDb db_;
74 
75  /// Stores the database's path+name, declared during construction.
76  std::string path_;
77 
78  /// table names already existing (created) in the sqlite db.
79  std::set<std::string> tbl_names_;
80 
81  std::map<std::string, SqlStatement::Ptr> stmts_;
82  std::map<std::string, std::vector<DbTypes> > schemas_;
83 };
84 
85 } // namespace cyclus
86 
87 #endif // CYCLUS_SRC_SQLITE_BACK_H_
An Recorder backend that writes data to an sqlite database.
Definition: sqlite_back.h:17
Interface implemented by backends that support recording and querying.
Meta data and results of a query.
An abstraction over the Sqlite native C interface to simplify database creation and data insertion...
Definition: sqlite_db.h:77
std::string Name()
Returns a unique name for this backend.
Definition: sqlite_back.cc:172
DbTypes
This is the master list of all supported database types.
Definition: query_backend.h:26
virtual void Notify(DatumList data)
Writes Datum objects immediately to the database as a single transaction.
Definition: sqlite_back.cc:70
virtual QueryResult Query(std::string table, std::vector< Cond > *conds)
Return a set of rows from the specificed table that match all given conditions.
Definition: sqlite_back.cc:93
SqliteBack(std::string path)
Creates a new sqlite backend that will write to the database file specified by path.
Definition: sqlite_back.cc:47
Used to specify and send a collection of key-value pairs to the Recorder for recording.
Definition: datum.h:15
virtual std::set< std::string > Tables()
Return a set of all table names currently in the database.
Definition: sqlite_back.cc:137
void Flush()
Executes all pending commands.
Definition: sqlite_back.cc:91
boost::shared_ptr< SqlStatement > Ptr
Definition: sqlite_db.h:25
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
std::vector< Datum * > DatumList
Definition: rec_backend.h:12
virtual std::map< std::string, DbTypes > ColumnTypes(std::string table)
Return a map of column names of the specified table to the associated database type.
Definition: sqlite_back.cc:129
void Close()
Closes the backend, if approriate.
Definition: sqlite_back.h:37
virtual ~SqliteBack()
Definition: sqlite_back.cc:38
SqliteDb & db()
Returns the underlying sqlite database.
Definition: sqlite_back.cc:150