fb-cpp 0.0.1
A modern C++ wrapper for the Firebird database API
Loading...
Searching...
No Matches
Attachment.cpp
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 Adriano dos Santos Fernandes
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#include "Attachment.h"
26#include "Client.h"
27#include "Exception.h"
28
29using namespace fbcpp;
30using namespace fbcpp::impl;
31
32
33Attachment::Attachment(Client& client, const std::string& uri, const AttachmentOptions& options)
34 : client{client}
35{
36 const auto master = client.getMaster();
37
38 const auto status = client.newStatus();
39 StatusWrapper statusWrapper{client, status.get()};
40
41 auto dpbBuilder = fbUnique(master->getUtilInterface()->getXpbBuilder(&statusWrapper, fb::IXpbBuilder::DPB,
42 reinterpret_cast<const std::uint8_t*>(options.getDpb().data()),
43 static_cast<unsigned>(options.getDpb().size())));
44
45 if (const auto connectionCharSet = options.getConnectionCharSet())
46 dpbBuilder->insertString(&statusWrapper, isc_dpb_lc_ctype, connectionCharSet->c_str());
47
48 if (const auto userName = options.getUserName())
49 dpbBuilder->insertString(&statusWrapper, isc_dpb_user_name, userName->c_str());
50
51 if (const auto password = options.getPassword())
52 dpbBuilder->insertString(&statusWrapper, isc_dpb_password, password->c_str());
53
54 if (const auto role = options.getRole())
55 dpbBuilder->insertString(&statusWrapper, isc_dpb_sql_role_name, role->c_str());
56
57 auto dispatcher = fbRef(master->getDispatcher());
58 const auto dpbBuffer = dpbBuilder->getBuffer(&statusWrapper);
59 const auto dpbBufferLen = dpbBuilder->getBufferLength(&statusWrapper);
60
61 if (options.getCreateDatabase())
62 handle.reset(dispatcher->createDatabase(&statusWrapper, uri.c_str(), dpbBufferLen, dpbBuffer));
63 else
64 handle.reset(dispatcher->attachDatabase(&statusWrapper, uri.c_str(), dpbBufferLen, dpbBuffer));
65}
66
67void Attachment::disconnectOrDrop(bool drop)
68{
69 assert(isValid());
70
71 const auto status = client.newStatus();
72 StatusWrapper statusWrapper{client, status.get()};
73
74 if (drop)
75 handle->dropDatabase(&statusWrapper);
76 else
77 handle->detach(&statusWrapper);
78
79 handle.reset();
80}
81
82
84{
85 disconnectOrDrop(false);
86}
87
89{
90 disconnectOrDrop(true);
91}
Represents options used when creating an Attachment object.
Definition Attachment.h:49
const std::optional< std::string > & getPassword() const
Returns the password which will be used to connect to the database.
Definition Attachment.h:88
const std::optional< std::string > & getRole() const
Returns the role which will be used to connect to the database.
Definition Attachment.h:105
const std::vector< std::uint8_t > & getDpb() const
Returns the DPB (Database Parameter Block) which will be used to connect to the database.
Definition Attachment.h:122
const std::optional< std::string > & getUserName() const
Returns the user name which will be used to connect to the database.
Definition Attachment.h:71
bool getCreateDatabase() const
Returns whether the database should be created instead of connected to.
Definition Attachment.h:148
const std::optional< std::string > & getConnectionCharSet() const
Returns the character set which will be used for the connection.
Definition Attachment.h:54
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...
void disconnect()
Disconnects from the database.
bool isValid() noexcept
Returns whether the Attachment object is valid.
Definition Attachment.h:222
void dropDatabase()
Drops the database.
Represents a Firebird client library instance.
Definition Client.h:53
FbUniquePtr< fb::IStatus > newStatus()
Creates and returns a Firebird IStatus instance.
Definition Client.h:183
fb::IMaster * getMaster() noexcept
Returns the Firebird IMaster interface.
Definition Client.h:134
fb-cpp namespace.
Definition Attachment.h:42