<commit_before>package cyberwaste.kuzoff.command;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cyberwaste.kuzoff.database.DatabaseManager;
import cyberwaste.kuzoff.database.Row;
import cyberwaste.kuzoff.database.Table;
import cyberwaste.kuzoff.database.Type;
import cyberwaste.kuzoff.database.Value;
import cyberwaste.kuzoff.outputmanager.OutputManager;

public class CommandRunner {
    
    private final DatabaseManager databaseManager;
    private final OutputManager outputManager;
    
    public CommandRunner(String databaseFolder, OutputManager outputManager) {
        this.outputManager = outputManager;
        this.databaseManager = new DatabaseManager(databaseFolder);
    }

    public void runCommand(String commandName, Map<String, String> parameters) {
        if ("lstbl".equals(commandName)) {
            String tableName = getStringParameter(parameters, "name");
            
            if (tableName != null) {
                showTable(tableName);
            } else {
                listTables();
            }
        }
        
        else if ("mktbl".equals(commandName)) {
            String tableName = getStringParameter(parameters, "name");
            List<String> columnTypes = getListParameter(parameters, "column");
            
            createTable(tableName, columnTypes);
        }
        
        else if ("rmtbl".equals(commandName)) {
            String tableName = getStringParameter(parameters, "name");
            
            removeTable(tableName);
        }
        
        else if("addrw".equals(commandName)){
            String tableName = getStringParameter(parameters, "name");
            List<String> columnData = getListParameter(parameters,"column");
            
            addRow(tableName, columnData);
        }
        
        else if("rmvrw".equals(commandName)){
            String tableName = getStringParameter(parameters, "name");
            Map<Integer,String> columnData = getMapParameter(parameters, "column");
            
            removeRow(tableName, columnData);
        }
        
        else if("drpdb".equals(commandName)){   
            dropDatabase();
        }
        
        else if("swtbl".equals(commandName)){
            String tableName = getStringParameter(parameters, "name");
            showTableData(tableName);
        }
        else{
            outputManager.outputMessage("UNKNOWN OPERATION");
        }
    }
    
    private void showTableData(String tableName) {
        try {
            Table result = databaseManager.loadTable(tableName);
            outputManager.outputTable(result);
            List<Row> tableData = databaseManager.loadTableData(tableName);
            for(Row curRow : tableData){
                outputManager.outputRow(curRow);
            }
        } catch (IOException e) {
            outputManager.outputError(e);
        }
    }

    private void dropDatabase() {
        try{
            databaseManager.dropDatabase();
            outputManager.outputMessage("DATABASE " + databaseManager.getDatabaseName() + " deleted");
        }catch (IOException e) {
            outputManager.outputError(e);
        }
        
    }

    private Map<Integer, String> getMapParameter(Map<String,String> parameters, String key){
        Map<Integer, String> result = new HashMap<Integer, String>();
        
        int index = 1;
        String columnData;
        while ((columnData = getStringParameter(parameters, key + "-" + index)) != null) {
            result.put(index, columnData);
            index++;
        }
        
        return result;
    }
    
    private List<String> getListParameter(Map<String, String> parameters, String key) {
        List<String> result = new ArrayList<String>();
        
        int index = 1;
        String columnType;
        while ((columnType = getStringParameter(parameters, key + "-" + index)) != null) {
            result.add(columnType);
            index++;
        }
        
        return result;
    }

    private String getStringParameter(Map<String, String> parameters, String key) {
        return parameters.get(key);
    }

