summaryrefslogtreecommitdiff
path: root/src/main/java/com/redstoner/misc/mysql/elements/MysqlDatabase.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/redstoner/misc/mysql/elements/MysqlDatabase.java')
-rw-r--r--src/main/java/com/redstoner/misc/mysql/elements/MysqlDatabase.java92
1 files changed, 46 insertions, 46 deletions
diff --git a/src/main/java/com/redstoner/misc/mysql/elements/MysqlDatabase.java b/src/main/java/com/redstoner/misc/mysql/elements/MysqlDatabase.java
index 91c0fe4..3f1c288 100644
--- a/src/main/java/com/redstoner/misc/mysql/elements/MysqlDatabase.java
+++ b/src/main/java/com/redstoner/misc/mysql/elements/MysqlDatabase.java
@@ -1,5 +1,7 @@
package com.redstoner.misc.mysql.elements;
+import com.redstoner.misc.mysql.MysqlQueryHandler;
+
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
@@ -7,84 +9,82 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
-import com.redstoner.misc.mysql.MysqlQueryHandler;
-
public class MysqlDatabase {
private Connection connection;
-
+
public MysqlDatabase(Connection connection) {
this.connection = connection;
}
-
- public String getName() {
- try {
- return connection.getCatalog();
- } catch (SQLException e) {
- e.printStackTrace();
- return null;
- }
- }
-
+
public MysqlTable getTable(String name) {
return new MysqlTable(this, name);
}
-
+
public boolean createTable(String name, MysqlField... description) {
return MysqlQueryHandler.queryNoResult(connection, "CREATE TABLE `" + name + "` " + getDescription(description) + ";");
}
-
+
+ private String getDescription(MysqlField... description) {
+ String desc = "(";
+
+ for (int i = 0; i < description.length; i++) {
+ String nil = "";
+
+ if (description[i].canBeNull()) {
+ nil = " NOT NULL";
+ }
+
+ desc += "`" + description[i].getName() + "` " + description[i].getType().getName() + nil;
+
+ if (i < description.length - 1) {
+ desc += ",";
+ }
+ }
+
+ desc += ")";
+
+ return desc;
+ }
+
public boolean createTableIfNotExists(String name, MysqlField... description) {
return MysqlQueryHandler.queryNoResult(connection, "CREATE TABLE IF NOT EXISTS `" + name + "` " + getDescription(description) + ";");
}
-
+
public boolean dropTable(String name) {
return MysqlQueryHandler.queryNoResult(connection, "DROP TABLE `" + name + "`;");
}
-
+
public boolean drop() {
return MysqlQueryHandler.queryNoResult(connection, "DROP DATABASE `" + getName() + "`;");
}
-
+
+ public String getName() {
+ try {
+ return connection.getCatalog();
+ } catch (SQLException e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
public List<MysqlTable> getTables() {
try {
- List<MysqlTable> tables = new ArrayList<>();
- DatabaseMetaData metadata = connection.getMetaData();
- ResultSet queryResults = metadata.getTables(null, null, "%", null);
-
+ List<MysqlTable> tables = new ArrayList<>();
+ DatabaseMetaData metadata = connection.getMetaData();
+ ResultSet queryResults = metadata.getTables(null, null, "%", null);
+
while (queryResults.next()) {
tables.add(new MysqlTable(this, queryResults.getString(3)));
}
-
+
return tables;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
-
+
protected Connection getConnection() {
return connection;
}
-
- private String getDescription(MysqlField... description) {
- String desc = "(";
-
- for (int i = 0; i < description.length; i++) {
- String nil = "";
-
- if (description[i].canBeNull()) {
- nil = " NOT NULL";
- }
-
- desc += "`" + description[i].getName() + "` " + description[i].getType().getName() + nil;
-
- if (i < description.length - 1) {
- desc += ",";
- }
- }
-
- desc += ")";
-
- return desc;
- }
}