libsqlite3x  2007.10.18
sqlite3x_settings_db.cpp
1 
2 #include "sqlite3x_settings_db.hpp"
3 
4 namespace sqlite3x
5 {
6 
8  : m_db( 0 )
9  {
10  }
11 
12  settings_db::settings_db( std::string const & dbname )
13  : m_db( 0 )
14  {
15  this->open( dbname );
16  }
17 
19  {
20  try
21  {
22  this->close();
23  }
24  catch(...)
25  {
26  }
27  }
28 
29  bool settings_db::is_open() const
30  {
31  return 0 != this->m_db;
32  }
33 
34  void settings_db::open( std::string const & dbname )
35  {
36  this->close();
37  this->m_db = new sqlite3_connection( dbname );
38  this->init();
39  }
40 
42  {
43  if( this->m_db )
44  {
45  this->m_db->close();
46  delete this->m_db;
47  }
48  this->m_db = 0;
49  }
50 
52  {
53  this->m_db->executenonquery( "delete from settings" );
54  }
55 
56  void settings_db::clear( std::string const & where )
57  {
58  this->m_db->executenonquery( "delete from settings " + where );
59  }
60 
61 
63  {
64  return this->m_db;
65  }
66 
67  static std::string SettingsDb_Set_SQL = "insert into settings values(?,?)";
68 
69  void settings_db::set( std::string const & key, int val )
70  {
71  sqlite3_command st( *this->m_db, SettingsDb_Set_SQL );
72  st.bind( 1, key );
73  st.bind( 2, val );
74  st.executenonquery();
75  }
76 
77  void settings_db::set( std::string const & key, sqlite_int64 val )
78  {
79  sqlite3_command st( *this->m_db, SettingsDb_Set_SQL );
80  st.bind( 1, key );
81  st.bind( 2, val );
82  st.executenonquery();
83  }
84 
85  void settings_db::set( std::string const & key, bool val )
86  {
87  sqlite3_command st( *this->m_db, SettingsDb_Set_SQL );
88  st.bind( 1, key );
89  st.bind( 2, val ? 1 : 0 );
90  st.executenonquery();
91  }
92 
93  void settings_db::set( std::string const & key, double val )
94  {
95  sqlite3_command st( *this->m_db, SettingsDb_Set_SQL );
96  st.bind( 1, key );
97  st.bind( 2, val );
98  st.executenonquery();
99  }
100 
101  void settings_db::set( std::string const & key, std::string const & val )
102  {
103  sqlite3_command st( *this->m_db, SettingsDb_Set_SQL );
104  st.bind( 1, key );
105  st.bind( 2, val );
106  st.executenonquery();
107  }
108 
109  void settings_db::set( std::string const & key, char const * val )
110  {
111  sqlite3_command st( *this->m_db, SettingsDb_Set_SQL );
112  st.bind( 1, key );
113  st.bind( 2, val ? val : "" );
114  st.executenonquery();
115  }
116 
117  void settings_db::init()
118  {
119  this->m_db->executenonquery( "create table if not exists settings(key PRIMARY KEY ON CONFLICT REPLACE,value)" );
120  //this->m_db->executenonquery( "PRAGMA temp_store = MEMORY" ); // i don't like this, but want to speed up access
121  //this->m_db->executenonquery( "PRAGMA synchronous = OFF" ); // again: i don't like this but want more speed
122  }
123 
124  static std::string SettingsDb_Get_SQL = "select value from settings where key = ?";
125 
126  bool settings_db::get( std::string const & key, int & val )
127  {
128  try
129  {
130  sqlite3_command st( *this->m_db, SettingsDb_Get_SQL );
131  st.bind( 1, key );
132  val = st.executeint();
133  }
134  catch( ... )
135  {
136  return false;
137  }
138  return true;
139  }
140 
141  bool settings_db::get( std::string const & key, sqlite_int64 & val )
142  {
143  try
144  {
145  sqlite3_command st( *this->m_db, SettingsDb_Get_SQL );
146  st.bind( 1, key );
147  val = st.executeint64();
148  }
149  catch( ... )
150  {
151  return false;
152  }
153  return true;
154  }
155  bool settings_db::get( std::string const & key, bool & val )
156  {
157  try
158  {
159  sqlite3_command st( *this->m_db, SettingsDb_Get_SQL );
160  st.bind( 1, key );
161  val = (st.executeint() ? true : false);
162  }
163  catch( ... )
164  {
165  return false;
166  }
167  return true;
168  }
169  bool settings_db::get( std::string const & key, double & val )
170  {
171  try
172  {
173  sqlite3_command st( *this->m_db, SettingsDb_Get_SQL );
174  st.bind( 1, key );
175  val = st.executedouble();
176  }
177  catch( ... )
178  {
179  return false;
180  }
181  return true;
182  }
183  bool settings_db::get( std::string const & key, std::string & val )
184  {
185  try
186  {
187  sqlite3_command st( *this->m_db, SettingsDb_Get_SQL );
188  st.bind( 1, key );
189  val = st.executestring();
190  }
191  catch( ... )
192  {
193  return false;
194  }
195  return true;
196  }
197 
198 
199 
200 } // namespace
void clear()
Empties the database.
void open(std::string const &dbname)
Opens the database dbname or throws on error.
settings_db()
Creates an unopened database.
~settings_db()
Closes this database.
bool is_open() const
Returns true if open() has succeeded.
sqlite3_connection * db()
If you want low-level info about the db, here's the handle to it.
bool get(std::string const &key, int &val)
Fetches the given key from the db.
void set(std::string const &key, int val)
Sets the given key/value pair.
void close()
Closes this database.
Encapsulates a command to send to an sqlite3_connection.
Definition: sqlite3x.hpp:592
int executeint()
Executes the query, which is expected to have an integer field as the first result field.
void executenonquery()
Executes the query and provides no way to get the results.
double executedouble()
Executes the query, which is expected to have a double field as the first result field.
std::string executestring()
Executes the query, which is expected to have a string or blob field as the first result field.
int64_t executeint64()
Executes the query, which is expected to have a (int64_t) field as the first result field.
void bind(int index)
Binds NULL to the given index.
Represents a connection to an sqlite3 database.
Definition: sqlite3x.hpp:149
void close()
Closes this database.
void executenonquery(const std::string &sql)
Executes a command which is assumed to have a single step and a void result.
This namespace encapsulates a C++ API wrapper for sqlite3 databases.
Definition: sqlite3x.hpp:120