<commit_before>package org.intellij.ibatis.actions;

import com.intellij.codeInsight.hint.HintUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiType;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.util.*;

/**
 * SQL code preview form
 *
 * @author jacky
 */
public class SQLPopupView {
    private JTextPane textPane;
    public JPanel mainPanel;
    public JTable paramsTable;
    private String rawSqlCode;
    private Map<String, String> inlineParameters = new HashMap<String, String>();
    private PsiClass parameterClass;

    /**
     * default construction
     */
    public SQLPopupView() {
        paramsTable.setAutoscrolls(true);
        textPane.setAutoscrolls(true);
    }

    /**
     * construct parameter table and SQL panel
     *
     * @param parameterClass parameter class
     * @param parameters     parameter list
     * @param rawSqlCode     SQL code
     */
    public SQLPopupView(PsiClass parameterClass, Set<String> parameters, String rawSqlCode) {
        this.rawSqlCode = rawSqlCode;
        this.parameterClass = parameterClass;
        try {
            ParametersTableModel tableModel = new ParametersTableModel();
            for (String parameter : parameters) {
                tableModel.add(parameter, "string", "");
                updateParameterValue(parameter, "");
            }
            paramsTable.setModel(tableModel);
            textPane.setText(rawSqlCode);
            textPane.setBackground(HintUtil.INFORMATION_COLOR);
        }
        catch (Exception e) {
            textPane.setText(e.getMessage());
        }
    }

    /**
     * update parameter value
     *
     * @param name  name
     * @param value value
     */
    public void updateParameterValue(String name, String value) {
        PsiMethod[] methodNames = parameterClass.findMethodsByName("get" + StringUtil.capitalize(name), true);
        if (methodNames.length > 0l) {
            PsiType returnType = methodNames[0].getReturnType();
            if (returnType != null && "string".equalsIgnoreCase(returnType.getPresentableText())) {
                value = "\"" + value + "\"";
            }
        }
        inlineParameters.put(name, value);
        textPane.setText(getClearSQLCode());
    }

    /**
     * get clear SQL Code
     *
     * @return clear SQL Code
     */
    private String getClearSQLCode() {
        String clearSQLCode = rawSqlCode;
        for (Map.Entry<String, String> entry : inlineParameters.entrySet()) {
            clearSQLCode = clearSQLCode.replaceAll("#" + entry.getKey() + "([^#]*)#", entry.getValue());
        }
        return clearSQLCode;
    }

    /**
     * parameter table model
     */
    class ParametersTableModel extends AbstractTableModel {
        List<List<String>> rowData = new ArrayList<List<String>>();<commit_msg>bug fix to convert string to varchar<commit_after>package org.intellij.ibatis.actions;

import com.intellij.codeInsight.hint.HintUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiType;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.util.*;

/**
 * SQL code preview form
 *
 * @author jacky
 */
public class SQLPopupView {
    private JTextPane textPane;
    public JPanel mainPanel;
    public JTable paramsTable;
    private String rawSqlCode;
    private Map<String, String> inlineParameters = new HashMap<String, String>();
    private PsiClass parameterClass;

    /**
     * default construction
     */
    public SQLPopupView() {
        paramsTable.setAutoscrolls(true);
        textPane.setAutoscrolls(true);
    }

    /**
     * construct parameter table and SQL panel
     *
     * @param parameterClass parameter class
     * @param parameters     parameter list
     * @param rawSqlCode     SQL code
     */
    public SQLPopupView(PsiClass parameterClass, Set<String> parameters, String rawSqlCode) {
        this.rawSqlCode = rawSqlCode;
        this.parameterClass = parameterClass;
        try {
            ParametersTableModel tableModel = new ParametersTableModel();
            for (String parameter : parameters) {
                tableModel.add(parameter, "string", "");
                updateParameterValue(parameter, "");
            }
            paramsTable.setModel(tableModel);
            textPane.setText(rawSqlCode);
            textPane.setBackground(HintUtil.INFORMATION_COLOR);
        }
        catch (Exception e) {
            textPane.setText(e.getMessage());
        }
    }

    /**
     * update parameter value
     *
     * @param name  name
     * @param value value
     */
    public void updateParameterValue(String name, String value) {
        PsiMethod[] methodNames = parameterClass.findMethodsByName("get" + StringUtil.capitalize(name), true);
        if (methodNames.length > 0l) {
            PsiType returnType = methodNames[0].getReturnType();
            if (returnType != null && "string".equalsIgnoreCase(returnType.getPresentableText())) {
                value = "'" + value + "'";
            }
        }
        inlineParameters.put(name, value);
        textPane.setText(getClearSQLCode());
    }

    /**
     * get clear SQL Code
     *
     * @return clear SQL Code
     */
    private String getClearSQLCode() {
        String clearSQLCode = rawSqlCode;
        for (Map.Entry<String, String> entry : inlineParameters.entrySet()) {
            clearSQLCode = clearSQLCode.replaceAll("#" + entry.getKey() + "([^#]*)#", entry.getValue());
        }
        return clearSQLCode;
    }

    /**
     * parameter table model
     */
    class ParametersTableModel extends AbstractTableModel {
        List<List<String>> rowData = new ArrayList<List<String>>();
{'fraction_non_alphanumeric': 0.1078148838786235, 'fraction_numerical': 0.0006780810306831666, 'mean_word_length': 2.5223880597014925, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, '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': '2973851', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1601, 'n_words': 353}