25#include "Attachment.h"
30#include "Transaction.h"
33using namespace fbcpp::impl;
41 StatusWrapper statusWrapper{client};
43 auto dpbBuilder =
fbUnique(client.
getUtil()->getXpbBuilder(&statusWrapper, fb::IXpbBuilder::DPB,
44 reinterpret_cast<const std::uint8_t*
>(options.
getDpb().data()),
45 static_cast<unsigned>(options.
getDpb().size())));
48 dpbBuilder->insertString(&statusWrapper, isc_dpb_lc_ctype, connectionCharSet->c_str());
51 dpbBuilder->insertString(&statusWrapper, isc_dpb_user_name, userName->c_str());
54 dpbBuilder->insertString(&statusWrapper, isc_dpb_password, password->c_str());
56 if (
const auto role = options.
getRole())
57 dpbBuilder->insertString(&statusWrapper, isc_dpb_sql_role_name, role->c_str());
60 dpbBuilder->insertInt(&statusWrapper, isc_dpb_sql_dialect,
static_cast<int>(*sqlDialect));
63 dpbBuilder->insertInt(&statusWrapper, isc_dpb_force_write, *forcedWrites ? 1 : 0);
65 auto dispatcher =
fbRef(master->getDispatcher());
66 const auto dpbBuffer = dpbBuilder->getBuffer(&statusWrapper);
67 const auto dpbBufferLen = dpbBuilder->getBufferLength(&statusWrapper);
70 handle.reset(dispatcher->createDatabase(&statusWrapper, uri.c_str(), dpbBufferLen, dpbBuffer));
72 handle.reset(dispatcher->attachDatabase(&statusWrapper, uri.c_str(), dpbBufferLen, dpbBuffer));
75void Attachment::disconnectOrDrop(
bool drop)
79 StatusWrapper statusWrapper{*client};
82 handle->dropDatabase(&statusWrapper);
84 handle->detach(&statusWrapper);
92 disconnectOrDrop(
false);
97 disconnectOrDrop(
true);
104 StatusWrapper statusWrapper{*client};
106 handle->ping(&statusWrapper);
113 StatusWrapper statusWrapper{*client};
116 &statusWrapper,
nullptr, 0,
"alter session reset", SQL_DIALECT_V6,
nullptr,
nullptr,
nullptr,
nullptr);
122 if (!blobId.has_value())
125 Blob reader{*
this, transaction, blobId.value(), options};
126 const auto lengthHint = reader.getLength();
128 value.reserve(lengthHint);
129 std::vector<char> buffer(std::numeric_limits<std::uint16_t>::max());
133 const auto read = reader.readSegment(buffer);
138 value.append(buffer.data(), read);
149 if (!blobId.has_value())
152 Blob reader{*
this, transaction, blobId.value(), options};
153 const auto lengthHint = reader.getLength();
154 std::vector<std::byte> value;
155 value.reserve(lengthHint);
156 std::vector<std::byte> buffer(std::numeric_limits<std::uint16_t>::max());
160 const auto read = reader.readSegment(buffer);
165 value.insert(value.end(), buffer.data(), buffer.data() + read);
176 if (!value.has_value())
179 Blob writer{*
this, transaction, options};
180 writer.write(std::span<const char>{value->data(), value->size()});
183 return writer.getId();
189 if (!value.has_value())
192 Blob writer{*
this, transaction, options};
193 writer.write(value.value());
196 return writer.getId();
201 Statement statement{*
this, transaction, sql, options};
202 return statement.execute(transaction);
208 Statement statement{*
this, transaction, sql, options};
209 return queryPreparedRowSet(statement, transaction, maxRows);
224 throw FbCppException(
"Cannot use procedure without output columns with Attachment::queryRowSet");
227 throw FbCppException(
"Cannot use non-query SQL with Attachment::queryRowSet");
230 statement.
execute(transaction);
233 return RowSet{statement, effectiveMaxRows};
Represents options used when creating an Attachment object.
const std::optional< std::string > & getPassword() const
Returns the password which will be used to connect to the database.
const std::optional< std::string > & getRole() const
Returns the role which will be used to connect to the database.
const std::vector< std::uint8_t > & getDpb() const
Returns the DPB (Database Parameter Block) which will be used to connect to the database.
const std::optional< std::string > & getUserName() const
Returns the user name which will be used to connect to the database.
bool getCreateDatabase() const
Returns whether the database should be created instead of connected to.
const std::optional< std::string > & getConnectionCharSet() const
Returns the character set which will be used for the connection.
const std::optional< bool > & getForcedWrites() const
Returns whether forced writes should be enabled when creating the database.
const std::optional< std::uint32_t > & getSqlDialect() const
Returns the SQL dialect which will be used to connect to the database.
Attachment(Client &client, const std::string &uri, const AttachmentOptions &options={})
Constructs an Attachment object that connects to (or creates) the database specified by the URI using...
std::optional< std::vector< std::byte > > blobIdToBytes(Transaction &transaction, std::optional< BlobId > blobId, const BlobOptions &options={})
Reads a blob as bytes, or returns nullopt for a null identifier.
bool execute(Transaction &transaction, std::string_view sql, const StatementOptions &options={})
Prepares and executes an SQL statement using the supplied transaction.
void disconnect()
Disconnects from the database.
void ping()
Checks if the connection to the database is alive.
bool isValid() noexcept
Returns whether the Attachment object is valid.
std::optional< BlobId > blobIdFromBytes(Transaction &transaction, std::optional< std::span< const std::byte > > value, const BlobOptions &options={})
Creates a blob from bytes, 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 a string, or returns nullopt for a null value.
void dropDatabase()
Drops the database.
std::optional< std::string > blobIdToString(Transaction &transaction, std::optional< BlobId > blobId, const BlobOptions &options={})
Reads a blob as a string, or returns nullopt for a null identifier.
void resetSession()
Resets the session state.
RowSet queryRowSet(Transaction &transaction, std::string_view sql, unsigned maxRows, const StatementOptions &options={})
Prepares and executes a query using the supplied transaction and returns up to maxRows rows.
Additional options used when creating or opening blobs.
Provides read and write access to Firebird blobs.
Represents a Firebird client library instance.
fb::IUtil * getUtil()
Returns a Firebird IUtil interface.
fb::IMaster * getMaster() noexcept
Returns the Firebird IMaster interface.
Base exception class for all fb-cpp exceptions.
A disconnected buffer of rows fetched from a Statement's result set.
Represents options used when preparing a Statement.
Prepares, executes, and fetches SQL statements against a Firebird attachment.
const std::vector< Descriptor > & getOutputDescriptors() noexcept
Provides cached descriptors for each output column.
StatementType getType() noexcept
Returns the type classification reported by the server.
bool execute(Transaction &transaction)
Executes a prepared statement using the supplied transaction.
Represents a transaction in one or more Firebird databases.
FbRef< T > fbRef(T *arg) noexcept
Creates a reference-counted smart pointer for a Firebird object.
@ EXEC_PROCEDURE
Statement executes a stored procedure.
@ SELECT
Server classified the statement as a SELECT.
@ SELECT_FOR_UPDATE
Cursor-based SELECT that allows updates.
FbUniquePtr< T > fbUnique(T *obj) noexcept
Creates a unique pointer for a Firebird disposable object.