00001
00002 #include "sqlite3x_settings_db.hpp"
00003
00004 namespace sqlite3x
00005 {
00006
00007 settings_db::settings_db()
00008 : m_db( 0 )
00009 {
00010 }
00011
00012 settings_db::settings_db( std::string const & dbname )
00013 : m_db( 0 )
00014 {
00015 this->open( dbname );
00016 }
00017
00018 settings_db::~settings_db()
00019 {
00020 try
00021 {
00022 this->close();
00023 }
00024 catch(...)
00025 {
00026 }
00027 }
00028
00029 bool settings_db::is_open() const
00030 {
00031 return 0 != this->m_db;
00032 }
00033
00034 void settings_db::open( std::string const & dbname )
00035 {
00036 this->close();
00037 this->m_db = new sqlite3_connection( dbname );
00038 this->init();
00039 }
00040
00041 void settings_db::close()
00042 {
00043 if( this->m_db )
00044 {
00045 this->m_db->close();
00046 delete this->m_db;
00047 }
00048 this->m_db = 0;
00049 }
00050
00051 void settings_db::clear()
00052 {
00053 this->m_db->executenonquery( "delete from settings" );
00054 }
00055
00056 void settings_db::clear( std::string const & where )
00057 {
00058 this->m_db->executenonquery( "delete from settings " + where );
00059 }
00060
00061
00062 sqlite3_connection * settings_db::db()
00063 {
00064 return this->m_db;
00065 }
00066
00067 static std::string SettingsDb_Set_SQL = "insert into settings values(?,?)";
00068
00069 void settings_db::set( std::string const & key, int val )
00070 {
00071 sqlite3_command st( *this->m_db, SettingsDb_Set_SQL );
00072 st.bind( 1, key );
00073 st.bind( 2, val );
00074 st.executenonquery();
00075 }
00076
00077 void settings_db::set( std::string const & key, sqlite_int64 val )
00078 {
00079 sqlite3_command st( *this->m_db, SettingsDb_Set_SQL );
00080 st.bind( 1, key );
00081 st.bind( 2, val );
00082 st.executenonquery();
00083 }
00084
00085 void settings_db::set( std::string const & key, bool val )
00086 {
00087 sqlite3_command st( *this->m_db, SettingsDb_Set_SQL );
00088 st.bind( 1, key );
00089 st.bind( 2, val ? 1 : 0 );
00090 st.executenonquery();
00091 }
00092
00093 void settings_db::set( std::string const & key, double val )
00094 {
00095 sqlite3_command st( *this->m_db, SettingsDb_Set_SQL );
00096 st.bind( 1, key );
00097 st.bind( 2, val );
00098 st.executenonquery();
00099 }
00100
00101 void settings_db::set( std::string const & key, std::string const & val )
00102 {
00103 sqlite3_command st( *this->m_db, SettingsDb_Set_SQL );
00104 st.bind( 1, key );
00105 st.bind( 2, val );
00106 st.executenonquery();
00107 }
00108
00109 void settings_db::set( std::string const & key, char const * val )
00110 {
00111 sqlite3_command st( *this->m_db, SettingsDb_Set_SQL );
00112 st.bind( 1, key );
00113 st.bind( 2, val ? val : "" );
00114 st.executenonquery();
00115 }
00116
00117 void settings_db::init()
00118 {
00119 this->m_db->executenonquery( "create table if not exists settings(key PRIMARY KEY ON CONFLICT REPLACE,value)" );
00120
00121
00122 }
00123
00124 static std::string SettingsDb_Get_SQL = "select value from settings where key = ?";
00125
00126 bool settings_db::get( std::string const & key, int & val )
00127 {
00128 try
00129 {
00130 sqlite3_command st( *this->m_db, SettingsDb_Get_SQL );
00131 st.bind( 1, key );
00132 val = st.executeint();
00133 }
00134 catch( ... )
00135 {
00136 return false;
00137 }
00138 return true;
00139 }
00140
00141 bool settings_db::get( std::string const & key, sqlite_int64 & val )
00142 {
00143 try
00144 {
00145 sqlite3_command st( *this->m_db, SettingsDb_Get_SQL );
00146 st.bind( 1, key );
00147 val = st.executeint64();
00148 }
00149 catch( ... )
00150 {
00151 return false;
00152 }
00153 return true;
00154 }
00155 bool settings_db::get( std::string const & key, bool & val )
00156 {
00157 try
00158 {
00159 sqlite3_command st( *this->m_db, SettingsDb_Get_SQL );
00160 st.bind( 1, key );
00161 val = (st.executeint() ? true : false);
00162 }
00163 catch( ... )
00164 {
00165 return false;
00166 }
00167 return true;
00168 }
00169 bool settings_db::get( std::string const & key, double & val )
00170 {
00171 try
00172 {
00173 sqlite3_command st( *this->m_db, SettingsDb_Get_SQL );
00174 st.bind( 1, key );
00175 val = st.executedouble();
00176 }
00177 catch( ... )
00178 {
00179 return false;
00180 }
00181 return true;
00182 }
00183 bool settings_db::get( std::string const & key, std::string & val )
00184 {
00185 try
00186 {
00187 sqlite3_command st( *this->m_db, SettingsDb_Get_SQL );
00188 st.bind( 1, key );
00189 val = st.executestring();
00190 }
00191 catch( ... )
00192 {
00193 return false;
00194 }
00195 return true;
00196 }
00197
00198
00199
00200 }