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, Rafal Bobrowski
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_FIREBIRD_SOURCE
00009 #include "soci-firebird.h"
00010 #include "common.h"
00011 
00012 using namespace soci;
00013 using namespace soci::details;
00014 using namespace soci::details::firebird;
00015 
00016 void firebird_vector_into_type_backend::define_by_pos(
00017     int & position, void * data, exchange_type type)
00018 {
00019     position_ = position-1;
00020     data_ = data;
00021     type_ = type;
00022 
00023     ++position;
00024 
00025     statement_.intoType_ = eVector;
00026     statement_.intos_.push_back(static_cast<void*>(this));
00027 
00028     XSQLVAR *var = statement_.sqldap_->sqlvar+position_;
00029 
00030     buf_ = allocBuffer(var);
00031     var->sqldata = buf_;
00032     var->sqlind = &indISCHolder_;
00033 }
00034 
00035 void firebird_vector_into_type_backend::pre_fetch()
00036 {
00037     // Nothing to do here.
00038 }
00039 
00040 namespace // anonymous
00041 {
00042 template <typename T>
00043 void setIntoVector(void *p, std::size_t indx, T const &val)
00044 {
00045     std::vector<T> *dest =
00046         static_cast<std::vector<T> *>(p);
00047 
00048     std::vector<T> &v = *dest;
00049     v[indx] = val;
00050 }
00051 
00052 } // namespace anonymous
00053 
00054 // this will exchange data with vector user buffers
00055 void firebird_vector_into_type_backend::exchangeData(std::size_t row)
00056 {
00057     XSQLVAR *var = statement_.sqldap_->sqlvar+position_;
00058 
00059     switch (type_)
00060     {
00061         // simple cases
00062     case x_char:
00063         setIntoVector(data_, row, getTextParam(var)[0]);
00064         break;
00065     case x_short:
00066         {
00067             short tmp = from_isc<short>(var);
00068             setIntoVector(data_, row, tmp);
00069         }
00070         break;
00071     case x_integer:
00072         {
00073             int tmp = from_isc<int>(var);
00074             setIntoVector(data_, row, tmp);
00075         }
00076         break;
00077     case x_unsigned_long:
00078         {
00079             unsigned long tmp = from_isc<unsigned long>(var);
00080             setIntoVector(data_, row, tmp);
00081         }
00082         break;
00083     case x_long_long:
00084         {
00085             long long tmp = from_isc<long long>(var);
00086             setIntoVector(data_, row, tmp);
00087         }
00088         break;
00089     case x_double:
00090         {
00091             double tmp = from_isc<double>(var);
00092             setIntoVector(data_, row, tmp);
00093         }
00094         break;
00095 
00096         // cases that require adjustments and buffer management
00097     case x_stdstring:
00098         setIntoVector(data_, row, getTextParam(var));
00099         break;
00100     case x_stdtm:
00101         {
00102             std::tm data;
00103             tmDecode(var->sqltype, buf_, &data);
00104             setIntoVector(data_, row, data);
00105         }
00106         break;
00107 
00108     default:
00109         throw soci_error("Into vector element used with non-supported type.");
00110     } // switch
00111 
00112 }
00113 
00114 void firebird_vector_into_type_backend::post_fetch(
00115     bool gotData, indicator * ind)
00116 {
00117     // Here we have to set indicators only. Data was exchanged with user
00118     // buffers during fetch()
00119     if (gotData)
00120     {
00121         std::size_t rows = statement_.inds_[0].size();
00122 
00123         for (std::size_t i = 0; i<rows; ++i)
00124         {
00125             if (statement_.inds_[position_][i] == i_null && (ind == NULL))
00126             {
00127                 throw soci_error("Null value fetched and no indicator defined.");
00128             }
00129             else if (ind != NULL)
00130             {
00131                 ind[i] = statement_.inds_[position_][i];
00132             }
00133         }
00134     }
00135 }
00136 
00137 void firebird_vector_into_type_backend::resize(std::size_t sz)
00138 {
00139     switch (type_)
00140     {
00141     case x_char:
00142         resizeVector<char> (data_, sz);
00143         break;
00144     case x_short:
00145         resizeVector<short> (data_, sz);
00146         break;
00147     case x_integer:
00148         resizeVector<int> (data_, sz);
00149         break;
00150     case x_unsigned_long:
00151         resizeVector<unsigned long>(data_, sz);
00152         break;
00153     case x_long_long:
00154         resizeVector<long long> (data_, sz);
00155         break;
00156     case x_double:
00157         resizeVector<double> (data_, sz);
00158         break;
00159     case x_stdstring:
00160         resizeVector<std::string> (data_, sz);
00161         break;
00162     case x_stdtm:
00163         resizeVector<std::tm> (data_, sz);
00164         break;
00165 
00166     default:
00167         throw soci_error("Into vector element used with non-supported type.");
00168     }
00169 }
00170 
00171 std::size_t firebird_vector_into_type_backend::size()
00172 {
00173     std::size_t sz = 0; // dummy initialization to please the compiler
00174     switch (type_)
00175     {
00176         // simple cases
00177     case x_char:
00178         sz = getVectorSize<char> (data_);
00179         break;
00180     case x_short:
00181         sz = getVectorSize<short> (data_);
00182         break;
00183     case x_integer:
00184         sz = getVectorSize<int> (data_);
00185         break;
00186     case x_unsigned_long:
00187         sz = getVectorSize<unsigned long>(data_);
00188         break;
00189     case x_long_long:
00190         sz = getVectorSize<long long> (data_);
00191         break;
00192     case x_double:
00193         sz = getVectorSize<double> (data_);
00194         break;
00195     case x_stdstring:
00196         sz = getVectorSize<std::string> (data_);
00197         break;
00198     case x_stdtm:
00199         sz = getVectorSize<std::tm> (data_);
00200         break;
00201 
00202     default:
00203         throw soci_error("Into vector element used with non-supported type.");
00204     }
00205 
00206     return sz;
00207 }
00208 
00209 void firebird_vector_into_type_backend::clean_up()
00210 {
00211     if (buf_ != NULL)
00212     {
00213         delete [] buf_;
00214         buf_ = NULL;
00215     }
00216 }
 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