sqlite3x.hpp

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 #ifndef __SQLITE3X_HPP__
00027 #define __SQLITE3X_HPP__
00028 
00029 #include <string>
00030 #include "zypp/base/Exception.h"
00031 #include "zypp/ProgressData.h"
00032 #include <boost/utility.hpp>
00033 
00034 #define SQLITE3X_THROW zypp::ZYPP_THROW
00035 
00036 namespace sqlite3x
00037 {
00038 class sqlite3_connection : boost::noncopyable
00039 {
00040 private:
00041   friend class sqlite3_command;
00042   friend class database_error;
00043   struct sqlite3 *db;
00044   zypp::ProgressData _ticks;
00045 public:
00046   sqlite3_connection();
00047   sqlite3_connection(const char *db);
00048   sqlite3_connection(const wchar_t *db);
00049   
00055   void setprogresshandler( const zypp::ProgressData::ReceiverFnc &fnc,
00056                            int n = 100 );
00060   void resetprogresshandler();
00061   
00062   ~sqlite3_connection();
00063 
00064   void open(const char *db);
00065   void open(const wchar_t *db);
00066   void close();
00067 
00068   long long insertid();
00069   void setbusytimeout(int ms);
00070 
00071   void execute(const std::string &sql);
00072 
00073   void executenonquery(const char *sql);
00074   void executenonquery(const wchar_t *sql);
00075   void executenonquery(const std::string &sql);
00076   void executenonquery(const std::wstring &sql);
00077 
00078   int executeint(const char *sql);
00079   int executeint(const wchar_t *sql);
00080   int executeint(const std::string &sql);
00081   int executeint(const std::wstring &sql);
00082 
00083   long long executeint64(const char *sql);
00084   long long executeint64(const wchar_t *sql);
00085   long long executeint64(const std::string &sql);
00086   long long executeint64(const std::wstring &sql);
00087 
00088   double executedouble(const char *sql);
00089   double executedouble(const wchar_t *sql);
00090   double executedouble(const std::string &sql);
00091   double executedouble(const std::wstring &sql);
00092 
00093   std::string executestring(const char *sql);
00094   std::string executestring(const wchar_t *sql);
00095   std::string executestring(const std::string &sql);
00096   std::string executestring(const std::wstring &sql);
00097 
00098   std::wstring executestring16(const char *sql);
00099   std::wstring executestring16(const wchar_t *sql);
00100   std::wstring executestring16(const std::string &sql);
00101   std::wstring executestring16(const std::wstring &sql);
00102 
00103   std::string executeblob(const char *sql);
00104   std::string executeblob(const wchar_t *sql);
00105   std::string executeblob(const std::string &sql);
00106   std::string executeblob(const std::wstring &sql);
00107   
00108   int _progress_handler_accessor(void* ptr);
00109 };
00110 
00111 class sqlite3_command;
00112 
00113 class database_error : public zypp::Exception
00114 {
00115 public:
00116   database_error(const char *msg);
00117   database_error(const std::string & msg);
00118   database_error(sqlite3_connection &con);
00119 };
00120 
00121 class sqlite3_transaction : boost::noncopyable
00122 {
00123 private:
00124   sqlite3_connection &con;
00125   bool intrans;
00126 
00127 public:
00128   sqlite3_transaction(sqlite3_connection &con, bool start=true);
00129   ~sqlite3_transaction();
00130 
00131   void begin();
00132   void commit();
00133   void rollback();
00134 };
00135 
00136 class sqlite3_reader
00137 {
00138 private:
00139   friend class sqlite3_command;
00140 
00141   sqlite3_command *cmd;
00142 
00143   sqlite3_reader(sqlite3_command *cmd);
00144 
00145 public:
00146   sqlite3_reader();
00147   sqlite3_reader(const sqlite3_reader &copy);
00148   ~sqlite3_reader();
00149 
00150   sqlite3_reader& operator=(const sqlite3_reader &copy);
00151 
00152   bool read();
00153   void reset();
00154   void close();
00155 
00156   int getint(int index);
00157   long long getint64(int index);
00158   double getdouble(int index);
00159   std::string getstring(int index);
00160   std::wstring getstring16(int index);
00161   std::string getblob(int index);
00162 
00163   std::string getcolname(int index);
00164   std::wstring getcolname16(int index);
00165 };
00166 
00167 class sqlite3_command : boost::noncopyable
00168 {
00169 private:
00170   friend class sqlite3_reader;
00171 
00172   sqlite3_connection &con;
00173   struct sqlite3_stmt *stmt;
00174   unsigned int refs;
00175   int argc;
00176 
00177 public:
00178   sqlite3_command(sqlite3_connection &con, const char *sql);
00179   sqlite3_command(sqlite3_connection &con, const wchar_t *sql);
00180   sqlite3_command(sqlite3_connection &con, const std::string &sql);
00181   sqlite3_command(sqlite3_connection &con, const std::wstring &sql);
00182   ~sqlite3_command();
00183 
00184   void bind(int index);
00185   void bind(int index, int data);
00186   void bind(int index, long long data);
00187   void bind(int index, double data);
00188   void bind(int index, const char *data, int datalen);
00189   void bind(int index, const wchar_t *data, int datalen);
00190   void bind(int index, const void *data, int datalen);
00191   void bind(int index, const std::string &data);
00192   void bind(int index, const std::wstring &data);
00193 
00194   void bind(const std::string &param);
00195   void bind(const std::string &param, int data);
00196   void bind(const std::string &param, long long data);
00197   void bind(const std::string &param, double data);
00198   void bind(const std::string &param, const char *data, int datalen);
00199   void bind(const std::string &param, const wchar_t *data, int datalen);
00200   void bind(const std::string &param, const void *data, int datalen);
00201   void bind(const std::string &param, const std::string &data);
00202   void bind(const std::string &param, const std::wstring &data);
00203 
00204   sqlite3_reader executereader();
00205   void executenonquery();
00206   int executeint();
00207   long long executeint64();
00208   double executedouble();
00209   std::string executestring();
00210   std::wstring executestring16();
00211   std::string executeblob();
00212 };
00213 
00214 }
00215 
00216 #endif

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