/*
 * DBeaver - Universal Database Manager
 * Copyright (C) 2010-2020 DBeaver Corp and others
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jkiss.dbeaver.ext.sqlite.edit;

import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.ext.generic.edit.GenericTableColumnManager;
import org.jkiss.dbeaver.ext.generic.model.GenericTableBase;
import org.jkiss.dbeaver.ext.generic.model.GenericTableColumn;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.edit.DBECommandContext;
import org.jkiss.dbeaver.model.edit.DBEObjectRenamer;
import org.jkiss.dbeaver.model.edit.DBEPersistAction;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction;
import org.jkiss.dbeaver.model.impl.jdbc.struct.JDBCTableColumn;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * SQLiteTableColumnManager
 */
public class SQLiteTableColumnManager extends GenericTableColumnManager
    implements DBEObjectRenamer<GenericTableColumn>
{

    @Override
    protected void addObjectDeleteActions(DBRProgressMonitor monitor, DBCExecutionContext executionContext, List<DBEPersistAction> actions, ObjectDeleteCommand command, Map<String, Object> options) {
        final GenericTableColumn column = command.getObject();
        final GenericTableBase table = column.getTable();
        final String attributes;

        try {
            attributes = table.getAttributes(monitor).stream()
                    .filter(x -> x != column)
                    .map(JDBCTableColumn::getName)
                    .collect(Collectors.joining(", "));
        } catch (DBException e) {
            throw new RuntimeException(e);
        }

        final String tableName = DBUtils.getQuotedIdentifier(table);

        StringBuilder ddl = new StringBuilder()
                .append("-- Drop column ").append(DBUtils.getQuotedIdentifier(column)).append("\n")
                .append("CREATE TEMPORARY TABLE temp AS SELECT ").append(attributes).append(" FROM ").append(tableName).append(";\n")
                .append("DROP TABLE ").append(tableName).append(";\n")
                .append("CREATE TABLE ").append(tableName).append(" AS SELECT ").append(attributes).append(" FROM temp;\n")
                .append("DROP TABLE temp;\n");

        actions.add(new SQLDatabasePersistAction(
                "Drop table column",
                ddl.toString()
        ));
    }

    @Override
    protected void addObjectRenameActions(DBRProgressMonitor monitor, DBCExecutionContext executionContext, List<DBEPersistAction> actions, ObjectRenameCommand command, Map<String, Object> options)
    {
        final GenericTableColumn column = command.getObject();

        actions.add(
            new SQLDatabasePersistAction(
                "Rename column",
                "ALTER TABLE " + DBUtils.getQuotedIdentifier(column.getTable()) + " RENAME COLUMN " +
                    DBUtils.getQuotedIdentifier(column.getDataSource(), command.getOldName()) +
                    " TO " + DBUtils.getQuotedIdentifier(column.getDataSource(), command.getNewName())));
    }

    @Override
    public void renameObject(DBECommandContext commandContext, GenericTableColumn object, String newName) throws DBException {
        processObjectRename(commandContext, object, newName);
    }

}

@@ -1243,32 +1243,106 @@
 ePersistAction;%0A
+import org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment;%0A
 import org.jkiss
@@ -2464,76 +2464,113 @@
     
-StringBuilder ddl = new StringBuilder()%0A                .append(%22-- 
+actions.add(new SQLDatabasePersistActionComment(%0A                table.getDataSource(),%0A                %22
 Drop
@@ -2578,25 +2578,19 @@
 column %22
-).append(
+ + 
 DBUtils.
@@ -2620,47 +2620,149 @@
 umn)
-).append(%22%5Cn%22)%0A                .append(
+%0A        ));%0A        actions.add(new SQLDatabasePersistAction(%0A                %22Create temporary table from original table%22,%0A                
 %22CRE
@@ -2793,33 +2793,27 @@
  AS SELECT %22
-).append(
+ + 
 attributes).
@@ -2814,58 +2814,132 @@
 utes
-).append(%22 FROM %22).append(tableName).append(%22;%5Cn%22)
+ + %22 FROM %22 + tableName%0A        ));%0A        actions.add(new SQLDatabasePersistAction(%0A                %22Drop original table%22,
 %0A   
@@ -2947,32 +2947,24 @@
             
-.append(
 %22DROP TABLE 
@@ -2968,41 +2968,144 @@
 LE %22
-).append(tableName).append(%22;%5Cn%22)
+ + tableName%0A        ));%0A        actions.add(new SQLDatabasePersistAction(%0A                %22Create original table from temporary table%22,
 %0A   
@@ -3117,24 +3117,16 @@
         
-.append(
 %22CREATE 
@@ -3136,35 +3136,23 @@
 LE %22
-).append(tableName).append(
+ + tableName + 
 %22 AS
@@ -3160,25 +3160,19 @@
 SELECT %22
-).append(
+ + 
 attribut
@@ -3177,17 +3177,11 @@
 utes
-).append(
+ + 
 %22 FR
@@ -3187,21 +3187,17 @@
 ROM temp
-;%5Cn%22)
+%22
 %0A       
@@ -3201,48 +3201,12 @@
     
-        .append(%22DROP TABLE temp;%5Cn%22
+)
 );%0A
-%0A
     
@@ -3278,19 +3278,22 @@
 op t
-able column
+emporary table
 %22,%0A 
@@ -3311,22 +3311,25 @@
     
-ddl.toString()
+%22DROP TABLE temp%22
 %0A   

#5447 Separate each statement into its own action