-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsqlite3x_reader.cpp
129 lines (102 loc) · 4.28 KB
/
sqlite3x_reader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Copyright (C) 2004-2005 Cory Nelson
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
CVS Info :
$Author: phrostbyte $
$Date: 2005/06/16 20:46:40 $
$Revision: 1.1 $
*/
#include <sqlite3.h>
#include "sqlite3x.hpp"
namespace sqlite3x {
sqlite3_reader::sqlite3_reader() : cmd(NULL) {}
sqlite3_reader::sqlite3_reader(const sqlite3_reader ©) : cmd(copy.cmd) {
if(this->cmd) ++this->cmd->refs;
}
sqlite3_reader::sqlite3_reader(sqlite3_command *cmd) : cmd(cmd) {
++cmd->refs;
}
sqlite3_reader::~sqlite3_reader() {
this->close();
}
sqlite3_reader& sqlite3_reader::operator=(const sqlite3_reader ©) {
this->close();
this->cmd=copy.cmd;
if(this->cmd) ++this->cmd->refs;
return *this;
}
bool sqlite3_reader::read() {
if(!this->cmd) throw database_error("reader is closed");
switch(sqlite3_step(this->cmd->stmt)) {
case SQLITE_ROW:
return true;
case SQLITE_DONE:
return false;
default:
throw database_error(this->cmd->con);
}
}
void sqlite3_reader::reset() {
if(!this->cmd) throw database_error("reader is closed");
if(sqlite3_reset(this->cmd->stmt)!=SQLITE_OK)
throw database_error(this->cmd->con);
}
void sqlite3_reader::close() {
if(this->cmd) {
if(--this->cmd->refs==0) sqlite3_reset(this->cmd->stmt);
this->cmd=NULL;
}
}
int sqlite3_reader::getint(int index) {
if(!this->cmd) throw database_error("reader is closed");
if((index)>(this->cmd->argc-1)) throw std::out_of_range("index out of range");
return sqlite3_column_int(this->cmd->stmt, index);
}
long long sqlite3_reader::getint64(int index) {
if(!this->cmd) throw database_error("reader is closed");
if((index)>(this->cmd->argc-1)) throw std::out_of_range("index out of range");
return sqlite3_column_int64(this->cmd->stmt, index);
}
double sqlite3_reader::getdouble(int index) {
if(!this->cmd) throw database_error("reader is closed");
if((index)>(this->cmd->argc-1)) throw std::out_of_range("index out of range");
return sqlite3_column_double(this->cmd->stmt, index);
}
std::string sqlite3_reader::getstring(int index) {
if(!this->cmd) throw database_error("reader is closed");
if((index)>(this->cmd->argc-1)) throw std::out_of_range("index out of range");
return std::string((const char*)sqlite3_column_text(this->cmd->stmt, index), sqlite3_column_bytes(this->cmd->stmt, index));
}
std::wstring sqlite3_reader::getstring16(int index) {
if(!this->cmd) throw database_error("reader is closed");
if((index)>(this->cmd->argc-1)) throw std::out_of_range("index out of range");
return std::wstring((const wchar_t*)sqlite3_column_text16(this->cmd->stmt, index), sqlite3_column_bytes16(this->cmd->stmt, index)/2);
}
std::string sqlite3_reader::getblob(int index) {
if(!this->cmd) throw database_error("reader is closed");
if((index)>(this->cmd->argc-1)) throw std::out_of_range("index out of range");
return std::string((const char*)sqlite3_column_blob(this->cmd->stmt, index), sqlite3_column_bytes(this->cmd->stmt, index));
}
std::string sqlite3_reader::getcolname(int index) {
if(!this->cmd) throw database_error("reader is closed");
if((index)>(this->cmd->argc-1)) throw std::out_of_range("index out of range");
return sqlite3_column_name(this->cmd->stmt, index);
}
std::wstring sqlite3_reader::getcolname16(int index) {
if(!this->cmd) throw database_error("reader is closed");
if((index)>(this->cmd->argc-1)) throw std::out_of_range("index out of range");
return (const wchar_t*)sqlite3_column_name16(this->cmd->stmt, index);
}
}