5#include "../SqlConnection.hpp"
6#include "../SqlError.hpp"
7#include "../SqlRetryPolicy.hpp"
8#include "SqlBackup.hpp"
23 #pragma clang diagnostic push
24 #pragma clang diagnostic ignored "-Wnullability-extension"
28 #pragma clang diagnostic pop
31namespace Lightweight::SqlBackup::detail
37constexpr size_t MaxBinaryLobBufferSize = 16 * 1024 * 1024;
65LIGHTWEIGHT_API
bool IsTransientError(SqlErrorInfo
const& error);
74LIGHTWEIGHT_API std::chrono::milliseconds CalculateRetryDelay(
unsigned attempt, RetrySettings
const& settings)
noexcept;
96[[nodiscard]] LIGHTWEIGHT_API RetryAction ClassifyRetryOutcome(SqlErrorInfo
const& error,
97 unsigned attemptsSoFar,
98 RetrySettings
const& settings);
112[[nodiscard]] LIGHTWEIGHT_API SqlRetryDecision ClassifyRetryDecision(SqlConnection
const& conn,
113 SqlErrorInfo
const& error,
114 unsigned attemptsSoFar,
115 RetrySettings
const& settings);
125[[nodiscard]] LIGHTWEIGHT_API
bool IsPermanentError(SqlConnection
const& conn,
126 SqlErrorInfo
const& error,
127 unsigned attemptsSoFar,
128 RetrySettings
const& settings);
138LIGHTWEIGHT_API
bool ConnectWithRetry(SqlConnection& conn,
139 SqlConnectionString
const& connectionString,
140 RetrySettings
const& settings,
141 ProgressManager& progress,
142 std::string
const& operation);
157template <
typename Func>
158auto RetryOnTransientError(Func func, RetrySettings
const& settings, ProgressManager& progress, std::string
const& operation)
161 auto policy = SqlRetryPolicy { settings };
163 policy.SetRetryObserver([&progress, &operation, &settings](SqlRetryAttempt
const& attempt) {
164 progress.Update({ .state = Progress::State::Warning,
165 .tableName = operation,
167 .totalRows = std::nullopt,
170 .message = std::format(
"Transient error, retry {}/{}: {}",
173 std::format(
"{}", attempt.error)) });
176 return policy.Execute(std::move(func));
182LIGHTWEIGHT_API std::string CurrentDateTime();
191template <
typename Container>
192Container ReadZipEntry(zip_t* zip, zip_int64_t index, zip_uint64_t size)
194 zip_file_t* file = zip_fopen_index(zip,
static_cast<zip_uint64_t
>(index), 0);
201 zip_int64_t bytesRead = zip_fread(file, data.data(), size);
204 if (bytesRead < 0 || std::cmp_not_equal(bytesRead, size))
215LIGHTWEIGHT_API std::string FormatTableName(std::string_view schema, std::string_view table);
225LIGHTWEIGHT_API std::string ChunkDirectory(std::string_view schema, std::string_view table);
231LIGHTWEIGHT_API std::optional<std::string_view> ChunkDirectoryOf(std::string_view entryName);
234inline constexpr std::string_view AttachmentsPrefix =
"attachments/";
244[[nodiscard]] LIGHTWEIGHT_API std::expected<void, std::string> ValidateAttachmentNames(std::span<std::string const> names);
253LIGHTWEIGHT_API std::string JoinNames(std::vector<std::string>
const& names);
259LIGHTWEIGHT_API std::string ToLowerAscii(std::string_view text);
267LIGHTWEIGHT_API std::vector<std::string> NormalizeSchemaNames(std::vector<std::string> schemas,
bool caseInsensitive);
279LIGHTWEIGHT_API std::string DescribeTableNameCollisions(SqlSchema::TableList
const& tables,
bool multiSchema);
288LIGHTWEIGHT_API
bool DropTableIfExists(SqlConnection& conn,
289 std::string
const& schema,
290 std::string
const& tableName,
291 ProgressManager& progress);