sqlite3x_connection.cpp

Go to the documentation of this file.
00001 /*
00002         Copyright (C) 2004-2005 Cory Nelson
00003 
00004         This software is provided 'as-is', without any express or implied
00005         warranty.  In no event will the authors be held liable for any damages
00006         arising from the use of this software.
00007 
00008         Permission is granted to anyone to use this software for any purpose,
00009         including commercial applications, and to alter it and redistribute it
00010         freely, subject to the following restrictions:
00011 
00012         1. The origin of this software must not be misrepresented; you must not
00013                 claim that you wrote the original software. If you use this software
00014                 in a product, an acknowledgment in the product documentation would be
00015                 appreciated but is not required.
00016         2. Altered source versions must be plainly marked as such, and must not be
00017                 misrepresented as being the original software.
00018         3. This notice may not be removed or altered from any source distribution.
00019 
00020         CVS Info :
00021                 $Author: phrostbyte $
00022                 $Date: 2005/06/16 20:46:40 $
00023                 $Revision: 1.1 $
00024 */
00025 
00026 /*
00027   this source contains modifications by Novell Inc.
00028 
00029   Changes:
00030 
00031   * dmacvicar@novell.com
00032     Wrap sqlite3_exec
00033 
00034 */
00035 
00036 #include <sqlite3.h>
00037 #include "sqlite3x.hpp"
00038 
00039 static int global_progress_handler(void* ptr)
00040 {
00041   //RepoImpl *r = dynamic_cast<RepoImpl *>(ptr);
00042   sqlite3x::sqlite3_connection *r = (sqlite3x::sqlite3_connection *)(ptr);
00043   if ( r )
00044     return r->_progress_handler_accessor(ptr);
00045   return 0;
00046 }
00047 
00048 namespace sqlite3x
00049 {
00050 
00051 int sqlite3_connection::_progress_handler_accessor(void* ptr)
00052 {
00053   if ( _ticks.tick() )
00054     return 0;
00055   return 1;
00056 }
00057  
00058 sqlite3_connection::sqlite3_connection() : db(NULL)
00059 {}
00060 
00061 sqlite3_connection::sqlite3_connection(const char *db) : db(NULL)
00062 {
00063   this->open(db);
00064 }
00065 
00066 sqlite3_connection::sqlite3_connection(const wchar_t *db) : db(NULL)
00067 {
00068   this->open(db);
00069 }
00070 
00071 sqlite3_connection::~sqlite3_connection()
00072 {
00073   _ticks = zypp::ProgressData();
00074   resetprogresshandler();
00075   if (this->db) sqlite3_close(this->db);
00076 }
00077 
00078 void sqlite3_connection::setprogresshandler( const zypp::ProgressData::ReceiverFnc &fnc,
00079                                              int n)
00080 {
00081   _ticks.sendTo(fnc);
00082   if ( fnc )
00083     sqlite3_progress_handler(db, n, global_progress_handler, (void*)this);
00084   else
00085     sqlite3_progress_handler(db, n, NULL, (void*)this);
00086 }
00087 
00088 void sqlite3_connection::resetprogresshandler()
00089 {
00090   _ticks = zypp::ProgressData();
00091   sqlite3_progress_handler(db, 0, NULL, (void*)this);
00092 }
00093 
00094 void sqlite3_connection::open(const char *db)
00095 {
00096   if (sqlite3_open(db, &this->db)!=SQLITE_OK)
00097   {
00098     std::string msg( "unable to open database at " );
00099     msg += ( db ? db : "NULL" );
00100     SQLITE3X_THROW(database_error(msg));
00101   }
00102 }
00103 
00104 void sqlite3_connection::open(const wchar_t *db)
00105 {
00106   if (sqlite3_open16(db, &this->db)!=SQLITE_OK)
00107     SQLITE3X_THROW(database_error("unable to open database"));
00108 }
00109 
00110 void sqlite3_connection::close()
00111 {
00112   if (this->db)
00113   {
00114     if (sqlite3_close(this->db)!=SQLITE_OK)
00115       SQLITE3X_THROW(database_error(*this));
00116     this->db=NULL;
00117   }
00118 }
00119 
00120 long long sqlite3_connection::insertid()
00121 {
00122   if (!this->db) throw database_error("database is not open");
00123   return sqlite3_last_insert_rowid(this->db);
00124 }
00125 
00126 void sqlite3_connection::setbusytimeout(int ms)
00127 {
00128   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00129 
00130   if (sqlite3_busy_timeout(this->db, ms)!=SQLITE_OK)
00131     SQLITE3X_THROW(database_error(*this));
00132 }
00133 
00134 void sqlite3_connection::executenonquery(const char *sql)
00135 {
00136   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00137   sqlite3_command(*this, sql).executenonquery();
00138 }
00139 
00140 void sqlite3_connection::executenonquery(const wchar_t *sql)
00141 {
00142   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00143   sqlite3_command(*this, sql).executenonquery();
00144 }
00145 
00146 void sqlite3_connection::executenonquery(const std::string &sql)
00147 {
00148   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00149   sqlite3_command(*this, sql).executenonquery();
00150 }
00151 
00152 void sqlite3_connection::executenonquery(const std::wstring &sql)
00153 {
00154   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00155   sqlite3_command(*this, sql).executenonquery();
00156 }
00157 
00158 int sqlite3_connection::executeint(const char *sql)
00159 {
00160   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00161   return sqlite3_command(*this, sql).executeint();
00162 }
00163 
00164 int sqlite3_connection::executeint(const wchar_t *sql)
00165 {
00166   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00167   return sqlite3_command(*this, sql).executeint();
00168 }
00169 
00170 int sqlite3_connection::executeint(const std::string &sql)
00171 {
00172   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00173   return sqlite3_command(*this, sql).executeint();
00174 }
00175 
00176 int sqlite3_connection::executeint(const std::wstring &sql)
00177 {
00178   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00179   return sqlite3_command(*this, sql).executeint();
00180 }
00181 
00182 long long sqlite3_connection::executeint64(const char *sql)
00183 {
00184   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00185   return sqlite3_command(*this, sql).executeint64();
00186 }
00187 
00188 long long sqlite3_connection::executeint64(const wchar_t *sql)
00189 {
00190   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00191   return sqlite3_command(*this, sql).executeint64();
00192 }
00193 
00194 long long sqlite3_connection::executeint64(const std::string &sql)
00195 {
00196   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00197   return sqlite3_command(*this, sql).executeint64();
00198 }
00199 
00200 long long sqlite3_connection::executeint64(const std::wstring &sql)
00201 {
00202   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00203   return sqlite3_command(*this, sql).executeint64();
00204 }
00205 
00206 double sqlite3_connection::executedouble(const char *sql)
00207 {
00208   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00209   return sqlite3_command(*this, sql).executedouble();
00210 }
00211 
00212 double sqlite3_connection::executedouble(const wchar_t *sql)
00213 {
00214   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00215   return sqlite3_command(*this, sql).executedouble();
00216 }
00217 
00218 double sqlite3_connection::executedouble(const std::string &sql)
00219 {
00220   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00221   return sqlite3_command(*this, sql).executedouble();
00222 }
00223 
00224 double sqlite3_connection::executedouble(const std::wstring &sql)
00225 {
00226   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00227   return sqlite3_command(*this, sql).executedouble();
00228 }
00229 
00230 std::string sqlite3_connection::executestring(const char *sql)
00231 {
00232   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00233   return sqlite3_command(*this, sql).executestring();
00234 }
00235 
00236 std::string sqlite3_connection::executestring(const wchar_t *sql)
00237 {
00238   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00239   return sqlite3_command(*this, sql).executestring();
00240 }
00241 
00242 std::string sqlite3_connection::executestring(const std::string &sql)
00243 {
00244   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00245   return sqlite3_command(*this, sql).executestring();
00246 }
00247 
00248 std::string sqlite3_connection::executestring(const std::wstring &sql)
00249 {
00250   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00251   return sqlite3_command(*this, sql).executestring();
00252 }
00253 
00254 std::wstring sqlite3_connection::executestring16(const char *sql)
00255 {
00256   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00257   return sqlite3_command(*this, sql).executestring16();
00258 }
00259 
00260 std::wstring sqlite3_connection::executestring16(const wchar_t *sql)
00261 {
00262   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00263   return sqlite3_command(*this, sql).executestring16();
00264 }
00265 
00266 std::wstring sqlite3_connection::executestring16(const std::string &sql)
00267 {
00268   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00269   return sqlite3_command(*this, sql).executestring16();
00270 }
00271 
00272 std::wstring sqlite3_connection::executestring16(const std::wstring &sql)
00273 {
00274   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00275   return sqlite3_command(*this, sql).executestring16();
00276 }
00277 
00278 std::string sqlite3_connection::executeblob(const char *sql)
00279 {
00280   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00281   return sqlite3_command(*this, sql).executeblob();
00282 }
00283 
00284 std::string sqlite3_connection::executeblob(const wchar_t *sql)
00285 {
00286   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00287   return sqlite3_command(*this, sql).executeblob();
00288 }
00289 
00290 std::string sqlite3_connection::executeblob(const std::string &sql)
00291 {
00292   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00293   return sqlite3_command(*this, sql).executeblob();
00294 }
00295 
00296 std::string sqlite3_connection::executeblob(const std::wstring &sql)
00297 {
00298   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00299   return sqlite3_command(*this, sql).executeblob();
00300 }
00301 
00302 void sqlite3_connection::execute(const std::string &sql)
00303 {
00304   if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
00305 
00306   char *err_msg;
00307 
00308   if ( sqlite3_exec( this->db, sql.c_str(), NULL, NULL, &err_msg ) != SQLITE_OK )
00309   {
00310     std::string err(err_msg);
00311     sqlite3_free(err_msg);
00312     SQLITE3X_THROW(database_error(err.c_str()));
00313   }
00314 }
00315 
00316 }

Generated on Tue Sep 25 19:23:00 2007 for libzypp by  doxygen 1.5.3