package data;

import util.Config;

import java.sql.*;
import java.util.Properties;

/**
 * Created by Peter Mösenthin.
 *
 * Class to handle the actual MySQL Database.
 */
public class MySQLConnection {


    private Connection mConnection;

    public static final String DEBUG_TAG = MySQLConnection.class.getSimpleName();

    public boolean open(){
        Properties connectionProps = new Properties();
        connectionProps.put("user", Config.MYSQL_USER);
        connectionProps.put("password", Config.MYSQL_PASSWORD);

        /*
        String url = Config.MYSQL_HOST + Config.MYSQL_DB_NAME
                + "?user=" + Config.MYSQL_USER
                + "?password=" + Config.MYSQL_PASSWORD;

        Log.add(DEBUG_TAG,"MySQLConnection with: " + url);
        */
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //mConnection = DriverManager.getConnection(url);
            mConnection = DriverManager.getConnection(
                    "jdbc:mysql://" +
                            Config.MYSQL_HOST +
                            ":" + Config.MYSQL_PORT + "/",
                    connectionProps);

        } catch(ClassNotFoundException e) {
            Log.add(DEBUG_TAG,"Could not open database: " + e);
            return false;

        } catch(SQLException e) {
            Log.add(DEBUG_TAG,"Could not open database: " + e);
            return false;
        }
        return true;
    }

    public void createDatabase(){
        Log.add(DEBUG_TAG,"Creating database");
        Statement statement = null;
        try {
            statement = mConnection.createStatement();
            statement.execute(
                    "CREATE DATABASE IF NOT EXISTS " + Config.MYSQL_DB_NAME);
        } catch (SQLException e) {
            Log.add(DEBUG_TAG, "Could not create database: " + e);
        }
    }

    public void selectDatabase(){
        Log.add(DEBUG_TAG,"selecting database");
        Statement statement = null;
        try {
            statement = mConnection.createStatement();
            statement.execute(
                    "USE " + Config.MYSQL_DB_NAME);
        } catch (SQLException e) {
            Log.add(DEBUG_TAG, "Could not select database: " + e);
        }
    }

    public void createTable(String name){
        Log.add(DEBUG_TAG,"Creating table");
        Statement statement = null;
        try {
            statement = mConnection.createStatement();
            statement.execute(
                    "CREATE TABLE IF NOT EXISTS " + name + "( temp INT )");
        } catch (SQLException e) {
            Log.add(DEBUG_TAG, "Could not create table: " + e);
        }
    }

    public void close(){
        Log.add(DEBUG_TAG,"Closing connection");
        try {
            mConnection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void insertTemp(int temp){
        Log.add(DEBUG_TAG,"Inserting temperature data");
        Statement statement = null;
        try {
            statement = mConnection.createStatement();
            statement.execute(
                    "INSERT INTO " + "test" + " (temp) VALUES (1.4),(2.5),(3.6)");
        } catch (SQLException e) {
            Log.add(DEBUG_TAG, "write test data: " + e);
        }
    }

    public void test() {
        Log.add(DEBUG_TAG,"Testing connection!");
        Statement statement = null;
        try {
            statement = mConnection.createStatement();
            statement.execute(
                    "INSERT INTO " + "test" + " (temp) VALUES (1.4),(2.5),(3.6)");
        } catch (SQLException e) {
            Log.add(DEBUG_TAG, "write test data: " + e);
        }
    }
}

@@ -317,16 +317,59 @@
 eName();
+%0A    public static final boolean D = false;
 %0A%0A    pu
@@ -1510,32 +1510,38 @@
 base()%7B%0A        
+if(D)%7B
 Log.add(DEBUG_TA
@@ -1555,32 +1555,33 @@
 ting database%22);
+%7D
 %0A        Stateme
@@ -1932,32 +1932,38 @@
 base()%7B%0A        
+if(D)%7B
 Log.add(DEBUG_TA
@@ -1982,24 +1982,25 @@
  database%22);
+%7D
 %0A        Sta
@@ -2337,32 +2337,38 @@
  name)%7B%0A        
+if(D)%7B
 Log.add(DEBUG_TA
@@ -2387,16 +2387,17 @@
 table%22);
+%7D
 %0A       
@@ -2742,32 +2742,38 @@
 lose()%7B%0A        
+if(D)%7B
 Log.add(DEBUG_TA
@@ -2796,16 +2796,17 @@
 ction%22);
+%7D
 %0A       
@@ -2968,32 +2968,38 @@
  temp)%7B%0A        
+if(D)%7B
 Log.add(DEBUG_TA
@@ -3030,16 +3030,17 @@
  data%22);
+%7D
 %0A       
@@ -3385,32 +3385,38 @@
 est() %7B%0A        
+if(D)%7B
 Log.add(DEBUG_TA
@@ -3440,16 +3440,17 @@
 tion!%22);
+%7D
 %0A       

Add debug flag