summaryrefslogtreecommitdiff
path: root/libodbcpp/param.cpp
blob: 53b55b5cfcbe6a7a1d0089f8a19dcab629c4550c (plain)
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
#include <sqlext.h>
#include "param.h"
#include "command.h"
#include "error.h"
#include <string.h>

ODBC::Param::Param() :
	bound(false)
{
}

ODBC::Param::~Param(){
}

template <class t>
ODBC::_Param<t>*
ODBC::Param::makeParam(ODBC::Param*& p)
{
	if (p) {
		_Param<t>* np = dynamic_cast<_Param<t>*>(p);
		if (np) {
			return np;
		}
		delete p;
	}
	_Param<t>* np = new _Param<t>();
	p = np;
	return np;
}

void
ODBC::Param::bind(SQLHANDLE hStmt, SQLUINTEGER col, SQLSMALLINT ctype, SQLSMALLINT stype,
		SQLINTEGER colsize, SQLINTEGER dp, const void* value, size_t buflen)
{
	RETCODE rc = SQLBindParameter(hStmt, col, SQL_PARAM_INPUT, ctype, stype,
			colsize, dp, (void*)value, buflen, (SQLINTEGER*)&bindLen);
	if (rc != SQL_SUCCESS) {
		throw Error(rc, SQL_HANDLE_STMT, hStmt, "%s: Bind for column %lu",
				__FUNCTION__, col);
	}
}

template <class T>
void
ODBC::Param::makeBindLen(T*& p, size_t newLen)
{
	if (bindSize <= newLen) {
		if (bindSize) {
			delete p;
		}
		bindSize = newLen;
		p = new T[newLen];
	}
	bindLen = newLen;
}

void
ODBC::Command::bindParamI(unsigned int i, int val)
{
	if (i < params.size()) {
		_Param<SQLINTEGER>* p = Param::makeParam<SQLINTEGER>(params[i]);
		p->value = val;
		if (!p->bound) {
			p->bind(this->hStmt, i + 1, SQL_C_SLONG, SQL_C_LONG, 0, 0,
					&p->value, sizeof(SQLINTEGER));
		}
		return;
	}
	throw Error("%s: Bind out of bounds", __FUNCTION__);
}
void
ODBC::Command::bindParamI(unsigned int i, long val)
{
	if (i < params.size()) {
		_Param<SQLINTEGER>* p = Param::makeParam<SQLINTEGER>(params[i]);
		p->value = val;
		if (!p->bound) {
			p->bind(this->hStmt, i + 1, SQL_C_SLONG, SQL_C_LONG, 0, 0,
					&p->value, sizeof(SQLINTEGER));
		}
		return;
	}
	throw Error("%s: Bind out of bounds", __FUNCTION__);
}
void
ODBC::Command::bindParamI(unsigned int i, long long unsigned int val)
{
	if (i < params.size()) {
		_Param<SQLUINTEGER>* p = Param::makeParam<SQLUINTEGER>(params[i]);
		p->value = val;
		if (!p->bound) {
			p->bind(this->hStmt, i + 1, SQL_C_ULONG, SQL_C_ULONG, 0, 0,
					&p->value, sizeof(SQLUINTEGER));
		}
		return;
	}
	throw Error("%s: Bind out of bounds", __FUNCTION__);
}
void
ODBC::Command::bindParamF(unsigned int i, double val)
{
	if (i < params.size()) {
		_Param<SQLDOUBLE>* p = Param::makeParam<SQLDOUBLE>(params[i]);
		p->value = val;
		if (!p->bound) {
			p->bind(this->hStmt, i + 1, SQL_C_DOUBLE, SQL_DOUBLE, 0, 0,
					&p->value, sizeof(SQLDOUBLE));
		}
		return;
	}
	throw Error("%s: Bind out of bounds", __FUNCTION__);
}
void
ODBC::Command::bindParamS(unsigned int i, const unsigned char * val, size_t length)
{
	if (i < params.size()) {
		_Param<SQLCHAR*>* p = Param::makeParam<SQLCHAR*>(params[i]);
		p->makeBindLen(p->value, length);
		memcpy(p->value, val, length);
		if (!p->bound) {
			p->bind(this->hStmt, i + 1, SQL_C_CHAR, SQL_CHAR, 0, 0, p->value, length);
		}
		return;
	}
	throw Error("%s: Bind out of bounds", __FUNCTION__);
}
void
ODBC::Command::bindParamT(unsigned int i, const TimeTypePair & val)
{
	if (i < params.size()) {
		_Param<TimeTypePair>* p = Param::makeParam<TimeTypePair>(params[i]);
		p->value = val;
		if (!p->bound) {
			p->bind(this->hStmt, i + 1, SQL_C_TYPE_TIMESTAMP, SQL_TYPE_TIMESTAMP,
					sizeof(SQL_TIMESTAMP_STRUCT), 0, &p->value.sql(), sizeof(SQL_TIMESTAMP_STRUCT));
		}
		return;
	}
	throw Error("%s: Bind out of bounds", __FUNCTION__);
}



// Wrappers for all the roughly compatable types

void
ODBC::Command::bindParamI(unsigned int i, long unsigned int val)
{
	bindParamI(i, (long long unsigned int)val);
}
void
ODBC::Command::bindParamI(unsigned int i, unsigned int val)
{
	bindParamI(i, (long long unsigned int)val);
}
void
ODBC::Command::bindParamS(unsigned int i, String val)
{
	bindParamS(i, val.c_str(), val.size());
}
void
ODBC::Command::bindParamS(unsigned int i, const unsigned char * val)
{
	const unsigned char * x = val;
	while (*val++) ;
	bindParamS(i, x, val - x);
}
void
ODBC::Command::bindParamS(unsigned int i, const char * val)
{
	bindParamS(i, (unsigned char *)val, strlen(val));
}
void
ODBC::Command::bindParamS(unsigned int i, std::string val)
{
	bindParamS(i, (unsigned char *)val.c_str(), val.size());
}
void
ODBC::Command::bindParamF(unsigned int i, float val)
{
	bindParamF(i, (double)val);
}
void
ODBC::Command::bindParamT(unsigned int i, time_t val)
{
	struct tm t;
	gmtime_r(&val, &t);
	bindParamT(i, &t);
}
void
ODBC::Command::bindParamT(unsigned int i, const struct tm * val)
{
	bindParamT(i, TimeTypePair(*val));
}