All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.asteraether.tomlib.sqlib.SQLFullDatabase Maven / Gradle / Ivy

The newest version!
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.github.asteraether.tomlib.sqlib;

import com.github.asteraether.tomlib.sqlib.exceptions.PropertyNotFoundException;
import com.github.asteraether.tomlib.sqlib.exceptions.SQLConnectionPresentException;
import com.github.asteraether.tomlib.sqlib.exceptions.SQLNoConnectionException;
import com.github.asteraether.tomlib.sqlib.exceptions.SQLPreparedQueryNotDefinedException;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author Thomas
 * @param 
 * @param 
 */
public class SQLFullDatabase extends SQLDatabase {

    private CachedStatementConnection cachConn;
    private final PreparedStatementConnection prepConn;
    private final CallableStatementConnection callConn;

    /**
     *
     * @param driverClass
     * @param driver
     * @param queryStatements
     * @param callStatements
     * @throws ClassNotFoundException
     */
    public SQLFullDatabase(String driverClass, String driver, List queryStatements, List callStatements) throws ClassNotFoundException {
        super(driverClass, driver);
        List l = new LinkedList<>();
        queryStatements.stream().forEach((s) -> l.add(s));
        prepConn = new PreparedStatementConnection<>(null, l);
        l.clear();
        callStatements.stream().forEach(s -> l.add(s));
        callConn = new CallableStatementConnection<>(null, l);
    }

    /**
     *
     * @param prop
     * @param queryStatements
     * @param callStatements
     * @throws ClassNotFoundException
     */
    public SQLFullDatabase(SQLDatabaseProperties prop, List queryStatements, List callStatements) throws ClassNotFoundException {
        super(prop);
        List l = new LinkedList<>();
        queryStatements.stream().forEach((s) -> l.add(s));
        prepConn = new PreparedStatementConnection<>(null, l);
        l.clear();
        callStatements.stream().forEach(s -> l.add(s));
        callConn = new CallableStatementConnection<>(null, l);
    }

    /**
     *
     * @param driverClass
     * @param driver
     * @param queryStatements
     * @param callStatements
     * @throws ClassNotFoundException
     */
    public SQLFullDatabase(String driverClass, String driver, T[] queryStatements, K[] callStatements) throws ClassNotFoundException {
        super(driverClass, driver);
        prepConn = new PreparedStatementConnection<>(null, queryStatements);
        callConn = new CallableStatementConnection<>(null, callStatements);
    }

    /**
     *
     * @param prop
     * @param queryStatements
     * @param callStatements
     * @throws ClassNotFoundException
     */
    public SQLFullDatabase(SQLDatabaseProperties prop, T[] queryStatements, K[] callStatements) throws ClassNotFoundException {
        super(prop);
        prepConn = new PreparedStatementConnection<>(null, queryStatements);
        callConn = new CallableStatementConnection<>(null, callStatements);
    }

    /**
     *
     * @param key
     * @return
     * @throws SQLNoConnectionException
     * @throws SQLPreparedQueryNotDefinedException
     * @throws SQLException
     */
    public PreparedStatement getPreparedStatement(T key) throws SQLNoConnectionException, SQLPreparedQueryNotDefinedException, SQLException {
        return prepConn.getPreparedStatement(key);
    }

    /**
     *
     * @param key
     * @return
     * @throws SQLNoConnectionException
     * @throws SQLPreparedQueryNotDefinedException
     * @throws SQLException
     */
    public ResultSet executePreparedQuery(T key) throws SQLNoConnectionException, SQLPreparedQueryNotDefinedException, SQLException {
        return prepConn.executeQuery(key);
    }

    /**
     *
     * @param key
     * @return
     * @throws SQLNoConnectionException
     * @throws SQLPreparedQueryNotDefinedException
     * @throws SQLException
     */
    public int executePreparedUpdate(T key) throws SQLNoConnectionException, SQLPreparedQueryNotDefinedException, SQLException {
        return prepConn.executeUpdate(key);
    }

    /**
     *
     * @param key
     * @param index
     * @param values
     * @return
     * @throws SQLNoConnectionException
     * @throws SQLPreparedQueryNotDefinedException
     * @throws SQLException
     */
    public int executePreparedUpdate(T key, int[] index, Object[] values) throws SQLNoConnectionException, SQLPreparedQueryNotDefinedException, SQLException {
        return prepConn.executeUpdate(key, index, values);
    }

    /**
     *
     * @param key
     * @param values
     * @return
     * @throws SQLNoConnectionException
     * @throws SQLPreparedQueryNotDefinedException
     * @throws SQLException
     */
    public int executePreparedUpdate(T key, Object... values) throws SQLNoConnectionException, SQLPreparedQueryNotDefinedException, SQLException {
        return prepConn.executeUpdate(key, values);
    }

    /**
     *
     * @param key
     * @param values
     * @return
     * @throws SQLNoConnectionException
     * @throws SQLPreparedQueryNotDefinedException
     * @throws SQLException
     */
    public ResultSet executePreparedQuery(T key, Map values) throws SQLNoConnectionException, SQLPreparedQueryNotDefinedException, SQLException {
        return prepConn.executeQuery(key, values);
    }

    /**
     *
     * @param key
     * @param index
     * @param values
     * @return
     * @throws SQLNoConnectionException
     * @throws SQLPreparedQueryNotDefinedException
     * @throws SQLException
     */
    public ResultSet executePreparedQuery(T key, int[] index, Object[] values) throws SQLNoConnectionException, SQLPreparedQueryNotDefinedException, SQLException {
        return prepConn.executeQuery(key, index, values);
    }

