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 #include "soci-sqlite3.h"
00009 #include "common.h"
00010 //
00011 #if defined(SOCI_HEADERS_BURIED)
00012 #       include <soci/core/soci-platform.h>
00013 #       include <soci/core/rowid.h>
00014 #       include <soci/core/blob.h>
00015 #else
00016 #       include <soci-platform.h>
00017 #       include <rowid.h>
00018 #       include <blob.h>
00019 #endif
00020 // std
00021 #include <cstdlib>
00022 #include <ctime>
00023 #include <string>
00024 
00025 using namespace soci;
00026 using namespace soci::details;
00027 using namespace soci::details::sqlite3;
00028 
00029 void sqlite3_standard_into_type_backend::define_by_pos(int & position, void * data,
00030                                                        exchange_type type)
00031 {
00032     data_ = data;
00033     type_ = type;
00034     position_ = position++;
00035 }
00036 
00037 void sqlite3_standard_into_type_backend::pre_fetch()
00038 {
00039     // ...
00040 }
00041 
00042 void sqlite3_standard_into_type_backend::post_fetch(bool gotData,
00043                                                bool calledFromFetch,
00044                                                indicator * ind)
00045 {
00046     if (calledFromFetch == true && gotData == false)
00047     {
00048         // this is a normal end-of-rowset condition,
00049         // no need to do anything (fetch() will return false)
00050         return;
00051     }
00052 
00053     // sqlite columns start at 0
00054     int pos = position_ - 1;
00055 
00056     if (gotData)
00057     {
00058         // first, deal with indicators
00059         if (sqlite3_column_type(statement_.stmt_, pos) == SQLITE_NULL)
00060         {
00061             if (ind == NULL)
00062             {
00063                 throw soci_error(
00064                     "Null value fetched and no indicator defined.");
00065             }
00066 
00067             *ind = i_null;
00068             return;
00069         }
00070         else
00071         {
00072             if (ind != NULL)
00073             {
00074                 *ind = i_ok;
00075             }
00076         }
00077 
00078         const char *buf =
00079         reinterpret_cast<const char*>(sqlite3_column_text(
00080                                           statement_.stmt_,
00081                                           pos));
00082 
00083         if (buf == NULL)
00084         {
00085             buf = "";
00086         }
00087 
00088 
00089         switch (type_)
00090         {
00091         case x_char:
00092         {
00093             char *dest = static_cast<char*>(data_);
00094             *dest = *buf;
00095         }
00096         break;
00097         case x_stdstring:
00098         {
00099             std::string *dest = static_cast<std::string *>(data_);
00100             dest->assign(buf);
00101         }
00102         break;
00103         case x_short:
00104         {
00105             short *dest = static_cast<short*>(data_);
00106             long val = std::strtol(buf, NULL, 10);
00107             *dest = static_cast<short>(val);
00108         }
00109         break;
00110         case x_integer:
00111         {
00112             int *dest = static_cast<int*>(data_);
00113             long val = std::strtol(buf, NULL, 10);
00114             *dest = static_cast<int>(val);
00115         }
00116         break;
00117         case x_unsigned_long:
00118         {
00119             unsigned long *dest = static_cast<unsigned long *>(data_);
00120             long long val = strtoll(buf, NULL, 10);
00121             *dest = static_cast<unsigned long>(val);
00122         }
00123         break;
00124         case x_long_long:
00125         {
00126             long long *dest = static_cast<long long *>(data_);
00127             *dest = strtoll(buf, NULL, 10);
00128         }
00129         break;
00130         case x_double:
00131         {
00132             double *dest = static_cast<double*>(data_);
00133             double val = strtod(buf, NULL);
00134             *dest = static_cast<double>(val);
00135         }
00136         break;
00137         case x_stdtm:
00138         {
00139             // attempt to parse the string and convert to std::tm
00140             std::tm *dest = static_cast<std::tm *>(data_);
00141             parseStdTm(buf, *dest);
00142         }
00143         break;
00144         case x_rowid:
00145         {
00146             // RowID is internally identical to unsigned long
00147 
00148             rowid *rid = static_cast<rowid *>(data_);
00149             sqlite3_rowid_backend *rbe = static_cast<sqlite3_rowid_backend *>(rid->get_backend());
00150             long long val = strtoll(buf, NULL, 10);
00151             rbe->value_ = static_cast<unsigned long>(val);
00152         }
00153         break;
00154         case x_blob:
00155         {
00156             blob *b = static_cast<blob *>(data_);
00157             sqlite3_blob_backend *bbe =
00158                 static_cast<sqlite3_blob_backend *>(b->get_backend());
00159 
00160             buf = reinterpret_cast<const char*>(sqlite3_column_blob(
00161                                                    statement_.stmt_,
00162                                                    pos));
00163 
00164             int len = sqlite3_column_bytes(statement_.stmt_, pos);
00165             bbe->set_data(buf, len);
00166         }
00167         break;
00168         default:
00169             throw soci_error("Into element used with non-supported type.");
00170         }
00171     }
00172 }
00173 
00174 void sqlite3_standard_into_type_backend::clean_up()
00175 {
00176     // ...
00177 }
 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