From 5f5f425c04fe03a4c4a538f6cc8ddda279c2a7a5 Mon Sep 17 00:00:00 2001 From: mathias Date: Thu, 19 Aug 2021 16:42:17 +0200 Subject: [PATCH] Added the sql table class --- .../de/gnmyt/SQLToolkit/storage/SQLTable.java | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 src/main/java/de/gnmyt/SQLToolkit/storage/SQLTable.java diff --git a/src/main/java/de/gnmyt/SQLToolkit/storage/SQLTable.java b/src/main/java/de/gnmyt/SQLToolkit/storage/SQLTable.java new file mode 100644 index 0000000..81947a0 --- /dev/null +++ b/src/main/java/de/gnmyt/SQLToolkit/storage/SQLTable.java @@ -0,0 +1,139 @@ +package de.gnmyt.SQLToolkit.storage; + +import de.gnmyt.SQLToolkit.drivers.MySQLConnection; +import de.gnmyt.SQLToolkit.types.SQLType; +import de.gnmyt.SQLToolkit.types.TableField; + +import java.util.ArrayList; + +public abstract class SQLTable { + + private MySQLConnection connection; + private ArrayList tableFields; + + /** + * The basic constructor of the {@link SQLTable} + * @param connection The mysql connection you want to use + */ + public SQLTable(MySQLConnection connection) { + this.connection = connection; + } + + protected abstract String tableName(); + + protected abstract void tableFields(); + + /** + * Adds a string to the table (without allowNull) + * @param name The name of the string you want to add + * @param length The length of the string you want to add + * @param defaultValue The default value of the string you want to add (leave empty if you don't want to use one) + * @param extras The extras you want to add to the string + */ + protected void string(String name, int length, String defaultValue, String... extras) { + custom(SQLType.STRING.getValue(), name, length, false, defaultValue, extras); + } + + /** + * Adds a string to the table (with allowNull) + * @param name The name of the string you want to add + * @param length The length of the string you want to add + * @param allowNull true if you want to allow a NULL in the field, otherwise false + * @param defaultValue The default value of the string you want to add (leave empty if you don't want to use one) + * @param extras The extras you want to add to the string + */ + protected void string(String name, int length, boolean allowNull, String defaultValue, String... extras) { + custom(SQLType.STRING.getValue(), name, length, allowNull, defaultValue, extras); + } + + /** + * Adds an integer to the table (without allowNull) + * @param name The name of the integer you want to add + * @param length The length of the integer you want to add + * @param defaultValue The default value of the integer you want to add (leave empty if you don't want to use one) + * @param extras The extras you want to add to the integer + */ + protected void integer(String name, int length, String defaultValue, String... extras) { + custom(SQLType.INTEGER.getValue(), name, length, false, defaultValue, extras); + } + + /** + * Adds an integer to the table (with allowNull) + * @param name The name of the integer you want to add + * @param length The length of the integer you want to add + * @param allowNull true if you want to allow a NULL in the field, otherwise false + * @param defaultValue The default value of the integer you want to add (leave empty if you don't want to use one) + * @param extras The extras you want to add to the integer + */ + protected void integer(String name, int length, boolean allowNull, String defaultValue, String... extras) { + custom(SQLType.INTEGER.getValue(), name, length, allowNull, defaultValue, extras); + } + + /** + * Adds a boolean to the table (without allowNull) + * @param name The name of the boolean you want to add + * @param length The length of the boolean you want to add + * @param defaultValue The default value of the boolean you want to add (leave empty if you don't want to use one) + * @param extras The extras you want to add to the boolean + */ + protected void bool(String name, int length, String defaultValue, String... extras) { + custom(SQLType.BOOLEAN.getValue(), name, length, false, defaultValue, extras); + } + + /** + * Adds a boolean to the table (with allowNull) + * @param name The name of the boolean you want to add + * @param length The length of the boolean you want to add + * @param allowNull true if you want to allow a NULL in the field, otherwise false + * @param defaultValue The default value of the boolean you want to add (leave empty if you don't want to use one) + * @param extras The extras you want to add to the boolean + */ + protected void bool(String name, int length, boolean allowNull, String defaultValue, String... extras) { + custom(SQLType.BOOLEAN.getValue(), name, length, allowNull, defaultValue, extras); + } + + /** + * Adds a custom field to the table + * @param type The type of the field + * @param name The name of the field + * @param length The length of the field + * @param allowNull true if you want to allow a NULL in the field, otherwise false + * @param defaultValue The default value of the field + * @param extras The extras that you want to add + */ + protected void custom(String type, String name, int length, boolean allowNull, String defaultValue, String... extras) { + custom(new TableField(name, type, length, allowNull, defaultValue).setExtra(extras)); + } + + /** + * Adds a table field to the table + * @param tableField The table field you want to add + */ + protected void custom(TableField tableField) { + tableFields.add(tableField); + } + + /** + * Generates the table sql + * @return the table sql + */ + public String generateSQL() { + tableFields = new ArrayList<>(); + StringBuilder query = new StringBuilder(); + + // Add table fields + integer("id", 255, false, "", "AUTO_INCREMENT"); + tableFields(); + + // Create the query + query.append("CREATE TABLE IF NOT EXISTS ").append(tableName()).append("("); + for (TableField tableField : tableFields) + query.append(tableField.generateSQLRow()).append(", "); + query.append("PRIMARY KEY (id)"); + query.append(") ENGINE=InnoDB;"); + + // Return the query + return query.toString(); + } + +}