Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/fb-cpp/Attachment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,86 @@ void Attachment::resetSession()
&statusWrapper, nullptr, 0, "alter session reset", SQL_DIALECT_V6, nullptr, nullptr, nullptr, nullptr);
}

std::optional<std::string> Attachment::blobIdToString(
Transaction& transaction, std::optional<BlobId> blobId, const BlobOptions& options)
{
if (!blobId.has_value())
return std::nullopt;

Blob reader{*this, transaction, blobId.value(), options};
const auto lengthHint = reader.getLength();
std::string value;
value.reserve(lengthHint);
std::vector<char> buffer(std::numeric_limits<std::uint16_t>::max());

for (;;)
{
const auto read = reader.readSegment(buffer);

if (read == 0u)
break;

value.append(buffer.data(), read);
}

reader.close();

return value;
}

std::optional<std::vector<std::byte>> Attachment::blobIdToBytes(
Transaction& transaction, std::optional<BlobId> blobId, const BlobOptions& options)
{
if (!blobId.has_value())
return std::nullopt;

Blob reader{*this, transaction, blobId.value(), options};
const auto lengthHint = reader.getLength();
std::vector<std::byte> value;
value.reserve(lengthHint);
std::vector<std::byte> buffer(std::numeric_limits<std::uint16_t>::max());

for (;;)
{
const auto read = reader.readSegment(buffer);

if (read == 0u)
break;

value.insert(value.end(), buffer.data(), buffer.data() + read);
}

reader.close();

return value;
}

std::optional<BlobId> Attachment::blobIdFromString(
Transaction& transaction, std::optional<std::string_view> value, const BlobOptions& options)
{
if (!value.has_value())
return std::nullopt;

Blob writer{*this, transaction, options};
writer.write(std::span<const char>{value->data(), value->size()});
writer.close();

return writer.getId();
}

std::optional<BlobId> Attachment::blobIdFromBytes(
Transaction& transaction, std::optional<std::span<const std::byte>> value, const BlobOptions& options)
{
if (!value.has_value())
return std::nullopt;

Blob writer{*this, transaction, options};
writer.write(value.value());
writer.close();

return writer.getId();
}

bool Attachment::execute(Transaction& transaction, std::string_view sql, const StatementOptions& options)
{
Statement statement{*this, transaction, sql, options};
Expand Down
25 changes: 25 additions & 0 deletions src/fb-cpp/Attachment.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "fb-cpp-export.h"
#include "fb-api.h"
#include "Blob.h"
#include "RowSet.h"
#include "SmartPtrs.h"
#include "StatementOptions.h"
Expand Down Expand Up @@ -297,6 +298,30 @@ namespace fbcpp
return handle;
}

///
/// Reads a blob as a string, or returns nullopt for a null identifier.
///
std::optional<std::string> blobIdToString(
Transaction& transaction, std::optional<BlobId> blobId, const BlobOptions& options = {});

///
/// Reads a blob as bytes, or returns nullopt for a null identifier.
///
std::optional<std::vector<std::byte>> blobIdToBytes(
Transaction& transaction, std::optional<BlobId> blobId, const BlobOptions& options = {});

///
/// Creates a blob from a string, or returns nullopt for a null value.
///
std::optional<BlobId> blobIdFromString(
Transaction& transaction, std::optional<std::string_view> value, const BlobOptions& options = {});

///
/// Creates a blob from bytes, or returns nullopt for a null value.
///
std::optional<BlobId> blobIdFromBytes(
Transaction& transaction, std::optional<std::span<const std::byte>> value, const BlobOptions& options = {});

///
/// Disconnects from the database.
///
Expand Down
59 changes: 59 additions & 0 deletions src/test/Attachment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "fb-cpp/Transaction.h"
#include <cstddef>
#include <exception>
#include <limits>
#include <string>
#include <vector>


Expand Down Expand Up @@ -95,6 +97,63 @@ BOOST_AUTO_TEST_CASE(createDatabaseWithForcedWritesOff)
transaction.commit();
}

BOOST_AUTO_TEST_CASE(blobIdConversions)
{
const auto database = getTempFile("Attachment-blobIdConversions.fdb");

Attachment attachment{getClient(), database,
AttachmentOptions().setCreateDatabase(true).setForcedWrites(false).setConnectionCharSet("UTF8")};
FbDropDatabase attachmentDrop{attachment};

Transaction transaction{attachment};
const auto streamOptions = BlobOptions().setType(BlobType::STREAM);
const auto multiSegmentSize =
static_cast<std::size_t>(std::numeric_limits<std::uint16_t>::max()) + std::size_t{1024};

std::string text(multiSegmentSize, '\0');
for (std::size_t i = 0; i < text.size(); ++i)
text[i] = static_cast<char>(i % 251);

const auto textBlobId = attachment.blobIdFromString(transaction, text, streamOptions);
BOOST_REQUIRE(textBlobId.has_value());
BOOST_CHECK(textBlobId->isEmpty() == false);

const auto textResult = attachment.blobIdToString(transaction, textBlobId, streamOptions);
BOOST_REQUIRE(textResult.has_value());
BOOST_CHECK_EQUAL(*textResult, text);

std::vector<std::byte> bytes(multiSegmentSize);
for (std::size_t i = 0; i < bytes.size(); ++i)
bytes[i] = static_cast<std::byte>(i % 251);

const auto bytesBlobId = attachment.blobIdFromBytes(transaction, bytes, streamOptions);
BOOST_REQUIRE(bytesBlobId.has_value());
BOOST_CHECK(bytesBlobId->isEmpty() == false);

const auto bytesResult = attachment.blobIdToBytes(transaction, bytesBlobId, streamOptions);
BOOST_REQUIRE(bytesResult.has_value());
BOOST_CHECK(*bytesResult == bytes);

std::string emptyText;
const auto emptyTextBlobId = attachment.blobIdFromString(transaction, emptyText, streamOptions);
BOOST_REQUIRE(emptyTextBlobId.has_value());
const auto emptyTextResult = attachment.blobIdToString(transaction, emptyTextBlobId, streamOptions);
BOOST_REQUIRE(emptyTextResult.has_value());
BOOST_CHECK(emptyTextResult->empty());

std::vector<std::byte> emptyBytes;
const auto emptyBytesBlobId = attachment.blobIdFromBytes(transaction, emptyBytes, streamOptions);
BOOST_REQUIRE(emptyBytesBlobId.has_value());
const auto emptyBytesResult = attachment.blobIdToBytes(transaction, emptyBytesBlobId, streamOptions);
BOOST_REQUIRE(emptyBytesResult.has_value());
BOOST_CHECK(emptyBytesResult->empty());

BOOST_CHECK(!attachment.blobIdFromString(transaction, std::nullopt, streamOptions).has_value());
BOOST_CHECK(!attachment.blobIdFromBytes(transaction, std::nullopt, streamOptions).has_value());
BOOST_CHECK(!attachment.blobIdToString(transaction, std::nullopt, streamOptions).has_value());
BOOST_CHECK(!attachment.blobIdToBytes(transaction, std::nullopt, streamOptions).has_value());
}

BOOST_AUTO_TEST_CASE(executePreparesAndExecutesStatement)
{
const auto database = getTempFile("Attachment-executePreparesAndExecutesStatement.fdb");
Expand Down
Loading