package com.krux.stdlib.utils;

import com.krux.stdlib.KruxStdLib;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A collection of utility methods to interact with the Krux Console RDS instance.
 *
 * @author Vivek S. Vaidya
 */
public class DbUtils {

    /**
     * Get a database connection for the specified environment.
     *
     * @return the database connection if one could be established; null otherwise
     */
    private static final Logger log = LoggerFactory.getLogger(DbUtils.class.getName());

    public static Connection getDbConnection() {
        ConfigProperties kprops = new ConfigProperties();
        Connection conn = null;
        String errorMsg = null;
        long start = System.currentTimeMillis();
        try {
            Class.forName(kprops.getJdbcDriver());
            conn = DriverManager.getConnection(kprops.getJdbcUrl(), kprops.getJdbcUser(),
                    kprops.getJdbcPassword());
        } catch (Exception ex) {
            errorMsg = ex.getMessage();
            conn = null;
        }
        long time = System.currentTimeMillis() - start;
        log.info("DB connection took " + time + "ms for whole request");
        KruxStdLib.STATSD.time(KruxStdLib.APP_NAME + "_HTTP_200", time);
        return conn;
    }

    /**
     * Close the specified database connection.
     *
     * @param conn the database connection to close.
     */
    public static void close(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException sqe) {
                // an error occured while closing the connection.
                // there's really not much we can do at this point, so we eat the exception and return
            }
        }
    }

    /**
     * Close the specified (Prepared) Statement
     *
     * @param ps the statement to close
     */
    public static void close(Statement ps) {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException sqe) {
                // an error occured while closing the statement
                // there's really not much we can do at this point, so we eat the exception and return
            }
        }
    }

    /**
     * Close the specified result set.
     *
     * @param rs the result set to close
     */
    public static void close(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException sqe) {
                // an error occured while closing the statement
                // there's really not much we can do at this point, so we eat the exception and return
            }
        }
    }
}

@@ -809,40 +809,8 @@
 ll;%0A
-        String errorMsg = null;%0A
     
@@ -1101,16 +1101,23 @@
         
+String 
 errorMsg
@@ -1132,24 +1132,68 @@
 tMessage();%0A
+            log.error(%22db_util%22, errorMsg);%0A
             
@@ -1292,16 +1292,20 @@
 nfo(%22DB 
+get 
 connecti
@@ -1330,26 +1330,9 @@
  %22ms
- for whole request
+.
 %22);%0A
@@ -1390,16 +1390,19 @@
 + %22_
-HTTP_200
+get_db_conn
 %22, t
@@ -1603,24 +1603,73 @@
 ion conn) %7B%0A
+        long start = System.currentTimeMillis();%0A
         if (
@@ -1905,35 +1905,35 @@
 is point, so we 
-eat
+log
  the exception a
@@ -1950,35 +1950,331 @@
             
-%7D%0A        %7D
+    String errorMsg = sqe.getMessage();%0A                log.error(%22db_util%22, errorMsg);%0A            %7D%0A        %7D%0A        long time = System.currentTimeMillis() - start;%0A        log.info(%22DB connection close took %22 + time + %22ms.%22);%0A        KruxStdLib.STATSD.time(KruxStdLib.APP_NAME + %22_close_db_conn%22, time);
 %0A    %7D%0A%0A    
@@ -2421,24 +2421,73 @@
 ement ps) %7B%0A
+        long start = System.currentTimeMillis();%0A
         if (
@@ -2717,35 +2717,35 @@
 is point, so we 
-eat
+log
  the exception a
@@ -2762,35 +2762,336 @@
             
-%7D%0A        %7D
+    String errorMsg = sqe.getMessage();%0A                log.error(%22db_util%22, errorMsg);%0A            %7D%0A        %7D%0A        long time = System.currentTimeMillis() - start;%0A        log.info(%22DB PreparedStatement close took %22 + time + %22ms.%22);%0A        KruxStdLib.STATSD.time(KruxStdLib.APP_NAME + %22_close_db_ps%22, time);
 %0A    %7D%0A%0A    
@@ -3230,24 +3230,73 @@
 ltSet rs) %7B%0A
+        long start = System.currentTimeMillis();%0A
         if (
@@ -3538,11 +3538,11 @@
  we 
-eat
+log
  the
@@ -3571,32 +3571,325 @@
             
-%7D%0A        %7D
+    String errorMsg = sqe.getMessage();%0A                log.error(%22db_util%22, errorMsg);%0A            %7D%0A        %7D%0A        long time = System.currentTimeMillis() - start;%0A        log.info(%22DB ResultSet close took %22 + time + %22ms.%22);%0A        KruxStdLib.STATSD.time(KruxStdLib.APP_NAME + %22_close_db_rs%22, time);
 %0A    %7D%0A%7D%0A

add stats for close conn, close ps, and close rs.