Files
qt5-legatus-legacy/Lts/Services/InfrastructureManagement/ism_db_impl_custom.cpp
T
2026-08-13 18:33:10 +02:00

203 lines
6.3 KiB
C++
Executable File

/**
* @RDE_HEADER@
* Custom db functions (extends tab_db_impl.cpp/.h)
* $Id$
* $HeadURL$
* $Author$
*/
#include "ism_db_impl_custom.h"
#include <ngldb/nsqlquery.h>
#include <ngldb/sql_query_janitor.h>
#include <ngldb/dbtransaction.h>
namespace InfrastructureManagement
{
// custom procs, body must be implemented by user
int DatabaseInterfaceImplCustom::copySchedule(int scheduleId)
{
int newId = 0;
const QString tableSchedule = getTableName(QStringLiteral("ism_mounting_point_schedule"));
//const QString tablePlan = getTableName(QStringLiteral("ism_mounting_point_plan"));
DBTransaction trans(this->database());
trans.begin();
// get the schedule name first
const QString stmtSelectName = QStringLiteral("SELECT name FROM ") + tableSchedule + QStringLiteral(" WHERE id = :id");
DEBUG_LOG(m_logger) << "Select statement: " << stmtSelectName;
if (!m_query)
{
ERROR_LOG(m_logger) << "m_query not initialized";
return -1;
}
QString fileName;
{
SqlQueryJanitor janitor(m_query);
if (!m_query->prepare(stmtSelectName))
{
ERROR_LOG(m_logger) << "Could not prepare statement: " << stmtSelectName;
return -1;
}
m_query->bindValue(QStringLiteral(":id"), scheduleId);
if (!m_query->execAndFetch())
{
ERROR_LOG(m_logger) << "Could not execute statement: " << stmtSelectName;
return -1;
}
fileName = m_query->value(0).toString();
} // query scope
DEBUG_LOG(m_logger) << "Original Plan name = " << fileName;
fileName = fileName + tr(" (Copy)");
DEBUG_LOG(m_logger) << "New Plan name = " << fileName;
const QString stmtInsertSchedule = QStringLiteral("INSERT INTO ") + tableSchedule +
QStringLiteral(" (name, status_id, time_prepared, time_start, time_end) VALUES (:newName, 1, 0, 0, 0) RETURNING id;");
//new schedule has always status created (1) and timestampes 0, time_created has default value
DEBUG_LOG(m_logger) << "Insert schedule statment: " << stmtInsertSchedule;
{
SqlQueryJanitor janitor(m_query);
if (!m_query->prepare(stmtInsertSchedule))
{
ERROR_LOG(m_logger) << "Could not prepare statement: " << stmtInsertSchedule;
return -1;
}
m_query->bindValue(QStringLiteral(":newName"), fileName);
m_query->bindValue(QStringLiteral(":oldId"), scheduleId);
if (!m_query->exec())
{
ERROR_LOG(m_logger) << "Could not execute statement: " << stmtInsertSchedule;
return -1;
}
m_query->first();
newId = m_query->value(0).toInt();
DEBUG_LOG(m_logger) << "Inserted new schedule id = " << newId;
}
const QString stmtInsertMPP = QStringLiteral("with rows as (select ") + QString::number(newId) +
QStringLiteral(" as mounting_point_schedule_id, structure_element_id from ism.ism_mounting_point_plan where mounting_point_schedule_id = :oldId) insert into ism.ism_mounting_point_plan (mounting_point_schedule_id, structure_element_id) select * from rows;");
{
SqlQueryJanitor janitor(m_query);
if (!m_query->prepare(stmtInsertMPP))
{
ERROR_LOG(m_logger) << "Could not prepare statement: " << stmtInsertMPP;
return -1;
}
m_query->bindValue(QStringLiteral(":oldId"), scheduleId);
//m_query->bindValue(QStringLiteral(":newId"), newId);
if (!m_query->exec())
{
ERROR_LOG(m_logger) << "Could not execute statement: " << stmtInsertMPP;
return -1;
}
DEBUG_LOG(m_logger) << "Inserted MMPs";
} // query scope
trans.commit(); // rollback is not needed, is done automatically for non-commited queries
return newId;
}
bool DatabaseInterfaceImplCustom::updateMountingPointPlan(int scheduleId, int mountingPointId, MountingPointState mountingPointState)
{
SqlQueryJanitor janitor(m_query);
const QString tableName(getTableName(QStringLiteral("ism_mounting_point_plan")));
const QString stmt(QStringLiteral("UPDATE ") + tableName +
QStringLiteral(" SET mounting_point_state = ") +
QString::number(mountingPointState.underlying()) +
QStringLiteral(" WHERE mounting_point_schedule_id = ") +
QString::number(scheduleId) +
QStringLiteral(" AND structure_element_id = ") +
QString::number(mountingPointId));
if (!m_query->exec(stmt, m_bEmitMessageOnError))
{
m_lastError = m_query->lastError();
return false;
}
return true;
}
bool DatabaseInterfaceImplCustom::deleteMountingPointPlan(int scheduleId, int mountingPointId)
{
SqlQueryJanitor janitor(m_query);
const QString tableName(getTableName(QStringLiteral("ism_mounting_point_plan")));
const QString stmt(QStringLiteral("DELETE FROM ") + tableName +
QStringLiteral(" WHERE mounting_point_schedule_id = ") +
QString::number(scheduleId) +
QStringLiteral(" AND structure_element_id = ") +
QString::number(mountingPointId));
if (!m_query->exec(stmt, m_bEmitMessageOnError))
{
m_lastError = m_query->lastError();
return false;
}
return true;
}
bool DatabaseInterfaceImplCustom::insertMountingPointPlans(const std::vector<Database::IsmMountingPointPlan> &mpps)
{
SqlQueryJanitor janitor(m_query);
{
const QString tableName(getTableName(QStringLiteral("ism_mounting_point_plan")));
const QString stmt(QStringLiteral("INSERT INTO ") + tableName +
QStringLiteral(" (") +
QStringLiteral(" mounting_point_schedule_id, structure_element_id, mounting_point_state"
" ) VALUES (") +
QStringLiteral(":mounting_point_schedule_id, :structure_element_id, :mounting_point_state )"));
if (!m_query->prepare(stmt))
{
m_lastError = m_query->lastError();
return false;
}
for (const auto &data : mpps)
{
m_query->bindValue(QStringLiteral(":mounting_point_schedule_id"), data.mountingPointScheduleId);
m_query->bindValue(QStringLiteral(":structure_element_id"), data.structureElementId);
m_query->bindValue(QStringLiteral(":mounting_point_state"), static_cast<int>(data.mountingPointState.underlying()));
if (!m_query->exec(m_bEmitMessageOnError))
{
m_lastError = m_query->lastError();
return false;
}
}
}
return true;
}
}