Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added H2 database support #804

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/src/main/java/com/griefcraft/lwc/LWCPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ public void loadDatabase() {

if (database.equalsIgnoreCase("mysql")) {
Database.DefaultType = Database.Type.MySQL;
} else if (database.equalsIgnoreCase("h2")) {
Database.DefaultType = Database.Type.H2;
} else {
Database.DefaultType = Database.Type.SQLite;
}
Expand Down
14 changes: 13 additions & 1 deletion core/src/main/java/com/griefcraft/sql/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public abstract class Database {
public enum Type {
MySQL("mysql.jar"), //
SQLite("sqlite.jar"), //
H2("H2.jar"), //
NONE("nil"); //

private String driver;
Expand Down Expand Up @@ -217,7 +218,7 @@ public boolean connect() throws Exception {
// load the database jar
ClassLoader classLoader;

if (currentType == Type.SQLite) {
if (currentType == Type.SQLite|| currentType == Type.H2) {
classLoader = new URLClassLoader(new URL[]{new URL("jar:file:" + new File(Updater.DEST_LIBRARY_FOLDER + currentType.getDriver()).getPath() + "!/")});
} else {
classLoader = Bukkit.getServer().getClass().getClassLoader();
Expand All @@ -227,6 +228,8 @@ public boolean connect() throws Exception {
String className = "";
if (currentType == Type.MySQL) {
className = "com.mysql.jdbc.Driver";
} else if (currentType == Type.H2) {
className = "org.h2.Driver";
} else {
className = "org.sqlite.JDBC";
}
Expand Down Expand Up @@ -261,6 +264,13 @@ public boolean connect() throws Exception {
}

public void dispose() {
for (PreparedStatement preparedStatement : statementCache.values()) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
statementCache.clear();

try {
Expand Down Expand Up @@ -289,6 +299,8 @@ public String getDatabasePath() {

if (currentType == Type.MySQL) {
return "//" + lwcConfiguration.getString("database.host") + "/" + lwcConfiguration.getString("database.database");
} else if (currentType == Type.H2) {
return lwcConfiguration.getString("database.path") + ".h2/lwc";
}

return lwcConfiguration.getString("database.path");
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/com/griefcraft/sql/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public void execute() {
buffer.append("PRIMARY KEY ");
}

if (column.shouldAutoIncrement() && database.getType() == Type.MySQL) {
if (column.shouldAutoIncrement() &&
(database.getType() == Type.MySQL || database.getType() == Type.H2)) {
buffer.append("AUTO_INCREMENT ");
}

Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/com/griefcraft/util/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ private void verifyFiles() {
// Native library
this.verifyFile(new UpdaterFile(getFullNativeLibraryPath(), UPDATE_SITE + "/shared/lib/" + getFullNativeLibraryPath().replaceAll(DEST_LIBRARY_FOLDER, "")));
}
if (Database.DefaultType == Database.Type.H2) {
// H2.jar
this.verifyFile(new UpdaterFile(DEST_LIBRARY_FOLDER + "H2.jar", "http://repo2.maven.org/maven2/com/h2database/h2/1.3.175/h2-1.3.175.jar"));
}
}

/**
Expand Down