    private void listTables() {
        try {
            Collection<Table> result = databaseManager.listTables();
            outputManager.outputListTables(result);
        } catch (IOException e) {<commit_msg>remove row bug fixed<commit_after>package cyberwaste.kuzoff.command;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cyberwaste.kuzoff.database.DatabaseManager;
import cyberwaste.kuzoff.database.Row;
import cyberwaste.kuzoff.database.Table;
import cyberwaste.kuzoff.database.Type;
import cyberwaste.kuzoff.database.Value;
import cyberwaste.kuzoff.outputmanager.OutputManager;

public class CommandRunner {
    
    private final DatabaseManager databaseManager;
    private final OutputManager outputManager;
    
    public CommandRunner(String databaseFolder, OutputManager outputManager) {
        this.outputManager = outputManager;
        this.databaseManager = new DatabaseManager(databaseFolder);
    }

    public void runCommand(String commandName, Map<String, String> parameters) {
        if ("lstbl".equals(commandName)) {
            String tableName = getStringParameter(parameters, "name");
            
            if (tableName != null) {
                showTable(tableName);
            } else {
                listTables();
            }
        }
        
        else if ("mktbl".equals(commandName)) {
            String tableName = getStringParameter(parameters, "name");
            List<String> columnTypes = getListParameter(parameters, "column");
            
            createTable(tableName, columnTypes);
        }
        
        else if ("rmtbl".equals(commandName)) {
            String tableName = getStringParameter(parameters, "name");
            
            removeTable(tableName);
        }
        
        else if("addrw".equals(commandName)){
            String tableName = getStringParameter(parameters, "name");
            List<String> columnData = getListParameter(parameters,"column");
            
            addRow(tableName, columnData);
        }
        
        else if("rmvrw".equals(commandName)){
            String tableName = getStringParameter(parameters, "name");
            int numColumns;
            try {
                numColumns = databaseManager.loadTable(tableName).columnTypes().size();
                Map<Integer,String> columnData = getMapParameter(parameters, "column", numColumns);
                removeRow(tableName, columnData);
            } catch (IOException e) {
                outputManager.outputError(e);
            }
        }
        
        else if("drpdb".equals(commandName)){   
            dropDatabase();
        }
        
        else if("swtbl".equals(commandName)){
            String tableName = getStringParameter(parameters, "name");
            showTableData(tableName);
        }
        else{
            outputManager.outputMessage("UNKNOWN OPERATION");
        }
    }
    
    private void showTableData(String tableName) {
        try {
            Table result = databaseManager.loadTable(tableName);
            outputManager.outputTable(result);
            List<Row> tableData = databaseManager.loadTableData(tableName);
            for(Row curRow : tableData){
                outputManager.outputRow(curRow);
            }
        } catch (IOException e) {
            outputManager.outputError(e);
        }
    }

    private void dropDatabase() {
        try{
            databaseManager.dropDatabase();
            outputManager.outputMessage("DATABASE " + databaseManager.getDatabaseName() + " deleted");
        }catch (IOException e) {
            outputManager.outputError(e);
        }
        
    }

    private Map<Integer, String> getMapParameter(Map<String,String> parameters, String key, int numColumns){
        Map<Integer, String> result = new HashMap<Integer, String>();
        
        String columnData;
        for (int index = 1;index<=numColumns;index++) {
            columnData = getStringParameter(parameters, key + "-" + index);
            if(columnData != null) result.put(index, columnData);
        }
        
        return result;
    }
    
    private List<String> getListParameter(Map<String, String> parameters, String key) {
        List<String> result = new ArrayList<String>();
        
        int index = 1;
        String columnType;
        while ((columnType = getStringParameter(parameters, key + "-" + index)) != null) {
            result.add(columnType);
            index++;
        }
        
        return result;
    }

    private String getStringParameter(Map<String, String> parameters, String key) {
        return parameters.get(key);
    }

    private void listTables() {
        try {
            Collection<Table> result = databaseManager.listTables();
            outputManager.outputListTables(result);
        } catch (IOException e) {
{'fraction_non_alphanumeric': 0.0996089506843363, 'fraction_numerical': 0.00043449923962633063, 'mean_word_length': 2.1924410540915393, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4439095', 'n_tokens_mistral': 2319, 'n_tokens_neox': 2298, 'n_words': 475}