Phasor  01.00.10.059
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sqlitepp.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 #include <map>
6 #include "../../sqlite/sqlite3.h"
7 #include "../Common/Common.h"
8 
9 #pragma comment(lib, "../release/sqlite.lib")
10 
11 namespace sqlite
12 {
13  using namespace Common;
14 
15  class SQLiteResult;
16  class SQLiteRow;
17  class SQLiteQuery;
18 
19  /* Error codes */
21  {
22  SQL_ERRCODE_FAILED_OPEN, // sqlite3_open failed for some reason
23  SQL_ERRCODE_NO_INIT, // Attempting to use data that hasn't been initialized
24  SQL_ERRCODE_BAD_INDEX, // Attempted to access out of bounds data
25  SQL_ERRCODE_NO_PARAM, // Attempting to bind to a non existent parameter
26  SQL_ERRCODE_MISUSE, // The API was used incorrectly
27  SQL_ERRCODE_CLOSED, // The database has been closed
28  SQL_ERRCODE_UNK // unknown error
29  };
30 
31  static const char* err_descs[] = {
32  {"Unable to open the database file."},
33  {"Attempted to use uninitialized data."},
34  {"Out of bounds access attempt on SQLiteResult or SQLiteRow"},
35  {"Attempted to bind to an unknown parameter"},
36  {"api misuse"},
37  {"The database has been closed and an operation was attempted."},
38  {"Unknown error"}
39  };
40 
41  /* Types of data that can be stored in SQLiteValue */
43  {
49  };
50 
51  //-----------------------------------------------------------------------------------------
52  // Class: SQLiteError
53  // Purpose: Exception class used by this namespace
54  class SQLiteError : public std::exception
55  {
56  public:
57  SQLiteError(int error);
58  SQLiteError(const char* e);
59  virtual ~SQLiteError();
60  };
61 
62  //-----------------------------------------------------------------------------------------
63  // Class: SQLite
64  // Purpose: Provide a basic wrapper around the sqlite3 C interface.
65  class SQLite
66  {
67  private:
68  sqlite3* sqlhandle;
69 
70  public:
71  SQLite(const std::string& path_to_db);
72  ~SQLite();
73 
74  // Closes the database connection
75  void Close();
76 
77  // Creates a new query
78  std::unique_ptr<SQLiteQuery> NewQuery(const std::wstring& query);
79 
80  // Returns a pointer to the open sqlite3 database. If the database
81  // has been closed an exception is thrown.
82  sqlite3* GetSQLite();
83 
84  // Gets the last error
85  const char* LastError();
86 
87  friend class SQLiteQuery;
88  friend class SQLiteObject;
89  };
90 
91  //-----------------------------------------------------------------------------------------
92  // Class: SQLiteObject
93  // Purpose: Base class for objects returned to the user which require
94  // an associated state.
96  {
97  protected:
99 
100  public:
101  SQLiteObject(SQLite& parent);
102  virtual ~SQLiteObject();
103 
104  friend class SQLite;
105  };
106 
107  //-----------------------------------------------------------------------------------------
108  // Class: SQLiteValue
109  // Purpose: Wrapper for converting types
110  struct SQLiteValue
111  {
114 
115  SQLiteValue(const std::string& val);
116  SQLiteValue(const std::wstring& val);
117  SQLiteValue(const char* val);
118  SQLiteValue(const wchar_t* val);
119  SQLiteValue(int val);
120  SQLiteValue(double val);
121  SQLiteValue(BYTE* val, size_t length);
122 
123  friend class SQLiteRow;
124  };
125 
126 
127  //-----------------------------------------------------------------------------------------
128  // Class: SQLiteQuery
129  // Purpose: Provide an interface for executing prepared queries
130  class SQLiteQuery : public SQLiteObject
131  {
132  private:
133  sqlite3_stmt* stmt;
134  std::wstring query;
135 
136  // Prepares the statement
137  void prepare_statement();
138 
139  public:
140 
141  SQLiteQuery(SQLite& parent, const std::wstring& query);
142  virtual ~SQLiteQuery();
143 
144  // Executes the statement
145  std::unique_ptr<SQLiteResult> Execute();
146 
147  // Resets the statement to use the given query
148  void Reset(const std::wstring& query);
149 
150  // Binds values to the required parameters
151  void BindValue(const char* name, const SQLiteValue& value);
152  void BindValue(int index, const SQLiteValue& value);
153 
154  friend class SQLite;
155  };
156 
157  //-----------------------------------------------------------------------------------------
158  // Class: SQLiteRow
159  // Purpose: Represent a row of data returned
160  class SQLiteRow
161  {
162  private:
163  typedef std::pair<std::string, Object::unique_ptr> pair_t;
164  std::map<std::string, Object::unique_ptr> columns;
165 
166  /* Adds data to the row */
167  void AddColumn(Object::unique_ptr value,
168  const std::string& name);
169  public:
170 
171  Object& operator[] (const std::string& key);
172  Object& get(const std::string& key);
173 
174  /* Returns the number of items in the row*/
175  size_t size();
176 
177  friend class SQLiteQuery;
178  friend class SQLiteResult;
179  };
180 
181  //-----------------------------------------------------------------------------------------
182  // Class: SQLiteResult
183  // Purpose: Represent a data set returned via SELECT statements
185  {
186  private:
187  // used to store the retrieved rows
188  std::vector<std::unique_ptr<SQLiteRow>> rows;
189 
190  // Adds a new row to the result
191  void AddRow(std::unique_ptr<SQLiteRow> row);
192 
193  public:
194 
195  // Returns the row at position i.
196  SQLiteRow& operator[] (size_t i);
197  SQLiteRow& get(size_t i);
198 
199  size_t size();
200  friend class SQLiteQuery;
201  };
202 }