bool BaseSqlDropTableService::execute(const Commands::ConstCommandPtr &command, CommandExecution::CommandExecutionContext &context) const { QSharedPointer<const Commands::DropTable> dropTable(command.staticCast<const Commands::DropTable>()); Q_ASSERT(dropTable); const Structure::Table originalTable( context.helperRepository().sqlStructureService().getTableDefinition(dropTable->tableName(), context.database()) ); bool success = execute(*dropTable, context); if (success) { context.setUndoCommand(Commands::CommandPtr(new Commands::CreateTable(originalTable))); } return success; }
bool SqliteDropColumnService::execute(const Commands::ConstCommandPtr &command, CommandExecution::CommandExecutionContext &context) const { QSharedPointer<const Commands::DropColumn> dropColumn(command.staticCast<const Commands::DropColumn>()); Structure::Table table( context.helperRepository().sqlStructureService().getTableDefinition(dropColumn->tableName(), context.database()) ); Structure::Table::Builder alteredTable(dropColumn->tableName()); const Structure::Column* originalColumn = Q_NULLPTR; foreach (const Structure::Column &column, table.columns()) { if (column.name() == dropColumn->columnName()) { originalColumn = &column; } else { alteredTable << column; } } if (!originalColumn) { ::qWarning() << "Column not found" << dropColumn->tableName() << dropColumn->columnName(); return false; } QString tempTableName = QString("%1%2").arg(context.migrationConfig().temporaryTablePrefix, dropColumn->tableName()); bool success = BaseSqlRenameTableService::execute(Commands::RenameTable(dropColumn->tableName(), tempTableName), context); if (!success) return false; success = BaseSqlCreateTableService::execute(Commands::CreateTable(alteredTable), context); if (!success) return false; const QString copyQuery = QString("INSERT INTO %1 SELECT %2 FROM %3") .arg(table.name()) .arg(table.columnNames().join(", ")) .arg(tempTableName); success = CommandExecution::BaseCommandExecutionService::executeQuery(copyQuery, context); if (!success) return false; success = BaseSqlDropTableService::execute(Commands::DropTable(tempTableName), context); if (success && context.isUndoUsed()) { context.setUndoCommand(Commands::CommandPtr(new Commands::AddColumn(*originalColumn, dropColumn->tableName()))); } return success; }
bool MysqlRenameColumnService::execute(const Commands::ConstCommandPtr &command, CommandExecution::CommandExecutionContext &context) const { QSharedPointer<const Commands::RenameColumn> renameColumn(command.staticCast<const Commands::RenameColumn>()); Q_ASSERT(renameColumn); const Structure::Column originalColumn( context.helperRepository().sqlStructureService() .getTableDefinition(renameColumn->tableName(), context.database()) .fetchColumnByName(renameColumn->oldName()) ); if (!originalColumn.isValid()) { ::qWarning() << "could not find column" << renameColumn->tableName() << renameColumn->oldName(); return false; } bool success = execute(*renameColumn, originalColumn, context); if (success && context.isUndoUsed()) { context.setUndoCommand(renameColumn->reverse()); } return success; }