Updated the TableField

This commit is contained in:
mathias 2021-08-27 12:22:07 +02:00
parent db70fda9a5
commit 0ab4ae3fed
No known key found for this signature in database
GPG Key ID: 8950DF62139C852A

View File

@ -1,13 +1,15 @@
package de.gnmyt.sqltoolkit.types; package de.gnmyt.sqltoolkit.types;
import de.gnmyt.sqltoolkit.querybuilder.StatementBuilder;
public class TableField { public class TableField {
private String name = "default"; private String name = "default";
private String type = SQLType.STRING.getValue(); private String type = SQLType.STRING.getValue();
private int length = 255;
private boolean allowNull = false; private boolean allowNull = false;
private String defaultValue = ""; private String[] extras = new String[0];
private String[] extra = new String[0]; private int length = 0;
private Object defaultValue;
/** /**
* Basic constructor of the {@link TableField} * Basic constructor of the {@link TableField}
@ -18,7 +20,7 @@ public class TableField {
* @param allowNull <code>true</code> if you want to allow a <b>NULL</b> in the field, otherwise <code>false</code> * @param allowNull <code>true</code> if you want to allow a <b>NULL</b> in the field, otherwise <code>false</code>
* @param defaultValue The default value of the field * @param defaultValue The default value of the field
*/ */
public TableField(String name, String type, int length, boolean allowNull, String defaultValue) { public TableField(String name, String type, int length, boolean allowNull, Object defaultValue) {
this.name = name; this.name = name;
this.type = type; this.type = type;
this.length = length; this.length = length;
@ -99,7 +101,7 @@ public class TableField {
* @return the length of the field * @return the length of the field
*/ */
public int getLength() { public int getLength() {
return length != 0 ? length : 255; return length;
} }
/** /**
@ -118,18 +120,18 @@ public class TableField {
* *
* @return the extra of the field * @return the extra of the field
*/ */
public String[] getExtra() { public String[] getExtras() {
return extra; return extras;
} }
/** /**
* Sets the extras of the field * Sets the extras of the field
* * @param extras The extras you want to add
* @param extra The new extras of the field
* @return this class * @return this class
*/ */
public TableField setExtra(String[] extra) { public TableField setExtras(String... extras) {
this.extra = extra; this.extras = extras;
return this; return this;
} }
@ -138,11 +140,11 @@ public class TableField {
* *
* @return the extras of the field as a string * @return the extras of the field as a string
*/ */
public String getExtras() { public String getExtraString() {
StringBuilder extras = new StringBuilder(); StatementBuilder extras = new StatementBuilder();
for (int i = 0; i < getExtra().length; i++) for (int i = 0; i < getExtras().length; i++)
extras.append(getExtra()[i]); extras.append(getExtras()[i]);
return extras.toString(); return extras.build();
} }
/** /**
@ -160,7 +162,10 @@ public class TableField {
* @return the default value of the field * @return the default value of the field
*/ */
public String getDefaultValue() { public String getDefaultValue() {
return !defaultValue.isEmpty() ? "DEFAULT '" + defaultValue + "'" : ""; if (defaultValue == null) return "";
if (defaultValue instanceof String && (((String) defaultValue).isEmpty())) return "";
return "DEFAULT " + (defaultValue instanceof String ? "'" + defaultValue + "'" : defaultValue);
} }
/** /**
@ -169,7 +174,7 @@ public class TableField {
* @param defaultValue The new default value of the field * @param defaultValue The new default value of the field
* @return this class * @return this class
*/ */
public TableField setDefaultValue(String defaultValue) { public TableField setDefaultValue(Object defaultValue) {
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
return this; return this;
} }
@ -200,7 +205,11 @@ public class TableField {
* @return the generated sql row * @return the generated sql row
*/ */
public String generateSQLRow() { public String generateSQLRow() {
return String.format("`%s` %s(%d) %s %s %s", getName(), getType(), getLength(), getNullAsSQL(), getDefaultValue(), getExtras()); return String.format("`%s` %s%s%s%s%s", getName(), getType(),
getLength() == 0 ? "" : "(" + getLength() + ")",
getNullAsSQL().isEmpty() ? "" : " " + getNullAsSQL(),
getDefaultValue().isEmpty() ? "" : " " + getDefaultValue(),
getExtraString().isEmpty() ? "" : " " + getExtraString());
} }