File indexing completed on 2025-02-23 05:15:19

0001 //
0002 // Copyright (C) 2004-2007 Maciej Sobczak, Stephen Hutton
0003 // Distributed under the Boost Software License, Version 1.0.
0004 // (See accompanying file LICENSE_1_0.txt or copy at
0005 // http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 
0008 #include "soci/oracle/soci-oracle.h"
0009 #include "error.h"
0010 #include "soci/statement.h"
0011 #include <cstring>
0012 #include <sstream>
0013 #include <cstdio>
0014 #include <ctime>
0015 #include <cctype>
0016 
0017 #ifdef _MSC_VER
0018 #pragma warning(disable:4355)
0019 #endif
0020 
0021 using namespace soci;
0022 using namespace soci::details;
0023 using namespace soci::details::oracle;
0024 
0025 oracle_blob_backend::oracle_blob_backend(oracle_session_backend &session)
0026     : session_(session)
0027 {
0028     sword res = OCIDescriptorAlloc(session.envhp_,
0029         reinterpret_cast<dvoid**>(&lobp_), OCI_DTYPE_LOB, 0, 0);
0030     if (res != OCI_SUCCESS)
0031     {
0032         throw soci_error("Cannot allocate the LOB locator");
0033     }
0034 }
0035 
0036 oracle_blob_backend::~oracle_blob_backend()
0037 {
0038     OCIDescriptorFree(lobp_, OCI_DTYPE_LOB);
0039 }
0040 
0041 std::size_t oracle_blob_backend::get_len()
0042 {
0043     ub4 len;
0044 
0045     sword res = OCILobGetLength(session_.svchp_, session_.errhp_,
0046         lobp_, &len);
0047 
0048     if (res != OCI_SUCCESS)
0049     {
0050         throw_oracle_soci_error(res, session_.errhp_);
0051     }
0052 
0053     return static_cast<std::size_t>(len);
0054 }
0055 
0056 std::size_t oracle_blob_backend::read_from_start(char *buf, std::size_t toRead, std::size_t offset)
0057 {
0058     ub4 amt = static_cast<ub4>(toRead);
0059 
0060     sword res = OCILobRead(session_.svchp_, session_.errhp_, lobp_, &amt,
0061         static_cast<ub4>(offset + 1), reinterpret_cast<dvoid*>(buf),
0062         amt, 0, 0, 0, 0);
0063     if (res != OCI_SUCCESS)
0064     {
0065         throw_oracle_soci_error(res, session_.errhp_);
0066     }
0067 
0068     return static_cast<std::size_t>(amt);
0069 }
0070 
0071 std::size_t oracle_blob_backend::write_from_start(char const *buf, std::size_t toWrite, std::size_t offset)
0072 {
0073     ub4 amt = static_cast<ub4>(toWrite);
0074 
0075     sword res = OCILobWrite(session_.svchp_, session_.errhp_, lobp_, &amt,
0076         static_cast<ub4>(offset + 1),
0077         reinterpret_cast<dvoid*>(const_cast<char*>(buf)),
0078         amt, OCI_ONE_PIECE, 0, 0, 0, 0);
0079     if (res != OCI_SUCCESS)
0080     {
0081         throw_oracle_soci_error(res, session_.errhp_);
0082     }
0083 
0084     return static_cast<std::size_t>(amt);
0085 }
0086 
0087 std::size_t oracle_blob_backend::append(char const *buf, std::size_t toWrite)
0088 {
0089     ub4 amt = static_cast<ub4>(toWrite);
0090 
0091     sword res = OCILobWriteAppend(session_.svchp_, session_.errhp_, lobp_,
0092         &amt, reinterpret_cast<dvoid*>(const_cast<char*>(buf)),
0093         amt, OCI_ONE_PIECE, 0, 0, 0, 0);
0094     if (res != OCI_SUCCESS)
0095     {
0096         throw_oracle_soci_error(res, session_.errhp_);
0097     }
0098 
0099     return static_cast<std::size_t>(amt);
0100 }
0101 
0102 void oracle_blob_backend::trim(std::size_t newLen)
0103 {
0104     sword res = OCILobTrim(session_.svchp_, session_.errhp_, lobp_,
0105         static_cast<ub4>(newLen));
0106     if (res != OCI_SUCCESS)
0107     {
0108         throw_oracle_soci_error(res, session_.errhp_);
0109     }
0110 }