sqlite3x_command.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 #include <sqlite3.h>
00027 #include "sqlite3x.hpp"
00028 
00029 namespace sqlite3x
00030 {
00031 
00032 sqlite3_command::sqlite3_command(sqlite3_connection &con, const char *sql) : con(con),refs(0)
00033 {
00034   const char *tail=NULL;
00035   if (sqlite3_prepare(con.db, sql, -1, &this->stmt, &tail)!=SQLITE_OK)
00036     SQLITE3X_THROW(database_error(con));
00037 
00038   this->argc=sqlite3_column_count(this->stmt);
00039 }
00040 
00041 sqlite3_command::sqlite3_command(sqlite3_connection &con, const wchar_t *sql) : con(con),refs(0)
00042 {
00043   const wchar_t *tail=NULL;
00044   const wchar_t **tmpptr=&tail;
00045   if (sqlite3_prepare16(con.db, sql, -1, &this->stmt, (const void**)tmpptr)!=SQLITE_OK)
00046     SQLITE3X_THROW(database_error(con));
00047 
00048   this->argc=sqlite3_column_count(this->stmt);
00049 }
00050 
00051 sqlite3_command::sqlite3_command(sqlite3_connection &con, const std::string &sql) : con(con),refs(0)
00052 {
00053   const char *tail=NULL;
00054   if (sqlite3_prepare(con.db, sql.data(), (int)sql.length(), &this->stmt, &tail)!=SQLITE_OK)
00055     SQLITE3X_THROW(database_error(con));
00056 
00057   this->argc=sqlite3_column_count(this->stmt);
00058 }
00059 
00060 sqlite3_command::sqlite3_command(sqlite3_connection &con, const std::wstring &sql) : con(con),refs(0)
00061 {
00062   const wchar_t *tail=NULL;
00063   const wchar_t **tmpptr=&tail;
00064   if (sqlite3_prepare16(con.db, sql.data(), (int)sql.length()*2, &this->stmt, (const void**)tmpptr)!=SQLITE_OK)
00065     SQLITE3X_THROW(database_error(con));
00066 
00067   this->argc=sqlite3_column_count(this->stmt);
00068 }
00069 
00070 sqlite3_command::~sqlite3_command()
00071 {
00072   sqlite3_finalize(this->stmt);
00073 }
00074 
00075 void sqlite3_command::bind(int index)
00076 {
00077   if (sqlite3_bind_null(this->stmt, index)!=SQLITE_OK)
00078     SQLITE3X_THROW(database_error(this->con));
00079 }
00080 
00081 void sqlite3_command::bind(int index, int data)
00082 {
00083   if (sqlite3_bind_int(this->stmt, index, data)!=SQLITE_OK)
00084     SQLITE3X_THROW(database_error(this->con));
00085 }
00086 
00087 void sqlite3_command::bind(int index, long long data)
00088 {
00089   if (sqlite3_bind_int64(this->stmt, index, data)!=SQLITE_OK)
00090     SQLITE3X_THROW(database_error(this->con));
00091 }
00092 
00093 void sqlite3_command::bind(int index, double data)
00094 {
00095   if (sqlite3_bind_double(this->stmt, index, data)!=SQLITE_OK)
00096     SQLITE3X_THROW(database_error(this->con));
00097 }
00098 
00099 void sqlite3_command::bind(int index, const char *data, int datalen)
00100 {
00101   if (sqlite3_bind_text(this->stmt, index, data, datalen, SQLITE_TRANSIENT)!=SQLITE_OK)
00102     SQLITE3X_THROW(database_error(this->con));
00103 }
00104 
00105 void sqlite3_command::bind(int index, const wchar_t *data, int datalen)
00106 {
00107   if (sqlite3_bind_text16(this->stmt, index, data, datalen, SQLITE_TRANSIENT)!=SQLITE_OK)
00108     SQLITE3X_THROW(database_error(this->con));
00109 }
00110 
00111 void sqlite3_command::bind(int index, const void *data, int datalen)
00112 {
00113   if (sqlite3_bind_blob(this->stmt, index, data, datalen, SQLITE_TRANSIENT)!=SQLITE_OK)
00114     SQLITE3X_THROW(database_error(this->con));
00115 }
00116 
00117 void sqlite3_command::bind(int index, const std::string &data)
00118 {
00119   if (sqlite3_bind_text(this->stmt, index, data.data(), (int)data.length(), SQLITE_TRANSIENT)!=SQLITE_OK)
00120     SQLITE3X_THROW(database_error(this->con));
00121 }
00122 
00123 void sqlite3_command::bind(int index, const std::wstring &data)
00124 {
00125   if (sqlite3_bind_text16(this->stmt, index, data.data(), (int)data.length()*2, SQLITE_TRANSIENT)!=SQLITE_OK)
00126     SQLITE3X_THROW(database_error(this->con));
00127 }
00128 
00129 // named parameters bind
00130 
00131 void sqlite3_command::bind(const std::string &param)
00132 {
00133   if (sqlite3_bind_null(this->stmt, sqlite3_bind_parameter_index( this->stmt, param.c_str() ) )!=SQLITE_OK)
00134     SQLITE3X_THROW(database_error(this->con));
00135 }
00136 
00137 void sqlite3_command::bind(const std::string &param, int data)
00138 {
00139   if (sqlite3_bind_int(this->stmt, sqlite3_bind_parameter_index( this->stmt, param.c_str() ) , data)!=SQLITE_OK)
00140     SQLITE3X_THROW(database_error(this->con));
00141 }
00142 
00143 void sqlite3_command::bind(const std::string &param, long long data)
00144 {
00145   if (sqlite3_bind_int64(this->stmt, sqlite3_bind_parameter_index( this->stmt, param.c_str() ) , data)!=SQLITE_OK)
00146     SQLITE3X_THROW(database_error(this->con));
00147 }
00148 
00149 void sqlite3_command::bind(const std::string &param, double data)
00150 {
00151   if (sqlite3_bind_double(this->stmt, sqlite3_bind_parameter_index( this->stmt, param.c_str() ) , data)!=SQLITE_OK)
00152     SQLITE3X_THROW(database_error(this->con));
00153 }
00154 
00155 void sqlite3_command::bind(const std::string &param, const char *data, int datalen)
00156 {
00157   if (sqlite3_bind_text(this->stmt, sqlite3_bind_parameter_index( this->stmt, param.c_str() ) , data, datalen, SQLITE_TRANSIENT)!=SQLITE_OK)
00158     SQLITE3X_THROW(database_error(this->con));
00159 }
00160 
00161 void sqlite3_command::bind(const std::string &param, const wchar_t *data, int datalen)
00162 {
00163   if (sqlite3_bind_text16(this->stmt, sqlite3_bind_parameter_index( this->stmt, param.c_str() ) , data, datalen, SQLITE_TRANSIENT)!=SQLITE_OK)
00164     SQLITE3X_THROW(database_error(this->con));
00165 }
00166 
00167 void sqlite3_command::bind(const std::string &param, const void *data, int datalen)
00168 {
00169   if (sqlite3_bind_blob(this->stmt, sqlite3_bind_parameter_index( this->stmt, param.c_str() ) , data, datalen, SQLITE_TRANSIENT)!=SQLITE_OK)
00170     SQLITE3X_THROW(database_error(this->con));
00171 }
00172 
00173 void sqlite3_command::bind(const std::string &param, const std::string &data)
00174 {
00175   if (sqlite3_bind_text(this->stmt, sqlite3_bind_parameter_index( this->stmt, param.c_str() ) , data.data(), (int)data.length(), SQLITE_TRANSIENT)!=SQLITE_OK)
00176     SQLITE3X_THROW(database_error(this->con));
00177 }
00178 
00179 void sqlite3_command::bind(const std::string &param, const std::wstring &data)
00180 {
00181   if (sqlite3_bind_text16(this->stmt, sqlite3_bind_parameter_index( this->stmt, param.c_str() ) , data.data(), (int)data.length()*2, SQLITE_TRANSIENT)!=SQLITE_OK)
00182     SQLITE3X_THROW(database_error(this->con));
00183 }
00184 
00185 
00186 
00187 sqlite3_reader sqlite3_command::executereader()
00188 {
00189   return sqlite3_reader(this);
00190 }
00191 
00192 void sqlite3_command::executenonquery()
00193 {
00194   this->executereader().read();
00195 }
00196 
00197 int sqlite3_command::executeint()
00198 {
00199   sqlite3_reader reader=this->executereader();
00200   if (!reader.read()) SQLITE3X_THROW(database_error("nothing to read"));
00201   return reader.getint(0);
00202 }
00203 
00204 long long sqlite3_command::executeint64()
00205 {
00206   sqlite3_reader reader=this->executereader();
00207   if (!reader.read()) SQLITE3X_THROW(database_error("nothing to read"));
00208   return reader.getint64(0);
00209 }
00210 
00211 double sqlite3_command::executedouble()
00212 {
00213   sqlite3_reader reader=this->executereader();
00214   if (!reader.read()) SQLITE3X_THROW(database_error("nothing to read"));
00215   return reader.getdouble(0);
00216 }
00217 
00218 std::string sqlite3_command::executestring()
00219 {
00220   sqlite3_reader reader=this->executereader();
00221   if (!reader.read()) SQLITE3X_THROW(database_error("nothing to read"));
00222   return reader.getstring(0);
00223 }
00224 
00225 std::wstring sqlite3_command::executestring16()
00226 {
00227   sqlite3_reader reader=this->executereader();
00228   if (!reader.read()) SQLITE3X_THROW(database_error("nothing to read"));
00229   return reader.getstring16(0);
00230 }
00231 
00232 std::string sqlite3_command::executeblob()
00233 {
00234   sqlite3_reader reader=this->executereader();
00235   if (!reader.read()) SQLITE3X_THROW(database_error("nothing to read"));
00236   return reader.getblob(0);
00237 }
00238 
00239 }

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