Removed the sql field & cleaned up the table generator
This commit is contained in:
parent
2d220c0211
commit
bbf1232684
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -1,43 +1,32 @@
|
||||
package de.gnmyt.SQLToolkit.generator;
|
||||
|
||||
import de.gnmyt.SQLToolkit.drivers.MySQLConnection;
|
||||
import de.gnmyt.SQLToolkit.fields.SQLField;
|
||||
import de.gnmyt.SQLToolkit.manager.UpdateManager;
|
||||
import de.gnmyt.SQLToolkit.types.SQLType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class TableGenerator {
|
||||
|
||||
private UpdateManager updateManager;
|
||||
private ArrayList<String> fields;
|
||||
private boolean useID;
|
||||
private String tableName;
|
||||
MySQLConnection connection;
|
||||
private final MySQLConnection connection;
|
||||
|
||||
private final ArrayList<String> fields;
|
||||
private final String tableName;
|
||||
|
||||
/**
|
||||
* Basic constructor for the TableGenerator
|
||||
*
|
||||
* @param updateManager Existing update de.gnmyt.SQLToolkit.manager
|
||||
* @param tableName Name of the table
|
||||
*/
|
||||
public TableGenerator(UpdateManager updateManager, String tableName) {
|
||||
this.updateManager = updateManager;
|
||||
public TableGenerator(MySQLConnection connection, String tableName) {
|
||||
this.tableName = tableName;
|
||||
this.connection = connection;
|
||||
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
|
||||
*
|
||||
* @param field String of the field
|
||||
* @return this class
|
||||
*/
|
||||
@ -48,29 +37,63 @@ public class TableGenerator {
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public TableGenerator addField(SQLField field) {
|
||||
String temp = "";
|
||||
temp += "`" + field.getName() + "` " + field.getSqlType().getValue() + "(" + field.getLength() + ")";
|
||||
temp += !field.getDefaultValue().isEmpty() ? " DEFAULT '" + field.getDefaultValue() + "'" : "";
|
||||
temp += !field.getOptionalParameter().isEmpty() ? " " + field.getOptionalParameter() : "";
|
||||
fields.add(temp);
|
||||
public TableGenerator addField(SQLType type, String name, Integer length, String defaultValue, String... optionalParameters) {
|
||||
StringBuilder temp = new StringBuilder();
|
||||
temp.append("`").append(name).append("` ").append(type.getValue()).append("(").append(length).append(")");
|
||||
temp.append(!defaultValue.isEmpty() ? " DEFAULT '" + defaultValue + "'" : "");
|
||||
|
||||
for (String optionalParameter : optionalParameters) {
|
||||
temp.append(" ").append(optionalParameter);
|
||||
}
|
||||
|
||||
fields.add(temp.toString());
|
||||
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
|
||||
*/
|
||||
public TableGenerator create() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("CREATE TABLE IF NOT EXISTS " + tableName + " ( ");
|
||||
AtomicBoolean used = new AtomicBoolean(false);
|
||||
if (useID) sb.append(used.get() ? ", " : "").append("`id` INT(255) NOT NULL AUTO_INCREMENT");used.set(true);
|
||||
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);
|
||||
sb.append("CREATE TABLE IF NOT EXISTS ").append(tableName).append(" ( ");
|
||||
|
||||
sb.append("`id` INT(255) NOT NULL AUTO_INCREMENT");
|
||||
|
||||
for (String field : fields) {
|
||||
sb.append(", ").append(field);
|
||||
}
|
||||
|
||||
sb.append(", PRIMARY KEY (`id`)");
|
||||
|
||||
sb.append(" ) ENGINE = InnoDB;");
|
||||
connection.update(sb.toString());
|
||||
return this;
|
||||
|
@ -188,7 +188,7 @@ public class UpdateManager {
|
||||
* @return the de.gnmyt.SQLToolkit.generator
|
||||
*/
|
||||
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
|
||||
*/
|
||||
public TableGenerator generateTable() {
|
||||
return (tableName.isEmpty()) ? null : new TableGenerator(this, tableName);
|
||||
return (tableName.isEmpty()) ? null : new TableGenerator(this.getConnection(), tableName);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user