Integrated the TableCreationQuery into the TableGenerator

This commit is contained in:
mathias 2021-08-27 12:44:08 +02:00
parent bf7565a8dc
commit 52ca4879f9
No known key found for this signature in database
GPG Key ID: 8950DF62139C852A

View File

@ -1,6 +1,9 @@
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.queries.TableCreationQuery;
import de.gnmyt.sqltoolkit.querybuilder.QueryParameter;
import de.gnmyt.sqltoolkit.querybuilder.SQLQuery;
import de.gnmyt.sqltoolkit.types.SQLType; import de.gnmyt.sqltoolkit.types.SQLType;
import de.gnmyt.sqltoolkit.types.TableField; import de.gnmyt.sqltoolkit.types.TableField;
@ -10,7 +13,7 @@ public class TableGenerator {
private final MySQLConnection connection; private final MySQLConnection connection;
private final ArrayList<String> fields; private final ArrayList<TableField> fields = new ArrayList<>();
private final String tableName; private final String tableName;
/** /**
@ -21,18 +24,7 @@ public class TableGenerator {
public TableGenerator(MySQLConnection connection, String tableName) { public TableGenerator(MySQLConnection connection, String tableName) {
this.tableName = tableName; this.tableName = tableName;
this.connection = connection; this.connection = connection;
this.fields = new ArrayList<>(); addField(SQLType.INTEGER, "id", 255, null, "AUTO_INCREMENT");
}
/**
* Add a field to the Table
*
* @param field String of the field
* @return this class
*/
public TableGenerator addField(String field) {
fields.add(field);
return this;
} }
/** /**
@ -42,19 +34,11 @@ public class TableGenerator {
* @param name The name 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 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 defaultValue The default value of the field (leave empty for no default value)
* @param optionalParameters Optional parameters you want to add to the statement * @param extras Optional parameters you want to add to the statement
* @return this class * @return this class
*/ */
public TableGenerator addField(SQLType type, String name, Integer length, String defaultValue, String... optionalParameters) { public TableGenerator addField(SQLType type, String name, Integer length, String defaultValue, String... extras) {
StringBuilder temp = new StringBuilder(); fields.add(new TableField().setType(type).setName(name).setLength(length).setDefaultValue(defaultValue).setExtras(extras));
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; return this;
} }
@ -70,8 +54,13 @@ public class TableGenerator {
return addField(type, name, length, ""); return addField(type, name, length, "");
} }
/**
* Add a field to the table
* @param field The field you want to add
* @return this class
*/
public TableGenerator addField(TableField field) { public TableGenerator addField(TableField field) {
fields.add(field.generateSQLRow()); fields.add(field);
return this; return this;
} }
@ -90,19 +79,12 @@ public class TableGenerator {
* Creates the table you wanted * Creates the table you wanted
*/ */
public void create() { public void create() {
StringBuilder sb = new StringBuilder(); SQLQuery query = connection.createQuery(TableCreationQuery.class)
sb.append("CREATE TABLE IF NOT EXISTS ").append(tableName).append(" ( "); .addParameter(QueryParameter.TABLE_NAME, tableName)
.addParameter(QueryParameter.FIELD_LIST, fields)
sb.append("`id` INT(255) NOT NULL AUTO_INCREMENT"); .addParameter(QueryParameter.PRIMARY_KEY, "id")
.build();
for (String field : fields) { connection.update(query);
sb.append(", ").append(field);
}
sb.append(", PRIMARY KEY (`id`)");
sb.append(" ) ENGINE = InnoDB;");
connection.update(sb.toString());
} }
} }