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 com.plotsquared.core.PlotSquared; 022import org.apache.logging.log4j.LogManager; 023import org.apache.logging.log4j.Logger; 024 025import java.io.File; 026import java.io.IOException; 027import java.sql.Connection; 028import java.sql.DriverManager; 029import java.sql.ResultSet; 030import java.sql.SQLException; 031import java.sql.Statement; 032 033/** 034 * Connects to and uses a SQLite database. 035 */ 036public class SQLite extends Database { 037 038 private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + SQLite.class.getSimpleName()); 039 040 private final String dbLocation; 041 private Connection connection; 042 043 /** 044 * Creates a new SQLite instance 045 * 046 * @param dbLocation Location of the Database (Must end in .db) 047 */ 048 public SQLite(File dbLocation) { 049 this.dbLocation = dbLocation.getAbsolutePath(); 050 } 051 052 @Override 053 public Connection openConnection() throws SQLException, ClassNotFoundException { 054 if (checkConnection()) { 055 return this.connection; 056 } 057 if (!PlotSquared.platform().getDirectory().exists()) { 058 PlotSquared.platform().getDirectory().mkdirs(); 059 } 060 File file = new File(this.dbLocation); 061 if (!file.exists()) { 062 try { 063 file.createNewFile(); 064 } catch (IOException ignored) { 065 LOGGER.error("Unable to create database"); 066 } 067 } 068 Class.forName("org.sqlite.JDBC"); 069 this.connection = DriverManager.getConnection("jdbc:sqlite:" + this.dbLocation); 070 return this.connection; 071 } 072 073 @Override 074 public boolean checkConnection() throws SQLException { 075 return (this.connection != null) && !this.connection.isClosed(); 076 } 077 078 @Override 079 public Connection getConnection() { 080 return this.connection; 081 } 082 083 @Override 084 public boolean closeConnection() throws SQLException { 085 if (this.connection == null) { 086 return false; 087 } 088 this.connection.close(); 089 this.connection = null; 090 return true; 091 } 092 093 @Override 094 public ResultSet querySQL(String query) throws SQLException, ClassNotFoundException { 095 if (checkConnection()) { 096 openConnection(); 097 } 098 try (Statement statement = this.connection.createStatement()) { 099 return statement.executeQuery(query); 100 } 101 } 102 103 @Override 104 public int updateSQL(String query) throws SQLException, ClassNotFoundException { 105 if (checkConnection()) { 106 openConnection(); 107 } 108 try (Statement statement = this.connection.createStatement()) { 109 return statement.executeUpdate(query); 110 } 111 } 112 113 @Override 114 public Connection forceConnection() throws SQLException, ClassNotFoundException { 115 Class.forName("org.sqlite.JDBC"); 116 this.connection = DriverManager.getConnection("jdbc:sqlite:" + this.dbLocation); 117 return this.connection; 118 } 119 120}