5#include "../SqlConnection.hpp"
6#include "../SqlError.hpp"
7#include "SqlBackup.hpp"
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wnullability-extension"
23 #pragma clang diagnostic pop
26namespace Lightweight::SqlBackup::detail
32constexpr size_t MaxBinaryLobBufferSize = 16 * 1024 * 1024;
53LIGHTWEIGHT_API
bool IsTransientError(SqlErrorInfo
const& error);
60LIGHTWEIGHT_API std::chrono::milliseconds CalculateRetryDelay(
unsigned attempt, RetrySettings
const& settings)
noexcept;
65enum class RetryAction : uint8_t
83[[nodiscard]] LIGHTWEIGHT_API RetryAction ClassifyRetryOutcome(SqlErrorInfo
const& error,
84 unsigned attemptsSoFar,
85 RetrySettings
const& settings);
95LIGHTWEIGHT_API
bool ConnectWithRetry(SqlConnection& conn,
96 SqlConnectionString
const& connectionString,
97 RetrySettings
const& settings,
98 ProgressManager& progress,
99 std::string
const& operation);
110template <
typename Func>
111auto RetryOnTransientError(Func&& func,
112 RetrySettings
const& settings,
113 ProgressManager& progress,
114 std::string
const& operation) ->
decltype(func())
116 unsigned attempts = 0;
124 catch (SqlException
const& e)
126 if (ClassifyRetryOutcome(e.info(), attempts, settings) == RetryAction::GiveUp)
131 { .state = Progress::State::Warning,
132 .tableName = operation,
134 .totalRows = std::nullopt,
135 .message = std::format(
"Transient error, retry {}/{}: {}", attempts, settings.maxRetries, e.what()) });
137 std::this_thread::sleep_for(CalculateRetryDelay(attempts - 1, settings));
145LIGHTWEIGHT_API std::string CurrentDateTime();
154template <
typename Container>
155Container ReadZipEntry(zip_t* zip, zip_int64_t index, zip_uint64_t size)
157 zip_file_t* file = zip_fopen_index(zip,
static_cast<zip_uint64_t
>(index), 0);
164 zip_int64_t bytesRead = zip_fread(file, data.data(), size);
167 if (bytesRead < 0 || std::cmp_not_equal(bytesRead, size))
178LIGHTWEIGHT_API std::string FormatTableName(std::string_view schema, std::string_view table);
187LIGHTWEIGHT_API
bool DropTableIfExists(SqlConnection& conn,
188 std::string
const& schema,
189 std::string
const& tableName,
190 ProgressManager& progress);