diff --git a/src/com/pablo67340/SQLiteLib/Database/Database.java b/src/com/pablo67340/SQLiteLib/Database/Database.java
index 12f23a2..87ca8c5 100644
--- a/src/com/pablo67340/SQLiteLib/Database/Database.java
+++ b/src/com/pablo67340/SQLiteLib/Database/Database.java
@@ -16,7 +16,6 @@
import com.pablo67340.SQLiteLib.Main.SQLiteLib;
public abstract class Database {
-
protected Connection connection;
public abstract Connection getSQLConnection();
@@ -31,17 +30,17 @@ public void initialize() {
close(ps, rs);
} catch (SQLException ex) {
- SQLiteLib.getInstance().getLogger().log(Level.SEVERE, "Unable to retreive connection", ex);
+ SQLiteLib.getInstance().getLogger().log(Level.SEVERE, "Unable to retrieve connection", ex);
}
}
/**
+ *
* Execute any statement using this method. This will return a success or
* failure boolean.
- *
+ *
*
- * @param The
- * statement to execute.
+ * @param statement The statement to execute.
*
* @return the {@link Database}'s success or failure (true/false).
*/
@@ -53,17 +52,14 @@ public Boolean executeStatement(String statement) {
ps = conn.prepareStatement(statement);
return !ps.execute();
} catch (SQLException ex) {
- SQLiteLib.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
+ SQLiteLib.log(Errors.sqlConnectionExecute(), ex);
return false;
} finally {
try {
- if (ps != null)
- ps.close();
- if (conn != null)
- conn.close();
+ if (ps != null) ps.close();
+ if (conn != null) conn.close();
} catch (SQLException ex) {
- SQLiteLib.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
- return false;
+ SQLiteLib.log(Errors.sqlConnectionClose(), ex);
}
}
}
@@ -72,13 +68,9 @@ public Boolean executeStatement(String statement) {
* Get a single value from the database. Your If your statement returns multiple
* values, only the first value will return. Use queryRow for multiple values in
* 1 row.
- *
*
- * @param The
- * statement to execute.
- *
- * @param The
- * row you would like to store data from.
+ * @param statement The statement to execute.
+ * @param row The row you would like to store data from.
*
* @return the {@link Database}'s Query in Object format. Casting required to
* change variables into their original form.
@@ -86,25 +78,21 @@ public Boolean executeStatement(String statement) {
public Object queryValue(String statement, String row) {
Connection conn = null;
PreparedStatement ps = null;
- ResultSet rs = null;
+ ResultSet rs;
try {
conn = getSQLConnection();
ps = conn.prepareStatement(statement);
rs = ps.executeQuery();
- while (rs.next()) {
- return rs.getObject(row);
- }
+ if (rs.next()) return rs.getObject(row);
} catch (SQLException ex) {
- SQLiteLib.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
+ SQLiteLib.log(Errors.sqlConnectionExecute(), ex);
} finally {
try {
- if (ps != null)
- ps.close();
- if (conn != null)
- conn.close();
+ if (ps != null) ps.close();
+ if (conn != null) conn.close();
} catch (SQLException ex) {
- SQLiteLib.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
+ SQLiteLib.log(Errors.sqlConnectionClose(), ex);
}
}
return null;
@@ -115,41 +103,36 @@ public Object queryValue(String statement, String row) {
* querying for multiple values, in 1 row. I.E If you had a row called test,
* with numbers 1 to 10. This method could be used to build a list containing
* the data 1 to 10 from this row.
- *
*
- * @param The
- * statement to execute.
- *
- * @param The
- * row you would like to store data from.
- *
- * @return the {@link Database}'s Query in List format. Casting required
- * to change variables into their original form.
+ * @param statement The statement to execute.
+ * @param row The row you would like to store data from.
+ *
+ * @return the {@link Database}'s Query in {@code List} format.
+ * Casting required to change variables into their original form.
*/
public List queryRow(String statement, String row) {
Connection conn = null;
PreparedStatement ps = null;
- ResultSet rs = null;
+ ResultSet rs;
+
List objects = new ArrayList<>();
+
try {
conn = getSQLConnection();
ps = conn.prepareStatement(statement);
rs = ps.executeQuery();
- while (rs.next()) {
+ while (rs.next())
objects.add(rs.getObject(row));
- }
return objects;
} catch (SQLException ex) {
- SQLiteLib.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
+ SQLiteLib.log(Errors.sqlConnectionExecute(), ex);
} finally {
try {
- if (ps != null)
- ps.close();
- if (conn != null)
- conn.close();
+ if (ps != null) ps.close();
+ if (conn != null) conn.close();
} catch (SQLException ex) {
- SQLiteLib.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
+ SQLiteLib.log(Errors.sqlConnectionClose(), ex);
}
}
return null;
@@ -158,95 +141,83 @@ public List queryRow(String statement, String row) {
/**
* Get a map which contains lists for each row specified. For each row, a list
* is stored containing all the queried values.
- *
- * You can access the list for each row using format. The list is in
- * List format.
*
+ * You can access the list for each row using {@code } format. The list is in
+ * List format.
*
- * @param The
- * statement to execute.
- *
- * @param The
- * row(s) you would like to store data from. Minimum 1 row required.
+ * @param statement The statement to execute.
+ * @param row The row(s) you would like to store data from. Minimum 1 row required.
*
- * @return the {@link Database}'s Query in Map> format. Casting
+ * @return the {@link Database}'s Query in {@code Map>} format. Casting
* required to change variables into their original form.
*/
public Map> queryMultipleRows(String statement, String... row) {
Connection conn = null;
PreparedStatement ps = null;
- ResultSet rs = null;
+ ResultSet rs;
+
List objects = new ArrayList<>();
Map> map = new HashMap<>();
+
try {
conn = getSQLConnection();
ps = conn.prepareStatement(statement);
rs = ps.executeQuery();
while (rs.next()) {
- for (String singleRow : row) {
+ for (String singleRow : row)
objects.add(rs.getObject(singleRow));
- }
- for (String singleRow : row) {
+ for (String singleRow : row)
map.put(singleRow, objects);
- }
-
}
+
return map;
} catch (SQLException ex) {
- SQLiteLib.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
+ SQLiteLib.log(Errors.sqlConnectionExecute(), ex);
} finally {
try {
- if (ps != null)
- ps.close();
- if (conn != null)
- conn.close();
+ if (ps != null) ps.close();
+ if (conn != null) conn.close();
} catch (SQLException ex) {
- SQLiteLib.getInstance().getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
+ SQLiteLib.log(Errors.sqlConnectionClose(), ex);
}
}
return null;
}
-
/**
* Close the current connection of the statement to the database.
- *
- *
- * @param The
- * statement previously used.
- *
- * @param The
- * result set that was returned from the statement.
*
+ * @param ps The statement previously used.
+ * @param rs The result set that was returned from the statement.
*/
public void close(PreparedStatement ps, ResultSet rs) {
try {
- if (ps != null)
- ps.close();
- if (rs != null)
- rs.close();
+ if (ps != null) ps.close();
+ if (rs != null) rs.close();
} catch (SQLException ex) {
- Error.close(SQLiteLib.getInstance(), ex);
+ Error.close(ex);
}
}
/**
- * Close the current connection to the database. The database will need to be
- * re-initialized if this is used. When intializing using the main class, it
- * will delete this current object and create a new object connected to the db.
- * If you'd like to reload this db without trashing the database object, invoke
- * the load() method through the global map of databases. E.g
- * getDatabase("name").load();.
- *
+ * Close the current connection to the database.
+ *
+ * The database will need to be re-initialized if this is used. When
+ * initializing using the main class, it will delete this current object
+ * and create a new object connected to the db. If you'd like to reload
+ * this db without trashing the database object, invoke the {@link #load()}
+ * method through the global map of databases.
+ *
+ * Ex: getDatabase("name").load();
*/
public void closeConnection() {
try {
connection.close();
- } catch (SQLException e) {
- Error.close(SQLiteLib.getInstance(), e);
+ } catch (SQLException ex) {
+ Error.close(ex);
}
}
}
diff --git a/src/com/pablo67340/SQLiteLib/Database/SQLite.java b/src/com/pablo67340/SQLiteLib/Database/SQLite.java
index f8d209c..f77aa08 100644
--- a/src/com/pablo67340/SQLiteLib/Database/SQLite.java
+++ b/src/com/pablo67340/SQLiteLib/Database/SQLite.java
@@ -11,24 +11,31 @@
import com.pablo67340.SQLiteLib.Main.SQLiteLib;
public class SQLite extends Database {
+ private final String dbname;
+ private final String createStatement;
+ private final File dataFolder;
- private String dbname;
-
- private String createTestTable = "CREATE TABLE IF NOT EXISTS test (" + "`test` varchar(32) NOT NULL,"
- + "PRIMARY KEY (`test`)" + ");";
-
- private String customCreateString;
-
- private File dataFolder;
-
+ /**
+ * Create a new SQLite database.
+ *
+ * @param databaseName the database's name.
+ * @param createStatement the statement to create our first table.
+ * @param folder the folder the database file will be created.
+ */
public SQLite(String databaseName, String createStatement, File folder) {
- dbname = databaseName;
- customCreateString = createStatement;
- dataFolder = folder;
+ this.dbname = databaseName;
+ this.createStatement = createStatement;
+ this.dataFolder = folder;
}
+ /**
+ * Get the connection to the database file.
+ *
+ * @return The connection to the database.
+ */
public Connection getSQLConnection() {
File folder = new File(dataFolder, dbname + ".db");
+
if (!folder.exists()) {
try {
folder.createNewFile();
@@ -36,28 +43,33 @@ public Connection getSQLConnection() {
SQLiteLib.getInstance().getLogger().log(Level.SEVERE, "File write error: " + dbname + ".db");
}
}
+
try {
- if (connection != null && !connection.isClosed()) {
- return connection;
- }
+ if (connection != null && !connection.isClosed()) return connection;
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" + folder);
return connection;
} catch (SQLException ex) {
- SQLiteLib.getInstance().getLogger().log(Level.SEVERE, "SQLite exception on initialize", ex);
+ SQLiteLib.log("SQLite exception on initialize", ex);
} catch (ClassNotFoundException ex) {
SQLiteLib.getInstance().getLogger().log(Level.SEVERE,
"You need the SQLite JBDC library. Google it. Put it in /lib folder.");
}
+
return null;
}
+ /**
+ * Load the database.
+ */
public void load() {
connection = getSQLConnection();
try {
Statement s = connection.createStatement();
+ String createTestTable = "CREATE TABLE IF NOT EXISTS test (" + "`test` varchar(32) NOT NULL,"
+ + "PRIMARY KEY (`test`)" + ");";
s.executeUpdate(createTestTable);
- s.executeUpdate(customCreateString);
+ s.executeUpdate(createStatement);
s.close();
} catch (SQLException e) {
e.printStackTrace();
diff --git a/src/com/pablo67340/SQLiteLib/Error/Error.java b/src/com/pablo67340/SQLiteLib/Error/Error.java
index 55b5c40..f42a18f 100644
--- a/src/com/pablo67340/SQLiteLib/Error/Error.java
+++ b/src/com/pablo67340/SQLiteLib/Error/Error.java
@@ -1,15 +1,13 @@
package com.pablo67340.SQLiteLib.Error;
-import java.util.logging.Level;
-
import com.pablo67340.SQLiteLib.Main.SQLiteLib;
public class Error {
- public static void execute(SQLiteLib plugin, Exception ex) {
- plugin.getLogger().log(Level.SEVERE, "Couldn't execute MySQL statement: ", ex);
+ public static void execute(Exception ex) {
+ SQLiteLib.log("Couldn't execute MySQL statement: ", ex);
}
- public static void close(SQLiteLib plugin, Exception ex) {
- plugin.getLogger().log(Level.SEVERE, "Failed to close MySQL connection: ", ex);
+ public static void close(Exception ex) {
+ SQLiteLib.log("Failed to close MySQL connection: ", ex);
}
}
diff --git a/src/com/pablo67340/SQLiteLib/Error/Errors.java b/src/com/pablo67340/SQLiteLib/Error/Errors.java
index 30130d3..2e3295b 100644
--- a/src/com/pablo67340/SQLiteLib/Error/Errors.java
+++ b/src/com/pablo67340/SQLiteLib/Error/Errors.java
@@ -11,7 +11,7 @@ public static String sqlConnectionClose() {
}
public static String noSQLConnection() {
- return "Unable to retreive MYSQL connection: ";
+ return "Unable to retrieve MYSQL connection: ";
}
public static String noTableFound() {
diff --git a/src/com/pablo67340/SQLiteLib/Main/SQLiteLib.java b/src/com/pablo67340/SQLiteLib/Main/SQLiteLib.java
index 3d72808..42b562e 100644
--- a/src/com/pablo67340/SQLiteLib/Main/SQLiteLib.java
+++ b/src/com/pablo67340/SQLiteLib/Main/SQLiteLib.java
@@ -2,6 +2,7 @@
import java.util.HashMap;
import java.util.Map;
+import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
@@ -17,7 +18,7 @@ public class SQLiteLib extends JavaPlugin {
private static SQLiteLib INSTANCE;
- private Map databases = new HashMap<>();
+ private final Map databases = new HashMap<>();
/**
* Override the onEnable Method, which runs only when the plugin is fully
@@ -30,18 +31,28 @@ public void onEnable() {
}
/**
- * Placeholder method for future features (Crash saving)
+ * Placeholder method for future features.
+ *
+ * For right now it is crash saving.
*/
@Override
public void onDisable() {
}
+ /**
+ * Log a severe message.
+ * @param msg The string message.
+ * @param thrown Throwable associated with log message.
+ */
+ public static void log(String msg, Throwable thrown) {
+ SQLiteLib.getInstance().getLogger().log(Level.SEVERE, msg, thrown);
+ }
+
/**
* Get the current instance of the plugin within the server.
- * Do not use this to hook into SQLiteLib, as it can become unsafe
- * in the future. Use the hookSQLiteLib method.
- *
+ * Do not use this to hook into SQLiteLib, as it can become
+ * unsafe in the future. Use the hookSQLiteLib method.
*
* @return the {@link SQLiteLib}'s prefix.
*/
@@ -50,9 +61,8 @@ public static SQLiteLib getInstance() {
}
/**
- * Get the current instance of the plugin within the server. Needed to hook into
- * the API to save things.
- *
+ * Get the current instance of the plugin within the server.
+ * Needed to hook into the API to save things.
*
* @return the {@link SQLiteLib}'s prefix.
*/
@@ -66,14 +76,14 @@ public static SQLiteLib hookSQLiteLib(Plugin hostPlugin) {
}
/**
+ * Create and load a new database
*
- * @param Database
- * name
- * @param Initial
- * statement once the database is created. Usually used to create
- * tables.
- *
- * Sets the string sent to player when an item cannot be purchased.
+ * @param databaseName Database name.
+ * @param createStatement Initial statement once the database is
+ * created. Usually used to create tables.
+ *
+ * Sets the string sent to player when an
+ * item cannot be purchased.
*/
public void initializeDatabase(String databaseName, String createStatement) {
Database db = new SQLite(databaseName, createStatement, this.getDataFolder());
@@ -82,15 +92,16 @@ public void initializeDatabase(String databaseName, String createStatement) {
}
/**
- *
- * @param Database
- * name
- * @param Initial
- * statement once the database is created. Usually used to create
- * tables.
- *
- * Sets the string sent to player when an item cannot be purchased.
- * @param Plugin to create database file inside.
+ * Create and load a new database within a different plugin's
+ * folder.
+ *
+ * @param plugin Plugin to create database file inside.
+ * @param databaseName Database name.
+ * @param createStatement Initial statement once the database is
+ * created. Usually used to create tables.
+ *
+ * Sets the string sent to player when an
+ * item cannot be purchased.
*/
public void initializeDatabase(Plugin plugin, String databaseName, String createStatement) {
Database db = new SQLite(databaseName, createStatement, plugin.getDataFolder());
@@ -100,7 +111,6 @@ public void initializeDatabase(Plugin plugin, String databaseName, String create
/**
* Get the global list of currently loaded databased.
- *
*
* @return the {@link SQLiteLib}'s global database list.
*/
@@ -109,18 +119,18 @@ public Map getDatabases() {
}
/**
- *
- * @param Database
- * name
- *
- * Gets a specific {@link Database}'s class.
+ * Gets a specific {@link Database}'s class.
+ *
+ * @param databaseName Database name
+ *
+ * @return The database.
*/
public Database getDatabase(String databaseName) {
return getDatabases().get(databaseName);
}
@Override
- public boolean onCommand(CommandSender sender, Command command, String label, String args[]) {
+ public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase("sqlite") || command.getName().equalsIgnoreCase("sl")) {
if (sender.hasPermission("sqlite.use") || sender.isOp() || sender instanceof ConsoleCommandSender) {
if (args.length != 0) {
@@ -129,14 +139,15 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
if (args[0].equalsIgnoreCase("execute")) {
if (args.length > 2) {
String database = args[1];
- String statement = "";
+ StringBuilder statement = new StringBuilder();
- for (int x = 2; x <= args.length - 1; x++) {
- statement += args[x] + " ";
- }
- statement = statement.trim();
- System.out.println("Statement: " + statement);
- if (getDatabase(database).executeStatement(statement)) {
+ for (int x = 2; x <= args.length - 1; x++)
+ statement.append(args[x]).append(" ");
+ statement = new StringBuilder(statement.toString().trim());
+
+ //System.out.println("Statement: " + statement);
+
+ if (getDatabase(database).executeStatement(statement.toString())) {
sender.sendMessage("Query successful!");
} else {
sender.sendMessage("Query failure!");
@@ -150,16 +161,17 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
// INIT/Create
} else if (args[0].equalsIgnoreCase("init")) {
- if (args.length == 2) {
+ if (args.length > 2) {
String database = args[1];
- String statement = "";
+ StringBuilder statement = new StringBuilder();
- for (int x = 2; x <= args.length - 1; x++) {
- statement += args[x] + " ";
- }
- statement = statement.trim();
- initializeDatabase(database, statement);
- sender.sendMessage("Database intialized!");
+ for (int x = 2; x <= args.length - 1; x++)
+ statement.append(args[x]).append(" ");
+ statement = new StringBuilder(statement.toString().trim());
+
+ initializeDatabase(database, statement.toString());
+
+ sender.sendMessage("Database initialized!");
} else {
printHelp(sender);
}
@@ -169,34 +181,30 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
// QUERY VALUE
} else if (args[0].equalsIgnoreCase("queryvalue")) {
- if (args.length > 1) {
+ if (args.length >= 3) {
String database = args[1];
String row = args[2];
- String statement = "";
+ StringBuilder statement = new StringBuilder();
- for (int x = 3; x <= args.length - 1; x++) {
- statement += args[x] + " ";
- }
- statement = statement.trim();
-
- sender.sendMessage((String)getDatabase(database).queryValue(statement, row));
+ for (int x = 3; x <= args.length - 1; x++)
+ statement.append(args[x]).append(" ");
+ statement = new StringBuilder(statement.toString().trim());
+ sender.sendMessage((String)getDatabase(database).queryValue(statement.toString(), row));
}
}else if (args[0].equalsIgnoreCase("queryrow")) {
- if (args.length > 1) {
+ if (args.length > 3) {
String database = args[1];
String row = args[2];
- String statement = "";
+ StringBuilder statement = new StringBuilder();
- for (int x = 3; x <= args.length - 1; x++) {
- statement += args[x] + " ";
- }
- statement = statement.trim();
+ for (int x = 3; x <= args.length - 1; x++)
+ statement.append(args[x]).append(" ");
+ statement = new StringBuilder(statement.toString().trim());
- for(Object obj : getDatabase(database).queryRow(statement, row)) {
+ for(Object obj : getDatabase(database).queryRow(statement.toString(), row))
sender.sendMessage((String)obj);
- }
-
+
}
}
} else {