Menu

[r222]: / dbixx / trunk / row.cpp  Maximize  Restore  History

Download this file

216 lines (197 with data), 3.8 kB

  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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "dbixx.h"
namespace dbixx {
using namespace std;
row::~row()
{
if(res && owner) {
dbi_result_free(res);
}
}
void row::reset()
{
if(res && owner) {
dbi_result_free(res);
}
res=NULL;
owner=false;
}
bool row::isempty()
{
return res==NULL;
}
unsigned int row::cols()
{
unsigned c;
if(res && (c=dbi_result_get_numfields(res))!=DBI_FIELD_ERROR) {
return c;
}
throw dbixx_error("Failed to fetch number of columns");
}
void row::set(dbi_result &r)
{
if(res && r!=res && owner) {
dbi_result_free(res);
}
owner=false;
res=r;
current=0;
}
void row::assign(dbi_result &r)
{
if(res && r!=res && owner) {
dbi_result_free(res);
}
owner=true;
res=r;
current=0;
if(!dbi_result_next_row(res)) {
reset();
}
}
void row::check_set()
{
if(!res)
throw dbixx_error("Using unititilized row");
}
bool row::isnull(int inx)
{
check_set();
int r;
r=dbi_result_field_is_null_idx(res,inx);
if(r == DBI_FIELD_FLAG_ERROR ) {
throw dbixx_error("Invalid field");
}
return r;
}
bool row::isnull(std::string const &id)
{
check_set();
int r;
r=dbi_result_field_is_null(res,id.c_str());
if(r == DBI_FIELD_FLAG_ERROR ) {
throw dbixx_error("Invalid field");
}
return r;
}
bool row::fetch(int pos,int &value)
{
long long v;
bool r=fetch(pos,v);
if(r)
value=v;
return r;
}
bool row::fetch(int pos,unsigned &value)
{
unsigned long long v;
bool r=fetch(pos,v);
if(r)
value=v;
return r;
}
bool row::fetch(int pos,long long &v)
{
if(isnull(pos)) return false;
int type=dbi_result_get_field_type_idx(res,pos);
switch(type) {
case DBI_TYPE_INTEGER:
case DBI_TYPE_DECIMAL:
v=dbi_result_get_longlong_idx(res,pos);
break;
case DBI_TYPE_STRING:
v=atol(dbi_result_get_string_idx(res,pos));
break;
default:
dbixx_error("Bad cast to integer type");
}
return true;
}
bool row::fetch(int pos,unsigned long long &v)
{
if(isnull(pos)) return false;
int type=dbi_result_get_field_type_idx(res,pos);
switch(type) {
case DBI_TYPE_INTEGER:
case DBI_TYPE_DECIMAL:
v=dbi_result_get_ulonglong_idx(res,pos);
break;
case DBI_TYPE_STRING:
v=atol(dbi_result_get_string_idx(res,pos));
break;
default:
dbixx_error("Bad cast to integer type");
}
return true;
}
bool row::fetch(int pos,string &v)
{
char const *tmp;
if(isnull(pos)) return false;
int type=dbi_result_get_field_type_idx(res,pos);
switch(type) {
case DBI_TYPE_STRING:
tmp=dbi_result_get_string_idx(res,pos);
if(tmp)
v=tmp;
else
return false;
break;
default:
dbixx_error("Bad cast to string type");
}
return true;
}
bool row::fetch(int pos,double &v)
{
if(isnull(pos)) return false;
int type=dbi_result_get_field_type_idx(res,pos);
switch(type) {
case DBI_TYPE_DECIMAL:
if(dbi_result_get_field_attribs_idx(res,pos) & DBI_DECIMAL_SIZE8)
v=dbi_result_get_double_idx(res,pos);
else
v=dbi_result_get_float_idx(res,pos);
break;
case DBI_TYPE_INTEGER:
v=dbi_result_get_longlong_idx(res,pos);
break;
case DBI_TYPE_STRING:
v=atof(dbi_result_get_string_idx(res,pos));
break;
default:
dbixx_error("Bad cast to double type");
}
return true;
}
bool row::fetch(int pos,std::tm &t)
{
if(isnull(pos)) return false;
int type=dbi_result_get_field_type_idx(res,pos);
time_t v;
switch(type) {
case DBI_TYPE_DATETIME:
v=dbi_result_get_datetime_idx(res,pos);
gmtime_r(&v,&t);
break;
case DBI_TYPE_STRING:
{
char const *d=dbi_result_get_string_idx(res,pos);
if(sscanf(d,"%d-%d-%d %d:%d:%d",
&t.tm_year,&t.tm_mon,&t.tm_mday,
&t.tm_hour,&t.tm_min,&t.tm_sec)!=6)
{
dbixx_error("Bad cast to datetime type");
}
t.tm_year-=1900;
t.tm_mon-=1;
// Fill correct rest of fields
v=mktime(&t);
localtime_r(&v,&t);
}
break;
default:
dbixx_error("Bad cast to datetime type");
}
return true;
}
}// Namespace dbixx
MongoDB Logo MongoDB