/*
 * This file is part of Total Economy, licensed under the MIT License (MIT).
 *
 * Copyright (c) Eric Grandt <https://www.ericgrandt.com>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.erigitic.sql;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Optional;

public class SQLQuery {
    private String statement;
    private DataSource dataSource;
    private ResultSet resultSet;

    private SQLQuery(SQLQueryBuilder builder) {
        statement = builder.statement;
        dataSource = builder.dataSource;

        resultSet = getResultSet();
    }

    private ResultSet getResultSet() {
        try {
            Connection conn = dataSource.getConnection();

            Optional<ResultSet> resultSetOpt = Optional.of(conn.prepareStatement(statement).executeQuery());

            if (resultSetOpt.isPresent()) {
                return resultSetOpt.get();
            }

            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        throw new NullPointerException("No result set present");
    }

    public boolean getBoolean() {
        try {
            if (resultSet.next())
                return resultSet.getBoolean(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        throw new NullPointerException("Could not get boolean from ResultSet");
    }

    public static class SQLQueryBuilder {
        private DataSource dataSource;
        private String statement = "";

        public SQLQueryBuilder(DataSource dataSource) {
            this.dataSource = dataSource;
        }

        public SQLQueryBuilder select(String column) {
            statement += "SELECT " + column;

            return this;
        }

        public SQLQueryBuilder from(String table) {
            statement += " FROM " + table;

            return this;
        }

        public SQLQueryBuilder where(String comp) {
            statement += " WHERE " + comp;

            return this;
        }

        public SQLQueryBuilder equals(String compVal) {
            statement += "=" + compVal;

            return this;
        }

        public SQLQueryBuilder and(String comp) {
            statement += " AND " + comp;

            return this;
        }

        public SQLQueryBuilder insert(String table) {
            statement += "INSERT IGNORE INTO " + table;

            return this;
        }

        public SQLQueryBuilder columns(String... columns) {
            String columnsJoined = String.join(",", columns);
            statement += " (" + columnsJoined + ")";

            return this;
        }

        public SQLQueryBuilder values(String... values) {
            // Join all the values with a comma deliminator and surround with ()
            String valuesJoined = "('" + String.join("','", values) + "')";

            statement += "VALUES " + valuesJoined;

            return this;
        }

        public SQLQuery build() {
            return new SQLQuery(this);
        }
    }
}

@@ -1557,24 +1557,16 @@
 QLQuery(
-SQLQuery
 Builder 
@@ -1669,77 +1669,175 @@
     
-resultSet = getResultSet();%0A    %7D%0A%0A    private ResultSet getResultSet
+executeQuery();%0A    %7D%0A%0A    public static SQLQuery.Builder builder(DataSource dataSource) %7B%0A        return new Builder(dataSource);%0A    %7D%0A%0A    private void executeQuery
 () %7B
@@ -2074,36 +2074,41 @@
               re
-turn
+sultSet =
  resultSetOpt.ge
@@ -2235,72 +2235,236 @@
   %7D%0A
-%0A        throw new NullPointerException(%22No result set present%22)
+    %7D%0A%0A    public boolean recordExists() %7B%0A        try %7B%0A            if (resultSet.isBeforeFirst())%0A                return true;%0A        %7D catch (SQLException e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        return false
 ;%0A  
@@ -2788,24 +2788,16 @@
 c class 
-SQLQuery
 Builder 
@@ -2888,32 +2888,24 @@
      public 
-SQLQuery
 Builder(Data
@@ -2989,32 +2989,24 @@
      public 
-SQLQuery
 Builder sele
@@ -3118,32 +3118,24 @@
      public 
-SQLQuery
 Builder from
@@ -3242,32 +3242,24 @@
      public 
-SQLQuery
 Builder wher
@@ -3366,32 +3366,24 @@
      public 
-SQLQuery
 Builder equa
@@ -3430,16 +3430,17 @@
 nt += %22=
+'
 %22 + comp
@@ -3442,16 +3442,22 @@
  compVal
+ + %22'%22
 ;%0A%0A     
@@ -3498,32 +3498,24 @@
      public 
-SQLQuery
 Builder and(
@@ -3618,32 +3618,24 @@
      public 
-SQLQuery
 Builder inse
@@ -3757,32 +3757,24 @@
      public 
-SQLQuery
 Builder colu
@@ -3965,24 +3965,16 @@
  public 
-SQLQuery
 Builder 

Update SQLQuery builder code