TF-PlotSquared/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLite.java

111 lines
4.5 KiB
Java
Raw Normal View History

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-23 19:02:17 +02:00
package com.intellectualcrafters.plot.database;
import java.io.File;
import java.io.IOException;
2015-01-14 03:38:15 +11:00
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
2014-09-23 19:02:17 +02:00
2015-02-19 17:08:15 +11:00
import com.intellectualcrafters.plot.PlotSquared;
2014-09-23 19:02:17 +02:00
/**
* Connects to and uses a SQLite database
*
2014-09-23 19:02:17 +02:00
* @author Citymonstret
* @author tips48
*/
public class SQLite extends Database {
2014-11-05 14:42:08 +11:00
private final String dbLocation;
2014-12-17 20:15:11 -06:00
private Connection connection;
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
/**
* Creates a new SQLite instance
*
2014-12-17 20:15:11 -06:00
* @param dbLocation Location of the Database (Must end in .db)
2014-11-05 14:42:08 +11:00
*/
2015-02-19 17:08:15 +11:00
public SQLite(final PlotSquared plotsquared, final String dbLocation) {
super(plotsquared);
2014-11-05 14:42:08 +11:00
this.dbLocation = dbLocation;
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
@Override
public Connection openConnection() throws SQLException, ClassNotFoundException {
if (checkConnection()) {
return this.connection;
}
2015-02-20 15:32:40 +11:00
if (!PlotSquared.IMP.getDirectory().exists()) {
PlotSquared.IMP.getDirectory().mkdirs();
2014-11-05 14:42:08 +11:00
}
2015-01-10 02:02:02 +11:00
final File file = new File(this.dbLocation);
2014-11-05 14:42:08 +11:00
if (!(file.exists())) {
try {
file.createNewFile();
2014-12-17 20:15:11 -06:00
} catch (final IOException e) {
2015-02-20 15:32:40 +11:00
PlotSquared.log("&cUnable to create database!");
2014-11-05 14:42:08 +11:00
}
}
Class.forName("org.sqlite.JDBC");
2015-01-10 02:02:02 +11:00
this.connection = DriverManager.getConnection("jdbc:sqlite:" + this.dbLocation);
2014-11-05 14:42:08 +11:00
return this.connection;
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
@Override
public boolean checkConnection() throws SQLException {
return (this.connection != null) && !this.connection.isClosed();
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
@Override
public Connection getConnection() {
return this.connection;
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
@Override
public boolean closeConnection() throws SQLException {
if (this.connection == null) {
return false;
}
this.connection.close();
return true;
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
@Override
public ResultSet querySQL(final String query) throws SQLException, ClassNotFoundException {
if (checkConnection()) {
openConnection();
}
final Statement statement = this.connection.createStatement();
2014-11-21 23:45:46 +01:00
return statement.executeQuery(query);
2014-11-05 14:42:08 +11:00
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
@Override
public int updateSQL(final String query) throws SQLException, ClassNotFoundException {
if (checkConnection()) {
openConnection();
}
final Statement statement = this.connection.createStatement();
2014-11-21 23:45:46 +01:00
return statement.executeUpdate(query);
2014-11-05 14:42:08 +11:00
}
}