DCL 4.0
Loading...
Searching...
No Matches
MyParam.cpp
Go to the documentation of this file.
1#include <dcl/Config.h>
2
3#include <string.h> // memset
4
5#include <dcl/Object.h>
6#if __DCL_HAVE_ALLOC_DEBUG
7#undef __DCL_ALLOC_LEVEL
8#define __DCL_ALLOC_LEVEL __DCL_ALLOC_INTERNAL
9#endif
10
11#include <dcl/size_t.h>
12#include <dcl/SQLCore.h>
13#include <dcl/Numeric.h>
14#include <dcl/InputStream.h>
15#include <dcl/Charset.h>
16
17#include "MyConnection.h"
18#include "MyQuery.h"
19#include "MyParam.h"
20
21#define __TRACE_THIS 0
22#if __TRACE_THIS
23#define __DCL_TRACE0_N __DCL_TRACE0
24#define __DCL_TRACE1_N __DCL_TRACE1
25#define __DCL_TRACE2_N __DCL_TRACE2
26#define __DCL_TRACE3_N __DCL_TRACE3
27#define __DCL_TRACE4_N __DCL_TRACE4
28#else
29#define __DCL_TRACE0_N(fmt)
30#define __DCL_TRACE1_N(fmt, arg)
31#define __DCL_TRACE2_N(fmt, arg1, arg2)
32#define __DCL_TRACE3_N(fmt, arg1, arg2, arg3)
33#define __DCL_TRACE4_N(fmt, arg1, arg2, arg3, arg4)
34#endif
35
36#if __DCL_HAVE_THIS_FILE__
37#undef __THIS_FILE__
38static const wchar_t __THIS_FILE__[] = __T("dcl/sql/MyParam.cpp");
39#endif
40
41__DCL_BEGIN_NAMESPACE
42
44
45MyParam::MyParam() : Param(NULL)
46{
47 __index = 0;
48 __bind = NULL;
49 __input = NULL;
50}
51
53{
54
55}
56
57bool MyParam::init(SQL::Query* _query, unsigned int _index, MYSQL_BIND* _bind)
58{
59 __DCL_ASSERT(Param::__queryHandle == NULL);
60 Param::__queryHandle = _query;
61 __index = _index;
62 __bind = _bind;
63
64 return true;
65}
66
68{
69 if (__input) {
70 try {
71 char buf[4096];
72 size_t total = 0;
73 for (; ; ) {
74 size_t n = __MIN(__size - total, sizeof(buf));
75 if (n && (n = __input->read(buf, n))) {
76 __DCL_TRACE1_N(L"mysql_stmt_send_long_data [%u]\n", n);
77 if (mysql_stmt_send_long_data(query()->stmt(),
78 __index, buf, (unsigned int) n)) {
79 __SET_ERROR_MSG(ByteString::format("(%u) %hs",
80 mysql_stmt_errno(query()->stmt()),
81 mysql_stmt_error(query()->stmt())));
82 return false;
83 }
84 }
85 else {
86 break;
87 }
88 }
89 }
90 catch (IOException* e) {
91 __SET_ERROR_MSG(UTF8Encoder::encode(e->toStringAll()));
92 e->destroy();
93 return false;
94 }
95 }
96 return true;
97}
98
100{
101 Param::__dataType = SQL::typeUnknown;
102 __bind->buffer_length = 0;
103 __bind->buffer = NULL;
104 __bind->length_value = 0;
105 __bind->is_null_value = 1;
106 __input = NULL;
107 __size = 0;
108}
109
110// in MyField.cpp
111const wchar_t* __dataTypeName(enum_field_types _type, unsigned int _flags);
112
113const wchar_t* MyParam::serverDataTypeName() const
114{
115 unsigned int flags = 0;
116 switch (SQL::Param::__dataType) {
117 case SQL::typeUInteger :
118 flags = UNSIGNED_FLAG;
119 break;
120 case SQL::typeBinary :
122 case SQL::typeBlob :
123 flags = BINARY_FLAG;
124 break;
125 default:
126 ;
127 }
128
129 return __dataTypeName(__bind->buffer_type, flags);
130}
131
133{
134 Param::__dataType = SQL::typeUnknown;
135 __bind->buffer_length = 0;
136 __bind->buffer = NULL;
137 __bind->length_value = 0;
138 __bind->is_null_value = 1;
139 __input = NULL;
140 __size = 0;
141}
142
143#define __ABS(n) (n < 0) ? -n : n
144
146 _CONST void* _pv,
147 size_t _size,
148 SQL::DataType _dataType,
149 SQL::DataType _assignType
150 )
151{
152 switch(_dataType) {
153 case SQL::typeInteger: {
154 switch (_size) {
155 case sizeof(int8_t) : {
156 __data.i32 = (int32_t) * (int8_t*)_pv;
157 __bind->buffer_type = MYSQL_TYPE_LONG;
158 __bind->buffer_length = sizeof(int32_t);
159 __bind->buffer = &__data;
160 break;
161 }
162 case sizeof(int16_t) : {
163 __data.i32 = (int32_t) * (int16_t*)_pv;
164 __bind->buffer_type = MYSQL_TYPE_LONG;
165 __bind->buffer_length = sizeof(int32_t);
166 __bind->buffer = &__data;
167 break;
168 }
169 case sizeof(int32_t) : {
170 __data.i32 = (int32_t) * (int32_t*)_pv;
171 __bind->buffer_type = MYSQL_TYPE_LONG;
172 __bind->buffer_length = sizeof(int32_t);
173 __bind->buffer = &__data;
174 break;
175 }
176 case sizeof(int64_t) : {
177 __data.i64 = (int64_t) * (int64_t*)_pv;
178 __bind->buffer_type = MYSQL_TYPE_LONGLONG;
179 __bind->buffer_length = sizeof(int64_t);
180 __bind->buffer = &__data;
181 break;
182 }
183 default: {
185 return false;
186 }
187 }
188 __bind->is_unsigned = 0;
189 break;
190 }
191 case SQL::typeUInteger : {
192 switch (_size) {
193 case sizeof(uint8_t) : {
194 __data.u32 = (uint32_t) * (uint8_t*)_pv;
195 __bind->buffer_type = MYSQL_TYPE_LONG;
196 __bind->buffer_length = sizeof(uint32_t);
197 __bind->buffer = &__data;
198 break;
199 }
200 case sizeof(uint16_t) : {
201 __data.u32 = (uint32_t) * (uint16_t*)_pv;
202 __bind->buffer_type = MYSQL_TYPE_LONG;
203 __bind->buffer_length = sizeof(uint32_t);
204 __bind->buffer = &__data;
205 break;
206 }
207 case sizeof(uint32_t) : {
208 __data.u32 = (uint32_t) * (uint32_t*)_pv;
209 __bind->buffer_type = MYSQL_TYPE_LONG;
210 __bind->buffer_length = sizeof(uint32_t);
211 __bind->buffer = &__data;
212 break;
213 }
214 case sizeof(uint64_t) : {
215 __data.u64 = (uint64_t) * (uint64_t*)_pv;
216 __bind->buffer_type = MYSQL_TYPE_LONGLONG;
217 __bind->buffer_length = sizeof(uint64_t);
218 __bind->buffer = &__data;
219 break;
220 }
221 default: {
223 return false;
224 }
225 }
226 __bind->is_unsigned = 1;
227 break;
228 }
229 case SQL::typeFloat: {
230 switch (_size) {
231 case sizeof(float) : {
232 __data.f32 = *(float*)_pv;
233 __bind->buffer_type = MYSQL_TYPE_FLOAT;
234 __bind->buffer_length = sizeof(float);
235 __bind->buffer = &__data;
236 break;
237 }
238 case sizeof(double) : {
239 __data.f64 = *(double*)_pv;
240 __bind->buffer_type = MYSQL_TYPE_DOUBLE;
241 __bind->buffer_length = sizeof(double);
242 __bind->buffer = &__data;
243 break;
244 }
245 default: {
247 return false;
248 }
249 }
250 break;
251 }
252 case SQL::typeDate: {
253 if (_size == sizeof(SQL::Date)) {
254 const SQL::Date* p = (const SQL::Date*)_pv;
255 memset(&__data.time, 0, sizeof(MYSQL_TIME));
256 __data.time.year = __ABS(p->nYear);
257 __data.time.month = p->nMonth;
258 __data.time.day = p->nDay;
259 __data.time.neg = p->nYear < 0 ? 1 : 0;
260 __data.time.time_type = MYSQL_TIMESTAMP_DATE;
261 __bind->buffer_type = MYSQL_TYPE_DATE;
262 __bind->buffer_length = sizeof(MYSQL_TIME);
263 __bind->buffer = &__data;
264 }
265 else {
267 return false;
268 }
269 break;
270 }
271 case SQL::typeTime: {
272 if (_size == sizeof(SQL::Time)) {
273 const SQL::Time* p = (const SQL::Time*)_pv;
274 memset(&__data.time, 0, sizeof(MYSQL_TIME));
275 __data.time.hour = p->nHour;
276 __data.time.minute = p->nMin;
277 __data.time.second = p->nSec;
278 __data.time.second_part = p->nFrac / 1000;
279 __data.time.neg = 0;
280 __data.time.time_type = MYSQL_TIMESTAMP_TIME;
281 __bind->buffer_type = MYSQL_TYPE_TIME;
282 __bind->buffer_length = sizeof(MYSQL_TIME);
283 __bind->buffer = &__data;
284 }
285 else {
287 return false;
288 }
289 break;
290 }
291 case SQL::typeTimeStamp: {
292 if (_size == sizeof(SQL::TimeStamp)) {
293 const SQL::TimeStamp* p = (const SQL::TimeStamp*)_pv;
294 //if (p->nTzMin != INT16_MIN) {
295 // __SET_ERROR(SQL::eInvalidData);
296 // return false;
297 //}
298 __data.time.year = __ABS(p->nYear);
299 __data.time.month = p->nMonth;
300 __data.time.day = p->nDay;
301 __data.time.hour = p->nHour;
302 __data.time.minute = p->nMin;
303 __data.time.second = p->nSec;
304 __data.time.second_part = p->nFrac / 1000;
305 __data.time.neg = p->nYear < 0 ? 1 : 0;
306 __data.time.time_type = MYSQL_TIMESTAMP_DATETIME;
307 __bind->buffer_type = MYSQL_TYPE_DATETIME;
308 __bind->buffer_length = sizeof(MYSQL_TIME);
309 __bind->buffer = &__data;
310 }
311 else {
313 return false;
314 }
315 break;
316 }
319 case SQL::typeIntervalDs: {
320 if (_size == sizeof(SQL::Interval)) {
321 const SQL::Interval* p = (const SQL::Interval*)_pv;
322 __data.time.year = 0;
323 __data.time.month = 0;
324 __data.time.day = __ABS(p->nYears * 365 + p->nMonths * 30 + p->nDays);
325 __data.time.hour = __ABS(p->nHours);
326 __data.time.minute = __ABS(p->nMins);
327 __data.time.second = __ABS(p->nSecs);
328 __data.time.second_part = __ABS(p->nFracs) / 1000;
329 __data.time.neg = p->nYears < 0 || p->nMonths < 0 || p->nDays
330 || p->nHours < 0 || p->nMins || p->nSecs || p->nFracs < 0 ? 1 : 0;
331 __data.time.time_type = MYSQL_TIMESTAMP_TIME;
332 __bind->buffer_type = MYSQL_TYPE_TIME;
333 __bind->buffer_length = sizeof(MYSQL_TIME);
334 __bind->buffer = &__data;
335 }
336 else {
338 return false;
339 }
340 break;
341 }
342 case SQL::typeText :
343 case SQL::typeBinary: {
344 if (_size > UINT32_MAX) {
346 return false;
347 }
348 __bind->buffer_type = MYSQL_TYPE_STRING;
349 __bind->buffer_length = (unsigned int) _size;
350 __bind->buffer = _pv;
351 break;
352 }
353 case SQL::typeInputStream : {
354 if (_size != (size_t)-1 && _size > UINT32_MAX) {
356 return false;
357 }
358 __bind->buffer_type = MYSQL_TYPE_STRING;
359 __bind->buffer_length = 0;
360 __bind->buffer = NULL;
361 __input = (InputStream*)_pv;
362 __size = _size;
363 break;
364 }
365 default:
368 return false;
369 }
370 }
371
372 __bind->is_null_value = 0;
373 Param::__dataType = _assignType;
374 return true;
375}
376
377__DCL_END_NAMESPACE
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:340
#define _CONST
Definition Config.h:353
#define UINT32_MAX
Definition Config.h:323
#define __DCL_TRACE1_N(fmt, arg)
#define __SET_ERROR_MSG(_message)
const wchar_t * __dataTypeName(const ifx_sqlvar_t *_sqlvar)
Definition IFXField.cpp:290
#define __ABS(n)
Definition MyParam.cpp:143
const wchar_t * __dataTypeName(enum_field_types _type, unsigned int _flags)
Definition MyField.cpp:224
#define __DCL_ASSERT(expr)
Definition Object.h:371
#define IMPLEMENT_CLASSINFO(class_name, base_class_name)
Definition Object.h:228
#define __T(str)
Definition Object.h:44
ByteBuffer * buf
#define __SET_ERROR(_errorCode)
Definition SQLCore.cpp:150
void CharsetConvertException *size_t n
Definition SQLField.cpp:253
virtual void destroy()
Definition Exception.cpp:74
String toStringAll() const
Definition Exception.cpp:45
virtual bool __setData(_CONST void *_pv, size_t _size, SQL::DataType _dataType, SQL::DataType _assignType)
Definition MyParam.cpp:145
virtual void setNull()
Definition MyParam.cpp:132
bool init(SQL::Query *_query, unsigned int _index, MYSQL_BIND *_bind)
Definition MyParam.cpp:57
virtual const wchar_t * serverDataTypeName() const
Definition MyParam.cpp:113
virtual ~MyParam()
Definition MyParam.cpp:52
MyQuery * query() const
Definition MyParam.h:74
void onAfterExecute()
Definition MyParam.cpp:99
bool onBeforeExecute()
Definition MyParam.cpp:67
DataType __dataType
Definition SQLCore.h:184
DataType
Definition SQLCore.h:62
@ typeBinary
Definition SQLCore.h:76
@ typeTime
Definition SQLCore.h:69
@ typeLongBinary
Definition SQLCore.h:78
@ typeUInteger
Definition SQLCore.h:65
@ typeUnknown
Definition SQLCore.h:63
@ typeTimeStamp
Definition SQLCore.h:70
@ typeBlob
Definition SQLCore.h:80
@ typeInputStream
Definition SQLCore.h:83
@ typeTimeStampTz
Definition SQLCore.h:71
@ typeInterval
Definition SQLCore.h:72
@ typeIntervalDs
Definition SQLCore.h:74
@ typeDate
Definition SQLCore.h:68
@ typeText
Definition SQLCore.h:75
@ typeFloat
Definition SQLCore.h:66
@ typeInteger
Definition SQLCore.h:64
@ typeIntervalYm
Definition SQLCore.h:73
@ eNotSupportDataType
Definition SQLCore.h:48
@ eInvalidDataSize
Definition SQLCore.h:57
size_t __MIN(size_t x, size_t y)
Definition size_t.h:27
uint8_t nMonth
Definition SQLCore.h:97
int16_t nYear
Definition SQLCore.h:96
uint8_t nDay
Definition SQLCore.h:98
int8_t nHours
Definition SQLCore.h:128
int32_t nFracs
Definition SQLCore.h:131
int8_t nSecs
Definition SQLCore.h:130
int8_t nMins
Definition SQLCore.h:129
int32_t nYears
Definition SQLCore.h:125
int8_t nMonths
Definition SQLCore.h:126
int32_t nDays
Definition SQLCore.h:127
uint8_t nHour
Definition SQLCore.h:103
uint8_t nMin
Definition SQLCore.h:104
uint8_t nSec
Definition SQLCore.h:105
uint32_t nFrac
Definition SQLCore.h:106
int16_t nYear
Definition SQLCore.h:111
uint8_t nDay
Definition SQLCore.h:113
uint8_t nHour
Definition SQLCore.h:114
uint8_t nSec
Definition SQLCore.h:116
uint8_t nMonth
Definition SQLCore.h:112
uint8_t nMin
Definition SQLCore.h:115
uint32_t nFrac
Definition SQLCore.h:117