From f83d806674bb1774240ea1b33c0f4e628feb007e Mon Sep 17 00:00:00 2001 From: mathias Date: Thu, 19 Aug 2021 16:40:35 +0200 Subject: [PATCH] Added the table field class --- .../de/gnmyt/SQLToolkit/types/TableField.java | 189 ++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 src/main/java/de/gnmyt/SQLToolkit/types/TableField.java diff --git a/src/main/java/de/gnmyt/SQLToolkit/types/TableField.java b/src/main/java/de/gnmyt/SQLToolkit/types/TableField.java new file mode 100644 index 0000000..ce6a25c --- /dev/null +++ b/src/main/java/de/gnmyt/SQLToolkit/types/TableField.java @@ -0,0 +1,189 @@ +package de.gnmyt.SQLToolkit.types; + +public class TableField { + + private String name; + private String type; + private int length; + private boolean allowNull; + private String defaultValue; + private String[] extra; + + /** + * Basic constructor of the {@link TableField} + * @param name The name of the field + * @param type The type 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 + */ + public TableField(String name, String type, int length, boolean allowNull, String defaultValue) { + this.name = name; + this.type = type; + this.length = length; + this.allowNull = allowNull; + this.defaultValue = defaultValue; + } + + /** + * Simple constructor of the {@link TableField} + * @param name The name of the field + */ + public TableField(String name) { + this.name = name; + } + + /** + * Simple constructor with no prefilled variables + */ + public TableField() { + + } + + /** + * Gets the name of the field + * @return the name of the field + */ + public String getName() { + return name; + } + + /** + * Gets the type of the field + * @return the type of the field + */ + public String getType() { + return type; + } + + /** + * Gets the length of the field + * @return the length of the field + */ + public int getLength() { + return length != 0 ? length : 255; + } + + /** + * Gets the extra of the field + * @return the extra of the field + */ + public String[] getExtra() { + return extra; + } + + /** + * Gets the extras of the field as a string + * @return the extras of the field as a string + */ + public String getExtras() { + StringBuilder extras = new StringBuilder(); + for (int i=0; i < getExtra().length; i++) + extras.append(getExtra()[i]); + return extras.toString(); + } + + /** + * Gets the allowNull variable as sql string + * @return the allowNull variable as sql string + */ + public String getNullAsSQL() { + return !isAllowNull() ? "NOT NULL" : ""; + } + + /** + * Gets the default value of the field + * @return the default value of the field + */ + public String getDefaultValue() { + return !defaultValue.isEmpty() ? "DEFAULT '" + defaultValue + "'" : ""; + } + + /** + * Checks if the field is allowed to have a NULL + * @return true if the field is NULL, otherwise false + */ + public boolean isAllowNull() { + return allowNull; + } + + /** + * Sets the name of the field + * @param name The new name of the field + * @return this class + */ + public TableField setName(String name) { + this.name = name; + return this; + } + + /** + * Sets the type of the field + * @param type The new type of the field + * @return this class + */ + public TableField setType(String type) { + this.type = type; + return this; + } + + /** + * Sets the type of the field (with a sql type) + * @param type The new type of the field + * @return this class + */ + public TableField setType(SQLType type) { + this.type = type.getValue(); + return this; + } + + /** + * Sets the length of the field + * @param length The new length of the field + * @return this class + */ + public TableField setLength(int length) { + this.length = length; + return this; + } + + /** + * Sets the allowNull variable of the field + * @param allowNull true if you want to allow a NULL in the field, otherwise false + * @return this class + */ + public TableField setAllowNull(boolean allowNull) { + this.allowNull = allowNull; + return this; + } + + /** + * Sets the default value of the field + * @param defaultValue The new default value of the field + * @return this class + */ + public TableField setDefaultValue(String defaultValue) { + this.defaultValue = defaultValue; + return this; + } + + /** + * Sets the extras of the field + * @param extra The new extras of the field + * @return this class + */ + public TableField setExtra(String[] extra) { + this.extra = extra; + return this; + } + + /** + * Gets the generated sql row + * @return the generated sql row + */ + public String generateSQLRow() { + return String.format("`%s` %s(%d) %s %s %s", getName(), getType(), getLength(), getNullAsSQL(), getDefaultValue(), getExtras()); + } + + +}