00001 // 00002 // Copyright (C) 2008 Maciej Sobczak 00003 // Distributed under the Boost Software License, Version 1.0. 00004 // (See accompanying file LICENSE_1_0.txt or copy at 00005 // http://www.boost.org/LICENSE_1_0.txt) 00006 // 00007 00008 #define SOCI_SOURCE 00009 00010 #include "soci-simple.h" 00011 #include "soci.h" 00012 00013 #include <cstddef> 00014 #include <cstdio> 00015 #include <ctime> 00016 #include <exception> 00017 #include <map> 00018 #include <string> 00019 #include <vector> 00020 00021 using namespace soci; 00022 00023 namespace // unnamed 00024 { 00025 00026 struct session_wrapper 00027 { 00028 session sql; 00029 00030 bool is_ok; 00031 std::string error_message; 00032 }; 00033 00034 } // namespace unnamed 00035 00036 00037 SOCI_DECL session_handle soci_create_session(char const * connection_string) 00038 { 00039 session_wrapper * wrapper = NULL; 00040 try 00041 { 00042 wrapper = new session_wrapper(); 00043 } 00044 catch (...) 00045 { 00046 return NULL; 00047 } 00048 00049 try 00050 { 00051 wrapper->sql.open(connection_string); 00052 wrapper->is_ok = true; 00053 } 00054 catch (std::exception const & e) 00055 { 00056 wrapper->is_ok = false; 00057 wrapper->error_message = e.what(); 00058 } 00059 00060 return wrapper; 00061 } 00062 00063 SOCI_DECL void soci_destroy_session(session_handle s) 00064 { 00065 session_wrapper * wrapper = static_cast<session_wrapper *>(s); 00066 delete wrapper; 00067 } 00068 00069 SOCI_DECL void soci_begin(session_handle s) 00070 { 00071 session_wrapper * wrapper = static_cast<session_wrapper *>(s); 00072 try 00073 { 00074 wrapper->sql.begin(); 00075 wrapper->is_ok = true; 00076 } 00077 catch (std::exception const & e) 00078 { 00079 wrapper->is_ok = false; 00080 wrapper->error_message = e.what(); 00081 } 00082 } 00083 00084 SOCI_DECL void soci_commit(session_handle s) 00085 { 00086 session_wrapper * wrapper = static_cast<session_wrapper *>(s); 00087 try 00088 { 00089 wrapper->sql.commit(); 00090 wrapper->is_ok = true; 00091 } 00092 catch (std::exception const & e) 00093 { 00094 wrapper->is_ok = false; 00095 wrapper->error_message = e.what(); 00096 } 00097 } 00098 00099 SOCI_DECL void soci_rollback(session_handle s) 00100 { 00101 session_wrapper * wrapper = static_cast<session_wrapper *>(s); 00102 try 00103 { 00104 wrapper->sql.rollback(); 00105 wrapper->is_ok = true; 00106 } 00107 catch (std::exception const & e) 00108 { 00109 wrapper->is_ok = false; 00110 wrapper->error_message = e.what(); 00111 } 00112 } 00113 00114 // this will not be needed until dynamic row is exposed 00115 // SOCI_DECL void soci_uppercase_column_names(session_handle s, bool forceToUpper) 00116 // { 00117 // session_wrapper * wrapper = static_cast<session_wrapper *>(s); 00118 // wrapper->sql.uppercase_column_names(forceToUpper); 00119 // wrapper->is_ok = true; 00120 // } 00121 00122 SOCI_DECL int soci_session_state(session_handle s) 00123 { 00124 session_wrapper * wrapper = static_cast<session_wrapper *>(s); 00125 00126 return wrapper->is_ok ? 1 : 0; 00127 } 00128 00129 SOCI_DECL char const * soci_session_error_message(session_handle s) 00130 { 00131 session_wrapper * wrapper = static_cast<session_wrapper *>(s); 00132 00133 return wrapper->error_message.c_str(); 00134 } 00135 00136 00137 // statement 00138 00139 00140 namespace // unnamed 00141 { 00142 00143 struct statement_wrapper 00144 { 00145 statement_wrapper(session & sql) 00146 : st(sql), statement_state(clean), into_kind(empty), use_kind(empty), 00147 next_position(0), is_ok(true) {} 00148 00149 statement st; 00150 00151 enum state { clean, defining, executing } statement_state; 00152 enum kind { empty, single, bulk } into_kind, use_kind; 00153 00154 // into elements 00155 int next_position; 00156 std::vector<data_type> into_types; // for both single and bulk 00157 std::vector<indicator> into_indicators; 00158 std::map<int, std::string> into_strings; 00159 std::map<int, int> into_ints; 00160 std::map<int, long long> into_longlongs; 00161 std::map<int, double> into_doubles; 00162 std::map<int, std::tm> into_dates; 00163 00164 std::vector<std::vector<indicator> > into_indicators_v; 00165 std::map<int, std::vector<std::string> > into_strings_v; 00166 std::map<int, std::vector<int> > into_ints_v; 00167 std::map<int, std::vector<long long> > into_longlongs_v; 00168 std::map<int, std::vector<double> > into_doubles_v; 00169 std::map<int, std::vector<std::tm> > into_dates_v; 00170 00171 // use elements 00172 std::map<std::string, indicator> use_indicators; 00173 std::map<std::string, std::string> use_strings; 00174 std::map<std::string, int> use_ints; 00175 std::map<std::string, long long> use_longlongs; 00176 std::map<std::string, double> use_doubles; 00177 std::map<std::string, std::tm> use_dates; 00178 00179 std::map<std::string, std::vector<indicator> > use_indicators_v; 00180 std::map<std::string, std::vector<std::string> > use_strings_v; 00181 std::map<std::string, std::vector<int> > use_ints_v; 00182 std::map<std::string, std::vector<long long> > use_longlongs_v; 00183 std::map<std::string, std::vector<double> > use_doubles_v; 00184 std::map<std::string, std::vector<std::tm> > use_dates_v; 00185 00186 // format is: "YYYY MM DD hh mm ss" 00187 char date_formatted[20]; 00188 00189 bool is_ok; 00190 std::string error_message; 00191 }; 00192 00193 // helper for checking if the attempt was made to add more into/use elements 00194 // after the statement was set for execution 00195 bool cannot_add_elements(statement_wrapper & wrapper, statement_wrapper::kind k, bool into) 00196 { 00197 if (wrapper.statement_state == statement_wrapper::executing) 00198 { 00199 wrapper.is_ok = false; 00200 wrapper.error_message = "Cannot add more data items."; 00201 return true; 00202 } 00203 00204 if (into) 00205 { 00206 if (k == statement_wrapper::single && wrapper.into_kind == statement_wrapper::bulk) 00207 { 00208 wrapper.is_ok = false; 00209 wrapper.error_message = "Cannot add single into data items."; 00210 return true; 00211 } 00212 if (k == statement_wrapper::bulk && wrapper.into_kind == statement_wrapper::single) 00213 { 00214 wrapper.is_ok = false; 00215 wrapper.error_message = "Cannot add vector into data items."; 00216 return true; 00217 } 00218 } 00219 else 00220 { 00221 // trying to add use elements 00222 if (k == statement_wrapper::single && wrapper.use_kind == statement_wrapper::bulk) 00223 { 00224 wrapper.is_ok = false; 00225 wrapper.error_message = "Cannot add single use data items."; 00226 return true; 00227 } 00228 if (k == statement_wrapper::bulk && wrapper.use_kind == statement_wrapper::single) 00229 { 00230 wrapper.is_ok = false; 00231 wrapper.error_message = "Cannot add vector use data items."; 00232 return true; 00233 } 00234 } 00235 00236 wrapper.is_ok = true; 00237 return false; 00238 } 00239 00240 // helper for checking if the expected into element exists on the given position 00241 bool position_check_failed(statement_wrapper & wrapper, statement_wrapper::kind k, 00242 int position, data_type expected_type, char const * type_name) 00243 { 00244 if (position < 0 || position >= wrapper.next_position) 00245 { 00246 wrapper.is_ok = false; 00247 wrapper.error_message = "Invalid position."; 00248 return true; 00249 } 00250 00251 if (wrapper.into_types[position] != expected_type) 00252 { 00253 wrapper.is_ok = false; 00254 wrapper.error_message = "No into "; 00255 if (k == statement_wrapper::bulk) 00256 { 00257 wrapper.error_message += "vector "; 00258 } 00259 wrapper.error_message += type_name; 00260 wrapper.error_message += " element at this position."; 00261 return true; 00262 } 00263 00264 wrapper.is_ok = true; 00265 return false; 00266 } 00267 00268 // helper for checking if the into element on the given position 00269 // is not null 00270 bool not_null_check_failed(statement_wrapper & wrapper, int position) 00271 { 00272 if (wrapper.into_indicators[position] == i_null) 00273 { 00274 wrapper.is_ok = false; 00275 wrapper.error_message = "Element is null."; 00276 return true; 00277 } 00278 00279 wrapper.is_ok = true; 00280 return false; 00281 } 00282 00283 // overloaded version for vectors 00284 bool not_null_check_failed(statement_wrapper & wrapper, int position, int index) 00285 { 00286 if (wrapper.into_indicators_v[position][index] == i_null) 00287 { 00288 wrapper.is_ok = false; 00289 wrapper.error_message = "Element is null."; 00290 return true; 00291 } 00292 00293 wrapper.is_ok = true; 00294 return false; 00295 } 00296 00297 // helper for checking the index value 00298 template <typename T> 00299 bool index_check_failed(std::vector<T> const & v, 00300 statement_wrapper & wrapper, int index) 00301 { 00302 if (index < 0 || index >= static_cast<int>(v.size())) 00303 { 00304 wrapper.is_ok = false; 00305 wrapper.error_message = "Invalid index."; 00306 return true; 00307 } 00308 00309 wrapper.is_ok = true; 00310 return false; 00311 } 00312 00313 // helper for checking the uniqueness of the use element's name 00314 bool name_unique_check_failed(statement_wrapper & wrapper, 00315 statement_wrapper::kind k, char const * name) 00316 { 00317 bool is_unique; 00318 if (k == statement_wrapper::single) 00319 { 00320 typedef std::map<std::string, indicator>::const_iterator iterator; 00321 iterator const it = wrapper.use_indicators.find(name); 00322 is_unique = it == wrapper.use_indicators.end(); 00323 } 00324 else 00325 { 00326 // vector version 00327 00328 typedef std::map 00329 < 00330 std::string, 00331 std::vector<indicator> 00332 >::const_iterator iterator; 00333 00334 iterator const it = wrapper.use_indicators_v.find(name); 00335 is_unique = it == wrapper.use_indicators_v.end(); 00336 } 00337 00338 if (is_unique) 00339 { 00340 wrapper.is_ok = true; 00341 return false; 00342 } 00343 else 00344 { 00345 wrapper.is_ok = false; 00346 wrapper.error_message = "Name of use element should be unique."; 00347 return true; 00348 } 00349 } 00350 00351 // helper for checking if the use element with the given name exists 00352 bool name_exists_check_failed(statement_wrapper & wrapper, 00353 char const * name, data_type expected_type, 00354 statement_wrapper::kind k, char const * type_name) 00355 { 00356 bool name_exists = false; 00357 if (k == statement_wrapper::single) 00358 { 00359 switch (expected_type) 00360 { 00361 case dt_string: 00362 { 00363 typedef std::map 00364 < 00365 std::string, 00366 std::string 00367 >::const_iterator iterator; 00368 iterator const it = wrapper.use_strings.find(name); 00369 name_exists = (it != wrapper.use_strings.end()); 00370 } 00371 break; 00372 case dt_integer: 00373 { 00374 typedef std::map<std::string, int>::const_iterator iterator; 00375 iterator const it = wrapper.use_ints.find(name); 00376 name_exists = (it != wrapper.use_ints.end()); 00377 } 00378 break; 00379 case dt_long_long: 00380 { 00381 typedef std::map<std::string, long long>::const_iterator 00382 iterator; 00383 iterator const it = wrapper.use_longlongs.find(name); 00384 name_exists = (it != wrapper.use_longlongs.end()); 00385 } 00386 break; 00387 case dt_double: 00388 { 00389 typedef std::map<std::string, double>::const_iterator iterator; 00390 iterator const it = wrapper.use_doubles.find(name); 00391 name_exists = (it != wrapper.use_doubles.end()); 00392 } 00393 break; 00394 case dt_date: 00395 { 00396 typedef std::map<std::string, std::tm>::const_iterator iterator; 00397 iterator const it = wrapper.use_dates.find(name); 00398 name_exists = (it != wrapper.use_dates.end()); 00399 } 00400 break; 00401 default: 00402 assert(false); 00403 } 00404 } 00405 else 00406 { 00407 // vector version 00408 00409 switch (expected_type) 00410 { 00411 case dt_string: 00412 { 00413 typedef std::map 00414 < 00415 std::string, 00416 std::vector<std::string> 00417 >::const_iterator iterator; 00418 iterator const it = wrapper.use_strings_v.find(name); 00419 name_exists = (it != wrapper.use_strings_v.end()); 00420 } 00421 break; 00422 case dt_integer: 00423 { 00424 typedef std::map 00425 < 00426 std::string, 00427 std::vector<int> 00428 >::const_iterator iterator; 00429 iterator const it = wrapper.use_ints_v.find(name); 00430 name_exists = (it != wrapper.use_ints_v.end()); 00431 } 00432 break; 00433 case dt_long_long: 00434 { 00435 typedef std::map 00436 < 00437 std::string, 00438 std::vector<long long> 00439 >::const_iterator iterator; 00440 iterator const it = wrapper.use_longlongs_v.find(name); 00441 name_exists = (it != wrapper.use_longlongs_v.end()); 00442 } 00443 break; 00444 case dt_double: 00445 { 00446 typedef std::map<std::string, 00447 std::vector<double> >::const_iterator iterator; 00448 iterator const it = wrapper.use_doubles_v.find(name); 00449 name_exists = (it != wrapper.use_doubles_v.end()); 00450 } 00451 break; 00452 case dt_date: 00453 { 00454 typedef std::map<std::string, 00455 std::vector<std::tm> >::const_iterator iterator; 00456 iterator const it = wrapper.use_dates_v.find(name); 00457 name_exists = (it != wrapper.use_dates_v.end()); 00458 } 00459 break; 00460 default: 00461 assert(false); 00462 } 00463 } 00464 00465 if (name_exists) 00466 { 00467 wrapper.is_ok = true; 00468 return false; 00469 } 00470 else 00471 { 00472 wrapper.is_ok = false; 00473 wrapper.error_message = "No use "; 00474 wrapper.error_message += type_name; 00475 wrapper.error_message += " element with this name."; 00476 return true; 00477 } 00478 } 00479 00480 // helper function for resizing all vectors<T> in the map 00481 template <typename T> 00482 void resize_in_map(std::map<std::string, std::vector<T> > & m, int new_size) 00483 { 00484 typedef typename std::map<std::string, std::vector<T> >::iterator iterator; 00485 iterator it = m.begin(); 00486 iterator const end = m.end(); 00487 for ( ; it != end; ++it) 00488 { 00489 std::vector<T> & v = it->second; 00490 v.resize(new_size); 00491 } 00492 } 00493 00494 // helper for formatting date values 00495 char const * format_date(statement_wrapper & wrapper, std::tm const & d) 00496 { 00497 std::sprintf(wrapper.date_formatted, "%d %d %d %d %d %d", 00498 d.tm_year + 1900, d.tm_mon + 1, d.tm_mday, 00499 d.tm_hour, d.tm_min, d.tm_sec); 00500 00501 return wrapper.date_formatted; 00502 } 00503 00504 bool string_to_date(char const * val, std::tm & /* out */ dt, 00505 statement_wrapper & wrapper) 00506 { 00507 // format is: "YYYY MM DD hh mm ss" 00508 int year; 00509 int month; 00510 int day; 00511 int hour; 00512 int minute; 00513 int second; 00514 int const converted = std::sscanf(val, "%d %d %d %d %d %d", 00515 &year, &month, &day, &hour, &minute, &second); 00516 if (converted != 6) 00517 { 00518 wrapper.is_ok = false; 00519 wrapper.error_message = "Cannot convert date."; 00520 return false; 00521 } 00522 00523 wrapper.is_ok = true; 00524 00525 dt.tm_year = year - 1900; 00526 dt.tm_mon = month - 1; 00527 dt.tm_mday = day; 00528 dt.tm_hour = hour; 00529 dt.tm_min = minute; 00530 dt.tm_sec = second; 00531 00532 return true; 00533 } 00534 00535 } // namespace unnamed 00536 00537 00538 SOCI_DECL statement_handle soci_create_statement(session_handle s) 00539 { 00540 session_wrapper * session_w = static_cast<session_wrapper *>(s); 00541 try 00542 { 00543 statement_wrapper * statement_w = new statement_wrapper(session_w->sql); 00544 return statement_w; 00545 } 00546 catch (std::exception const & e) 00547 { 00548 session_w->is_ok = false; 00549 session_w->error_message = e.what(); 00550 return NULL; 00551 } 00552 } 00553 00554 SOCI_DECL void soci_destroy_statement(statement_handle st) 00555 { 00556 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00557 delete wrapper; 00558 } 00559 00560 SOCI_DECL int soci_into_string(statement_handle st) 00561 { 00562 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00563 00564 if (cannot_add_elements(*wrapper, statement_wrapper::single, true)) 00565 { 00566 return -1; 00567 } 00568 00569 wrapper->statement_state = statement_wrapper::defining; 00570 wrapper->into_kind = statement_wrapper::single; 00571 00572 wrapper->into_types.push_back(dt_string); 00573 wrapper->into_indicators.push_back(i_ok); 00574 wrapper->into_strings[wrapper->next_position]; // create new entry 00575 return wrapper->next_position++; 00576 } 00577 00578 SOCI_DECL int soci_into_int(statement_handle st) 00579 { 00580 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00581 00582 if (cannot_add_elements(*wrapper, statement_wrapper::single, true)) 00583 { 00584 return -1; 00585 } 00586 00587 wrapper->statement_state = statement_wrapper::defining; 00588 wrapper->into_kind = statement_wrapper::single; 00589 00590 wrapper->into_types.push_back(dt_integer); 00591 wrapper->into_indicators.push_back(i_ok); 00592 wrapper->into_ints[wrapper->next_position]; // create new entry 00593 return wrapper->next_position++; 00594 } 00595 00596 SOCI_DECL int soci_into_long_long(statement_handle st) 00597 { 00598 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00599 00600 if (cannot_add_elements(*wrapper, statement_wrapper::single, true)) 00601 { 00602 return -1; 00603 } 00604 00605 wrapper->statement_state = statement_wrapper::defining; 00606 wrapper->into_kind = statement_wrapper::single; 00607 00608 wrapper->into_types.push_back(dt_long_long); 00609 wrapper->into_indicators.push_back(i_ok); 00610 wrapper->into_longlongs[wrapper->next_position]; // create new entry 00611 return wrapper->next_position++; 00612 } 00613 00614 SOCI_DECL int soci_into_double(statement_handle st) 00615 { 00616 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00617 00618 if (cannot_add_elements(*wrapper, statement_wrapper::single, true)) 00619 { 00620 return -1; 00621 } 00622 00623 wrapper->statement_state = statement_wrapper::defining; 00624 wrapper->into_kind = statement_wrapper::single; 00625 00626 wrapper->into_types.push_back(dt_double); 00627 wrapper->into_indicators.push_back(i_ok); 00628 wrapper->into_doubles[wrapper->next_position]; // create new entry 00629 return wrapper->next_position++; 00630 } 00631 00632 SOCI_DECL int soci_into_date(statement_handle st) 00633 { 00634 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00635 00636 if (cannot_add_elements(*wrapper, statement_wrapper::single, true)) 00637 { 00638 return -1; 00639 } 00640 00641 wrapper->statement_state = statement_wrapper::defining; 00642 wrapper->into_kind = statement_wrapper::single; 00643 00644 wrapper->into_types.push_back(dt_date); 00645 wrapper->into_indicators.push_back(i_ok); 00646 wrapper->into_dates[wrapper->next_position]; // create new entry 00647 return wrapper->next_position++; 00648 } 00649 00650 SOCI_DECL int soci_into_string_v(statement_handle st) 00651 { 00652 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00653 00654 if (cannot_add_elements(*wrapper, statement_wrapper::bulk, true)) 00655 { 00656 return -1; 00657 } 00658 00659 wrapper->statement_state = statement_wrapper::defining; 00660 wrapper->into_kind = statement_wrapper::bulk; 00661 00662 wrapper->into_types.push_back(dt_string); 00663 wrapper->into_indicators_v.push_back(std::vector<indicator>()); 00664 wrapper->into_strings_v[wrapper->next_position]; 00665 return wrapper->next_position++; 00666 } 00667 00668 SOCI_DECL int soci_into_int_v(statement_handle st) 00669 { 00670 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00671 00672 if (cannot_add_elements(*wrapper, statement_wrapper::bulk, true)) 00673 { 00674 return -1; 00675 } 00676 00677 wrapper->statement_state = statement_wrapper::defining; 00678 wrapper->into_kind = statement_wrapper::bulk; 00679 00680 wrapper->into_types.push_back(dt_integer); 00681 wrapper->into_indicators_v.push_back(std::vector<indicator>()); 00682 wrapper->into_ints_v[wrapper->next_position]; 00683 return wrapper->next_position++; 00684 } 00685 00686 SOCI_DECL int soci_into_long_long_v(statement_handle st) 00687 { 00688 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00689 00690 if (cannot_add_elements(*wrapper, statement_wrapper::bulk, true)) 00691 { 00692 return -1; 00693 } 00694 00695 wrapper->statement_state = statement_wrapper::defining; 00696 wrapper->into_kind = statement_wrapper::bulk; 00697 00698 wrapper->into_types.push_back(dt_long_long); 00699 wrapper->into_indicators_v.push_back(std::vector<indicator>()); 00700 wrapper->into_longlongs_v[wrapper->next_position]; 00701 return wrapper->next_position++; 00702 } 00703 00704 SOCI_DECL int soci_into_double_v(statement_handle st) 00705 { 00706 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00707 00708 if (cannot_add_elements(*wrapper, statement_wrapper::bulk, true)) 00709 { 00710 return -1; 00711 } 00712 00713 wrapper->statement_state = statement_wrapper::defining; 00714 wrapper->into_kind = statement_wrapper::bulk; 00715 00716 wrapper->into_types.push_back(dt_double); 00717 wrapper->into_indicators_v.push_back(std::vector<indicator>()); 00718 wrapper->into_doubles_v[wrapper->next_position]; 00719 return wrapper->next_position++; 00720 } 00721 00722 SOCI_DECL int soci_into_date_v(statement_handle st) 00723 { 00724 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00725 00726 if (cannot_add_elements(*wrapper, statement_wrapper::bulk, true)) 00727 { 00728 return -1; 00729 } 00730 00731 wrapper->statement_state = statement_wrapper::defining; 00732 wrapper->into_kind = statement_wrapper::bulk; 00733 00734 wrapper->into_types.push_back(dt_date); 00735 wrapper->into_indicators_v.push_back(std::vector<indicator>()); 00736 wrapper->into_dates_v[wrapper->next_position]; 00737 return wrapper->next_position++; 00738 } 00739 00740 SOCI_DECL int soci_get_into_state(statement_handle st, int position) 00741 { 00742 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00743 00744 if (position < 0 || position >= wrapper->next_position) 00745 { 00746 wrapper->is_ok = false; 00747 wrapper->error_message = "Invalid position."; 00748 return 0; 00749 } 00750 00751 wrapper->is_ok = true; 00752 return wrapper->into_indicators[position] == i_ok ? 1 : 0; 00753 } 00754 00755 SOCI_DECL char const * soci_get_into_string(statement_handle st, int position) 00756 { 00757 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00758 00759 if (position_check_failed(*wrapper, 00760 statement_wrapper::single, position, dt_string, "string") || 00761 not_null_check_failed(*wrapper, position)) 00762 { 00763 return ""; 00764 } 00765 00766 return wrapper->into_strings[position].c_str(); 00767 } 00768 00769 SOCI_DECL int soci_get_into_int(statement_handle st, int position) 00770 { 00771 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00772 00773 if (position_check_failed(*wrapper, 00774 statement_wrapper::single, position, dt_integer, "int") || 00775 not_null_check_failed(*wrapper, position)) 00776 { 00777 return 0; 00778 } 00779 00780 return wrapper->into_ints[position]; 00781 } 00782 00783 SOCI_DECL long long soci_get_into_long_long(statement_handle st, int position) 00784 { 00785 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00786 00787 if (position_check_failed(*wrapper, 00788 statement_wrapper::single, position, dt_long_long, "long long") || 00789 not_null_check_failed(*wrapper, position)) 00790 { 00791 return 0LL; 00792 } 00793 00794 return wrapper->into_longlongs[position]; 00795 } 00796 00797 SOCI_DECL double soci_get_into_double(statement_handle st, int position) 00798 { 00799 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00800 00801 if (position_check_failed(*wrapper, 00802 statement_wrapper::single, position, dt_double, "double") || 00803 not_null_check_failed(*wrapper, position)) 00804 { 00805 return 0.0; 00806 } 00807 00808 return wrapper->into_doubles[position]; 00809 } 00810 00811 SOCI_DECL char const * soci_get_into_date(statement_handle st, int position) 00812 { 00813 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00814 00815 if (position_check_failed(*wrapper, 00816 statement_wrapper::single, position, dt_date, "date") || 00817 not_null_check_failed(*wrapper, position)) 00818 { 00819 return ""; 00820 } 00821 00822 // format is: "YYYY MM DD hh mm ss" 00823 std::tm const & d = wrapper->into_dates[position]; 00824 return format_date(*wrapper, d); 00825 } 00826 00827 SOCI_DECL int soci_into_get_size_v(statement_handle st) 00828 { 00829 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00830 00831 if (wrapper->into_kind != statement_wrapper::bulk) 00832 { 00833 wrapper->is_ok = false; 00834 wrapper->error_message = "No vector into elements."; 00835 return -1; 00836 } 00837 00838 return static_cast<int>(wrapper->into_indicators_v[0].size()); 00839 } 00840 00841 SOCI_DECL void soci_into_resize_v(statement_handle st, int new_size) 00842 { 00843 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00844 00845 if (new_size <= 0) 00846 { 00847 wrapper->is_ok = false; 00848 wrapper->error_message = "Invalid size."; 00849 return; 00850 } 00851 00852 if (wrapper->into_kind != statement_wrapper::bulk) 00853 { 00854 wrapper->is_ok = false; 00855 wrapper->error_message = "No vector into elements."; 00856 return; 00857 } 00858 00859 for (int i = 0; i != wrapper->next_position; ++i) 00860 { 00861 wrapper->into_indicators_v[i].resize(new_size); 00862 00863 switch (wrapper->into_types[i]) 00864 { 00865 case dt_string: 00866 wrapper->into_strings_v[i].resize(new_size); 00867 break; 00868 case dt_integer: 00869 wrapper->into_ints_v[i].resize(new_size); 00870 break; 00871 case dt_long_long: 00872 wrapper->into_longlongs_v[i].resize(new_size); 00873 break; 00874 case dt_double: 00875 wrapper->into_doubles_v[i].resize(new_size); 00876 break; 00877 case dt_date: 00878 wrapper->into_dates_v[i].resize(new_size); 00879 break; 00880 default: 00881 assert(false); 00882 } 00883 } 00884 00885 wrapper->is_ok = true; 00886 } 00887 00888 SOCI_DECL int soci_get_into_state_v(statement_handle st, int position, int index) 00889 { 00890 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00891 00892 if (position < 0 || position >= wrapper->next_position) 00893 { 00894 wrapper->is_ok = false; 00895 wrapper->error_message = "Invalid position."; 00896 return 0; 00897 } 00898 00899 std::vector<indicator> const & v = wrapper->into_indicators_v[position]; 00900 if (index_check_failed(v, *wrapper, index)) 00901 { 00902 return 0; 00903 } 00904 00905 return v[index] == i_ok ? 1 : 0; 00906 } 00907 00908 SOCI_DECL char const * soci_get_into_string_v(statement_handle st, int position, int index) 00909 { 00910 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00911 00912 if (position_check_failed(*wrapper, 00913 statement_wrapper::bulk, position, dt_string, "string")) 00914 { 00915 return ""; 00916 } 00917 00918 std::vector<std::string> const & v = wrapper->into_strings_v[position]; 00919 if (index_check_failed(v, *wrapper, index) || 00920 not_null_check_failed(*wrapper, position, index)) 00921 { 00922 return ""; 00923 } 00924 00925 return v[index].c_str(); 00926 } 00927 00928 SOCI_DECL int soci_get_into_int_v(statement_handle st, int position, int index) 00929 { 00930 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00931 00932 if (position_check_failed(*wrapper, 00933 statement_wrapper::bulk, position, dt_integer, "int")) 00934 { 00935 return 0; 00936 } 00937 00938 std::vector<int> const & v = wrapper->into_ints_v[position]; 00939 if (index_check_failed(v, *wrapper, index) || 00940 not_null_check_failed(*wrapper, position, index)) 00941 { 00942 return 0; 00943 } 00944 00945 return v[index]; 00946 } 00947 00948 SOCI_DECL long long soci_get_into_long_long_v(statement_handle st, int position, int index) 00949 { 00950 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00951 00952 if (position_check_failed(*wrapper, 00953 statement_wrapper::bulk, position, dt_long_long, "long long")) 00954 { 00955 return 0; 00956 } 00957 00958 std::vector<long long> const & v = wrapper->into_longlongs_v[position]; 00959 if (index_check_failed(v, *wrapper, index) || 00960 not_null_check_failed(*wrapper, position, index)) 00961 { 00962 return 0; 00963 } 00964 00965 return v[index]; 00966 } 00967 00968 SOCI_DECL double soci_get_into_double_v(statement_handle st, int position, int index) 00969 { 00970 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00971 00972 if (position_check_failed(*wrapper, 00973 statement_wrapper::bulk, position, dt_double, "double")) 00974 { 00975 return 0.0; 00976 } 00977 00978 std::vector<double> const & v = wrapper->into_doubles_v[position]; 00979 if (index_check_failed(v, *wrapper, index) || 00980 not_null_check_failed(*wrapper, position, index)) 00981 { 00982 return 0.0; 00983 } 00984 00985 return v[index]; 00986 } 00987 00988 SOCI_DECL char const * soci_get_into_date_v(statement_handle st, int position, int index) 00989 { 00990 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 00991 00992 if (position_check_failed(*wrapper, 00993 statement_wrapper::bulk, position, dt_date, "date")) 00994 { 00995 return ""; 00996 } 00997 00998 std::vector<std::tm> const & v = wrapper->into_dates_v[position]; 00999 if (index_check_failed(v, *wrapper, index) || 01000 not_null_check_failed(*wrapper, position, index)) 01001 { 01002 return ""; 01003 } 01004 01005 return format_date(*wrapper, v[index]); 01006 } 01007 01008 SOCI_DECL void soci_use_string(statement_handle st, char const * name) 01009 { 01010 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01011 01012 if (cannot_add_elements(*wrapper, statement_wrapper::single, false) || 01013 name_unique_check_failed(*wrapper, statement_wrapper::single, name)) 01014 { 01015 return; 01016 } 01017 01018 wrapper->statement_state = statement_wrapper::defining; 01019 wrapper->use_kind = statement_wrapper::single; 01020 01021 wrapper->use_indicators[name] = i_ok; // create new entry 01022 wrapper->use_strings[name]; // create new entry 01023 } 01024 01025 SOCI_DECL void soci_use_int(statement_handle st, char const * name) 01026 { 01027 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01028 01029 if (cannot_add_elements(*wrapper, statement_wrapper::single, false) || 01030 name_unique_check_failed(*wrapper, statement_wrapper::single, name)) 01031 { 01032 return; 01033 } 01034 01035 wrapper->statement_state = statement_wrapper::defining; 01036 wrapper->use_kind = statement_wrapper::single; 01037 01038 wrapper->use_indicators[name] = i_ok; // create new entry 01039 wrapper->use_ints[name]; // create new entry 01040 } 01041 01042 SOCI_DECL void soci_use_long_long(statement_handle st, char const * name) 01043 { 01044 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01045 01046 if (cannot_add_elements(*wrapper, statement_wrapper::single, false) || 01047 name_unique_check_failed(*wrapper, statement_wrapper::single, name)) 01048 { 01049 return; 01050 } 01051 01052 wrapper->statement_state = statement_wrapper::defining; 01053 wrapper->use_kind = statement_wrapper::single; 01054 01055 wrapper->use_indicators[name] = i_ok; // create new entry 01056 wrapper->use_longlongs[name]; // create new entry 01057 } 01058 01059 SOCI_DECL void soci_use_double(statement_handle st, char const * name) 01060 { 01061 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01062 01063 if (cannot_add_elements(*wrapper, statement_wrapper::single, false) || 01064 name_unique_check_failed(*wrapper, statement_wrapper::single, name)) 01065 { 01066 return; 01067 } 01068 01069 wrapper->statement_state = statement_wrapper::defining; 01070 wrapper->use_kind = statement_wrapper::single; 01071 01072 wrapper->use_indicators[name] = i_ok; // create new entry 01073 wrapper->use_doubles[name]; // create new entry 01074 } 01075 01076 SOCI_DECL void soci_use_date(statement_handle st, char const * name) 01077 { 01078 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01079 01080 if (cannot_add_elements(*wrapper, statement_wrapper::single, false) || 01081 name_unique_check_failed(*wrapper, statement_wrapper::single, name)) 01082 { 01083 return; 01084 } 01085 01086 wrapper->statement_state = statement_wrapper::defining; 01087 wrapper->use_kind = statement_wrapper::single; 01088 01089 wrapper->use_indicators[name] = i_ok; // create new entry 01090 wrapper->use_dates[name]; // create new entry 01091 } 01092 01093 SOCI_DECL void soci_use_string_v(statement_handle st, char const * name) 01094 { 01095 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01096 01097 if (cannot_add_elements(*wrapper, statement_wrapper::bulk, false) || 01098 name_unique_check_failed(*wrapper, statement_wrapper::bulk, name)) 01099 { 01100 return; 01101 } 01102 01103 wrapper->statement_state = statement_wrapper::defining; 01104 wrapper->use_kind = statement_wrapper::bulk; 01105 01106 wrapper->use_indicators_v[name]; // create new entry 01107 wrapper->use_strings_v[name]; // create new entry 01108 } 01109 01110 SOCI_DECL void soci_use_int_v(statement_handle st, char const * name) 01111 { 01112 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01113 01114 if (cannot_add_elements(*wrapper, statement_wrapper::bulk, false) || 01115 name_unique_check_failed(*wrapper, statement_wrapper::bulk, name)) 01116 { 01117 return; 01118 } 01119 01120 wrapper->statement_state = statement_wrapper::defining; 01121 wrapper->use_kind = statement_wrapper::bulk; 01122 01123 wrapper->use_indicators_v[name]; // create new entry 01124 wrapper->use_ints_v[name]; // create new entry 01125 } 01126 01127 SOCI_DECL void soci_use_long_long_v(statement_handle st, char const * name) 01128 { 01129 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01130 01131 if (cannot_add_elements(*wrapper, statement_wrapper::bulk, false) || 01132 name_unique_check_failed(*wrapper, statement_wrapper::bulk, name)) 01133 { 01134 return; 01135 } 01136 01137 wrapper->statement_state = statement_wrapper::defining; 01138 wrapper->use_kind = statement_wrapper::bulk; 01139 01140 wrapper->use_indicators_v[name]; // create new entry 01141 wrapper->use_longlongs_v[name]; // create new entry 01142 } 01143 01144 SOCI_DECL void soci_use_double_v(statement_handle st, char const * name) 01145 { 01146 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01147 01148 if (cannot_add_elements(*wrapper, statement_wrapper::bulk, false) || 01149 name_unique_check_failed(*wrapper, statement_wrapper::bulk, name)) 01150 { 01151 return; 01152 } 01153 01154 wrapper->statement_state = statement_wrapper::defining; 01155 wrapper->use_kind = statement_wrapper::bulk; 01156 01157 wrapper->use_indicators_v[name]; // create new entry 01158 wrapper->use_doubles_v[name]; // create new entry 01159 } 01160 01161 SOCI_DECL void soci_use_date_v(statement_handle st, char const * name) 01162 { 01163 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01164 01165 if (cannot_add_elements(*wrapper, statement_wrapper::bulk, false) || 01166 name_unique_check_failed(*wrapper, statement_wrapper::bulk, name)) 01167 { 01168 return; 01169 } 01170 01171 wrapper->statement_state = statement_wrapper::defining; 01172 wrapper->use_kind = statement_wrapper::bulk; 01173 01174 wrapper->use_indicators_v[name]; // create new entry 01175 wrapper->use_dates_v[name]; // create new entry 01176 } 01177 01178 SOCI_DECL void soci_set_use_state(statement_handle st, char const * name, int state) 01179 { 01180 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01181 01182 typedef std::map<std::string, indicator>::const_iterator iterator; 01183 iterator const it = wrapper->use_indicators.find(name); 01184 if (it == wrapper->use_indicators.end()) 01185 { 01186 wrapper->is_ok = false; 01187 wrapper->error_message = "Invalid name."; 01188 return; 01189 } 01190 01191 wrapper->is_ok = true; 01192 wrapper->use_indicators[name] = (state != 0 ? i_ok : i_null); 01193 } 01194 01195 SOCI_DECL void soci_set_use_string(statement_handle st, char const * name, char const * val) 01196 { 01197 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01198 01199 if (name_exists_check_failed(*wrapper, 01200 name, dt_string, statement_wrapper::single, "string")) 01201 { 01202 return; 01203 } 01204 01205 wrapper->use_indicators[name] = i_ok; 01206 wrapper->use_strings[name] = val; 01207 } 01208 01209 SOCI_DECL void soci_set_use_int(statement_handle st, char const * name, int val) 01210 { 01211 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01212 01213 if (name_exists_check_failed(*wrapper, 01214 name, dt_integer, statement_wrapper::single, "int")) 01215 { 01216 return; 01217 } 01218 01219 wrapper->use_indicators[name] = i_ok; 01220 wrapper->use_ints[name] = val; 01221 } 01222 01223 SOCI_DECL void soci_set_use_long_long(statement_handle st, char const * name, long long val) 01224 { 01225 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01226 01227 if (name_exists_check_failed(*wrapper, 01228 name, dt_long_long, statement_wrapper::single, "long long")) 01229 { 01230 return; 01231 } 01232 01233 wrapper->use_indicators[name] = i_ok; 01234 wrapper->use_longlongs[name] = val; 01235 } 01236 01237 SOCI_DECL void soci_set_use_double(statement_handle st, char const * name, double val) 01238 { 01239 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01240 01241 if (name_exists_check_failed(*wrapper, 01242 name, dt_double, statement_wrapper::single, "double")) 01243 { 01244 return; 01245 } 01246 01247 wrapper->use_indicators[name] = i_ok; 01248 wrapper->use_doubles[name] = val; 01249 } 01250 01251 SOCI_DECL void soci_set_use_date(statement_handle st, char const * name, char const * val) 01252 { 01253 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01254 01255 if (name_exists_check_failed(*wrapper, 01256 name, dt_date, statement_wrapper::single, "date")) 01257 { 01258 return; 01259 } 01260 01261 std::tm dt; 01262 bool const converted = string_to_date(val, dt, *wrapper); 01263 if (converted == false) 01264 { 01265 return; 01266 } 01267 01268 wrapper->use_indicators[name] = i_ok; 01269 wrapper->use_dates[name] = dt; 01270 } 01271 01272 SOCI_DECL int soci_use_get_size_v(statement_handle st) 01273 { 01274 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01275 01276 if (wrapper->use_kind != statement_wrapper::bulk) 01277 { 01278 wrapper->is_ok = false; 01279 wrapper->error_message = "No vector use elements."; 01280 return -1; 01281 } 01282 01283 typedef std::map<std::string, 01284 std::vector<indicator> >::const_iterator iterator; 01285 iterator const any_element = wrapper->use_indicators_v.begin(); 01286 assert(any_element != wrapper->use_indicators_v.end()); 01287 01288 return static_cast<int>(any_element->second.size()); 01289 } 01290 01291 SOCI_DECL void soci_use_resize_v(statement_handle st, int new_size) 01292 { 01293 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01294 01295 if (new_size <= 0) 01296 { 01297 wrapper->is_ok = false; 01298 wrapper->error_message = "Invalid size."; 01299 return; 01300 } 01301 01302 if (wrapper->use_kind != statement_wrapper::bulk) 01303 { 01304 wrapper->is_ok = false; 01305 wrapper->error_message = "No vector use elements."; 01306 return; 01307 } 01308 01309 resize_in_map(wrapper->use_indicators_v, new_size); 01310 resize_in_map(wrapper->use_strings_v, new_size); 01311 resize_in_map(wrapper->use_ints_v, new_size); 01312 resize_in_map(wrapper->use_longlongs_v, new_size); 01313 resize_in_map(wrapper->use_doubles_v, new_size); 01314 resize_in_map(wrapper->use_dates_v, new_size); 01315 01316 wrapper->is_ok = true; 01317 } 01318 01319 SOCI_DECL void soci_set_use_state_v(statement_handle st, 01320 char const * name, int index, int state) 01321 { 01322 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01323 01324 typedef std::map<std::string, std::vector<indicator> >::iterator iterator; 01325 iterator const it = wrapper->use_indicators_v.find(name); 01326 if (it == wrapper->use_indicators_v.end()) 01327 { 01328 wrapper->is_ok = false; 01329 wrapper->error_message = "Invalid name."; 01330 return; 01331 } 01332 01333 std::vector<indicator> & v = it->second; 01334 if (index_check_failed(v, *wrapper, index)) 01335 { 01336 return; 01337 } 01338 01339 v[index] = (state != 0 ? i_ok : i_null); 01340 } 01341 01342 SOCI_DECL void soci_set_use_string_v(statement_handle st, 01343 char const * name, int index, char const * val) 01344 { 01345 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01346 01347 if (name_exists_check_failed(*wrapper, 01348 name, dt_string, statement_wrapper::bulk, "vector string")) 01349 { 01350 return; 01351 } 01352 01353 std::vector<std::string> & v = wrapper->use_strings_v[name]; 01354 if (index_check_failed(v, *wrapper, index)) 01355 { 01356 return; 01357 } 01358 01359 wrapper->use_indicators_v[name][index] = i_ok; 01360 v[index] = val; 01361 } 01362 01363 SOCI_DECL void soci_set_use_int_v(statement_handle st, 01364 char const * name, int index, int val) 01365 { 01366 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01367 01368 if (name_exists_check_failed(*wrapper, 01369 name, dt_integer, statement_wrapper::bulk, "vector int")) 01370 { 01371 return; 01372 } 01373 01374 std::vector<int> & v = wrapper->use_ints_v[name]; 01375 if (index_check_failed(v, *wrapper, index)) 01376 { 01377 return; 01378 } 01379 01380 wrapper->use_indicators_v[name][index] = i_ok; 01381 v[index] = val; 01382 } 01383 01384 SOCI_DECL void soci_set_use_long_long_v(statement_handle st, 01385 char const * name, int index, long long val) 01386 { 01387 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01388 01389 if (name_exists_check_failed(*wrapper, 01390 name, dt_long_long, statement_wrapper::bulk, "vector long long")) 01391 { 01392 return; 01393 } 01394 01395 std::vector<long long> & v = wrapper->use_longlongs_v[name]; 01396 if (index_check_failed(v, *wrapper, index)) 01397 { 01398 return; 01399 } 01400 01401 wrapper->use_indicators_v[name][index] = i_ok; 01402 v[index] = val; 01403 } 01404 01405 SOCI_DECL void soci_set_use_double_v(statement_handle st, 01406 char const * name, int index, double val) 01407 { 01408 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01409 01410 if (name_exists_check_failed(*wrapper, 01411 name, dt_double, statement_wrapper::bulk, "vector double")) 01412 { 01413 return; 01414 } 01415 01416 std::vector<double> & v = wrapper->use_doubles_v[name]; 01417 if (index_check_failed(v, *wrapper, index)) 01418 { 01419 return; 01420 } 01421 01422 wrapper->use_indicators_v[name][index] = i_ok; 01423 v[index] = val; 01424 } 01425 01426 SOCI_DECL void soci_set_use_date_v(statement_handle st, 01427 char const * name, int index, char const * val) 01428 { 01429 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01430 01431 if (name_exists_check_failed(*wrapper, 01432 name, dt_date, statement_wrapper::bulk, "vector date")) 01433 { 01434 return; 01435 } 01436 01437 std::vector<std::tm> & v = wrapper->use_dates_v[name]; 01438 if (index_check_failed(v, *wrapper, index)) 01439 { 01440 return; 01441 } 01442 01443 std::tm dt; 01444 bool const converted = string_to_date(val, dt, *wrapper); 01445 if (converted == false) 01446 { 01447 return; 01448 } 01449 01450 wrapper->use_indicators_v[name][index] = i_ok; 01451 v[index] = dt; 01452 } 01453 01454 SOCI_DECL int soci_get_use_state(statement_handle st, char const * name) 01455 { 01456 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01457 01458 typedef std::map<std::string, indicator>::const_iterator iterator; 01459 iterator const it = wrapper->use_indicators.find(name); 01460 if (it == wrapper->use_indicators.end()) 01461 { 01462 wrapper->is_ok = false; 01463 wrapper->error_message = "Invalid name."; 01464 return 0; 01465 } 01466 01467 wrapper->is_ok = true; 01468 return wrapper->use_indicators[name] == i_ok ? 1 : 0; 01469 } 01470 01471 SOCI_DECL char const * soci_get_use_string(statement_handle st, char const * name) 01472 { 01473 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01474 01475 if (name_exists_check_failed(*wrapper, 01476 name, dt_string, statement_wrapper::bulk, "string")) 01477 { 01478 return ""; 01479 } 01480 01481 return wrapper->use_strings[name].c_str(); 01482 } 01483 01484 SOCI_DECL int soci_get_use_int(statement_handle st, char const * name) 01485 { 01486 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01487 01488 if (name_exists_check_failed(*wrapper, 01489 name, dt_integer, statement_wrapper::bulk, "int")) 01490 { 01491 return 0; 01492 } 01493 01494 return wrapper->use_ints[name]; 01495 } 01496 01497 SOCI_DECL long long soci_get_use_long_long(statement_handle st, char const * name) 01498 { 01499 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01500 01501 if (name_exists_check_failed(*wrapper, 01502 name, dt_long_long, statement_wrapper::bulk, "long long")) 01503 { 01504 return 0LL; 01505 } 01506 01507 return wrapper->use_longlongs[name]; 01508 } 01509 01510 SOCI_DECL double soci_get_use_double(statement_handle st, char const * name) 01511 { 01512 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01513 01514 if (name_exists_check_failed(*wrapper, 01515 name, dt_double, statement_wrapper::bulk, "double")) 01516 { 01517 return 0.0; 01518 } 01519 01520 return wrapper->use_doubles[name]; 01521 } 01522 01523 SOCI_DECL char const * soci_get_use_date(statement_handle st, char const * name) 01524 { 01525 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01526 01527 if (name_exists_check_failed(*wrapper, 01528 name, dt_date, statement_wrapper::bulk, "date")) 01529 { 01530 return ""; 01531 } 01532 01533 // format is: "YYYY MM DD hh mm ss" 01534 std::tm const & d = wrapper->use_dates[name]; 01535 std::sprintf(wrapper->date_formatted, "%d %d %d %d %d %d", 01536 d.tm_year + 1900, d.tm_mon + 1, d.tm_mday, 01537 d.tm_hour, d.tm_min, d.tm_sec); 01538 01539 return wrapper->date_formatted; 01540 } 01541 01542 SOCI_DECL void soci_prepare(statement_handle st, char const * query) 01543 { 01544 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01545 01546 try 01547 { 01548 wrapper->statement_state = statement_wrapper::executing; 01549 01550 // bind all into elements 01551 01552 int const into_elements = static_cast<int>(wrapper->into_types.size()); 01553 if (wrapper->into_kind == statement_wrapper::single) 01554 { 01555 for (int i = 0; i != into_elements; ++i) 01556 { 01557 switch (wrapper->into_types[i]) 01558 { 01559 case dt_string: 01560 wrapper->st.exchange( 01561 into(wrapper->into_strings[i], wrapper->into_indicators[i])); 01562 break; 01563 case dt_integer: 01564 wrapper->st.exchange( 01565 into(wrapper->into_ints[i], wrapper->into_indicators[i])); 01566 break; 01567 case dt_long_long: 01568 wrapper->st.exchange( 01569 into(wrapper->into_longlongs[i], wrapper->into_indicators[i])); 01570 break; 01571 case dt_double: 01572 wrapper->st.exchange( 01573 into(wrapper->into_doubles[i], wrapper->into_indicators[i])); 01574 break; 01575 case dt_date: 01576 wrapper->st.exchange( 01577 into(wrapper->into_dates[i], wrapper->into_indicators[i])); 01578 break; 01579 default: 01580 assert(false); 01581 } 01582 } 01583 } 01584 else 01585 { 01586 // vector elements 01587 for (int i = 0; i != into_elements; ++i) 01588 { 01589 switch (wrapper->into_types[i]) 01590 { 01591 case dt_string: 01592 wrapper->st.exchange( 01593 into(wrapper->into_strings_v[i], wrapper->into_indicators_v[i])); 01594 break; 01595 case dt_integer: 01596 wrapper->st.exchange( 01597 into(wrapper->into_ints_v[i], wrapper->into_indicators_v[i])); 01598 break; 01599 case dt_long_long: 01600 wrapper->st.exchange( 01601 into(wrapper->into_longlongs_v[i], wrapper->into_indicators_v[i])); 01602 break; 01603 case dt_double: 01604 wrapper->st.exchange( 01605 into(wrapper->into_doubles_v[i], wrapper->into_indicators_v[i])); 01606 break; 01607 case dt_date: 01608 wrapper->st.exchange( 01609 into(wrapper->into_dates_v[i], wrapper->into_indicators_v[i])); 01610 break; 01611 default: 01612 assert(false); 01613 } 01614 } 01615 } 01616 01617 // bind all use elements 01618 { 01619 // strings 01620 typedef std::map<std::string, std::string>::iterator iterator; 01621 iterator uit = wrapper->use_strings.begin(); 01622 iterator const uend = wrapper->use_strings.end(); 01623 for ( ; uit != uend; ++uit) 01624 { 01625 std::string const & use_name = uit->first; 01626 std::string & use_string = uit->second; 01627 indicator & use_ind = wrapper->use_indicators[use_name]; 01628 wrapper->st.exchange(use(use_string, use_ind, use_name)); 01629 } 01630 } 01631 { 01632 // ints 01633 typedef std::map<std::string, int>::iterator iterator; 01634 iterator uit = wrapper->use_ints.begin(); 01635 iterator const uend = wrapper->use_ints.end(); 01636 for ( ; uit != uend; ++uit) 01637 { 01638 std::string const & use_name = uit->first; 01639 int & use_int = uit->second; 01640 indicator & use_ind = wrapper->use_indicators[use_name]; 01641 wrapper->st.exchange(use(use_int, use_ind, use_name)); 01642 } 01643 } 01644 { 01645 // longlongs 01646 typedef std::map<std::string, long long>::iterator iterator; 01647 iterator uit = wrapper->use_longlongs.begin(); 01648 iterator const uend = wrapper->use_longlongs.end(); 01649 for ( ; uit != uend; ++uit) 01650 { 01651 std::string const & use_name = uit->first; 01652 long long & use_longlong = uit->second; 01653 indicator & use_ind = wrapper->use_indicators[use_name]; 01654 wrapper->st.exchange(use(use_longlong, use_ind, use_name)); 01655 } 01656 } 01657 { 01658 // doubles 01659 typedef std::map<std::string, double>::iterator iterator; 01660 iterator uit = wrapper->use_doubles.begin(); 01661 iterator const uend = wrapper->use_doubles.end(); 01662 for ( ; uit != uend; ++uit) 01663 { 01664 std::string const & use_name = uit->first; 01665 double & use_double = uit->second; 01666 indicator & use_ind = wrapper->use_indicators[use_name]; 01667 wrapper->st.exchange(use(use_double, use_ind, use_name)); 01668 } 01669 } 01670 { 01671 // dates 01672 typedef std::map<std::string, std::tm>::iterator iterator; 01673 iterator uit = wrapper->use_dates.begin(); 01674 iterator const uend = wrapper->use_dates.end(); 01675 for ( ; uit != uend; ++uit) 01676 { 01677 std::string const & use_name = uit->first; 01678 std::tm & use_date = uit->second; 01679 indicator & use_ind = wrapper->use_indicators[use_name]; 01680 wrapper->st.exchange(use(use_date, use_ind, use_name)); 01681 } 01682 } 01683 01684 // bind all use vecctor elements 01685 { 01686 // strings 01687 typedef std::map<std::string, 01688 std::vector<std::string> >::iterator iterator; 01689 iterator uit = wrapper->use_strings_v.begin(); 01690 iterator const uend = wrapper->use_strings_v.end(); 01691 for ( ; uit != uend; ++uit) 01692 { 01693 std::string const & use_name = uit->first; 01694 std::vector<std::string> & use_string = uit->second; 01695 std::vector<indicator> & use_ind = 01696 wrapper->use_indicators_v[use_name]; 01697 wrapper->st.exchange(use(use_string, use_ind, use_name)); 01698 } 01699 } 01700 { 01701 // ints 01702 typedef std::map<std::string, 01703 std::vector<int> >::iterator iterator; 01704 iterator uit = wrapper->use_ints_v.begin(); 01705 iterator const uend = wrapper->use_ints_v.end(); 01706 for ( ; uit != uend; ++uit) 01707 { 01708 std::string const & use_name = uit->first; 01709 std::vector<int> & use_int = uit->second; 01710 std::vector<indicator> & use_ind = 01711 wrapper->use_indicators_v[use_name]; 01712 wrapper->st.exchange(use(use_int, use_ind, use_name)); 01713 } 01714 } 01715 { 01716 // longlongs 01717 typedef std::map<std::string, 01718 std::vector<long long> >::iterator iterator; 01719 iterator uit = wrapper->use_longlongs_v.begin(); 01720 iterator const uend = wrapper->use_longlongs_v.end(); 01721 for ( ; uit != uend; ++uit) 01722 { 01723 std::string const & use_name = uit->first; 01724 std::vector<long long> & use_longlong = uit->second; 01725 std::vector<indicator> & use_ind = 01726 wrapper->use_indicators_v[use_name]; 01727 wrapper->st.exchange(use(use_longlong, use_ind, use_name)); 01728 } 01729 } 01730 { 01731 // doubles 01732 typedef std::map<std::string, 01733 std::vector<double> >::iterator iterator; 01734 iterator uit = wrapper->use_doubles_v.begin(); 01735 iterator const uend = wrapper->use_doubles_v.end(); 01736 for ( ; uit != uend; ++uit) 01737 { 01738 std::string const & use_name = uit->first; 01739 std::vector<double> & use_double = uit->second; 01740 std::vector<indicator> & use_ind = 01741 wrapper->use_indicators_v[use_name]; 01742 wrapper->st.exchange(use(use_double, use_ind, use_name)); 01743 } 01744 } 01745 { 01746 // dates 01747 typedef std::map<std::string, 01748 std::vector<std::tm> >::iterator iterator; 01749 iterator uit = wrapper->use_dates_v.begin(); 01750 iterator const uend = wrapper->use_dates_v.end(); 01751 for ( ; uit != uend; ++uit) 01752 { 01753 std::string const & use_name = uit->first; 01754 std::vector<std::tm> & use_date = uit->second; 01755 std::vector<indicator> & use_ind = 01756 wrapper->use_indicators_v[use_name]; 01757 wrapper->st.exchange(use(use_date, use_ind, use_name)); 01758 } 01759 } 01760 01761 wrapper->st.alloc(); 01762 wrapper->st.prepare(query); 01763 wrapper->st.define_and_bind(); 01764 01765 wrapper->is_ok = true; 01766 } 01767 catch (std::exception const & e) 01768 { 01769 wrapper->is_ok = false; 01770 wrapper->error_message = e.what(); 01771 } 01772 } 01773 01774 SOCI_DECL int soci_execute(statement_handle st, int withDataExchange) 01775 { 01776 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01777 01778 try 01779 { 01780 bool const gotData = wrapper->st.execute(withDataExchange != 0); 01781 01782 wrapper->is_ok = true; 01783 01784 return gotData ? 1 : 0; 01785 } 01786 catch (std::exception const & e) 01787 { 01788 wrapper->is_ok = false; 01789 wrapper->error_message = e.what(); 01790 01791 return 0; 01792 } 01793 } 01794 01795 SOCI_DECL int soci_fetch(statement_handle st) 01796 { 01797 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01798 01799 try 01800 { 01801 bool const gotData = wrapper->st.fetch(); 01802 01803 wrapper->is_ok = true; 01804 01805 return gotData ? 1 : 0; 01806 } 01807 catch (std::exception const & e) 01808 { 01809 wrapper->is_ok = false; 01810 wrapper->error_message = e.what(); 01811 01812 return 0; 01813 } 01814 } 01815 01816 SOCI_DECL int soci_got_data(statement_handle st) 01817 { 01818 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01819 01820 return wrapper->st.got_data() ? 1 : 0; 01821 } 01822 01823 SOCI_DECL int soci_statement_state(statement_handle st) 01824 { 01825 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01826 01827 return wrapper->is_ok ? 1 : 0; 01828 } 01829 01830 SOCI_DECL char const * soci_statement_error_message(statement_handle st) 01831 { 01832 statement_wrapper * wrapper = static_cast<statement_wrapper *>(st); 01833 01834 return wrapper->error_message.c_str(); 01835 }
Generated on Sun Oct 3 2010 17:42:17 for EXTRAS-SOCI by Doxygen 1.7.1