package com.palacesun.engine.spy;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class P6RegisterDriver {
   public static void register(String url) {
      Properties properties = new Properties();

      try {
         InputStream input = P6RegisterDriver.class.getResourceAsStream("/drivers.properties");
         properties.load(input);
         input.close();
      } catch (IOException ex) {
         ex.printStackTrace();
      }

      try {
         String drivers = "";
         if (url.contains("mysql")) {
            drivers = properties.getProperty("drivers.mysql");
         } else if (url.contains("postgresql")) {
            drivers = properties.getProperty("drivers.postgresql");
         } else if (url.contains("oracle")) {
            drivers = properties.getProperty("drivers.oracle");
         } else if (url.contains("sqlserver")) {
            drivers = properties.getProperty("drivers.sqlserver");
         } else if (url.contains("kingbase")) {
            drivers = properties.getProperty("drivers.kingbase");
         } else if (url.contains("opengauss")) {
            drivers = properties.getProperty("drivers.opengauss");
         } else if (url.contains("dm")) {
            drivers = properties.getProperty("drivers.dm");
         } else if (url.contains("greenplum")) {
            drivers = properties.getProperty("drivers.greenplum");
         } else if (url.contains("db2")) {
            drivers = properties.getProperty("drivers.db2");
         } else if (url.contains("mariadb")) {
            drivers = properties.getProperty("drivers.mariadb");
         } else if (url.contains("hive") || url.contains("transwarp2") || url.contains("inceptor2")) {
            drivers = properties.getProperty("drivers.hive");
         }

         String[] driverList = drivers.split(",");

         for(int i = 0; i < driverList.length; ++i) {
            Class.forName(driverList[i]);
         }

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

   public static void main(String[] args) {
      register("postgresql");
      System.out.println("1");
   }
}
