00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "db/db.h"
00029 #include "base/numeric.h"
00030
00031 BEGIN_TERIMBER_NAMESPACE
00032 #pragma pack(4)
00033
00034 inline
00035 bool
00036 binder::set_as_null(db_param_type in_out, dbtypes type)
00037 {
00038 deallocate_value();
00039
00040 _in_out = in_out;
00041 _type = type;
00042 _value.nullVal = true;
00043 memset(&_value.val, 0, sizeof(terimber_xml_value));
00044 return true;
00045 }
00046
00047 inline
00048 bool
00049 binder::set_as_bool(db_param_type in_out, bool val)
00050 {
00051 deallocate_value();
00052
00053 _in_out = in_out;
00054 _type = db_bool;
00055 _value.nullVal = false;
00056 _value.val.boolVal = val;
00057 return true;
00058 }
00059
00060 inline
00061 bool
00062 binder::set_as_char(db_param_type in_out, sb1_t val)
00063 {
00064 deallocate_value();
00065
00066 _in_out = in_out;
00067 _type = db_sb1;
00068 _value.nullVal = false;
00069 _value.val.cVal = val;
00070 return true;
00071 }
00072
00073 inline
00074 bool
00075 binder::set_as_byte(db_param_type in_out, ub1_t val)
00076 {
00077 deallocate_value();
00078
00079 _in_out = in_out;
00080 _type = db_ub1;
00081 _value.nullVal = false;
00082 _value.val.bVal = val;
00083 return true;
00084 }
00085
00086 inline
00087 bool
00088 binder::set_as_short(db_param_type in_out, sb2_t val)
00089 {
00090 deallocate_value();
00091
00092 _in_out = in_out;
00093 _type = db_sb2;
00094 _value.nullVal = false;
00095 _value.val.iVal = val;
00096 return true;
00097 }
00098
00099 inline
00100 bool
00101 binder::set_as_word(db_param_type in_out, ub2_t val)
00102 {
00103 deallocate_value();
00104
00105 _in_out = in_out;
00106 _type = db_ub2;
00107 _value.nullVal = false;
00108 _value.val.uiVal = val;
00109 return true;
00110 }
00111
00112 inline
00113 bool
00114 binder::set_as_long(db_param_type in_out, sb4_t val)
00115 {
00116 deallocate_value();
00117
00118 _in_out = in_out;
00119 _type = db_sb4;
00120 _value.nullVal = false;
00121 _value.val.lVal = val;
00122 return true;
00123 }
00124
00125 inline
00126 bool
00127 binder::set_as_dword(db_param_type in_out, ub4_t val)
00128 {
00129 deallocate_value();
00130
00131 _in_out = in_out;
00132 _type = db_ub4;
00133 _value.nullVal = false;
00134 _value.val.ulVal = val;
00135 return true;
00136 }
00137
00138 inline
00139 bool
00140 binder::set_as_float(db_param_type in_out, float val)
00141 {
00142 deallocate_value();
00143
00144 _in_out = in_out;
00145 _type = db_float;
00146 _value.nullVal = false;
00147 _value.val.fltVal = val;
00148 return true;
00149 }
00150
00151 inline
00152 bool
00153 binder::set_as_double(db_param_type in_out, double val)
00154 {
00155 #ifdef OS_64BIT
00156 deallocate_value();
00157 _type = db_double;
00158 _value.val.dblVal = val;
00159 #else
00160 if (_type == db_double && _value.val.dblVal)
00161 {
00162 *const_cast< double* >(_value.val.dblVal) = val;
00163 }
00164 else
00165 {
00166 try
00167 {
00168 deallocate_value();
00169 _type = db_double;
00170 *(double*)allocate_value(0) = val;
00171 }
00172 catch (exception&)
00173 {
00174
00175
00176 return false;
00177 }
00178 }
00179 #endif
00180
00181 _in_out = in_out;
00182 _value.nullVal = false;
00183 return true;
00184 }
00185
00186 inline
00187 bool
00188 binder::set_as_long64(db_param_type in_out, sb8_t val)
00189 {
00190 #ifdef OS_64BIT
00191 deallocate_value();
00192 _type = db_sb8;
00193 _value.val.intVal = val;
00194 #else
00195 if ((_type == db_date || _type == db_sb8 || _type == db_ub8)
00196 && _value.val.intVal)
00197 {
00198 _type = db_sb8;
00199 *const_cast< sb8_t* >(_value.val.intVal) = val;
00200 }
00201 else
00202 {
00203 try
00204 {
00205 deallocate_value();
00206 _type = db_sb8;
00207 *(sb8_t*)allocate_value(0) = val;
00208 }
00209 catch (exception&)
00210 {
00211
00212
00213 return false;
00214 }
00215 }
00216 #endif
00217
00218 _in_out = in_out;
00219 _value.nullVal = false;
00220 return true;
00221 }
00222
00223 inline
00224 bool
00225 binder::set_as_dword64(db_param_type in_out, ub8_t val)
00226 {
00227 #ifdef OS_64BIT
00228 deallocate_value();
00229 _type = db_ub8;
00230 _value.val.uintVal = val;
00231 #else
00232 if ((_type == db_date || _type == db_sb8 || _type == db_ub8)
00233 && _value.val.uintVal)
00234 {
00235 _type = db_ub8;
00236 *const_cast< ub8_t* >(_value.val.uintVal) = val;
00237 }
00238 else
00239 {
00240 try
00241 {
00242 deallocate_value();
00243 _type = db_ub8;
00244 *(ub8_t*)allocate_value(0) = val;
00245 }
00246 catch (exception&)
00247 {
00248
00249
00250 return false;
00251 }
00252 }
00253 #endif
00254
00255 _in_out = in_out;
00256 _value.nullVal = false;
00257 return true;
00258 }
00259
00260 inline
00261 bool
00262 binder::set_as_guid(db_param_type in_out, const guid_t& val)
00263 {
00264 if (_type == db_guid && _value.val.guidVal)
00265 {
00266 _type = db_guid;
00267 *const_cast< guid_t* >(_value.val.guidVal) = val;
00268 }
00269 else
00270 {
00271 try
00272 {
00273 deallocate_value();
00274 _type = db_guid;
00275 *(guid_t*)allocate_value(0) = val;
00276 }
00277 catch (exception&)
00278 {
00279
00280
00281 return false;
00282 }
00283 }
00284
00285 _in_out = in_out;
00286 _value.nullVal = false;
00287 return true;
00288 }
00289
00290 inline
00291 bool
00292 binder::set_as_numeric(db_param_type in_out, const char* val, char delimeter)
00293 {
00294 numeric num(val, delimeter);
00295
00296 if ((_type == db_numeric || _type == db_decimal)
00297 && _value.val.bufVal
00298 )
00299 {
00300
00301 numeric old_num;
00302 if (old_num.parse_orcl((const ub1_t*)_value.val.bufVal)
00303 && old_num.orcl_len() >= num.orcl_len())
00304 {
00305 _type = db_numeric;
00306 _in_out = in_out;
00307 _value.nullVal = false;
00308 return num.persist_orcl((ub1_t*)_value.val.bufVal);
00309 }
00310 }
00311
00312 try
00313 {
00314 deallocate_value();
00315 _type = db_numeric;
00316 _in_out = in_out;
00317 _value.nullVal = false;
00318 return num.persist_orcl((ub1_t*)allocate_value(num.orcl_len()));
00319 }
00320 catch (exception&)
00321 {
00322
00323
00324 return false;
00325 }
00326
00327 return true;
00328 }
00329
00330 inline
00331 bool
00332 binder::set_as_decimal(db_param_type in_out, const char* val, char delimeter)
00333 {
00334 numeric num(val, delimeter);
00335
00336 if ((_type == db_numeric || _type == db_decimal)
00337 && _value.val.bufVal
00338 )
00339 {
00340
00341 numeric old_num;
00342 if (old_num.parse_orcl((const ub1_t*)_value.val.bufVal)
00343 && old_num.orcl_len() >= num.orcl_len())
00344 {
00345 _type = db_decimal;
00346 _in_out = in_out;
00347 _value.nullVal = false;
00348 return num.persist_orcl((ub1_t*)_value.val.bufVal);
00349 }
00350 }
00351
00352 try
00353 {
00354 deallocate_value();
00355 _type = db_decimal;
00356 _in_out = in_out;
00357 _value.nullVal = false;
00358
00359 return num.persist_orcl((ub1_t*)allocate_value(num.orcl_len()));
00360 }
00361 catch (exception&)
00362 {
00363
00364
00365 return false;
00366 }
00367
00368 return true;
00369 }
00370
00371 bool
00372 binder::set_as_date(db_param_type in_out, ub4_t year, ub1_t month, ub1_t day, ub1_t hour, ub1_t minute, ub1_t second, ub2_t millisec)
00373 {
00374 sb8_t dt = 0;
00375 if (!date::convert_to(year, month, day, hour, minute, second, millisec, dt))
00376 return false;
00377 #ifdef OS_64BIT
00378 deallocate_value();
00379 _type = db_date;
00380 _value.val.intVal = dt;
00381 #else
00382 if ((_type == db_date || _type == db_sb8 || _type == db_ub8)
00383 && _value.val.intVal)
00384 {
00385 _type = db_date;
00386 *const_cast< sb8_t* >(_value.val.intVal) = dt;
00387 }
00388 else
00389 {
00390 try
00391 {
00392 deallocate_value();
00393 _type = db_date;
00394 *(sb8_t*)allocate_value(0) = dt;
00395 }
00396 catch (exception&)
00397 {
00398
00399
00400 return false;
00401 }
00402 }
00403 #endif
00404
00405 _in_out = in_out;
00406 _value.nullVal = false;
00407 return true;
00408 }
00409
00410 inline
00411 bool
00412 binder::set_as_string(db_param_type in_out, const char* val, size_t len)
00413 {
00414 size_t len_in = len == os_minus_one ? str_template::strlen(val) : len;
00415
00416 if (_type == db_string && _value.val.strVal && str_template::strlen(_value.val.strVal) >= len_in)
00417 {
00418 str_template::strcpy((char*)_value.val.strVal, val, len_in);
00419 }
00420 else
00421 {
00422 try
00423 {
00424 deallocate_value();
00425 _type = db_string;
00426 str_template::strcpy((char*)allocate_value(len_in), val, len_in);
00427 }
00428 catch (exception&)
00429 {
00430
00431
00432 return false;
00433 }
00434 }
00435
00436 _in_out = in_out;
00437 _value.nullVal = false;
00438 return true;
00439 }
00440
00441 inline
00442 bool
00443 binder::set_as_string_ptr(const char* val)
00444 {
00445 deallocate_value();
00446
00447 _value.val.strVal = val;
00448 _in_out = db_param_in;
00449 _value.nullVal = false;
00450 _type = db_string;
00451 return true;
00452 }
00453
00454 bool
00455 binder::set_as_wstring(db_param_type in_out, const wchar_t* val, size_t len)
00456 {
00457 size_t len_in = len == os_minus_one ? str_template::strlen(val) : len;
00458 if (_type == db_wstring && _value.val.wstrVal && str_template::strlen(_value.val.wstrVal) >= len_in)
00459 {
00460 str_template::strcpy((wchar_t*)_value.val.wstrVal, val, len_in);
00461 }
00462 else
00463 {
00464 try
00465 {
00466 deallocate_value();
00467 _type = db_wstring;
00468 str_template::strcpy((wchar_t*)allocate_value(len_in), val, len_in);
00469 }
00470 catch (exception&)
00471 {
00472
00473
00474 return false;
00475 }
00476 }
00477 _in_out = in_out;
00478 _value.nullVal = false;
00479 return true;
00480 }
00481
00482 inline
00483 bool
00484 binder::set_as_wstring_ptr(const wchar_t* val)
00485 {
00486 deallocate_value();
00487
00488 _value.val.wstrVal = val;
00489 _in_out = db_param_in;
00490 _value.nullVal = false;
00491 _type = db_wstring;
00492 return true;
00493 }
00494
00495 inline
00496 bool
00497 binder::set_as_binary(db_param_type in_out, const ub1_t* val, size_t len)
00498 {
00499 if (_type == db_binary && _value.val.bufVal && *(size_t*)_value.val.bufVal >= len)
00500 {
00501 *(size_t*)_value.val.bufVal = len;
00502 memcpy((ub1_t*)_value.val.bufVal + sizeof(size_t), val, len);
00503 }
00504 else
00505 {
00506 try
00507 {
00508 deallocate_value();
00509 _type = db_binary;
00510 memcpy((ub1_t*)allocate_value(len), val, len);
00511 }
00512 catch (exception&)
00513 {
00514
00515
00516 return false;
00517 }
00518 }
00519 _in_out = in_out;
00520 _value.nullVal = false;
00521 return true;
00522 }
00523
00524 bool
00525 binder::set_as_binary_ptr(const ub1_t* val)
00526 {
00527 deallocate_value();
00528
00529 _value.val.bufVal = val;
00530 _in_out = db_param_in;
00531 _value.nullVal = false;
00532 _type = db_binary;
00533 return true;
00534 }
00535
00536
00538 inline
00539 terimber_xml_value
00540 dbserver_impl::get_param_as_value(size_t index, vt_types type) const
00541 {
00542 try
00543 {
00544 if (index >= _params.size())
00545 exception::_throw("Out of index");
00546 vt_types type_ = convert_types(_params[index]._type);
00547 _temp_allocator->reset();
00548 return type_ != type ? parse_value(type,
00549 persist_value(type_, _params[index]._value.val, _temp_allocator),
00550 0xffffffff,
00551 _temp_allocator) : _params[index]._value.val;
00552 }
00553 catch (...)
00554 {
00555 terimber_xml_value val;
00556 memset(&val, 0, sizeof(terimber_xml_value));
00557 return val;
00558 }
00559 }
00560
00563 inline
00564 terimber_xml_value
00565 dbserver_impl::get_value_as_value(size_t index, vt_types type) const
00566 {
00567 try
00568 {
00569 if (_iter == get_iter_end() || index >= _cols.size() || (*_iter)[index].nullVal)
00570 exception::_throw("Out of range");
00571
00572 vt_types type_ = convert_types(_cols[index]._type);
00573 _temp_allocator->reset();
00574 return type_ != type ? parse_value(type,
00575 persist_value(type_, (*_iter)[index].val, _temp_allocator),
00576 0xffffffff,
00577 _temp_allocator) : (*_iter)[index].val;
00578 }
00579 catch (...)
00580 {
00581 terimber_xml_value val;
00582 memset(&val, 0, sizeof(terimber_xml_value));
00583 return val;
00584 }
00585 }
00586
00587
00588 inline
00589 vt_types
00590 dbserver_impl::convert_types(dbtypes type)
00591 {
00592 switch (type)
00593 {
00594 case db_unknown: return vt_unknown;
00595 case db_bool: return vt_bool;
00596 case db_sb1: return vt_sb1;
00597 case db_ub1: return vt_ub1;
00598 case db_sb2: return vt_sb2;
00599 case db_ub2: return vt_ub2;
00600 case db_sb4: return vt_sb4;
00601 case db_ub4: return vt_ub4;
00602 case db_float: return vt_float;
00603 case db_double: return vt_double;
00604 case db_sb8: return vt_sb8;
00605 case db_ub8: return vt_ub8;
00606 case db_date: return vt_date;
00607 case db_string: return vt_string;
00608 case db_wstring: return vt_wstring;
00609 case db_decimal: return vt_decimal;
00610 case db_numeric: return vt_numeric;
00611 case db_binary: return vt_binary;
00612 case db_guid: return vt_guid;
00613 default: return vt_unknown;
00614 }
00615 }
00616
00618
00621
00622 template < class T >
00623 void
00624 db_creator< T >::activate( db_entry* obj,
00625 const db_arg& arg
00626 )
00627 {
00628 if (!obj->_obj->is_connect())
00629 obj->_obj->connect(arg._trusted, arg._login_string);
00630 }
00631
00633
00634 template < class T >
00635 bool
00636 db_creator< T >::find(db_entry* obj,
00637 const db_arg& arg
00638 )
00639 {
00640 return static_cast< const db_arg& >(*obj) == arg;
00641 }
00642
00644
00645 template < class T >
00646 void
00647 db_creator< T >::back(db_entry* obj,
00648 const db_arg& arg
00649 )
00650 {
00651
00652 if (obj->_obj->is_in_transaction())
00653 obj->_obj->rollback();
00654
00655
00656 if (obj->_obj->is_open_sql())
00657 obj->_obj->close_sql();
00658 }
00659
00661
00662 template < class T >
00663 void
00664 db_creator< T >::destroy(db_entry* obj,
00665 const db_arg& arg
00666 )
00667 {
00668 deactivate(obj, arg);
00669
00670
00671 delete obj->_obj;
00672 delete obj;
00673 }
00674
00676
00677 template < class T >
00678 void
00679 db_creator< T >::deactivate(db_entry* obj,
00680 const db_arg& arg
00681 )
00682 {
00683 back(obj, arg);
00684
00685
00686 if (obj->_obj->is_connect())
00687 obj->_obj->disconnect();
00688 }
00689
00692
00693
00694
00695 #pragma pack()
00696 END_TERIMBER_NAMESPACE