SOCI Logo Get SOCI at SourceForge.net. Fast, secure and Free Open Source software downloads

vector-into-type.cpp

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton, David Courtney
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 #include "soci-sqlite3.h"
00009 #include "common.h"
00010 //
00011 #if defined(SOCI_HEADERS_BURIED)
00012 #       include <soci/core/soci-platform.h>
00013 #else
00014 #       include <soci-platform.h>
00015 #endif
00016 // std
00017 #include <cstddef>
00018 #include <cstdlib>
00019 #include <ctime>
00020 #include <string>
00021 #include <vector>
00022 
00023 using namespace soci;
00024 using namespace soci::details;
00025 using namespace soci::details::sqlite3;
00026 
00027 void sqlite3_vector_into_type_backend::define_by_pos(int & position, void * data,
00028                                                exchange_type type)
00029 {
00030     data_ = data;
00031     type_ = type;
00032     position_ = position++;
00033 }
00034 
00035 void sqlite3_vector_into_type_backend::pre_fetch()
00036 {
00037     // ...
00038 }
00039 
00040 namespace // anonymous
00041 {
00042 
00043 template <typename T>
00044 void setInVector(void *p, int indx, T const &val)
00045 {
00046     std::vector<T> *dest =
00047     static_cast<std::vector<T> *>(p);
00048 
00049     std::vector<T> &v = *dest;
00050     v[indx] = val;
00051 }
00052 
00053 } // namespace anonymous
00054 
00055 void sqlite3_vector_into_type_backend::post_fetch(bool gotData, indicator * ind)
00056 {
00057     if (gotData)
00058     {
00059         int endRow = static_cast<int>(statement_.dataCache_.size());
00060         for (int i = 0; i < endRow; ++i)
00061         {
00062             const sqlite3_column& curCol =
00063                 statement_.dataCache_[i][position_-1];
00064 
00065             if (curCol.isNull_)
00066             {
00067                 if (ind == NULL)
00068                 {
00069                     throw soci_error(
00070                         "Null value fetched and no indicator defined.");
00071                 }
00072 
00073                 ind[i] = i_null;
00074             }
00075             else
00076             {
00077                 if (ind != NULL)
00078                 {
00079                     ind[i] = i_ok;
00080                 }
00081             }
00082 
00083             const char * buf = curCol.data_.c_str();
00084 
00085 
00086             // set buf to a null string if a null pointer is returned
00087             if (buf == NULL)
00088             {
00089                 buf = "";
00090             }
00091 
00092             switch (type_)
00093             {
00094             case x_char:
00095                 setInVector(data_, i, *buf);
00096                 break;
00097             case x_stdstring:
00098                 setInVector<std::string>(data_, i, buf);
00099                 break;
00100             case x_short:
00101             {
00102                 long val = std::strtol(buf, NULL, 10);
00103                 setInVector(data_, i, static_cast<short>(val));
00104             }
00105             break;
00106             case x_integer:
00107             {
00108                 long val = std::strtol(buf, NULL, 10);
00109                 setInVector(data_, i, static_cast<int>(val));
00110             }
00111             break;
00112             case x_unsigned_long:
00113             {
00114                 long long val = strtoll(buf, NULL, 10);
00115                 setInVector(data_, i, static_cast<unsigned long>(val));
00116             }
00117             break;
00118             case x_long_long:
00119             {
00120                 long long val = strtoll(buf, NULL, 10);
00121                 setInVector(data_, i, val);
00122             }
00123             break;
00124             case x_double:
00125             {
00126                 double val = strtod(buf, NULL);
00127                 setInVector(data_, i, val);
00128             }
00129             break;
00130             case x_stdtm:
00131             {
00132                 // attempt to parse the string and convert to std::tm
00133                 std::tm t;
00134                 parseStdTm(buf, t);
00135 
00136                 setInVector(data_, i, t);
00137             }
00138             break;
00139 
00140             default:
00141                 throw soci_error("Into element used with non-supported type.");
00142             }
00143         }
00144     }
00145     else // no data retrieved
00146     {
00147         // nothing to do, into vectors are already truncated
00148     }
00149 }
00150 
00151 void sqlite3_vector_into_type_backend::resize(std::size_t sz)
00152 {
00153     switch (type_)
00154     {
00155         // simple cases
00156     case x_char:         resizeVector<char>         (data_, sz); break;
00157     case x_short:        resizeVector<short>        (data_, sz); break;
00158     case x_integer:      resizeVector<int>          (data_, sz); break;
00159     case x_unsigned_long: resizeVector<unsigned long>(data_, sz); break;
00160     case x_long_long:     resizeVector<long long>    (data_, sz); break;
00161     case x_double:       resizeVector<double>       (data_, sz); break;
00162     case x_stdstring:    resizeVector<std::string>  (data_, sz); break;
00163     case x_stdtm:        resizeVector<std::tm>      (data_, sz); break;
00164 
00165     default:
00166         throw soci_error("Into vector element used with non-supported type.");
00167     }
00168 }
00169 
00170 std::size_t sqlite3_vector_into_type_backend::size()
00171 {
00172     std::size_t sz = 0; // dummy initialization to please the compiler
00173     switch (type_)
00174     {
00175         // simple cases
00176     case x_char:         sz = getVectorSize<char>         (data_); break;
00177     case x_short:        sz = getVectorSize<short>        (data_); break;
00178     case x_integer:      sz = getVectorSize<int>          (data_); break;
00179     case x_unsigned_long: sz = getVectorSize<unsigned long>(data_); break;
00180     case x_long_long:     sz = getVectorSize<long long>    (data_); break;
00181     case x_double:       sz = getVectorSize<double>       (data_); break;
00182     case x_stdstring:    sz = getVectorSize<std::string>  (data_); break;
00183     case x_stdtm:        sz = getVectorSize<std::tm>      (data_); break;
00184 
00185     default:
00186         throw soci_error("Into vector element used with non-supported type.");
00187     }
00188 
00189     return sz;
00190 }
00191 
00192 void sqlite3_vector_into_type_backend::clean_up()
00193 {
00194     // ...
00195 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
SourceForge Logo

Generated on Sun Oct 3 2010 17:42:16 for EXTRAS-SOCI by Doxygen 1.7.1