libsq3  2007.10.18
sq3_settings_db.cpp
1 
2 #include "sq3_settings_db.hpp"
3 
4 namespace sq3
5 {
6 
8  : database()
9  {
10  }
11 
12  settings_db::settings_db( std::string const & dbname )
13  : database()
14  {
15  this->open( dbname );
16  }
17 
19  {
20  }
21 
23  {
24  return this->execute( "delete from settings" );
25  }
26 
27  int settings_db::clear( std::string const & where )
28  {
29  return this->execute( "delete from settings " + where );
30  }
31 
32 
33  static std::string SettingsDb_Set_SQL = "insert into settings values(?,?)";
34 
35  void settings_db::set( std::string const & key, int val )
36  {
37  statement st( *this, SettingsDb_Set_SQL );
38  st.bind( 1, key );
39  st.bind( 2, val );
40  st.execute();
41  }
42 
43  void settings_db::set( std::string const & key, sqlite_int64 val )
44  {
45  statement st( *this, SettingsDb_Set_SQL );
46  st.bind( 1, key );
47  st.bind( 2, val );
48  st.execute();
49  }
50 
51  void settings_db::set( std::string const & key, bool val )
52  {
53  statement st( *this, SettingsDb_Set_SQL );
54  st.bind( 1, key );
55  st.bind( 2, val ? 1 : 0 );
56  st.execute();
57  }
58 
59  void settings_db::set( std::string const & key, double val )
60  {
61  statement st( *this, SettingsDb_Set_SQL );
62  st.bind( 1, key );
63  st.bind( 2, val );
64  st.execute();
65  }
66 
67  void settings_db::set( std::string const & key, std::string const & val )
68  {
69  statement st( *this, SettingsDb_Set_SQL );
70  st.bind( 1, key );
71  st.bind( 2, val );
72  st.execute();
73  }
74 
75  void settings_db::set( std::string const & key, char const * val )
76  {
77  statement st( *this, SettingsDb_Set_SQL );
78  st.bind( 1, key );
79  st.bind( 2, val ? val : "" );
80  st.execute();
81  }
82 
83  int settings_db::on_open()
84  {
85  int rc = this->execute( "create table if not exists settings(key PRIMARY KEY ON CONFLICT REPLACE,value)" );
86  this->execute( "PRAGMA temp_store = MEMORY" ); // i don't like this, but want to speed up access
87  this->pragma( "synchronous = OFF" ); // again: i don't like this but want more speed
88  return rc_is_okay(rc) ? SQLITE_OK : rc;
89  }
90 
91  static std::string SettingsDb_Get_SQL = "select value from settings where key = ?";
92 
93  bool settings_db::get( std::string const & key, int & val )
94  {
95  try
96  {
97  statement st( *this, SettingsDb_Get_SQL );
98  st.bind( 1, key );
99  return rc_is_okay( st.execute(val) );
100  }
101  catch( ... )
102  {
103  return false;
104  }
105  return true;
106  }
107 
108  bool settings_db::get( std::string const & key, sqlite_int64 & val )
109  {
110  try
111  {
112  statement st( *this, SettingsDb_Get_SQL );
113  st.bind( 1, key );
114  return rc_is_okay( st.execute( val ) );
115  }
116  catch( ... )
117  {
118  return false;
119  }
120  return true;
121  }
122  bool settings_db::get( std::string const & key, bool & val )
123  {
124  try
125  {
126  statement st( *this, SettingsDb_Get_SQL );
127  st.bind( 1, key );
128  int foo;
129  int rc = st.execute(foo);
130  if( rc_is_okay( rc ) )
131  {
132  val = ( foo ? true : false);
133  return true;
134  }
135  return false;
136  }
137  catch( ... )
138  {
139  return false;
140  }
141  }
142  bool settings_db::get( std::string const & key, double & val )
143  {
144  try
145  {
146  statement st( *this, SettingsDb_Get_SQL );
147  st.bind( 1, key );
148  return rc_is_okay( st.execute(val) );
149  }
150  catch( ... )
151  {
152  return false;
153  }
154  return true;
155  }
156  bool settings_db::get( std::string const & key, std::string & val )
157  {
158  try
159  {
160  statement st( *this, SettingsDb_Get_SQL );
161  st.bind( 1, key );
162  return rc_is_okay( st.execute(val) );
163  }
164  catch( ... )
165  {
166  return false;
167  }
168  return true;
169  }
170 
171 
172 
173 } // namespace
Encapsulates a connection to an sqlite database.
Definition: sq3.hpp:232
int execute(const std::string &sql)
Functionally identical to execute(char const *).
Definition: sq3.cpp:698
settings_db()
Creates an unopened database.
int bind(int index)
Binds NULL to the given placeholder index (1-based, not 0-based!).
Definition: sq3.cpp:173
virtual int open(char const *, long flags=0)
Creates/opens the given db file.
Definition: sq3.cpp:591
int pragma(char const *code)
This is a convenience wrapper for execute( "pragma ..." ).
Definition: sq3.cpp:814
virtual int clear()
Overridden to just empty the settings db.
int execute()
Assumes this object's SQL statement is a single statement.
Definition: sq3.cpp:250
The sq3 namespace encapsulates an OO sqlite3 API very similar to the sqlite3x API,...
Definition: sq3.hpp:77
bool get(std::string const &key, int &val)
Fetches the given key from the db.
bool rc_is_okay(int rc)
rc_is_okay() is an easy way to check if rc is one of SQLITE_OK, SQLITE_ROW, or SQLITE_DONE.
Definition: sq3.cpp:56
void set(std::string const &key, int val)
Sets the given key/value pair.
~settings_db()
Closes this database.