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

standard-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 #define SOCI_ODBC_SOURCE
00009 #include "soci-odbc.h"
00010 #include <ctime>
00011 
00012 using namespace soci;
00013 using namespace soci::details;
00014 
00015 
00016 void odbc_standard_into_type_backend::define_by_pos(
00017     int & position, void * data, exchange_type type)
00018 {
00019     data_ = data;
00020     type_ = type;
00021     position_ = position++;
00022 
00023     SQLINTEGER size = 0;
00024 
00025     switch (type_)
00026     {
00027     case x_char:
00028         odbcType_ = SQL_C_CHAR;
00029         size = sizeof(char) + 1;
00030         buf_ = new char[size];
00031         data = buf_;
00032         break;
00033     case x_stdstring:
00034         odbcType_ = SQL_C_CHAR;
00035         size = 32769;
00036         buf_ = new char[size];
00037         data = buf_;
00038         break;
00039     case x_short:
00040         odbcType_ = SQL_C_SSHORT;
00041         size = sizeof(short);
00042         break;
00043     case x_integer:
00044         odbcType_ = SQL_C_SLONG;
00045         size = sizeof(long);
00046         break;
00047     case x_unsigned_long:
00048         odbcType_ = SQL_C_ULONG;
00049         size = sizeof(unsigned long);
00050         break;
00051     case x_double:
00052         odbcType_ = SQL_C_DOUBLE;
00053         size = sizeof(double);
00054         break;
00055     case x_stdtm:
00056         odbcType_ = SQL_C_TYPE_TIMESTAMP;
00057         size = sizeof(TIMESTAMP_STRUCT);
00058         buf_ = new char[size];
00059         data = buf_;
00060         break;
00061     case x_rowid:
00062         odbcType_ = SQL_C_ULONG;
00063         size = sizeof(unsigned long);
00064         break;
00065     default:
00066         throw soci_error("Into element used with non-supported type.");
00067     }
00068 
00069     valueLen_ = 0;
00070 
00071     SQLRETURN rc = SQLBindCol(statement_.hstmt_, static_cast<SQLUSMALLINT>(position_),
00072         static_cast<SQLUSMALLINT>(odbcType_), data, size, &valueLen_);
00073     if (is_odbc_error(rc))
00074     {
00075         throw odbc_soci_error(SQL_HANDLE_STMT, statement_.hstmt_,
00076                             "into type pre_fetch");
00077     }
00078 }
00079 
00080 void odbc_standard_into_type_backend::pre_fetch()
00081 {
00082     //...
00083 }
00084 
00085 void odbc_standard_into_type_backend::post_fetch(
00086     bool gotData, bool calledFromFetch, indicator * ind)
00087 {
00088     if (calledFromFetch == true && gotData == false)
00089     {
00090         // this is a normal end-of-rowset condition,
00091         // no need to do anything (fetch() will return false)
00092         return;
00093     }
00094 
00095     if (gotData)
00096     {
00097         // first, deal with indicators
00098         if (SQL_NULL_DATA == valueLen_)
00099         {
00100             if (ind == NULL)
00101             {
00102                 throw soci_error(
00103                     "Null value fetched and no indicator defined.");
00104             }
00105 
00106             *ind = i_null;
00107             return;
00108         }
00109         else
00110         {
00111             if (ind != NULL)
00112             {
00113                 *ind = i_ok;
00114             }
00115         }
00116 
00117         // only std::string and std::tm need special handling
00118         if (type_ == x_char)
00119         {
00120             char *c = static_cast<char*>(data_);
00121             *c = buf_[0];
00122         }
00123         if (type_ == x_stdstring)
00124         {
00125             std::string *s = static_cast<std::string *>(data_);
00126             *s = buf_;
00127         }
00128         else if (type_ == x_stdtm)
00129         {
00130             std::tm *t = static_cast<std::tm *>(data_);
00131 
00132             TIMESTAMP_STRUCT * ts = reinterpret_cast<TIMESTAMP_STRUCT*>(buf_);
00133             t->tm_isdst = -1;
00134             t->tm_year = ts->year - 1900;
00135             t->tm_mon = ts->month - 1;
00136             t->tm_mday = ts->day;
00137             t->tm_hour = ts->hour;
00138             t->tm_min = ts->minute;
00139             t->tm_sec = ts->second;
00140 
00141             // normalize and compute the remaining fields
00142             std::mktime(t);
00143         }
00144     }
00145 }
00146 
00147 void odbc_standard_into_type_backend::clean_up()
00148 {
00149     if (buf_)
00150     {
00151         delete [] buf_;
00152         buf_ = 0;
00153     }
00154 }
 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