package com.loafle.overflow.sql;

import com.loafle.overflow.crawler.Crawler;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by insanity on 17. 4. 11.
 */
public class SQLCrawler extends Crawler {

    public SQLCrawler() {}

    public Object get(String id) {
        TargetInfo info = getTargetInfo(id);
        return getMetrics(info);
    }

    private TargetInfo getTargetInfo(String id) {
        TargetInfo info = new TargetInfo();

//        //MySQL
        info.setUrl("jdbc:mysql://192.168.1.215:3306");
        info.setId("root");
        info.setPw("qwe123");
        info.setSSL(false);
        info.setQuery("show session status");
//        //MSSQL
//        info.setUrl("jdbc:sqlserver://192.168.1.106:1433");
//        info.setId("sa");
//        info.setPw("qwe123");
//        info.setSSL(false);
//        info.setQuery("select * from master.dbo.sysprocesses");
//        //PGSQL
//        info.setUrl("jdbc:postgresql://192.168.1.107:5432/postgres");
//        info.setId("postgres");
//        info.setPw("!@#$qwer1234");
//        info.setSSL(false);
//        info.setQuery("select * from pg_stat_activity");
//        //Oracle
//        info.setUrl("jdbc:oracle:thin:@192.168.1.30:1521/oracle.loafle.com");
//        info.setId("sys as sysdba");
//        info.setPw("qwer5795QWER");
//        info.setSSL(false);
//        info.setQuery("select * from v$sysstat");

        return info;
    }

    private List<Map<String, String>> getMetrics(TargetInfo info) {

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {

            conn = DriverManager.getConnection(info.getUrl(),info.getId(), info.getPw());
            stmt = conn.createStatement();

            rs = stmt.executeQuery(info.getQuery());

            ResultSetMetaData md = rs.getMetaData();
            int cnt = md.getColumnCount();

            List<DBMetaInfo> metaList = new ArrayList<DBMetaInfo>();

            for (int i =0 ; i < cnt ; ++i) {
                DBMetaInfo o = new DBMetaInfo();
                o.setName(md.getColumnName(i+1));
                metaList.add(o);
            }

            List<Map<String, String>> datas = new ArrayList<Map<String, String>>();
            while(rs.next()){
                Map<String, String> row = new HashMap<String, String>();
                for (DBMetaInfo o : metaList) {
                    row.put(o.getName(), rs.getString(o.getName()));
                }
                datas.add(row);
            }

            return datas;

        } catch (Exception e) {

            e.printStackTrace();

        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }

        return null;
    }

}
