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

com.github.asteraether.tomlib.sqlib.SQLDatabase 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 java.sql.Connection;
import java.sql.DriverManager;
import java.sql.JDBCType;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Thomas
 */
public class SQLDatabase implements AutoCloseable {

    /**
     *
     */
    protected Connection conn;

    /**
     *
     */
    protected final SQLDatabaseProperties prop;

    /**
     *
     */
    protected final List openStatements;

    /**
     *
     * @param driverClass
     * @param driver
     * @throws ClassNotFoundException
     */
    public SQLDatabase(String driverClass, String driver) throws ClassNotFoundException {
        Class.forName(driverClass);
        prop = new SQLDatabaseProperties();
        prop.setDriverClass(driverClass);
        prop.setDriver(driver);
        openStatements = new LinkedList<>();
    }

    /**
     *
     * @param prop
     * @throws ClassNotFoundException
     */
    public SQLDatabase(SQLDatabaseProperties prop) throws ClassNotFoundException {
        this.prop = prop;
        Class.forName(prop.getDriverClass());
        openStatements = new LinkedList<>();
    }

    /**
     *
     * @param hostname
     * @param dbName
     * @param username
     * @param password
     * @throws SQLException
     * @throws SQLConnectionPresentException
     */
    public void connect(String hostname, String dbName, String username, String password) throws SQLException, SQLConnectionPresentException {
        if (conn != null && !conn.isClosed()) {
            throw new SQLConnectionPresentException("Connection already established");
        }
        conn = DriverManager
                .getConnection(
                        String.format("jdbc:%s://%s/%s", prop.getDriver(), hostname, dbName), username, password);

        this.prop.setHostname(hostname);
        this.prop.setDatabaseName(dbName);
        this.prop.setUsername(username);
        this.prop.setPassword(password);
    }

    /**
     *
     * @param host
     * @param username
     * @param password
     * @throws SQLException
     * @throws SQLConnectionPresentException
     */
    public void connect(String host, String username, String password) throws SQLException, SQLConnectionPresentException {
        if (conn != null && !conn.isClosed()) {
            throw new SQLConnectionPresentException("Connection already established");
        }
        conn = DriverManager
                .getConnection(host, username, password);
        this.prop.setUsername(username);
        this.prop.setPassword(password);
    }

    /**
     *
     * @param prop
     * @throws SQLException
     * @throws PropertyNotFoundException
     * @throws SQLConnectionPresentException
     */
    public void connect(SQLDatabaseProperties prop) throws SQLException, PropertyNotFoundException, SQLConnectionPresentException {
        try {
            connect(prop.getHostname(), prop.getDatabaseName(), prop.getUsername(), prop.getPassword());
            this.prop.setHostname(prop.getHostname());
            this.prop.setDatabaseName(prop.getDatabaseName());
            this.prop.setUsername(prop.getUsername());
            this.prop.setPassword(prop.getPassword());
        } catch (NullPointerException ex) {
            throw new PropertyNotFoundException("A needed property was not found.");
        }
    }

    /**
     *
     * @throws SQLException
     * @throws PropertyNotFoundException
     * @throws SQLConnectionPresentException
     */
    public void connect() throws SQLException, PropertyNotFoundException, SQLConnectionPresentException {
        connect(prop);
    }

    /**
     *
     * @param key
     * @return
     * @throws SQLException
     */
    public boolean execute(ISQLKey key) throws SQLException {
        Statement s = conn.createStatement();
        openStatements.add(s);
        return s.execute(key.getSQL());

    }

    /**
     *
     * @param key
     * @return
     * @throws SQLException
     */
    public ResultSet executeQuery(ISQLKey key) throws SQLException {
        Statement s = conn.createStatement();
        openStatements.add(s);
        return s.executeQuery(key.getSQL());

    }

    /**
     *
     * @param key
     * @return
     * @throws SQLException
     */
    public int executeUpdate(ISQLKey key) throws SQLException {
        Statement s = conn.createStatement();
        openStatements.add(s);
        return s.executeUpdate(key.getSQL());

    }

    /**
     *
     * @throws SQLException
     */
    public void disconnect() throws SQLException {
        if (conn != null) {
            conn.close();
        }
    }

    /**
     *
     * @param prop
     */
    public void setProp(SQLDatabaseProperties prop) {
        prop.putAll(prop);
    }

    /**
     *
     * @return
     */
    public SQLDatabaseProperties getProp() {
        return prop;
    }

    /**
     *
     * @throws SQLException
     */
    @Override
    public void close() throws SQLException {
        closeOpenStatements();
        disconnect();
    }

    /**
     *
     * @throws SQLException
     */
    public void closeOpenStatements() throws SQLException {
        for (Statement openStatement : openStatements) {
            openStatement.close();
            openStatements.remove(openStatement);
        }
    }

    //Nicht sicher
    /**
     *
     * @return
     */
    public Connection getConnection() {
        return conn;
    }

    private static final String GETALLTABLES = "SELECT *\n"
            + "FROM INFORMATION_SCHEMA.TABLES\n"
            + "WHERE TABLE_CATALOG=?";

    private static final String GETALLCOLUMNS = "SELECT *\n"
            + "FROM INFORMATION_SCHEMA.COLUMNS\n"
            + "WHERE TABLE_NAME = ?";

    /**
     *
     * @return @throws SQLException
     */
    public List getAllTables() throws SQLException {
        LinkedList tables = new LinkedList<>();
        try (PreparedStatement s = conn.prepareCall(GETALLTABLES)) {
            s.setString(1, prop.getDatabaseName());
            try (PreparedStatement scol = conn.prepareCall(GETALLCOLUMNS); ResultSet set = s.executeQuery()) {
                while (set.next()) {
                    SQLTable tab = new SQLTable(set.getString("TABLE_NAME"), this);
                    scol.setString(1, tab.getName());
                    Map cols = new LinkedHashMap<>();
                    try (ResultSet setcol = scol.executeQuery()) {
                        while (setcol.next()) {
                            String type = setcol.getString("DATA_TYPE");
                            //Umwandeln von String zu JDBC
                            SQLColumn col = new SQLColumn(setcol.getString("COLUMN_NAME"), JDBCType.valueOf(type));
                            cols.put(col.getName(), col);
                        }
                    }
                    tab.setColumns(cols);
                    tables.add(tab);
                }
            }
        }
        return tables;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy