package com.logikz.dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


/**
 * Created by Nick on 6/13/2015.
 */
public class PostgresDAO {
    private static final Logger LOG = LoggerFactory.getLogger( PostgresDAO.class );

    private static PostgresDAO INSTANCE = getInstance();

    public static PostgresDAO getInstance() {
        if ( INSTANCE == null ) {
            INSTANCE = new PostgresDAO();
        }

        return INSTANCE;
    }

    private PostgresDAO() {
    }

    public void setToken( String stateId, String token, Connection connection ) throws URISyntaxException, SQLException, ClassNotFoundException {
        LOG.trace( "Set auth token" );

        Statement statement = connection.createStatement();
        statement.executeQuery( "DROP TABLE auth" );
        statement.executeQuery( "CREATE TABLE auth(stateid VARCHAR(50), token VARCHAR(500))" );
        statement.executeUpdate( "INSERT INTO auth VALUES ('" + stateId + "', '" + token + "')" );

    }

    public String getToken( String stateId, Connection connection ) throws URISyntaxException, SQLException, ClassNotFoundException {
        LOG.trace( "Get auth token" );
        //dump();

        Statement statement = connection.createStatement();
        String sql = "SELECT token FROM auth WHERE stateid='" + stateId + "'";
        LOG.trace( sql );
        try ( ResultSet resultSet = statement.executeQuery( sql ) ) {
            if ( resultSet.next() ) {
                LOG.trace( "Auth token: " + resultSet.getString( 1 ) );
                return resultSet.getString( 1 );
            }
        }

        return null;
    }

    private void dump( Connection connection ) throws ClassNotFoundException, SQLException, URISyntaxException {
        Statement statement = connection.createStatement();
        try ( ResultSet resultSet = statement.executeQuery( "SELECT * FROM auth" ) ) {
            while ( resultSet.next() ) {
                LOG.trace( "----ROW----" );
                LOG.trace( resultSet.getString( 0 ) );
                LOG.trace( resultSet.getString( 1 ) );
            }
        }
    }
}

@@ -132,102 +132,9 @@
 sql.
-Connection;%0Aimport java.sql.ResultSet;%0Aimport java.sql.SQLException;%0Aimport java.sql.Statement
+*
 ;%0A%0A%0A
@@ -734,37 +734,16 @@
 );%0A%0A
+%0A
        
- Statement statement =
  con
@@ -742,37 +742,38 @@
      connection.
-creat
+prepar
 eStatement();%0A  
@@ -771,256 +771,379 @@
 ent(
-);%0A        statement.executeQuery( %22DROP TABLE auth%22 );%0A        statement.executeQuery( %22CREATE TABLE auth(stateid VARCHAR(50), token VARCHAR(500))%22 );%0A        statement.executeUpdate( %22INSERT INTO auth VALUES ('%22 + stateId + %22', '%22 + token + %22')%22 
+ %22DROP TABLE auth%22 ).executeQuery();%0A        connection.prepareStatement( %22CREATE TABLE auth(stateid VARCHAR(50), token VARCHAR(500))%22 ).executeQuery();%0A        PreparedStatement statement = connection.prepareStatement( %22INSERT INTO auth VALUES (?, ?%22 );%0A        statement.setString( 1, stateId );%0A        statement.setString( 2, token );%0A        statement.executeUpdate(
 );%0A%0A
@@ -1341,32 +1341,40 @@
 ump();%0A%0A        
+Prepared
 Statement statem
@@ -1382,37 +1382,38 @@
 nt = connection.
-creat
+prepar
 eStatement();%0A  
@@ -1411,31 +1411,8 @@
 ent(
-);%0A        String sql =
  %22SE
@@ -1450,54 +1450,58 @@
 eid=
-'%22 + stateId + %22'%22;%0A        LOG.trace( sql
+?%22 );%0A        statement.setString( 1, stateId
  );%0A
+%0A
     
@@ -1559,13 +1559,8 @@
 ery(
- sql 
 ) ) 
@@ -1894,16 +1894,24 @@
         
+Prepared
 Statemen
@@ -1939,13 +1939,14 @@
 ion.
-creat
+prepar
 eSta
@@ -1952,18 +1952,39 @@
 atement(
+ %22SELECT * FROM auth%22 
 )
-;
 %0A       
@@ -2035,38 +2035,16 @@
 teQuery(
- %22SELECT * FROM auth%22 
 ) ) %7B%0A  

Change to prepared statements