All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ptsmods.mysqlw.table.ColumnStructure Maven / Gradle / Ivy

There is a newer version: 1.5.1
Show newest version
package com.ptsmods.mysqlw.table;

import com.ptsmods.mysqlw.Database;

import javax.annotation.Nullable;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * A structure used to describe a column.
* Used when making new tables.
* Acquire a {@link ColumnStructure} via {@link ColumnType#createStructure()} on any of the types in that class. * @param The type of the supplier or functions used to get the typeString of the {@link ColumnType} used to create this structure. */ public class ColumnStructure { private final ColumnType type; private String typeString = null; private boolean unique = false; private boolean primary = false; private ColumnDefault defValue = null; private ColumnAttributes attributes = null; private boolean nullAllowed = true; private boolean autoIncrement = false; private String comment = null; private String extra = null; private boolean readOnly = false; ColumnStructure(ColumnType type) { this.type = type; if (getSupplier() instanceof Supplier) typeString = ((Supplier) getSupplier()).get(); } public ColumnType getType() { return type; } /** * Basically what satiating the supplier does, except you put in raw data.
* Don't forget to include the type in here too.
* Example: VARCHAR(255) * @param typeString The new typeString. * @return This structure */ public ColumnStructure setTypeString(String typeString) { this.typeString = typeString; return this; } /** * Run and return the value of the supplier of the selected {@link ColumnType}. *

THIS MUST BE RAN, UNLESS THE SUPPLIER IS AN INSTANCE OF {@link Supplier}.

* @param run The function that gets the supplier and returns its value. * @return This structure */ public ColumnStructure satiateSupplier(Function run) { checkRO(); typeString = run.apply(getSupplier()); return this; } /** * @return The supplier that has to be satiated. * @see #satiateSupplier(Function) */ public S getSupplier() { return type.getSupplier(); } /** * @param unique Whether this column should only contain unique values. * @return This structure */ public ColumnStructure setUnique(boolean unique) { checkRO(); this.unique = unique; return this; } /** * @param primary Whether this column is the PRIMARY KEY of its table. * @return This structure */ public ColumnStructure setPrimary(boolean primary) { checkRO(); this.primary = primary; return this; } /** * @param defValue The default value of this column, either {@link ColumnDefault#NULL NULL} or {@link ColumnDefault#CURRENT_TIMESTAMP CURRENT_TIMESTAMP}. * @return This structure */ public ColumnStructure setDefault(@Nullable ColumnDefault defValue) { checkRO(); this.defValue = defValue; return this; } /** * @param attributes The attributes of the type of this column. * @return This structure */ public ColumnStructure setAttributes(@Nullable ColumnAttributes attributes) { checkRO(); this.attributes = attributes; return this; } /** * @param nullAllowed Whether this column can contain null values. * @return This structure */ public ColumnStructure setNullAllowed(boolean nullAllowed) { checkRO(); this.nullAllowed = nullAllowed; return this; } /** * @param autoIncrement Whether the value of this column should be incremented by one for each row inserted. * @return This structure. */ public ColumnStructure setAutoIncrement(boolean autoIncrement) { checkRO(); this.autoIncrement = autoIncrement; return this; } /** * @param comment The comment of this column. Used to describe what it's for. * @return This structure */ public ColumnStructure setComment(@Nullable String comment) { checkRO(); this.comment = comment; return this; } /** * @param extra Anything else you could possibly want to add that this class does not cover. It would also be appreciated if you could make a pull request or issue to cover this on the GitHub page. * @return This structure */ public ColumnStructure setExtra(@Nullable String extra) { checkRO(); this.extra = extra; return this; } /** * Makes this ColumnStructure immutable so it cannot be edited.
* Used when describing a table. * @return This ColumnStructure */ public ColumnStructure readOnly() { readOnly = true; return this; } private void checkRO() { if (readOnly) throw new IllegalArgumentException("This ColumnStructure is immutable and can thus not be edited anymore"); } /** * @return A shallow copy of this structure. */ @Override public ColumnStructure clone() { ColumnStructure clone = new ColumnStructure<>(type); clone.typeString = typeString; clone.unique = unique; clone.primary = primary; clone.defValue = defValue; clone.attributes = attributes; clone.nullAllowed = nullAllowed; clone.autoIncrement = autoIncrement; clone.comment = comment; clone.extra = extra; return clone; } @Override public String toString() { return toString(Database.RDBMS.UNKNOWN); } public String toString(Database.RDBMS type) { if (typeString == null) throw new IllegalArgumentException("Supplier has not yet been satiated."); StringBuilder builder = new StringBuilder(typeString); if (attributes != null) builder.append(' ').append(attributes.toString()); if (primary) builder.append(" PRIMARY KEY"); if (autoIncrement) builder.append(type == Database.RDBMS.SQLite ? " AUTOINCREMENT" : " AUTO_INCREMENT"); if (unique) builder.append(" UNIQUE"); if (defValue != null) builder.append(" DEFAULT ").append(defValue.getDefString()); builder.append(nullAllowed || defValue == ColumnDefault.NULL ? " NULL" : " NOT NULL"); if (comment != null) builder.append(" COMMENT ").append(Database.enquote(comment)); if (extra != null) builder.append(" ").append(extra); return builder.toString(); } }