2014-11-08 20:27:09 +01:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// PlotSquared - A plot manager and world generator for the Bukkit API /
|
|
|
|
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
|
|
|
|
// /
|
|
|
|
// This program is free software; you can redistribute it and/or modify /
|
|
|
|
// it under the terms of the GNU General Public License as published by /
|
|
|
|
// the Free Software Foundation; either version 3 of the License, or /
|
|
|
|
// (at your option) any later version. /
|
|
|
|
// /
|
|
|
|
// This program is distributed in the hope that it will be useful, /
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
|
|
|
|
// GNU General Public License for more details. /
|
|
|
|
// /
|
|
|
|
// You should have received a copy of the GNU General Public License /
|
|
|
|
// along with this program; if not, write to the Free Software Foundation, /
|
|
|
|
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
|
|
|
|
// /
|
|
|
|
// You can contact us via: support@intellectualsites.com /
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2014-09-22 13:02:14 +02:00
|
|
|
package com.intellectualcrafters.plot.database;
|
|
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
2015-02-19 07:08:15 +01:00
|
|
|
import com.intellectualcrafters.plot.PlotSquared;
|
|
|
|
|
2014-09-22 13:02:14 +02:00
|
|
|
/**
|
2014-12-18 03:15:11 +01:00
|
|
|
* Abstract Database class, serves as a base for any connection method (MySQL, SQLite, etc.)
|
2014-10-11 09:33:10 +02:00
|
|
|
*
|
2014-09-24 14:21:43 +02:00
|
|
|
* @author -_Husky_-
|
|
|
|
* @author tips48
|
|
|
|
*/
|
2014-09-22 13:02:14 +02:00
|
|
|
public abstract class Database {
|
2014-11-05 04:42:08 +01:00
|
|
|
/**
|
|
|
|
* Plugin instance, use for plugin.getDataFolder()
|
|
|
|
*/
|
2015-02-19 07:08:15 +01:00
|
|
|
protected final PlotSquared plotsquared;
|
2015-02-20 07:34:19 +01:00
|
|
|
|
2014-11-05 04:42:08 +01:00
|
|
|
/**
|
|
|
|
* Creates a new Database
|
|
|
|
*
|
2015-02-19 07:08:15 +01:00
|
|
|
* @param plotsquared Plugin instance
|
2014-11-05 04:42:08 +01:00
|
|
|
*/
|
2015-02-19 07:08:15 +01:00
|
|
|
protected Database(final PlotSquared plotsquared) {
|
|
|
|
this.plotsquared = plotsquared;
|
2014-11-05 04:42:08 +01:00
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
|
2014-11-05 04:42:08 +01:00
|
|
|
/**
|
|
|
|
* Opens a connection with the database
|
|
|
|
*
|
|
|
|
* @return Opened connection
|
2014-12-18 03:15:11 +01:00
|
|
|
*
|
|
|
|
* @throws SQLException if the connection can not be opened
|
|
|
|
* @throws ClassNotFoundException if the driver cannot be found
|
2014-11-05 04:42:08 +01:00
|
|
|
*/
|
|
|
|
public abstract Connection openConnection() throws SQLException, ClassNotFoundException;
|
2015-02-20 07:34:19 +01:00
|
|
|
|
2014-11-05 04:42:08 +01:00
|
|
|
/**
|
|
|
|
* Checks if a connection is open with the database
|
|
|
|
*
|
|
|
|
* @return true if the connection is open
|
2014-12-18 03:15:11 +01:00
|
|
|
*
|
|
|
|
* @throws SQLException if the connection cannot be checked
|
2014-11-05 04:42:08 +01:00
|
|
|
*/
|
|
|
|
public abstract boolean checkConnection() throws SQLException;
|
2015-02-20 07:34:19 +01:00
|
|
|
|
2014-11-05 04:42:08 +01:00
|
|
|
/**
|
|
|
|
* Gets the connection with the database
|
|
|
|
*
|
|
|
|
* @return Connection with the database, null if none
|
|
|
|
*/
|
|
|
|
public abstract Connection getConnection();
|
2015-02-20 07:34:19 +01:00
|
|
|
|
2014-11-05 04:42:08 +01:00
|
|
|
/**
|
|
|
|
* Closes the connection with the database
|
|
|
|
*
|
|
|
|
* @return true if successful
|
2014-12-18 03:15:11 +01:00
|
|
|
*
|
|
|
|
* @throws SQLException if the connection cannot be closed
|
2014-11-05 04:42:08 +01:00
|
|
|
*/
|
|
|
|
public abstract boolean closeConnection() throws SQLException;
|
2015-02-20 07:34:19 +01:00
|
|
|
|
2014-11-05 04:42:08 +01:00
|
|
|
/**
|
2014-12-18 03:15:11 +01:00
|
|
|
* Executes a SQL Query<br> If the connection is closed, it will be opened
|
|
|
|
*
|
|
|
|
* @param query Query to be run
|
2014-11-05 04:42:08 +01:00
|
|
|
*
|
|
|
|
* @return the results of the query
|
2014-12-18 03:15:11 +01:00
|
|
|
*
|
|
|
|
* @throws SQLException If the query cannot be executed
|
|
|
|
* @throws ClassNotFoundException If the driver cannot be found; see {@link #openConnection()}
|
2014-11-05 04:42:08 +01:00
|
|
|
*/
|
|
|
|
public abstract ResultSet querySQL(final String query) throws SQLException, ClassNotFoundException;
|
2015-02-20 07:34:19 +01:00
|
|
|
|
2014-11-05 04:42:08 +01:00
|
|
|
/**
|
2014-12-18 03:15:11 +01:00
|
|
|
* Executes an Update SQL Query<br> See {@link java.sql.Statement#executeUpdate(String)}<br> If the connection is
|
|
|
|
* closed, it will be opened
|
|
|
|
*
|
|
|
|
* @param query Query to be run
|
2014-11-05 04:42:08 +01:00
|
|
|
*
|
|
|
|
* @return Result Code, see {@link java.sql.Statement#executeUpdate(String)}
|
2014-12-18 03:15:11 +01:00
|
|
|
*
|
|
|
|
* @throws SQLException If the query cannot be executed
|
|
|
|
* @throws ClassNotFoundException If the driver cannot be found; see {@link #openConnection()}
|
2014-11-05 04:42:08 +01:00
|
|
|
*/
|
|
|
|
public abstract int updateSQL(final String query) throws SQLException, ClassNotFoundException;
|
2014-09-24 14:21:43 +02:00
|
|
|
}
|