<commit_before>package ru.job4j.jdbc;

import java.sql.*;

public class Optimization {
    private int maxCount;
    private String url;
    private String userName;
    private String password;

    public String getUrl() {
        return url;
    }

    public void setUrl(String serverUrl) {
        this.url = "jdbc:postgresql://" + serverUrl;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getMaxCount() {
        return maxCount;
    }

    public void setMaxCount(int maxCount) {
        this.maxCount = maxCount;
    }

    public Connection getConnection() throws ClassNotFoundException, SQLException {
        return DriverManager.getConnection(url, userName, password);
    }

    public void insertRows(Connection connection) {
        try {
            Statement st = connection.createStatement();
            st.executeUpdate("DROP TABLE IF EXISTS TEST");
            st.executeUpdate("CREATE TABLE TEST (FIELD INT)");
            st.execute("CREATE OR REPLACE FUNCTION insertCount(n INTEGER) RETURNS VOID AS $$\n" +
                    "DECLARE c INTEGER := 1;\n" +
                    "BEGIN\n" +
                    "  LOOP\n" +
                    "    IF c > n THEN\n" +
                    "      EXIT;\n" +
                    "    END IF;\n" +
                    "    INSERT INTO test (field) VALUES (c);\n" +
                    "    c := c + 1;\n" +
                    "  END LOOP;\n" +
                    "END;\n" +
                    "$$ LANGUAGE plpgsql;");
            PreparedStatement statement = connection.prepareStatement("SELECT insertcount(?)");
            statement.setInt(1, maxCount);
            statement.execute();
        } catch (SQLException e) {
            for (Throwable t : e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws ClassNotFoundException {
        String myURL = "localhost:5432/sqlite";
        String myUser = "postgres";
        String myPassword = "<PASSWORD>";
        int count = 1_000_000;
        Optimization opti = new Optimization();
        opti.setUrl(myURL);
        opti.setUserName(myUser);
        opti.setPassword(<PASSWORD>);
        opti.setMaxCount(count);
        try (Connection con = opti.getConnection()){
            opti.insertRows(con);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            for (Throwable t : e) {
                t.printStackTrace();
            }
        }
    }
}<commit_msg>Add SQL and JDBC<commit_after>package ru.job4j.jdbc;

import java.sql.*;

public class Optimization {
    private int maxCount;
    private String url;
    private String userName;
    private String password;

    public String getUrl() {
        return url;
    }

    public void setUrl(String serverUrl) {
        this.url = "jdbc:postgresql://" + serverUrl;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getMaxCount() {
        return maxCount;
    }

    public void setMaxCount(int maxCount) {
        this.maxCount = maxCount;
    }

    public Connection getConnection() throws ClassNotFoundException, SQLException {
        return DriverManager.getConnection(url, userName, password);
    }

    public void insertRowsFunc(Connection connection) {
        try {
            Statement st = connection.createStatement();
            st.executeUpdate("DROP TABLE IF EXISTS TEST");
            st.executeUpdate("CREATE TABLE TEST (FIELD INT)");
            st.execute("CREATE OR REPLACE FUNCTION insertCount(n INTEGER) RETURNS VOID AS $$\n" +
                    "DECLARE c INTEGER := 1;\n" +
                    "BEGIN\n" +
                    "  LOOP\n" +
                    "    IF c > n THEN\n" +
                    "      EXIT;\n" +
                    "    END IF;\n" +
                    "    INSERT INTO test (field) VALUES (c);\n" +
                    "    c := c + 1;\n" +
                    "  END LOOP;\n" +
                    "END;\n" +
                    "$$ LANGUAGE plpgsql;");
            PreparedStatement statement = connection.prepareStatement("SELECT insertcount(?)");
            statement.setInt(1, maxCount);
            statement.execute();
        } catch (SQLException e) {
            for (Throwable t : e) {
                t.printStackTrace();
            }
        }
    }

    public void insertRowsBatch(Connection con) {
        try {
            boolean autoCommit = con.getAutoCommit();
            con.setAutoCommit(false);
            Statement statement = con.createStatement();
            statement.executeUpdate("DROP TABLE IF EXISTS TEST");
            statement.executeUpdate("CREATE TABLE TEST (FIELD INT)");
            String command = "INSERT INTO test (field) VALUES (%s)";
            for (int i = 1; i < maxCount; i++) {
                statement.addBatch(String.format(command, i));
            }
            int[] counts = statement.executeBatch();
            if (counts.length != maxCount) {
                con.rollback();
            } else {
                con.commit();
            }
            con.setAutoCommit(autoCommit);
        } catch (SQLException e) {
            for (Throwable t : e) {
                t.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws ClassNotFoundException {
        String myURL = "localhost:5432/sqlite";
        String myUser = "postgres";
        String myPassword = "<PASSWORD>";
        int count = 1_000_000;
        Optimization opti = new Optimization();
        opti.setUrl(myURL);
        opti.setUserName(myUser);
        opti.setPassword(<PASSWORD>);
        opti.setMaxCount(count);
        try (Connection con = opti.getConnection()) {
            //opti.insertRowsFunc(con);
            opti.insertRowsBatch(con);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            for (Throwable t : e) {
                t.printStackTrace();
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.10488026674749924, 'fraction_numerical': 0.004698393452561382, 'mean_word_length': 1.6608870967741935, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, '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': '1088356', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1736, 'n_words': 437}