package leigh.example.calcite;

import leigh.example.calcite.adapter.ExampleSchema;
import leigh.jdbc.JDBCUtil;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import org.apache.calcite.adapter.java.ReflectiveSchema;
import org.apache.calcite.adapter.jdbc.JdbcSchema;
import org.apache.calcite.jdbc.CalciteConnection;
import org.apache.calcite.schema.Schema;
import org.apache.calcite.schema.SchemaPlus;
import org.apache.commons.dbcp.BasicDataSource;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class Example {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(Example.class);

    public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException {
        Class.forName("org.apache.calcite.jdbc.Driver");
        Properties info = new Properties();
        info.setProperty("lex", "JAVA");


        Properties p = new Properties();
        p.load(new FileInputStream("test.properties"));
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setUrl("jdbc:mysql://db1.leigh-co.com/aleigh_test");
        dataSource.setUsername("aleigh");
        dataSource.setPassword(String.valueOf(p.get("db1_password")));

        try (Connection con = DriverManager.getConnection("jdbc:calcite:", info);
             CalciteConnection calciteConnection = con.unwrap(CalciteConnection.class)) {


            SchemaPlus rootSchema = calciteConnection.getRootSchema();
            rootSchema.add("hr", new ReflectiveSchema(new HrSchema()));
            Schema jdbcSchema = JdbcSchema.create(rootSchema, "test", dataSource, null, "aleigh_test");
            rootSchema.add("test", jdbcSchema);
            rootSchema.add("example", new ExampleSchema(calciteConnection.getTypeFactory()));

            /*
            try (Statement stmt = calciteConnection.createStatement()) {
                stmt.executeQuery("insert into hr.emps (id,firstName,lastName) values (3,'Bob','Smith')");
            }
             */

            /*
            try (Statement stmt = calciteConnection.createStatement()) {
                try (ResultSet rs = stmt.executeQuery("SELECT * FROM example.clock")) {
                    JDBCUtil.printResultSet(logger, rs);
                }
            }

             */

            try (Statement stmt = calciteConnection.createStatement()) {
                stmt.execute("insert into example.box (msg) values ('Its Raining')");
            }

            try (Statement stmt = calciteConnection.createStatement()) {
                stmt.execute("insert into example.box (msg) values ('Its Raining')");
            }


            try (Statement stmt = calciteConnection.createStatement()) {
                try (ResultSet rs = stmt.executeQuery("SELECT * FROM example.box")) {
                    JDBCUtil.printResultSet(logger, rs);
                }
            }

/*
            try (Statement stmt = calciteConnection.createStatement()) {
                try (ResultSet rs = stmt.executeQuery("SELECT * FROM hr.emps where upper(lastName) like 'WIN%'")) {
                    JDBCUtil.printResultSet(logger, rs);
                }
            }
*/

            /*
            try (Statement stmt = con.createStatement()) {
                stmt.execute("insert into test.discounts(id, title,expired_date,amount) values (0, 'calcite', '2021-04-23', 3.72)");
            }

             */
        }
    }
}