sqlite3x_reader.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_reader::sqlite3_reader() : cmd(NULL)
00033 {}
00034 
00035 sqlite3_reader::sqlite3_reader(const sqlite3_reader &copy) : cmd(copy.cmd)
00036 {
00037   if (this->cmd) ++this->cmd->refs;
00038 }
00039 
00040 sqlite3_reader::sqlite3_reader(sqlite3_command *cmd) : cmd(cmd)
00041 {
00042   ++cmd->refs;
00043 }
00044 
00045 sqlite3_reader::~sqlite3_reader()
00046 {
00047   this->close();
00048 }
00049 
00050 sqlite3_reader& sqlite3_reader::operator=(const sqlite3_reader &copy)
00051 {
00052   this->close();
00053 
00054   this->cmd=copy.cmd;
00055   if (this->cmd) ++this->cmd->refs;
00056 
00057   return *this;
00058 }
00059 
00060 bool sqlite3_reader::read()
00061 {
00062   if (!this->cmd) SQLITE3X_THROW(database_error("reader is closed"));
00063 
00064   switch (sqlite3_step(this->cmd->stmt))
00065   {
00066   case SQLITE_ROW:
00067     return true;
00068   case SQLITE_DONE:
00069     return false;
00070   default:
00071     SQLITE3X_THROW(database_error(this->cmd->con));
00072   }
00073 }
00074 
00075 void sqlite3_reader::reset()
00076 {
00077   if (!this->cmd) SQLITE3X_THROW(database_error("reader is closed"));
00078 
00079   if (sqlite3_reset(this->cmd->stmt)!=SQLITE_OK)
00080     SQLITE3X_THROW(database_error(this->cmd->con));
00081 }
00082 
00083 void sqlite3_reader::close()
00084 {
00085   if (this->cmd)
00086   {
00087     if (--this->cmd->refs==0) sqlite3_reset(this->cmd->stmt);
00088     this->cmd=NULL;
00089   }
00090 }
00091 
00092 int sqlite3_reader::getint(int index)
00093 {
00094   if (!this->cmd) SQLITE3X_THROW(database_error("reader is closed"));
00095   if ((index)>(this->cmd->argc-1))  throw std::out_of_range("index out of range");
00096   return sqlite3_column_int(this->cmd->stmt, index);
00097 }
00098 
00099 long long sqlite3_reader::getint64(int index)
00100 {
00101   if (!this->cmd) SQLITE3X_THROW(database_error("reader is closed"));
00102   if ((index)>(this->cmd->argc-1))  throw std::out_of_range("index out of range");
00103   return sqlite3_column_int64(this->cmd->stmt, index);
00104 }
00105 
00106 double sqlite3_reader::getdouble(int index)
00107 {
00108   if (!this->cmd) SQLITE3X_THROW(database_error("reader is closed"));
00109   if ((index)>(this->cmd->argc-1))  throw std::out_of_range("index out of range");
00110   return sqlite3_column_double(this->cmd->stmt, index);
00111 }
00112 
00113 std::string sqlite3_reader::getstring(int index)
00114 {
00115   if (!this->cmd) SQLITE3X_THROW(database_error("reader is closed"));
00116   if ((index)>(this->cmd->argc-1))  throw std::out_of_range("index out of range");
00117   return std::string((const char*)sqlite3_column_text(this->cmd->stmt, index), sqlite3_column_bytes(this->cmd->stmt, index));
00118 }
00119 
00120 std::wstring sqlite3_reader::getstring16(int index)
00121 {
00122   if (!this->cmd) SQLITE3X_THROW(database_error("reader is closed"));
00123   if ((index)>(this->cmd->argc-1))  throw std::out_of_range("index out of range");
00124   return std::wstring((const wchar_t*)sqlite3_column_text16(this->cmd->stmt, index), sqlite3_column_bytes16(this->cmd->stmt, index)/2);
00125 }
00126 
00127 std::string sqlite3_reader::getblob(int index)
00128 {
00129   if (!this->cmd) SQLITE3X_THROW(database_error("reader is closed"));
00130   if ((index)>(this->cmd->argc-1))  throw std::out_of_range("index out of range");
00131   return std::string((const char*)sqlite3_column_blob(this->cmd->stmt, index), sqlite3_column_bytes(this->cmd->stmt, index));
00132 }
00133 
00134 std::string sqlite3_reader::getcolname(int index)
00135 {
00136   if (!this->cmd) SQLITE3X_THROW(database_error("reader is closed"));
00137   if ((index)>(this->cmd->argc-1))  throw std::out_of_range("index out of range");
00138   return sqlite3_column_name(this->cmd->stmt, index);
00139 }
00140 
00141 std::wstring sqlite3_reader::getcolname16(int index)
00142 {
00143   if (!this->cmd) SQLITE3X_THROW(database_error("reader is closed"));
00144   if ((index)>(this->cmd->argc-1))  throw std::out_of_range("index out of range");
00145   return (const wchar_t*)sqlite3_column_name16(this->cmd->stmt, index);
00146 }
00147 
00148 }

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