🛠️ Code cleanup & added many new features #1

Merged
gnmyt merged 121 commits from features/code-cleanup into master 2021-09-02 13:34:00 +00:00
3 changed files with 59 additions and 141 deletions
Showing only changes of commit bbf1232684 - Show all commits

View File

@ -1,105 +0,0 @@
package de.gnmyt.SQLToolkit.fields;
import de.gnmyt.SQLToolkit.types.SQLType;
public class SQLField {
private String name = "";
private String defaultValue = "";
private String optionalParameter = "";
private Integer length = 0;
private SQLType sqlType;
/**
* Basic constructor for the SQL field
* @param sqlType Type of the SQL field
* @param name Name of the SQL field
* @param length Length of the SQL field
*/
public SQLField(SQLType sqlType, String name, Integer length) {
this.name = name;
this.sqlType = sqlType;
this.length = length;
}
/**
* Set the default value for the SQL field
* @param defaultValue New default value
* @return this class
*/
public SQLField setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
return this;
}
/**
* Set optional parameter for the SQL field
* @param optionalParameter New optional parameter
* @return this class
*/
public SQLField setOptionalParameter(String optionalParameter) {
this.optionalParameter = optionalParameter;
return this;
}
/**
* Set the length of the SQL field
* @param length New length
* @return this class
*/
public SQLField setLength(Integer length) {
this.length = length;
return this;
}
/**
* Set the name of the SQL field
* @param name New name
* @return this class
*/
public SQLField setName(String name) {
this.name = name;
return this;
}
/**
* Get the length of the SQL Field
* @return length
*/
public Integer getLength() {
return length;
}
/**
* Get the default value of the SQL Field
* @return default value
*/
public String getDefaultValue() {
return defaultValue;
}
/**
* Get the optional parameters of the SQL Field
* @return this class
*/
public String getOptionalParameter() {
return optionalParameter;
}
/**
* Get the name of the SQL Field
* @return name
*/
public String getName() {
return name;
}
/**
* Get the SQL Type of the SQL Field
* @return SQL Type
*/
public SQLType getSqlType() {
return sqlType;
}
}

View File

@ -1,43 +1,32 @@
package de.gnmyt.SQLToolkit.generator; package de.gnmyt.SQLToolkit.generator;
import de.gnmyt.SQLToolkit.drivers.MySQLConnection; import de.gnmyt.SQLToolkit.drivers.MySQLConnection;
import de.gnmyt.SQLToolkit.fields.SQLField; import de.gnmyt.SQLToolkit.types.SQLType;
import de.gnmyt.SQLToolkit.manager.UpdateManager;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
public class TableGenerator { public class TableGenerator {
private UpdateManager updateManager; private final MySQLConnection connection;
private ArrayList<String> fields;
private boolean useID; private final ArrayList<String> fields;
private String tableName; private final String tableName;
MySQLConnection connection;
/** /**
* Basic constructor for the TableGenerator * Basic constructor for the TableGenerator
*
* @param updateManager Existing update de.gnmyt.SQLToolkit.manager * @param updateManager Existing update de.gnmyt.SQLToolkit.manager
* @param tableName Name of the table * @param tableName Name of the table
*/ */
public TableGenerator(UpdateManager updateManager, String tableName) { public TableGenerator(MySQLConnection connection, String tableName) {
this.updateManager = updateManager;
this.tableName = tableName; this.tableName = tableName;
this.connection = connection;
this.fields = new ArrayList<>(); this.fields = new ArrayList<>();
connection = updateManager.getConnection();
}
/**
* Add an 'id' field to the Table
* @return this class
*/
public TableGenerator useID() {
this.useID = true;
return this;
} }
/** /**
* Add a field to the Table * Add a field to the Table
*
* @param field String of the field * @param field String of the field
* @return this class * @return this class
*/ */
@ -48,29 +37,63 @@ public class TableGenerator {
/** /**
* Add a field to the Table * Add a field to the Table
* @param field Existing SQL Field * @param type The type of the field you want to add
* @param name The name of the field you want to add
* @param length The length of the field you want to add
* @param defaultValue The default value of the field (leave empty for no default value)
* @param optionalParameters Optional parameters you want to add to the statement
* @return this class * @return this class
*/ */
public TableGenerator addField(SQLField field) { public TableGenerator addField(SQLType type, String name, Integer length, String defaultValue, String... optionalParameters) {
String temp = ""; StringBuilder temp = new StringBuilder();
temp += "`" + field.getName() + "` " + field.getSqlType().getValue() + "(" + field.getLength() + ")"; temp.append("`").append(name).append("` ").append(type.getValue()).append("(").append(length).append(")");
temp += !field.getDefaultValue().isEmpty() ? " DEFAULT '" + field.getDefaultValue() + "'" : ""; temp.append(!defaultValue.isEmpty() ? " DEFAULT '" + defaultValue + "'" : "");
temp += !field.getOptionalParameter().isEmpty() ? " " + field.getOptionalParameter() : "";
fields.add(temp); for (String optionalParameter : optionalParameters) {
temp.append(" ").append(optionalParameter);
}
fields.add(temp.toString());
return this; return this;
} }
/** /**
* Create the table you wanted * Add a field to the Table
* @param type The type of the field you want to add
* @param name The name of the field you want to add
* @param length The length of the field you want to add
* @return this class
*/
public TableGenerator addField(SQLType type, String name, Integer length) {
return addField(type, name, length, "");
}
/**
* Add a field to the Table
* @param type The type of the field you want to add
* @param name The name of the field you want to add
* @return this class
*/
public TableGenerator addField(SQLType type, String name) {
return addField(type, name, 255, "");
}
/**
* Creates the table you wanted
* @return this class * @return this class
*/ */
public TableGenerator create() { public TableGenerator create() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("CREATE TABLE IF NOT EXISTS " + tableName + " ( "); sb.append("CREATE TABLE IF NOT EXISTS ").append(tableName).append(" ( ");
AtomicBoolean used = new AtomicBoolean(false);
if (useID) sb.append(used.get() ? ", " : "").append("`id` INT(255) NOT NULL AUTO_INCREMENT");used.set(true); sb.append("`id` INT(255) NOT NULL AUTO_INCREMENT");
fields.forEach(string -> { sb.append(used.get() ? ", " : "").append(string);used.set(true); });
if (useID) sb.append(used.get() ? ", " : "").append("PRIMARY KEY (`id`)");used.set(true); for (String field : fields) {
sb.append(", ").append(field);
}
sb.append(", PRIMARY KEY (`id`)");
sb.append(" ) ENGINE = InnoDB;"); sb.append(" ) ENGINE = InnoDB;");
connection.update(sb.toString()); connection.update(sb.toString());
return this; return this;

View File

@ -188,7 +188,7 @@ public class UpdateManager {
* @return the de.gnmyt.SQLToolkit.generator * @return the de.gnmyt.SQLToolkit.generator
*/ */
public TableGenerator generateTable(String tableName) { public TableGenerator generateTable(String tableName) {
return new TableGenerator(this, tableName); return new TableGenerator(this.getConnection(), tableName);
} }
/** /**
@ -196,7 +196,7 @@ public class UpdateManager {
* @return the de.gnmyt.SQLToolkit.generator * @return the de.gnmyt.SQLToolkit.generator
*/ */
public TableGenerator generateTable() { public TableGenerator generateTable() {
return (tableName.isEmpty()) ? null : new TableGenerator(this, tableName); return (tableName.isEmpty()) ? null : new TableGenerator(this.getConnection(), tableName);
} }
} }