00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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 ©) : 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 ©)
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 }