package org.mariadb.jdbc;

import java.sql.*;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class Test {
    static MariaDbDataSource dataSource = new MariaDbDataSource();
    static{
        try {
            dataSource.setUrl("jdbc:mariadb://localhost:3306/tt?useServerPrepStmts=true&cachePrepStmts=true"
                    + "&prepStmtCacheSize=5000");
            dataSource.setUser("root");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    public static Connection newConnection() throws SQLException {
        return dataSource.getConnection();
    }

    static final String sql = "select * from ( select tmp_table__.*,row_number() OVER(order by title_order_ asc,order_ asc) as tmp_rn__ from ( select id_,title_id_,enabled_show_,enabled_in_create_,enabled_in_select_,name_,order_,title_order_,desc_,sync_flag_,create_date_,create_user_,mod_date_,mod_user_ from ecl_portal1_group_ where title_id_ = ? and exists ( select 1 from ecl_portal1_item_ where exists( select 1 from ecl_portal1_item_auth_org_ where item_id_ = ecl_portal1_item_.id_ and key_ in ('selectAuth','handleAuth') and express_ in (?,?,?,?) ) and group_id_ = ecl_portal1_group_.id_ ) ) tmp_table__ ) tmp_table_rn__ where tmp_rn__ BETWEEN ? and ? order by tmp_rn__ ";
    public static void handleParam(PreparedStatement statement ) throws SQLException {
        int idx = 1;
        statement.setString(idx++,"1162533370687004673");
        statement.setString(idx++,"1171965500672323585");
        statement.setString(idx++,"1217283954274873345");
        statement.setString(idx++,"1169463060492431362");
        statement.setString(idx++,"1169463068423860228");
        statement.setInt(idx++,1);
        statement.setInt(idx++,10);
    }


    public static void main(String[] args) throws InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        final AtomicInteger idError = new AtomicInteger(0);
        final List<String> getRs = new CopyOnWriteArrayList<>();

    Runnable task =
        () -> {
          try (Connection connection = newConnection()) {
            for (int i = 0; i < 50; i++) {
              try (PreparedStatement statement = connection.prepareStatement(sql)) {
                handleParam(statement);
                try (ResultSet rs = statement.executeQuery()) {
                  while (rs.next()) {

                    String row = rs.getString("id_");
                    if (row == null || row.length() == 0) {
                      System.out.println(
                          "thread:" + ((MariaDbConnection) connection).getServerThreadId());
                        throw new SQLException("ERROR");
                    }
                    getRs.add(row);
                  }
                }
              }
            }
          } catch (Exception e) {
            e.printStackTrace();
          }
        };

        executor.execute(task);
        executor.execute(task);
        executor.execute(task);

        executor.shutdown();
        executor.awaitTermination(40, TimeUnit.SECONDS);

    }
}
