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

common.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 #include "common.h"
00009 //
00010 #if defined(SOCI_HEADERS_BURIED)
00011 #       include <soci/core/soci-backend.h>
00012 #else
00013 #       include <soci-backend.h>
00014 #endif
00015 //
00016 #include <ibase.h> // FireBird
00017 #include <cstddef>
00018 #include <cstring>
00019 #include <sstream>
00020 #include <string>
00021 
00022 namespace soci
00023 {
00024 
00025 namespace details
00026 {
00027 
00028 namespace firebird
00029 {
00030 
00031 char * allocBuffer(XSQLVAR* var)
00032 {
00033     std::size_t size;
00034     if ((var->sqltype & ~1) == SQL_VARYING)
00035     {
00036         size = var->sqllen + sizeof(short);
00037     }
00038     else
00039     {
00040         size = var->sqllen;
00041     }
00042 
00043     return new char[size];
00044 }
00045 
00046 void tmEncode(short type, std::tm * src, void * dst)
00047 {
00048     switch (type & ~1)
00049     {
00050         // In Interbase v6 DATE represents a date-only data type,
00051         // in InterBase v5 DATE represents a date+time data type.
00052     case SQL_TIMESTAMP:
00053         isc_encode_timestamp(src, static_cast<ISC_TIMESTAMP*>(dst));
00054         break;
00055     case SQL_TYPE_TIME:
00056         isc_encode_sql_time(src, static_cast<ISC_TIME*>(dst));
00057         break;
00058     case SQL_TYPE_DATE:
00059         isc_encode_sql_date(src, static_cast<ISC_DATE*>(dst));
00060         break;
00061     default:
00062         std::ostringstream msg;
00063         msg << "Unexpected type of date/time field (" << type << ")";
00064         throw soci_error(msg.str());
00065     }
00066 }
00067 
00068 void tmDecode(short type, void * src, std::tm * dst)
00069 {
00070     switch (type & ~1)
00071     {
00072     case SQL_TIMESTAMP:
00073         isc_decode_timestamp(static_cast<ISC_TIMESTAMP*>(src), dst);
00074         break;
00075     case SQL_TYPE_TIME:
00076         isc_decode_sql_time(static_cast<ISC_TIME*>(src), dst);
00077         break;
00078     case SQL_TYPE_DATE:
00079         isc_decode_sql_date(static_cast<ISC_DATE*>(src), dst);
00080         break;
00081     default:
00082         std::ostringstream msg;
00083         msg << "Unexpected type of date/time field (" << type << ")";
00084         throw soci_error(msg.str());
00085     }
00086 }
00087 
00088 void setTextParam(char const * s, std::size_t size, char * buf_,
00089     XSQLVAR * var)
00090 {
00091     short sz = 0;
00092     if (size < static_cast<std::size_t>(var->sqllen))
00093     {
00094         sz = static_cast<short>(size);
00095     }
00096     else
00097     {
00098         sz = var->sqllen;
00099     }
00100 
00101     if ((var->sqltype & ~1) == SQL_VARYING)
00102     {
00103         memcpy(buf_, &sz, sizeof(short));
00104         memcpy(buf_ + sizeof(short), s, sz);
00105     }
00106     else if ((var->sqltype & ~1) == SQL_TEXT)
00107     {
00108         memcpy(buf_, s, sz);
00109         if (sz < var->sqllen)
00110         {
00111             memset(buf_+sz, ' ', var->sqllen - sz);
00112         }
00113     }
00114     else
00115     {
00116         throw soci_error("Unexpected string type.");
00117     }
00118 }
00119 
00120 std::string getTextParam(XSQLVAR const *var)
00121 {
00122     short size;
00123     std::size_t offset = 0;
00124 
00125     if ((var->sqltype & ~1) == SQL_VARYING)
00126     {
00127         size = *reinterpret_cast<short*>(var->sqldata);
00128         offset = sizeof(short);
00129     }
00130     else if ((var->sqltype & ~1) == SQL_TEXT)
00131     {
00132         size = var->sqllen;
00133     }
00134     else throw soci_error("Unexpected string type");
00135 
00136     return std::string(var->sqldata + offset, size);
00137 }
00138 
00139 } // namespace firebird
00140 
00141 } // namespace details
00142 
00143 } // namespace soci
 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