<commit_before>package org.javaz.jdbc.util;

import java.sql.*;
import java.util.*;

/**
 *
 */
public class UnsafeSqlHelper implements JdbcConstants
{
    public static ArrayList runSqlUnsafe(ConnectionProviderI provider, String jdbcAddress, String query, int code, Map parameters)
    {
        Connection c = null;
        try
        {
            c = provider.getConnection(jdbcAddress);
            return runSqlUnsafe(c, query, code, parameters);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.err.println("Problem with query: {" + query + "} code " + code + " and params: " + parameters);
        }
        finally
        {
            if (c != null)
            {
                try
                {
                    c.close();
                }
                catch (SQLException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static ArrayList runSqlUnsafe(Connection c, String query, int code, Map parameters)
    {
        ArrayList listToReturn = new ArrayList();
        try
        {
            boolean generatedKeysSupportedAndRequired = false;
            PreparedStatement preparedStatement = null;
            if (code == ACTION_EXECUTE_UPDATE)
            {
                generatedKeysSupportedAndRequired = c.getMetaData().supportsGetGeneratedKeys();
                if (generatedKeysSupportedAndRequired)
                {
                    preparedStatement = c.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
                }
            }
            if (preparedStatement == null)
            {
                preparedStatement = c.prepareStatement(query);
            }
            if (parameters != null)
            {
                Set set = parameters.keySet();
                for (Iterator iterator = set.iterator(); iterator.hasNext(); )
                {
                    Integer key = (Integer) iterator.next();
                    Object parameterValue = parameters.get(key);
                    if (parameterValue != null && parameterValue instanceof java.util.Date
                            && !(parameterValue instanceof java.sql.Date)
                            && !(parameterValue instanceof java.sql.Time)
                            && !(parameterValue instanceof java.sql.Timestamp))
                    {
                        //we have here old plain java.util.Date.
                        //let's convert it into java.sql.Timestamp and let's see what comes out.
                        java.util.Date d = (java.util.Date) parameterValue;
                        parameterValue = new Timestamp(d.getTime());
                    }
                    preparedStatement.setObject(key, parameterValue);
                }
            }

            if (code == JdbcHelper.ACTION_EXECUTE_UPDATE || code == JdbcHelper.ACTION_EXECUTE_UPDATE_DATA_IGNORE)
            {
                int executeUpdateResult = preparedStatement.executeUpdate();
                if (generatedKeysSupportedAndRequired)
                {
                    ResultSet keys = preparedStatement.getGeneratedKeys();
                    if (keys != null && keys.next())
                    {
                        Object object = keys.getObject(1);
                        listToReturn.add(object);
                    }
                }
                //even in case of GeneratedKeys we return update result itself
                listToReturn.add(new Integer(executeUpdateResult));
                return listToReturn;
            }
            boolean successfulExecution = preparedStatement.execute();
            if (successfulExecution)
            {
                ResultSet resultSet = preparedStatement.getResultSet();
                if (code == JdbcHelper.ACTION_MAP_RESULTS_SET)
                {
                    while (resultSet.next())
                    {
                        HashMap results = new HashMap();
                        ResultSetMetaData setMetaData = resultSet.getMetaData();
                        for (int i = 1; i <= setMetaData.getColumnCount(); i++)
                        {
                            String name = setMetaData.getColumnLabel(i);
                            Object o = null;
                            try
                            {
                                o = resultSet.getObject(i);
                            }
                            catch (SQLException e)
                            {
                                //This can happen with MySQL, let's silent ignore it.
                                //System.out.println("Incorrect object in ResultSet");
                            }
                            results.put(name, o);
                        }

                        listToReturn.add(results);
                    }
                }
                if (code == JdbcHelper.ACTION_LIST_FIRST_OBJECTS)
                {
                    while (resultSet.next())
                    {
                        listToReturn.add(resultSet.getObject(1));
                    }
                }
                if (code == JdbcHelper.ACTION_COMPLEX_LIST_METADATA || code == JdbcHelper.ACTION_COMPLEX_LIST_NO_METADATA)
                {
                    ResultSetMetaData setMetaData = preparedStatement.getMetaData();
                    if (code == JdbcHelper.ACTION_COMPLEX_LIST_METADATA)
                    {
                        ArrayList a = new ArrayList();
                        for (int i = 1; i <= setMetaData.getColumnCount(); i++)
                        {
                            a.add(setMetaData.getColumnLabel(i));
                        }
                        listToReturn.add(a);
                    }

                    while (resultSet.next())
                    {
                        ArrayList a = new ArrayList();
                        for (int i = 1; i <= setMetaData.getColumnCount(); i++)
                        {
                            a.add(resultSet.getObject(i));
                        }<commit_msg>Maps from UnsafeSqlHelper now contains only lowercased keys<commit_after>package org.javaz.jdbc.util;

import java.sql.*;
import java.util.*;

/**
 *
 */
public class UnsafeSqlHelper implements JdbcConstants
{
    public static ArrayList runSqlUnsafe(ConnectionProviderI provider, String jdbcAddress, String query, int code, Map parameters)
    {
        Connection c = null;
        try
        {
            c = provider.getConnection(jdbcAddress);
            return runSqlUnsafe(c, query, code, parameters);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.err.println("Problem with query: {" + query + "} code " + code + " and params: " + parameters);
        }
        finally
        {
            if (c != null)
            {
                try
                {
                    c.close();
                }
                catch (SQLException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static ArrayList runSqlUnsafe(Connection c, String query, int code, Map parameters)
    {
        ArrayList listToReturn = new ArrayList();
        try
        {
            boolean generatedKeysSupportedAndRequired = false;
            PreparedStatement preparedStatement = null;
            if (code == ACTION_EXECUTE_UPDATE)
            {
                generatedKeysSupportedAndRequired = c.getMetaData().supportsGetGeneratedKeys();
                if (generatedKeysSupportedAndRequired)
                {
                    preparedStatement = c.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
                }
            }
            if (preparedStatement == null)
            {
                preparedStatement = c.prepareStatement(query);
            }
            if (parameters != null)
            {
                Set set = parameters.keySet();
                for (Iterator iterator = set.iterator(); iterator.hasNext(); )
                {
                    Integer key = (Integer) iterator.next();
                    Object parameterValue = parameters.get(key);
                    if (parameterValue != null && parameterValue instanceof java.util.Date
                            && !(parameterValue instanceof java.sql.Date)
                            && !(parameterValue instanceof java.sql.Time)
                            && !(parameterValue instanceof java.sql.Timestamp))
                    {
                        //we have here old plain java.util.Date.
                        //let's convert it into java.sql.Timestamp and let's see what comes out.
                        java.util.Date d = (java.util.Date) parameterValue;
                        parameterValue = new Timestamp(d.getTime());
                    }
                    preparedStatement.setObject(key, parameterValue);
                }
            }

            if (code == JdbcHelper.ACTION_EXECUTE_UPDATE || code == JdbcHelper.ACTION_EXECUTE_UPDATE_DATA_IGNORE)
            {
                int executeUpdateResult = preparedStatement.executeUpdate();
                if (generatedKeysSupportedAndRequired)
                {
                    ResultSet keys = preparedStatement.getGeneratedKeys();
                    if (keys != null && keys.next())
                    {
                        Object object = keys.getObject(1);
                        listToReturn.add(object);
                    }
                }
                //even in case of GeneratedKeys we return update result itself
                listToReturn.add(new Integer(executeUpdateResult));
                return listToReturn;
            }
            boolean successfulExecution = preparedStatement.execute();
            if (successfulExecution)
            {
                ResultSet resultSet = preparedStatement.getResultSet();
                if (code == JdbcHelper.ACTION_MAP_RESULTS_SET)
                {
                    while (resultSet.next())
                    {
                        HashMap results = new HashMap();
                        ResultSetMetaData setMetaData = resultSet.getMetaData();
                        for (int i = 1; i <= setMetaData.getColumnCount(); i++)
                        {
                            String name = setMetaData.getColumnLabel(i);
                            Object o = null;
                            try
                            {
                                o = resultSet.getObject(i);
                            }
                            catch (SQLException e)
                            {
                                //This can happen with MySQL, let's silent ignore it.
                                //System.out.println("Incorrect object in ResultSet");
                            }
                            results.put(name.toLowerCase(), o);
                        }

                        listToReturn.add(results);
                    }
                }
                if (code == JdbcHelper.ACTION_LIST_FIRST_OBJECTS)
                {
                    while (resultSet.next())
                    {
                        listToReturn.add(resultSet.getObject(1));
                    }
                }
                if (code == JdbcHelper.ACTION_COMPLEX_LIST_METADATA || code == JdbcHelper.ACTION_COMPLEX_LIST_NO_METADATA)
                {
                    ResultSetMetaData setMetaData = preparedStatement.getMetaData();
                    if (code == JdbcHelper.ACTION_COMPLEX_LIST_METADATA)
                    {
                        ArrayList a = new ArrayList();
                        for (int i = 1; i <= setMetaData.getColumnCount(); i++)
                        {
                            a.add(setMetaData.getColumnLabel(i).toLowerCase());
                        }
                        listToReturn.add(a);
                    }

                    while (resultSet.next())
                    {
                        ArrayList a = new ArrayList();
                        for (int i = 1; i <= setMetaData.getColumnCount(); i++)
                        {
                            a.add(resultSet.getObject(i));
                        }
{'fraction_non_alphanumeric': 0.08030254264563888, 'fraction_numerical': 0.0008046346958480849, 'mean_word_length': 1.105896306336835, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3106696', 'n_tokens_mistral': 2806, 'n_tokens_neox': 2638, 'n_words': 587}