    /**
     *
     * @param key
     * @param values
     * @return
     * @throws SQLNoConnectionException
     * @throws SQLPreparedQueryNotDefinedException
     * @throws SQLException
     */
    public ResultSet executePreparedQuery(T key, Object... values) throws SQLNoConnectionException, SQLPreparedQueryNotDefinedException, SQLException {
        return prepConn.executeQuery(key, values);
    }

    /**
     *
     * @param key
     * @return
     * @throws SQLNoConnectionException
     * @throws SQLPreparedQueryNotDefinedException
     * @throws SQLException
     */
    public boolean executePrepared(T key) throws SQLNoConnectionException, SQLPreparedQueryNotDefinedException, SQLException {
        return prepConn.execute(key);
    }

    /**
     *
     * @param key
     * @return
     * @throws SQLException
     */
    public boolean standardExecute(ISQLKey key) throws SQLException {
        Statement s = getStatement();
        boolean b = s.execute(key.getSQL());
        releaseStatement(s);
        return b;
    }

    /**
     *
     * @param key
     * @return
     * @throws SQLException
     */
    public ResultSet standardExecuteQuery(ISQLKey key) throws SQLException {
        Statement s = getStatement();
        ResultSet b = s.executeQuery(key.getSQL());
        releaseStatement(s);
        return b;
    }

    /**
     *
     * @param key
     * @return
     * @throws SQLException
     */
    public int standardExecuteUpdate(ISQLKey key) throws SQLException {
        Statement s = getStatement();
        int b = s.executeUpdate(key.getSQL());
        releaseStatement(s);
        return b;
    }

    /**
     *
     * @return
     */
    public Set getPreparedStatementKeySet() {
        return prepConn.getStatementKeySet();
    }

    /**
     *
     * @return
     */
    public Set getCallableStatementKeySet() {
        return callConn.getStatementKeySet();
    }

    /**
     *
     * @return @throws SQLNoConnectionException
     * @throws SQLException
     */
    public Statement getStatement() throws SQLNoConnectionException, SQLException {
        if (cachConn == null || conn.isClosed()) {
            throw new SQLNoConnectionException("No connection present");
        }
        return cachConn.getStatement();
    }

    /**
     *
     * @param stat
     * @throws SQLNoConnectionException
     * @throws SQLException
     */
    public void releaseStatement(Statement stat) throws SQLNoConnectionException, SQLException {
        if (cachConn == null || conn.isClosed()) {
            throw new SQLNoConnectionException("No connection present");
        }
        cachConn.releaseStatement(stat);
    }

    /**
     *
     * @param key
     * @return
     * @throws SQLNoConnectionException
     * @throws SQLPreparedQueryNotDefinedException
     * @throws SQLException
     */
    public CallableStatement getCallableStatement(K key) throws SQLNoConnectionException, SQLPreparedQueryNotDefinedException, SQLException {
        return callConn.getCallableStatement(key);
    }

    /**
     *
     * @param key
     * @param outType
     * @param params
     * @return
     * @throws SQLNoConnectionException
     * @throws SQLPreparedQueryNotDefinedException
     * @throws SQLException
     */
    public Object executeCall(K key, int outType, Object... params) throws SQLNoConnectionException, SQLPreparedQueryNotDefinedException, SQLException {
        return callConn.executeCall(key, outType, params);
    }

    /**
     *
     * @param key
     * @param outputtype
     * @param params
     * @return
     * @throws SQLNoConnectionException
     * @throws SQLPreparedQueryNotDefinedException
     * @throws SQLException
     */
    public Object executeTypeSpecificCall(ISQLKey key, int outputtype, Map.Entry... params) throws SQLNoConnectionException, SQLPreparedQueryNotDefinedException, SQLException {
        return callConn.executeTypeSpecificCall(key, outputtype, params);
    }

    /**
     *
     * @throws SQLException
     */
    @Override
    public void disconnect() throws SQLException {
        prepConn.closeStatements();
        prepConn.setConnection(null);
        callConn.closeStatements();
        callConn.setConnection(null);
        if (cachConn != null) {
            cachConn.closeStatements();
            cachConn = null;
        }
        super.disconnect();
    }

    /**
     *
     * @throws SQLException
     * @throws PropertyNotFoundException
     * @throws SQLConnectionPresentException
     */
    @Override
    public void connect() throws SQLException, PropertyNotFoundException, SQLConnectionPresentException {
        super.connect();
        cachConn = new CachedStatementConnection(conn);
        prepConn.setConnection(conn);
        callConn.setConnection(conn);
    }

    /**
     *
     * @param prop
     * @throws SQLException
     * @throws PropertyNotFoundException
     * @throws SQLConnectionPresentException
     */
    @Override
    public void connect(SQLDatabaseProperties prop) throws SQLException, PropertyNotFoundException, SQLConnectionPresentException {
        super.connect(prop);
        cachConn = new CachedStatementConnection(conn);
        prepConn.setConnection(conn);
        callConn.setConnection(conn);
    }

    /**
     *
     * @param host
     * @param username
     * @param password
     * @throws SQLException
     * @throws SQLConnectionPresentException
     */
    @Override
    public void connect(String host, String username, String password) throws SQLException, SQLConnectionPresentException {
        super.connect(host, username, password);
        cachConn = new CachedStatementConnection(conn);
        prepConn.setConnection(conn);
        callConn.setConnection(conn);
    }

    /**
     *
     * @param hostname
     * @param dbName
     * @param username
     * @param password
     * @throws SQLException
     * @throws SQLConnectionPresentException
     */
    @Override
    public void connect(String hostname, String dbName, String username, String password) throws SQLException, SQLConnectionPresentException {
        super.connect(hostname, dbName, username, password);
        cachConn = new CachedStatementConnection(conn);
        prepConn.setConnection(conn);
        callConn.setConnection(conn);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy