00001 // 00002 // Copyright (C) 2004-2008 Maciej Sobczak, Stephen Hutton 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_POSTGRESQL_SOURCE 00009 #include "soci-postgresql.h" 00010 #include <libpq/libpq-fs.h> // libpq 00011 #include <cctype> 00012 #include <cstdio> 00013 #include <cstring> 00014 #include <ctime> 00015 #include <sstream> 00016 00017 #ifdef SOCI_PGSQL_NOPARAMS 00018 #define SOCI_PGSQL_NOBINDBYNAME 00019 #endif // SOCI_PGSQL_NOPARAMS 00020 00021 #ifdef _MSC_VER 00022 #pragma warning(disable:4355) 00023 #endif 00024 00025 using namespace soci; 00026 using namespace soci::details; 00027 00028 00029 postgresql_blob_backend::postgresql_blob_backend( 00030 postgresql_session_backend & session) 00031 : session_(session), fd_(-1) 00032 { 00033 // nothing to do here, the descriptor is open in the postFetch 00034 // method of the Into element 00035 } 00036 00037 postgresql_blob_backend::~postgresql_blob_backend() 00038 { 00039 lo_close(session_.conn_, fd_); 00040 } 00041 00042 std::size_t postgresql_blob_backend::get_len() 00043 { 00044 int const pos = lo_lseek(session_.conn_, fd_, 0, SEEK_END); 00045 if (pos == -1) 00046 { 00047 throw soci_error("Cannot retrieve the size of BLOB."); 00048 } 00049 00050 return static_cast<std::size_t>(pos); 00051 } 00052 00053 std::size_t postgresql_blob_backend::read( 00054 std::size_t offset, char * buf, std::size_t toRead) 00055 { 00056 int const pos = lo_lseek(session_.conn_, fd_, 00057 static_cast<int>(offset), SEEK_SET); 00058 if (pos == -1) 00059 { 00060 throw soci_error("Cannot seek in BLOB."); 00061 } 00062 00063 int const readn = lo_read(session_.conn_, fd_, buf, toRead); 00064 if (readn < 0) 00065 { 00066 throw soci_error("Cannot read from BLOB."); 00067 } 00068 00069 return static_cast<std::size_t>(readn); 00070 } 00071 00072 std::size_t postgresql_blob_backend::write( 00073 std::size_t offset, char const * buf, std::size_t toWrite) 00074 { 00075 int const pos = lo_lseek(session_.conn_, fd_, 00076 static_cast<int>(offset), SEEK_SET); 00077 if (pos == -1) 00078 { 00079 throw soci_error("Cannot seek in BLOB."); 00080 } 00081 00082 int const writen = lo_write(session_.conn_, fd_, 00083 const_cast<char *>(buf), toWrite); 00084 if (writen < 0) 00085 { 00086 throw soci_error("Cannot write to BLOB."); 00087 } 00088 00089 return static_cast<std::size_t>(writen); 00090 } 00091 00092 std::size_t postgresql_blob_backend::append( 00093 char const * buf, std::size_t toWrite) 00094 { 00095 int const pos = lo_lseek(session_.conn_, fd_, 0, SEEK_END); 00096 if (pos == -1) 00097 { 00098 throw soci_error("Cannot seek in BLOB."); 00099 } 00100 00101 int const writen = lo_write(session_.conn_, fd_, 00102 const_cast<char *>(buf), toWrite); 00103 if (writen < 0) 00104 { 00105 throw soci_error("Cannot append to BLOB."); 00106 } 00107 00108 return static_cast<std::size_t>(writen); 00109 } 00110 00111 void postgresql_blob_backend::trim(std::size_t /* newLen */) 00112 { 00113 throw soci_error("Trimming BLOBs is not supported."); 00114 }
Generated on Sun Oct 3 2010 17:42:16 for EXTRAS-SOCI by Doxygen 1.7.1