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
216
217
218
|
#include <stdio.h>
#include <stdlib.h>
#include "column.h"
#include "command.h"
#include "error.h"
ODBC::Column::Column(const Glib::ustring & n, unsigned int i) :
colNo(i),
name(n),
composeCache(NULL),
bindSize(0)
{
}
ODBC::Column::~Column()
{
delete composeCache;
}
bool
ODBC::Column::isNull() const
{
return (bindLen == SQL_NULL_DATA);
}
#define ODBC_DEFAULT_COLUMN_CAST(ctype, rtype) \
ODBC::Column::operator rtype() const { \
return (dynamic_cast<const _Column<ctype>& >(*this)).value; \
}
ODBC_DEFAULT_COLUMN_CAST(SQLINTEGER, unsigned int);
ODBC_DEFAULT_COLUMN_CAST(SQLINTEGER, unsigned long long);
ODBC_DEFAULT_COLUMN_CAST(SQLINTEGER, long long);
ODBC_DEFAULT_COLUMN_CAST(SQLINTEGER, int);
ODBC_DEFAULT_COLUMN_CAST(SQLDOUBLE, double);
ODBC_DEFAULT_COLUMN_CAST(SQLDOUBLE, float);
ODBC_DEFAULT_COLUMN_CAST(SQLCHAR*, const unsigned char *);
ODBC::Column::operator Glib::ustring() const {
return Glib::ustring((const char *)((dynamic_cast<const _Column<SQLCHAR*>& >(*this)).value));
}
ODBC::Column::operator std::string() const {
return (const char*)((dynamic_cast<const _Column<SQLCHAR*>& >(*this)).value);
}
ODBC::Column::operator const char * () const {
return (const char*)((dynamic_cast<const _Column<SQLCHAR*>& >(*this)).value);
}
ODBC::Column::operator struct tm () const {
const _Column<SQL_TIMESTAMP_STRUCT>& c = dynamic_cast<const _Column<SQL_TIMESTAMP_STRUCT>& >(*this);
struct tm rtn;
rtn << c.value;
return rtn;
}
void
ODBC::Column::bind(SQLHANDLE hStmt, SQLUINTEGER col, SQLSMALLINT ctype, void * buf, size_t size)
{
bindSize = size;
RETCODE rc = SQLBindCol(hStmt, col, ctype, buf, bindSize, &bindLen);
if (rc != SQL_SUCCESS) {
throw Error(rc, SQL_HANDLE_STMT, hStmt, "%s: Bind column %lu", __FUNCTION__, col);
}
}
#define REBIND(t, p) \
template<> void _Column<t>::rebind(Command * cmd, unsigned int col) const \
{ \
cmd->p(col, value); \
}
namespace ODBC {
REBIND(int, bindParamI)
REBIND(long, bindParamI)
REBIND(unsigned int, bindParamI)
REBIND(long unsigned int, bindParamI)
REBIND(long long unsigned int, bindParamI)
REBIND(double, bindParamF)
REBIND(float, bindParamF)
REBIND(SQL_TIMESTAMP_STRUCT, bindParamT)
template<> void _Column<unsigned char *>::rebind(Command * cmd, unsigned int col) const \
{
cmd->bindParamS(col, (const char *)value);
}
template <>
int
_Column<SQLDOUBLE>::writeToBuf(char ** buf, const char * fmt) const
{
return asprintf(buf, fmt, value);
}
template <>
int
_Column<SQLDOUBLE>::writeToBuf(char ** buf) const
{
return writeToBuf(buf, "%g");
}
template <>
const Glib::ustring &
_Column<SQLDOUBLE>::compose() const
{
if (!composeCache) {
composeCache = new Glib::ustring(Glib::ustring::compose("%1", value));
}
return *composeCache;
}
template <>
Glib::ustring
_Column<SQLDOUBLE>::compose(const Glib::ustring & fmt) const
{
return Glib::ustring::compose(fmt, value);
}
template <>
int
_Column<SQLINTEGER>::writeToBuf(char ** buf, const char * fmt) const
{
return asprintf(buf, fmt, value);
}
template <>
int
_Column<SQLINTEGER>::writeToBuf(char ** buf) const
{
return writeToBuf(buf, "%ld");
}
template <>
const Glib::ustring &
_Column<SQLINTEGER>::compose() const
{
if (!composeCache) {
composeCache = new Glib::ustring(Glib::ustring::compose("%1", value));
}
return *composeCache;
}
template <>
Glib::ustring
_Column<SQLINTEGER>::compose(const Glib::ustring & fmt) const
{
return Glib::ustring::compose(fmt, value);
}
template <>
int
_Column<SQLCHAR*>::writeToBuf(char ** buf, const char * fmt) const
{
return asprintf(buf, fmt, value);
}
template <>
int
_Column<SQLCHAR*>::writeToBuf(char ** buf) const
{
return writeToBuf(buf, "%s");
}
template <>
const Glib::ustring &
_Column<SQLCHAR*>::compose() const
{
if (!composeCache) {
composeCache = new Glib::ustring((const char *)value);
}
return *composeCache;
}
template <>
Glib::ustring
_Column<SQLCHAR*>::compose(const Glib::ustring & fmt) const
{
return Glib::ustring::compose(fmt, value);
}
template <>
int
_Column<SQL_TIMESTAMP_STRUCT>::writeToBuf(char ** buf, const char * fmt) const
{
*buf = (char *)malloc(300);
struct tm t;
t << value;
return strftime(*buf, 300, fmt, &t);
}
template <>
int
_Column<SQL_TIMESTAMP_STRUCT>::writeToBuf(char ** buf) const
{
return writeToBuf(buf, "%F %T");
}
template <>
Glib::ustring
_Column<SQL_TIMESTAMP_STRUCT>::compose(const Glib::ustring & fmt) const
{
char buf[300];
struct tm t;
t << value;
int len = strftime(buf, sizeof(buf), fmt.c_str(), &t);
return Glib::ustring(buf, len);
}
template <>
const Glib::ustring &
_Column<SQL_TIMESTAMP_STRUCT>::compose() const
{
if (!composeCache) {
composeCache = new Glib::ustring(Glib::ustring(compose("%F %T")));
}
return *composeCache;
}
}
void operator << (SQL_TIMESTAMP_STRUCT & target, const struct tm & src)
{
target.year = src.tm_year + 1900;
target.month = src.tm_mon + 1;
target.day = src.tm_mday;
target.hour = src.tm_hour;
target.minute = src.tm_min;
target.second = src.tm_sec;
target.fraction = 0;
}
void operator << (struct tm & target, const SQL_TIMESTAMP_STRUCT & src)
{
target.tm_year = src.year - 1900;
target.tm_mon = src.month - 1;
target.tm_mday = src.day;
target.tm_hour = src.hour;
target.tm_min = src.minute;
target.tm_sec = src.second;
}
|