package org.springblade.mongodb;

import org.springframework.jdbc.datasource.DriverManagerDataSource;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class Main {
    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mongodb.jdbc.MongoDriver";
    //   static final String URL = "jdbc:mongodb://mhuser:pencil@localhost:27017/admin";
    static final String URL = "jdbc:mongodb://192.168.0.251:27017/etl";
    private static final TimeZone UTC = TimeZone.getTimeZone("UTC");

    // Data used for test, in the test.test and test2.test collections:
    //
    //{ "values" : [
    //    { "database" : "myDB", "table" : "foo", "tableAlias" : "foo", "column" : "a", "columnAlias" : "a", "value" : 1 },
    //    { "database" : "myDB", "table" : "foo", "tableAlias" : "foo", "column" : "b", "columnAlias" : "b", "value" : "hello" } ]
    //    }
    //{ "values" : [
    //    { "database" : "myDB", "table" : "foo", "tableAlias" : "foo", "column" : "a", "columnAlias" : "a", "value" : 42 },
    //    { "database" : "myDB", "table" : "foo", "tableAlias" : "foo", "column" : "b", "columnAlias" : "b", "value" : "hello 2" } ]
    //    }
    //
    public static void main(String[] args) {

        try {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName("com.mongodb.jdbc.MongoDriver");
            dataSource.setUrl("jdbc:mongodb://containers-us-west-125.railway.app:7555");
            dataSource.setSchema("enchere");
            dataSource.setUsername("mongo");
            dataSource.setPassword("ClIdcQNJCHBXLx6Td3nJ");
            Connection connection = dataSource.getConnection();

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void displayResultSet(ResultSet rs) throws java.sql.SQLException {
        Calendar c = new GregorianCalendar();
        c.setTimeZone(UTC);
        while (rs.next()) {
            //Retrieve by column name
            double a = rs.getDouble("a");
            String as = rs.getString("a");
            String b = rs.getString("b");
            java.sql.Timestamp bd;
            try {
                bd = rs.getTimestamp("b", c);
                System.out.println("b as a Timestamp is: " + bd);
            } catch (SQLException e) {
                System.out.println(e);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            ResultSetMetaData metaData = rs.getMetaData();
            System.out.println("a is: " + a + " as double"
                    + " b is: " + b + " as string");
            System.out.println("a as a string is: " + as);
        }
    }
}