Formatted includes/services/database

This commit is contained in:
Aaron Po
2026-07-19 04:10:32 -04:00
parent 6c07edf845
commit 4f0f56d7d7
4 changed files with 99 additions and 99 deletions

View File

@@ -14,38 +14,38 @@
* @brief Interface for services that persist generated brewery records. * @brief Interface for services that persist generated brewery records.
*/ */
class IExportService { class IExportService {
public: public:
IExportService() = default; IExportService() = default;
virtual ~IExportService() = default; virtual ~IExportService() = default;
IExportService(const IExportService&) = delete; IExportService(const IExportService&) = delete;
IExportService& operator=(const IExportService&) = delete; IExportService& operator=(const IExportService&) = delete;
IExportService(IExportService&&) = delete; IExportService(IExportService&&) = delete;
IExportService& operator=(IExportService&&) = delete; IExportService& operator=(IExportService&&) = delete;
/** /**
* @brief Prepares the export destination for a new run. * @brief Prepares the export destination for a new run.
*/ */
virtual void Initialize() = 0; virtual void Initialize() = 0;
/** /**
* @brief Persists one generated brewery record. * @brief Persists one generated brewery record.
* *
* @param brewery Generated brewery payload to store. * @param brewery Generated brewery payload to store.
*/ */
virtual uint64_t ProcessRecord(const BreweryRecord& brewery) = 0; virtual uint64_t ProcessRecord(const BreweryRecord& brewery) = 0;
/** /**
* @brief Persists one generated user record. * @brief Persists one generated user record.
* *
* @param user Generated user payload to store. * @param user Generated user payload to store.
*/ */
virtual uint64_t ProcessRecord(const UserRecord& user) = 0; virtual uint64_t ProcessRecord(const UserRecord& user) = 0;
/** /**
* @brief Finalizes the export destination. * @brief Finalizes the export destination.
*/ */
virtual void Finalize() = 0; virtual void Finalize() = 0;
}; };
#endif // BIERGARTEN_PIPELINE_INCLUDES_SERVICES_DATABASE_EXPORT_SERVICE_H_ #endif // BIERGARTEN_PIPELINE_INCLUDES_SERVICES_DATABASE_EXPORT_SERVICE_H_

View File

@@ -20,54 +20,54 @@
* @brief Persists generated brewery records into a fresh SQLite database. * @brief Persists generated brewery records into a fresh SQLite database.
*/ */
class SqliteExportService final : public IExportService { class SqliteExportService final : public IExportService {
public: public:
explicit SqliteExportService(const ApplicationOptions& options); explicit SqliteExportService(const ApplicationOptions& options);
~SqliteExportService() override; ~SqliteExportService() override;
SqliteExportService(const SqliteExportService&) = delete; SqliteExportService(const SqliteExportService&) = delete;
SqliteExportService& operator=(const SqliteExportService&) = delete; SqliteExportService& operator=(const SqliteExportService&) = delete;
SqliteExportService(SqliteExportService&&) = delete; SqliteExportService(SqliteExportService&&) = delete;
SqliteExportService& operator=(SqliteExportService&&) = delete; SqliteExportService& operator=(SqliteExportService&&) = delete;
void Initialize() override; void Initialize() override;
uint64_t ProcessRecord(const BreweryRecord& brewery) override; uint64_t ProcessRecord(const BreweryRecord& brewery) override;
uint64_t ProcessRecord(const UserRecord& user) override; uint64_t ProcessRecord(const UserRecord& user) override;
void Finalize() override; void Finalize() override;
private: private:
using SqliteDatabaseHandle = using SqliteDatabaseHandle =
sqlite_export_service_internal::SqliteDatabaseHandle; sqlite_export_service_internal::SqliteDatabaseHandle;
using SqliteStatementHandle = using SqliteStatementHandle =
sqlite_export_service_internal::SqliteStatementHandle; sqlite_export_service_internal::SqliteStatementHandle;
void InitializeSchema() const; void InitializeSchema() const;
void PrepareStatements(); void PrepareStatements();
void RollbackAndCloseNoThrow() noexcept; void RollbackAndCloseNoThrow() noexcept;
[[nodiscard]] std::filesystem::path BuildDatabasePath() const; [[nodiscard]] std::filesystem::path BuildDatabasePath() const;
[[nodiscard]] static std::string BuildCityKey(const City& city); [[nodiscard]] static std::string BuildCityKey(const City& city);
/** /**
* @brief Returns the row id for @p city, inserting it first if it has * @brief Returns the row id for @p city, inserting it first if it has
* not already been seen during this run. * not already been seen during this run.
* *
* Shared by both ProcessRecord() overloads so brewery and user addresses * Shared by both ProcessRecord() overloads so brewery and user addresses
* referencing the same city resolve to the same row. * referencing the same city resolve to the same row.
*/ */
[[nodiscard]] sqlite3_int64 ResolveCityId(const City& city); [[nodiscard]] sqlite3_int64 ResolveCityId(const City& city);
std::unique_ptr<IDateTimeProvider> date_time_provider_; std::unique_ptr<IDateTimeProvider> date_time_provider_;
std::filesystem::path output_path_; std::filesystem::path output_path_;
std::string run_timestamp_utc_; std::string run_timestamp_utc_;
std::filesystem::path database_path_; std::filesystem::path database_path_;
SqliteDatabaseHandle db_handle_; SqliteDatabaseHandle db_handle_;
SqliteStatementHandle insert_city_stmt_; SqliteStatementHandle insert_city_stmt_;
SqliteStatementHandle insert_brewery_stmt_; SqliteStatementHandle insert_brewery_stmt_;
SqliteStatementHandle insert_brewery_address_stmt_; SqliteStatementHandle insert_brewery_address_stmt_;
SqliteStatementHandle insert_user_stmt_; SqliteStatementHandle insert_user_stmt_;
SqliteStatementHandle insert_user_address_stmt_; SqliteStatementHandle insert_user_address_stmt_;
bool transaction_open_ = false; bool transaction_open_ = false;
std::unordered_map<std::string, sqlite3_int64> city_cache_; std::unordered_map<std::string, sqlite3_int64> city_cache_;
}; };
#endif // BIERGARTEN_PIPELINE_INCLUDES_SERVICES_DATABASE_SQLITE_EXPORT_SERVICE_H_ #endif // BIERGARTEN_PIPELINE_INCLUDES_SERVICES_DATABASE_SQLITE_EXPORT_SERVICE_H_

