fb-cpp 0.0.2
A modern C++ wrapper for the Firebird database API
Loading...
Searching...
No Matches
DatabaseManager.cpp
1/*
2 * MIT License
3 *
4 * Copyright (c) 2026 Popa Adrian Marius
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 "DatabaseManager.h"
26#include "Client.h"
27#include <cassert>
28
29using namespace fbcpp;
30using namespace fbcpp::impl;
31
32
34{
35 StatusWrapper statusWrapper{getClient()};
36 auto builder =
37 fbUnique(getClient().getUtil()->getXpbBuilder(&statusWrapper, fb::IXpbBuilder::SPB_START, nullptr, 0));
38 builder->insertTag(&statusWrapper, isc_action_svc_properties);
39 builder->insertString(&statusWrapper, isc_spb_dbname, options.getDatabase().c_str());
40
41 if (const auto replicaMode = options.getReplicaMode())
42 {
43 std::uint8_t modeVal = 0;
44
45 switch (*replicaMode)
46 {
48 modeVal = isc_spb_prp_rm_none;
49 break;
51 modeVal = isc_spb_prp_rm_readonly;
52 break;
54 modeVal = isc_spb_prp_rm_readwrite;
55 break;
56 default:
57 assert(false);
58 break;
59 }
60
61 builder->insertBytes(&statusWrapper, isc_spb_prp_replica_mode, &modeVal, 1u);
62 }
63
64 if (const auto shutdownMode = options.getShutdownMode())
65 {
66 std::uint8_t stateVal = 0;
67
68 switch (*shutdownMode)
69 {
71 stateVal = isc_spb_prp_sm_normal;
72 break;
74 stateVal = isc_spb_prp_sm_multi;
75 break;
77 stateVal = isc_spb_prp_sm_single;
78 break;
80 stateVal = isc_spb_prp_sm_full;
81 break;
82 default:
83 assert(false);
84 break;
85 }
86
87 builder->insertBytes(&statusWrapper, isc_spb_prp_shutdown_mode, &stateVal, 1u);
88 }
89
90 if (const auto shutdownType = options.getShutdownType())
91 {
92 const auto timeout = options.getShutdownTimeout().value_or(0);
93
94 switch (*shutdownType)
95 {
97 builder->insertInt(&statusWrapper, isc_spb_prp_deny_new_transactions, timeout);
98 break;
100 builder->insertInt(&statusWrapper, isc_spb_prp_deny_new_attachments, timeout);
101 break;
103 builder->insertInt(&statusWrapper, isc_spb_prp_force_shutdown, timeout);
104 break;
105 default:
106 assert(false);
107 break;
108 }
109 }
110
111 if (const auto online = options.getOnline())
112 {
113 if (*online)
114 {
115 std::uint8_t stateVal = isc_spb_prp_sm_normal;
116 builder->insertBytes(&statusWrapper, isc_spb_prp_online_mode, &stateVal, 1u);
117 }
118 }
119
120 const auto buffer = builder->getBuffer(&statusWrapper);
121 const auto length = builder->getBufferLength(&statusWrapper);
122
123 startAction(std::vector<std::uint8_t>(buffer, buffer + length));
124 waitForCompletion();
125}
126
127
129{
130 StatusWrapper statusWrapper{getClient()};
131 auto builder =
132 fbUnique(getClient().getUtil()->getXpbBuilder(&statusWrapper, fb::IXpbBuilder::SPB_START, nullptr, 0));
133
134 builder->insertTag(&statusWrapper, isc_action_svc_repair);
135 builder->insertString(&statusWrapper, isc_spb_dbname, options.getDatabase().c_str());
136
137 int optionsVal = 0;
138 if (options.getSweep())
139 optionsVal |= isc_spb_rpr_sweep_db;
140 if (options.getValidate())
141 optionsVal |= isc_spb_rpr_validate_db;
142 if (options.getMend())
143 optionsVal |= isc_spb_rpr_mend_db;
144 if (options.getIgnoreChecksum())
145 optionsVal |= isc_spb_rpr_ignore_checksum;
146 if (options.getKillShadows())
147 optionsVal |= isc_spb_rpr_kill_shadows;
148 if (options.getFull())
149 optionsVal |= isc_spb_rpr_full;
150 if (options.getCheckDb())
151 optionsVal |= isc_spb_rpr_check_db;
152 if (options.getIcu())
153 optionsVal |= isc_spb_rpr_icu;
154 if (options.getUpgradeDb())
155 optionsVal |= isc_spb_rpr_upgrade_db;
156
157 if (optionsVal != 0)
158 builder->insertInt(&statusWrapper, isc_spb_options, optionsVal);
159
160 if (const auto parallelWorkers = options.getParallelWorkers())
161 builder->insertInt(&statusWrapper, isc_spb_rpr_par_workers, static_cast<int>(*parallelWorkers));
162
163 const auto buffer = builder->getBuffer(&statusWrapper);
164 const auto length = builder->getBufferLength(&statusWrapper);
165
166 startAction(std::vector<std::uint8_t>(buffer, buffer + length));
167 waitForCompletion(options.getVerboseOutput());
168}
void setProperties(const DatabasePropertiesOptions &options)
Configures database properties using the provided options.
void repair(const DatabaseRepairOptions &options)
Runs a repair operation using the provided options.
Represents options used to configure database properties through the service manager.
const std::string & getDatabase() const
Returns the database path to be configured.
const std::optional< bool > & getOnline() const
Returns whether the database should be brought online.
const std::optional< ReplicaMode > & getReplicaMode() const
Returns the replica mode.
const std::optional< ShutdownMode > & getShutdownMode() const
Returns the shutdown mode.
const std::optional< ShutdownType > & getShutdownType() const
Returns the shutdown type.
const std::optional< int > & getShutdownTimeout() const
Returns the shutdown timeout in seconds.
Represents options used to run a database maintenance operation through the service manager.
bool getIgnoreChecksum() const
Returns whether checksum verification is configured to be ignored.
const std::optional< std::uint32_t > & getParallelWorkers() const
Returns the requested number of parallel workers.
bool getUpgradeDb() const
Returns whether database upgrade is configured to be run.
const ServiceManager::VerboseOutput & getVerboseOutput() const
Returns the verbose output callback.
bool getValidate() const
Returns whether database validation is configured to be run.
bool getCheckDb() const
Returns whether checking only metadata/structure is configured to be run.
bool getKillShadows() const
Returns whether killing database shadows is configured to be run.
bool getSweep() const
Returns whether database sweep is configured to be run.
const std::string & getDatabase() const
Returns the database path to be maintained.
bool getIcu() const
Returns whether recreating ICU indexes is configured to be run.
bool getMend() const
Returns whether database mending is configured to be run.
bool getFull() const
Returns whether full validation is configured to be run.
Client & getClient() noexcept
Returns the Client object reference used to create this ServiceManager object.
fb-cpp namespace.
Definition Attachment.h:45
@ DENY_TRANSACTIONS
Deny new transactions: waits for existing transactions to finish.
@ FORCED
Forced shutdown: disconnects all users immediately after the timeout.
@ DENY_ATTACHMENTS
Deny new attachments: waits for existing connections to finish.
@ SINGLE
Single-user shutdown.
@ NORMAL
Normal online state.
@ FULL
Full exclusive shutdown.
@ MULTI
Multi-user shutdown.
FbUniquePtr< T > fbUnique(T *obj) noexcept
Creates a unique pointer for a Firebird disposable object.
Definition SmartPtrs.h:59
@ READ_ONLY
Read-only replica.
@ READ_WRITE
Read-write replica.
@ NONE
The database is not a replica (operates as primary).