MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / tableRename

Method tableRename

app/src/API/Handlers/DataTablesHandler.cpp:456–491  ·  view source on GitHub ↗

* @brief Renames a table. Returns an error if the target name is taken. */

Source from the content-addressed store, hash-verified

454 * @brief Renames a table. Returns an error if the target name is taken.
455 */
456API::CommandResponse API::Handlers::DataTablesHandler::tableRename(const QString& id,
457 const QJsonObject& params)
458{
459 QString oldName;
460 QString newName;
461 if (!requireString(params, QStringLiteral("oldName"), oldName))
462 return CommandResponse::makeError(
463 id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: oldName"));
464
465 if (!requireString(params, QStringLiteral("newName"), newName))
466 return CommandResponse::makeError(
467 id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: newName"));
468
469 auto& pm = DataModel::ProjectModel::instance();
470
471 const auto& preTables = pm.tables();
472 const bool hasOld = std::any_of(
473 preTables.begin(), preTables.end(), [&oldName](const auto& t) { return t.name == oldName; });
474 const bool hasNew = std::any_of(
475 preTables.begin(), preTables.end(), [&newName](const auto& t) { return t.name == newName; });
476 const bool collides = hasNew && (oldName != newName);
477
478 bool applied = false;
479 if (hasOld && !collides) {
480 pm.renameTable(oldName, newName);
481 const auto& tables = pm.tables();
482 applied = std::any_of(
483 tables.begin(), tables.end(), [&newName](const auto& t) { return t.name == newName; });
484 }
485
486 QJsonObject result;
487 result[QStringLiteral("oldName")] = oldName;
488 result[QStringLiteral("newName")] = newName;
489 result[QStringLiteral("renamed")] = applied;
490 return CommandResponse::makeSuccess(id, result);
491}
492
493//--------------------------------------------------------------------------------------------------
494// Register mutations

Callers

nothing calls this directly

Calls 3

requireStringFunction · 0.85
beginMethod · 0.80
renameTableMethod · 0.80

Tested by

no test coverage detected