libsq3
2007.10.18
|
00001 00002 #include "sq3_settings_db.hpp" 00003 00004 namespace sq3 00005 { 00006 00007 settings_db::settings_db() 00008 : database() 00009 { 00010 } 00011 00012 settings_db::settings_db( std::string const & dbname ) 00013 : database() 00014 { 00015 this->open( dbname ); 00016 } 00017 00018 settings_db::~settings_db() 00019 { 00020 } 00021 00022 int settings_db::clear() 00023 { 00024 return this->execute( "delete from settings" ); 00025 } 00026 00027 int settings_db::clear( std::string const & where ) 00028 { 00029 return this->execute( "delete from settings " + where ); 00030 } 00031 00032 00033 static std::string SettingsDb_Set_SQL = "insert into settings values(?,?)"; 00034 00035 void settings_db::set( std::string const & key, int val ) 00036 { 00037 statement st( *this, SettingsDb_Set_SQL ); 00038 st.bind( 1, key ); 00039 st.bind( 2, val ); 00040 st.execute(); 00041 } 00042 00043 void settings_db::set( std::string const & key, sqlite_int64 val ) 00044 { 00045 statement st( *this, SettingsDb_Set_SQL ); 00046 st.bind( 1, key ); 00047 st.bind( 2, val ); 00048 st.execute(); 00049 } 00050 00051 void settings_db::set( std::string const & key, bool val ) 00052 { 00053 statement st( *this, SettingsDb_Set_SQL ); 00054 st.bind( 1, key ); 00055 st.bind( 2, val ? 1 : 0 ); 00056 st.execute(); 00057 } 00058 00059 void settings_db::set( std::string const & key, double val ) 00060 { 00061 statement st( *this, SettingsDb_Set_SQL ); 00062 st.bind( 1, key ); 00063 st.bind( 2, val ); 00064 st.execute(); 00065 } 00066 00067 void settings_db::set( std::string const & key, std::string const & val ) 00068 { 00069 statement st( *this, SettingsDb_Set_SQL ); 00070 st.bind( 1, key ); 00071 st.bind( 2, val ); 00072 st.execute(); 00073 } 00074 00075 void settings_db::set( std::string const & key, char const * val ) 00076 { 00077 statement st( *this, SettingsDb_Set_SQL ); 00078 st.bind( 1, key ); 00079 st.bind( 2, val ? val : "" ); 00080 st.execute(); 00081 } 00082 00083 int settings_db::on_open() 00084 { 00085 int rc = this->execute( "create table if not exists settings(key PRIMARY KEY ON CONFLICT REPLACE,value)" ); 00086 this->execute( "PRAGMA temp_store = MEMORY" ); // i don't like this, but want to speed up access 00087 this->pragma( "synchronous = OFF" ); // again: i don't like this but want more speed 00088 return rc_is_okay(rc) ? SQLITE_OK : rc; 00089 } 00090 00091 static std::string SettingsDb_Get_SQL = "select value from settings where key = ?"; 00092 00093 bool settings_db::get( std::string const & key, int & val ) 00094 { 00095 try 00096 { 00097 statement st( *this, SettingsDb_Get_SQL ); 00098 st.bind( 1, key ); 00099 return rc_is_okay( st.execute(val) ); 00100 } 00101 catch( ... ) 00102 { 00103 return false; 00104 } 00105 return true; 00106 } 00107 00108 bool settings_db::get( std::string const & key, sqlite_int64 & val ) 00109 { 00110 try 00111 { 00112 statement st( *this, SettingsDb_Get_SQL ); 00113 st.bind( 1, key ); 00114 return rc_is_okay( st.execute( val ) ); 00115 } 00116 catch( ... ) 00117 { 00118 return false; 00119 } 00120 return true; 00121 } 00122 bool settings_db::get( std::string const & key, bool & val ) 00123 { 00124 try 00125 { 00126 statement st( *this, SettingsDb_Get_SQL ); 00127 st.bind( 1, key ); 00128 int foo; 00129 int rc = st.execute(foo); 00130 if( rc_is_okay( rc ) ) 00131 { 00132 val = ( foo ? true : false); 00133 return true; 00134 } 00135 return false; 00136 } 00137 catch( ... ) 00138 { 00139 return false; 00140 } 00141 } 00142 bool settings_db::get( std::string const & key, double & val ) 00143 { 00144 try 00145 { 00146 statement st( *this, SettingsDb_Get_SQL ); 00147 st.bind( 1, key ); 00148 return rc_is_okay( st.execute(val) ); 00149 } 00150 catch( ... ) 00151 { 00152 return false; 00153 } 00154 return true; 00155 } 00156 bool settings_db::get( std::string const & key, std::string & val ) 00157 { 00158 try 00159 { 00160 statement st( *this, SettingsDb_Get_SQL ); 00161 st.bind( 1, key ); 00162 return rc_is_okay( st.execute(val) ); 00163 } 00164 catch( ... ) 00165 { 00166 return false; 00167 } 00168 return true; 00169 } 00170 00171 00172 00173 } // namespace