001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.database;
020
021import java.sql.Connection;
022import java.sql.ResultSet;
023import java.sql.SQLException;
024import java.sql.Statement;
025
026/**
027 * Abstract Database class, serves as a base for any connection method (MySQL, SQLite, etc.)
028 *
029 * @author -_Husky_-
030 * @author tips48
031 */
032public abstract class Database {
033
034    public abstract Connection forceConnection() throws SQLException, ClassNotFoundException;
035
036    /**
037     * Opens a connection with the database.
038     *
039     * @return Opened connection
040     * @throws SQLException           if the connection can not be opened
041     * @throws ClassNotFoundException if the driver cannot be found
042     */
043    public abstract Connection openConnection() throws SQLException, ClassNotFoundException;
044
045    /**
046     * Checks if a connection is open with the database.
047     *
048     * @return {@code true} if the connection is open
049     * @throws SQLException if the connection cannot be checked
050     */
051    public abstract boolean checkConnection() throws SQLException;
052
053    /**
054     * Gets the connection with the database.
055     *
056     * @return Connection with the database, null if none
057     */
058    public abstract Connection getConnection();
059
060    /**
061     * Closes the connection with the database.
062     *
063     * @return {@code true} if successful
064     * @throws SQLException if the connection cannot be closed
065     */
066    public abstract boolean closeConnection() throws SQLException;
067
068    /**
069     * Executes a SQL Query.
070     * If the connection is closed, it will be opened.
071     *
072     * @param query Query to be run
073     * @return the results of the query
074     * @throws SQLException           If the query cannot be executed
075     * @throws ClassNotFoundException If the driver cannot be found; see {@link #openConnection()}
076     */
077    public abstract ResultSet querySQL(String query) throws SQLException, ClassNotFoundException;
078
079    /**
080     * Executes an Update SQL Query.
081     * See {@link Statement#executeUpdate(String)}.
082     * If the connection is closed, it will be opened.
083     *
084     * @param query Query to be run
085     * @return Result Code, see {@link Statement#executeUpdate(String)}
086     * @throws SQLException           If the query cannot be executed
087     * @throws ClassNotFoundException If the driver cannot be found; see {@link #openConnection()}
088     */
089    public abstract int updateSQL(String query) throws SQLException, ClassNotFoundException;
090
091}