View File

@@ -13,11 +13,11 @@
namespace sqlite_export_service_internal { namespace sqlite_export_service_internal {
struct SqliteDatabaseDeleter { struct SqliteDatabaseDeleter {
void operator()(sqlite3* handle) const noexcept; void operator()(sqlite3* handle) const noexcept;
}; };
struct SqliteStatementDeleter { struct SqliteStatementDeleter {
void operator()(sqlite3_stmt* statement) const noexcept; void operator()(sqlite3_stmt* statement) const noexcept;
}; };
using SqliteDatabaseHandle = std::unique_ptr<sqlite3, SqliteDatabaseDeleter>; using SqliteDatabaseHandle = std::unique_ptr<sqlite3, SqliteDatabaseDeleter>;
@@ -28,9 +28,9 @@ using SqliteStatementHandle =
// N.B. indices are 1 based. // N.B. indices are 1 based.
template <typename T> template <typename T>
struct BoundParam { struct BoundParam {
int index; int index;
T value; T value;
std::string_view action; std::string_view action;
}; };
} // namespace sqlite_export_service_internal } // namespace sqlite_export_service_internal

View File

@@ -152,44 +152,44 @@ INSERT INTO user_addresses (
// sqlite3_bind_*() parameter indices are 1-based, matching the "?" // sqlite3_bind_*() parameter indices are 1-based, matching the "?"
// placeholder order in the SQL above. // placeholder order in the SQL above.
enum CityBindIndex { enum CityBindIndex {
kCityCityBindIndex = 1, kCityCityBindIndex = 1,
kCityStateProvinceBindIndex, kCityStateProvinceBindIndex,
kCityIso31662BindIndex, kCityIso31662BindIndex,
kCityCountryBindIndex, kCityCountryBindIndex,
kCityIso31661BindIndex, kCityIso31661BindIndex,
kCityLanguagesBindIndex, kCityLanguagesBindIndex,
kCityPostalCodeCountryFormatRegexBindIndex, kCityPostalCodeCountryFormatRegexBindIndex,
kCityPostalCodeCityRegexJsonBindIndex, kCityPostalCodeCityRegexJsonBindIndex,
}; };
enum BreweryBindIndex { enum BreweryBindIndex {
kBreweryEnglishNameBindIndex = 1, kBreweryEnglishNameBindIndex = 1,
kBreweryEnglishDescriptionBindIndex, kBreweryEnglishDescriptionBindIndex,
kBreweryLocalNameBindIndex, kBreweryLocalNameBindIndex,
kBreweryLocalDescriptionBindIndex, kBreweryLocalDescriptionBindIndex,
}; };
enum BreweryAddressBindIndex { enum BreweryAddressBindIndex {
kBreweryAddressBreweryIdBindIndex = 1, kBreweryAddressBreweryIdBindIndex = 1,
kBreweryAddressCityIdBindIndex, kBreweryAddressCityIdBindIndex,
kBreweryAddressPostalCodeBindIndex, kBreweryAddressPostalCodeBindIndex,
}; };
enum UserBindIndex { enum UserBindIndex {
kUserFirstNameBindIndex = 1, kUserFirstNameBindIndex = 1,
kUserLastNameBindIndex, kUserLastNameBindIndex,
kUserGenderBindIndex, kUserGenderBindIndex,
kUserUsernameBindIndex, kUserUsernameBindIndex,
kUserBioBindIndex, kUserBioBindIndex,
kUserActivityWeightBindIndex, kUserActivityWeightBindIndex,
kUserEmailBindIndex, kUserEmailBindIndex,
kUserDateOfBirthBindIndex, kUserDateOfBirthBindIndex,
}; };
enum UserAddressBindIndex { enum UserAddressBindIndex {
kUserAddressUserIdBindIndex = 1, kUserAddressUserIdBindIndex = 1,
kUserAddressCityIdBindIndex, kUserAddressCityIdBindIndex,
kUserAddressPostalCodeBindIndex, kUserAddressPostalCodeBindIndex,
}; };
SqliteStatementHandle PrepareStatement(const SqliteDatabaseHandle& db_handle, SqliteStatementHandle PrepareStatement(const SqliteDatabaseHandle& db_handle,