|
|
|
|
@@ -1,5 +1,6 @@
|
|
|
|
|
package com.redstoner.misc.mysql.elements;
|
|
|
|
|
|
|
|
|
|
import com.redstoner.misc.mysql.MysqlHandler;
|
|
|
|
|
import com.redstoner.misc.mysql.MysqlQueryHandler;
|
|
|
|
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
|
@@ -10,10 +11,14 @@ import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class MysqlDatabase {
|
|
|
|
|
private Connection connection;
|
|
|
|
|
private Connection connection = null;
|
|
|
|
|
|
|
|
|
|
public MysqlDatabase(Connection connection) {
|
|
|
|
|
this.connection = connection;
|
|
|
|
|
private final MysqlHandler handler;
|
|
|
|
|
private final String databaseName;
|
|
|
|
|
|
|
|
|
|
public MysqlDatabase(MysqlHandler handler, String databaseName) {
|
|
|
|
|
this.handler = handler;
|
|
|
|
|
this.databaseName = databaseName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MysqlTable getTable(String name) {
|
|
|
|
|
@@ -21,7 +26,7 @@ public class MysqlDatabase {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean createTable(String name, MysqlField... description) {
|
|
|
|
|
return MysqlQueryHandler.queryNoResult(connection, "CREATE TABLE `" + name + "` " + getDescription(description) + ";");
|
|
|
|
|
return MysqlQueryHandler.queryNoResult(getConnection(), "CREATE TABLE `" + name + "` " + getDescription(description) + ";");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getDescription(MysqlField... description) {
|
|
|
|
|
@@ -47,20 +52,20 @@ public class MysqlDatabase {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean createTableIfNotExists(String name, MysqlField... description) {
|
|
|
|
|
return MysqlQueryHandler.queryNoResult(connection, "CREATE TABLE IF NOT EXISTS `" + name + "` " + getDescription(description) + ";");
|
|
|
|
|
return MysqlQueryHandler.queryNoResult(getConnection(), "CREATE TABLE IF NOT EXISTS `" + name + "` " + getDescription(description) + ";");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean dropTable(String name) {
|
|
|
|
|
return MysqlQueryHandler.queryNoResult(connection, "DROP TABLE `" + name + "`;");
|
|
|
|
|
return MysqlQueryHandler.queryNoResult(getConnection(), "DROP TABLE `" + name + "`;");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean drop() {
|
|
|
|
|
return MysqlQueryHandler.queryNoResult(connection, "DROP DATABASE `" + getName() + "`;");
|
|
|
|
|
return MysqlQueryHandler.queryNoResult(getConnection(), "DROP DATABASE `" + getName() + "`;");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
|
try {
|
|
|
|
|
return connection.getCatalog();
|
|
|
|
|
return getConnection().getCatalog();
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return null;
|
|
|
|
|
@@ -70,7 +75,7 @@ public class MysqlDatabase {
|
|
|
|
|
public List<MysqlTable> getTables() {
|
|
|
|
|
try {
|
|
|
|
|
List<MysqlTable> tables = new ArrayList<>();
|
|
|
|
|
DatabaseMetaData metadata = connection.getMetaData();
|
|
|
|
|
DatabaseMetaData metadata = getConnection().getMetaData();
|
|
|
|
|
ResultSet queryResults = metadata.getTables(null, null, "%", null);
|
|
|
|
|
|
|
|
|
|
while (queryResults.next()) {
|
|
|
|
|
@@ -84,7 +89,16 @@ public class MysqlDatabase {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected Connection getConnection() {
|
|
|
|
|
Connection getConnection() {
|
|
|
|
|
try {
|
|
|
|
|
if (connection == null || connection.isClosed()) {
|
|
|
|
|
connection = handler.getConnection(databaseName);
|
|
|
|
|
}
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
connection = handler.getConnection(databaseName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return connection;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|