Skip to content

Commit

Permalink
Implement blob re-initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzmbrzl committed May 1, 2024
1 parent 2e8248e commit 4d4e959
Show file tree
Hide file tree
Showing 19 changed files with 62 additions and 6 deletions.
6 changes: 6 additions & 0 deletions include/private/soci-trivial-blob-backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define SOCI_PRIVATE_SOCI_TRIVIAL_BLOB_BACKEND_H_INCLUDED

#include "soci/soci-backend.h"
#include "soci/session.h"

#include <vector>
#include <cstring>
Expand All @@ -22,6 +23,8 @@ namespace details
class trivial_blob_backend : public details::blob_backend
{
public:
trivial_blob_backend(details::session_backend &backend) : session_(backend) {}

std::size_t get_len() override { return buffer_.size(); }

std::size_t read_from_start(void* buf, std::size_t toRead,
Expand Down Expand Up @@ -71,7 +74,10 @@ class trivial_blob_backend : public details::blob_backend

const std::uint8_t *get_buffer() const { return buffer_.data(); }

details::session_backend &get_session_backend() override { return session_; }

protected:
details::session_backend &session_;
std::vector< std::uint8_t > buffer_;
};

Expand Down
1 change: 1 addition & 0 deletions include/soci/blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class SOCI_DECL blob

// (Re)initializes this blob
void initialize(session &s);
void initialize(details::blob_backend *backend);

std::size_t get_len();

Expand Down
1 change: 1 addition & 0 deletions include/soci/db2/soci-db2.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ struct db2_blob_backend : details::blob_backend
std::size_t write_from_start(const void* buf, std::size_t toWrite, std::size_t offset = 0) override;
std::size_t append(const void* buf, std::size_t toWrite) override;
void trim(std::size_t newLen) override;
details::session_backend &get_session_backend() override;

db2_session_backend& session_;
};
Expand Down
2 changes: 2 additions & 0 deletions include/soci/empty/soci-empty.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ struct empty_blob_backend : details::blob_backend
std::size_t append(const void* buf, std::size_t toWrite) override;
void trim(std::size_t newLen) override;

details::session_backend &get_session_backend() override;

empty_session_backend& session_;
};

Expand Down
2 changes: 2 additions & 0 deletions include/soci/firebird/soci-firebird.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ struct firebird_blob_backend : details::blob_backend
std::size_t append(const void *buf, std::size_t toWrite) override;
void trim(std::size_t newLen) override;

details::session_backend &get_session_backend() override;

// Writes the current data into the database by allocating a new BLOB
// object for it.
//
Expand Down
1 change: 1 addition & 0 deletions include/soci/odbc/soci-odbc.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ struct odbc_blob_backend : details::blob_backend
std::size_t write_from_start(const void *buf, std::size_t toWrite, std::size_t offset = 0) override;
std::size_t append(const void *buf, std::size_t toWrite) override;
void trim(std::size_t newLen) override;
details::session_backend &get_session_backend() override;

odbc_session_backend &session_;
};
Expand Down
2 changes: 2 additions & 0 deletions include/soci/oracle/soci-oracle.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ struct oracle_blob_backend : details::blob_backend

void ensure_initialized();

oracle_session_backend &get_session_backend() override;

private:
std::size_t do_deprecated_read(std::size_t offset, void *buf, std::size_t toRead) override
{
Expand Down
2 changes: 2 additions & 0 deletions include/soci/postgresql/soci-postgresql.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ class postgresql_blob_backend : public details::blob_backend

void reset();

details::session_backend &get_session_backend() override;

private:
postgresql_session_backend & session_;
blob_details details_;
Expand Down
4 changes: 4 additions & 0 deletions include/soci/soci-backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ class rowid_backend
virtual ~rowid_backend() {}
};

class session_backend;

// polymorphic blob backend

class blob_backend
Expand All @@ -312,6 +314,8 @@ class blob_backend

virtual void trim(std::size_t newLen) = 0;

virtual session_backend &get_session_backend() = 0;

// Deprecated functions with backend-specific semantics preserved only for
// compatibility.
[[deprecated("Use read_from_start instead")]]
Expand Down
5 changes: 5 additions & 0 deletions src/backends/db2/blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ void db2_blob_backend::trim(std::size_t /* newLen */)
throw soci_error("BLOBs are not supported.");
}

details::session_backend &db2_blob_backend::get_session_backend()
{
return session_;
}

#ifdef _MSC_VER
# pragma warning(pop)
#endif
5 changes: 5 additions & 0 deletions src/backends/empty/blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ void empty_blob_backend::trim(std::size_t /* newLen */)
throw soci_error("BLOBs are not supported.");
}

details::session_backend &empty_blob_backend::get_session_backend()
{
return session_;
}

#ifdef _MSC_VER
# pragma warning(pop)
#endif
5 changes: 5 additions & 0 deletions src/backends/firebird/blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,8 @@ long firebird_blob_backend::getBLOBInfo()

return total_length;
}

details::session_backend &firebird_blob_backend::get_session_backend()
{
return session_;
}
4 changes: 2 additions & 2 deletions src/backends/mysql/blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
using namespace soci;
using namespace soci::details;

mysql_blob_backend::mysql_blob_backend(mysql_session_backend &)
: details::trivial_blob_backend()
mysql_blob_backend::mysql_blob_backend(mysql_session_backend &backend)
: details::trivial_blob_backend(backend)
{
}

Expand Down
5 changes: 5 additions & 0 deletions src/backends/odbc/blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ void odbc_blob_backend::trim(std::size_t /* newLen */)
throw soci_error("BLOBs are not supported.");
}

details::session_backend &odbc_blob_backend::get_session_backend()
{
return session_;
}

#ifdef _MSC_VER
# pragma warning(pop)
#endif
5 changes: 5 additions & 0 deletions src/backends/oracle/blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,8 @@ void oracle_blob_backend::ensure_initialized()
initialized_ = true;
}
}

details::session_backend &oracle_blob_backend::get_session_backend()
{
return session_;
}
5 changes: 5 additions & 0 deletions src/backends/postgresql/blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,8 @@ void postgresql_blob_backend::clone()
lo_close(session_.conn_, old_details.fd);
}
}

details::session_backend &postgresql_blob_backend::get_session_backend()
{
return session_;
}
4 changes: 2 additions & 2 deletions src/backends/sqlite3/blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

using namespace soci;

sqlite3_blob_backend::sqlite3_blob_backend(sqlite3_session_backend &)
: details::trivial_blob_backend()
sqlite3_blob_backend::sqlite3_blob_backend(sqlite3_session_backend &backend)
: details::trivial_blob_backend(backend)
{
}

Expand Down
7 changes: 6 additions & 1 deletion src/core/blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ bool blob::is_valid() const

void blob::initialize(session &session)
{
backEnd_.reset(session.make_blob_backend());
initialize(session.make_blob_backend());
}

void blob::initialize(details::blob_backend *backend)
{
backEnd_.reset(backend);
}

std::size_t blob::get_len()
Expand Down
2 changes: 1 addition & 1 deletion src/core/row.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ blob row::move_as<blob>(std::size_t pos) const
type_conversion<blob>::move_from_base(baseVal, *indicators_.at(pos), ret);

// Re-initialize blob object so it can be used in further queries
baseVal.initialize(session);
baseVal.initialize(ret.get_backend()->get_session_backend().make_blob_backend());

return ret;
}

0 comments on commit 4d4e959

Please sign in to comment.