Добавил запрос обновления идентификатора кластера
This commit is contained in:
@@ -25,6 +25,11 @@ namespace data_models {
|
||||
/// @returns Существование записи
|
||||
bool Has(int64_t id) const;
|
||||
|
||||
/// @brief Получение записи по идентификатору
|
||||
/// @param[in] id Идентификатор
|
||||
/// @returns Запись
|
||||
AnomalyLog Get(int64_t id) const;
|
||||
|
||||
/// @brief Получение всех записей с идентификатором больше указанного
|
||||
/// @details Если надо получить все записи, необходимо передать идентификатор -1
|
||||
/// @param[in] last_id Идентификатор
|
||||
@@ -36,6 +41,10 @@ namespace data_models {
|
||||
/// @returns Записи
|
||||
std::vector<AnomalyLog> GetByClusterId(int64_t cluster_id) const;
|
||||
|
||||
/// @brief Обновление записи
|
||||
/// @param[in] anomaly_log Запись
|
||||
void Update(const AnomalyLog& anomaly_log);
|
||||
|
||||
private:
|
||||
storage::Transaction& transaction_; ///< Транзакция
|
||||
};
|
||||
|
||||
@@ -28,10 +28,26 @@ AnomalyLogsModel::AnomalyLogsModel(storage::Transaction& transaction)
|
||||
|
||||
bool AnomalyLogsModel::Has(int64_t id) const {
|
||||
std::ostringstream query;
|
||||
query << "select exists(*) from anomaly_logs where id = " << id << ";";
|
||||
query << "select id from anomaly_logs where id = " << id << ";";
|
||||
|
||||
auto pqxx_result = transaction_.Do(query.str());
|
||||
return pqxx_result[0][0].as<bool>();
|
||||
return !pqxx_result.empty();
|
||||
}
|
||||
|
||||
AnomalyLog AnomalyLogsModel::Get(int64_t id) const {
|
||||
std::ostringstream query;
|
||||
query << "select id, cluster_id, data from anomaly_logs where id = " << id << ";";
|
||||
|
||||
auto pqxx_result = transaction_.Do(query.str());
|
||||
auto pqxx_row = pqxx_result.front();
|
||||
|
||||
AnomalyLog anomaly_log{
|
||||
pqxx_row[0].as<int64_t>(),
|
||||
pqxx_row[1].as<int64_t>(),
|
||||
pqxx_row[2].as<std::string>(),
|
||||
};
|
||||
|
||||
return anomaly_log;
|
||||
}
|
||||
|
||||
std::vector<AnomalyLog> AnomalyLogsModel::GetAll(int64_t last_id) const {
|
||||
@@ -49,3 +65,18 @@ std::vector<AnomalyLog> AnomalyLogsModel::GetByClusterId(int64_t cluster_id) con
|
||||
auto pqxx_result = transaction_.Do(query.str());
|
||||
return ConvertAnomalyLogs(std::move(pqxx_result));
|
||||
}
|
||||
|
||||
void AnomalyLogsModel::Update(const AnomalyLog& anomaly_log) {
|
||||
std::ostringstream query;
|
||||
query << "update anomaly_logs set "
|
||||
<< "cluster_id = " << anomaly_log.cluster_id;
|
||||
|
||||
if (!anomaly_log.data.empty()) {
|
||||
query << ", data = '" << anomaly_log.data << "' ";
|
||||
}
|
||||
|
||||
query << "where id = " << anomaly_log.id
|
||||
<< ";";
|
||||
|
||||
transaction_.Do(query.str());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user