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);
121 Statement statement{*
this, transaction, sql, options};
122 return statement.execute(transaction);
128 Statement statement{*
this, transaction, sql, options};
129 return queryPreparedRowSet(statement, transaction, maxRows);
144 throw FbCppException(
"Cannot use procedure without output columns with Attachment::queryRowSet");
147 throw FbCppException(
"Cannot use non-query SQL with Attachment::queryRowSet");
150 const auto hasRow = statement.
execute(transaction);
152 return RowSet{statement, hasRow ? effectiveMaxRows : 0u, hasRow};
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...
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.
void dropDatabase()
Drops the database.
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.
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.