Cleaned up code

This commit is contained in:
mathias 2021-08-19 16:44:15 +02:00
parent 961778bb0e
commit 2fecd3b402
No known key found for this signature in database
GPG Key ID: 8950DF62139C852A
11 changed files with 301 additions and 161 deletions

View File

@ -28,7 +28,7 @@ public class MySQLConnection {
private String tablePrefixVariable = "";
private String connectString = "";
private Connection con;
private TableFactory tableFactory = new TableFactory(this);
private final TableFactory tableFactory = new TableFactory(this);
/**
* Basic constructor for the connection
@ -194,6 +194,7 @@ public class MySQLConnection {
/**
* Gets the table factory
*
* @return the table factory
*/
public TableFactory getTableFactory() {
@ -267,6 +268,7 @@ public class MySQLConnection {
/**
* Get the current table Prefix
*
* @return the prefix
*/
public String getTablePrefix() {

View File

@ -9,7 +9,7 @@ public class TableFactory {
private final HashMap<Class<? extends SQLTable>, SQLTable> REGISTERED_TABLES = new HashMap<>();
private MySQLConnection connection;
private final MySQLConnection connection;
public TableFactory(MySQLConnection connection) {
this.connection = connection;
@ -17,6 +17,7 @@ public class TableFactory {
/**
* Registers and creates a sql table
*
* @param table The table you want to register
* @return this class
*/
@ -28,6 +29,7 @@ public class TableFactory {
/**
* Gets a registered table from the list
*
* @param tableClass The class of the table you want to get
* @return the instance of the table
*/

View File

@ -15,7 +15,8 @@ public class TableGenerator {
/**
* Basic constructor for the TableGenerator
* @param tableName Name of the table
*
* @param tableName Name of the table
*/
public TableGenerator(MySQLConnection connection, String tableName) {
this.tableName = tableName;
@ -36,10 +37,11 @@ public class TableGenerator {
/**
* 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
* @param defaultValue The default value of the field (leave empty for no default value)
*
* @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
*/
@ -58,8 +60,9 @@ public class TableGenerator {
/**
* 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 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
*/
@ -74,6 +77,7 @@ public class TableGenerator {
/**
* 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
@ -84,6 +88,7 @@ public class TableGenerator {
/**
* Creates the table you wanted
*
* @return this class
*/
public TableGenerator create() {

View File

@ -22,8 +22,9 @@ public class DataBaseSelection {
/**
* Basic constructor for selection
*
* @param connection The current Connection
* @param tableName The table name
* @param tableName The table name
*/
public DataBaseSelection(MySQLConnection connection, String tableName) {
this.whereList = new HashMap<>();
@ -35,6 +36,7 @@ public class DataBaseSelection {
/**
* Basic constructor for selection
*
* @param connection The current Connection
*/
public DataBaseSelection(MySQLConnection connection) {
@ -46,6 +48,7 @@ public class DataBaseSelection {
/**
* Get the temp-generated parameters
*
* @return ArrayList of parameters
*/
private ArrayList<Object> getTempParams() {
@ -57,6 +60,7 @@ public class DataBaseSelection {
/**
* Sets the table name
*
* @param tableName The table name
* @return this class
*/
@ -67,8 +71,9 @@ public class DataBaseSelection {
/**
* Adding another 'where'-statement
*
* @param column Table column name
* @param value Table value name
* @param value Table value name
* @return this class
*/
public DataBaseSelection where(String column, Object value) {
@ -78,6 +83,7 @@ public class DataBaseSelection {
/**
* Sets the limit of the rows
*
* @param limit The new limit
* @return this class
*/
@ -88,6 +94,7 @@ public class DataBaseSelection {
/**
* Sets the limit of the rows
*
* @param limit The new limit
* @return this class
*/
@ -98,6 +105,7 @@ public class DataBaseSelection {
/**
* Get the ResultManager
*
* @return ResultManager
*/
public ResultManager getResult() {
@ -106,6 +114,7 @@ public class DataBaseSelection {
/**
* Get the ResultSet
*
* @return ResultSet
*/
public ResultSet getResultSet() {
@ -114,6 +123,7 @@ public class DataBaseSelection {
/**
* Debug the current Statement
*
* @return this class
*/
public DataBaseSelection printStatement() {
@ -121,16 +131,20 @@ public class DataBaseSelection {
messageBuilder.append(processStatement());
messageBuilder.append(" | params» ");
AtomicBoolean added = new AtomicBoolean(false);
getTempParams().forEach(v -> { messageBuilder.append((added.get()) ? ", " : "").append(v);added.set(true); });
getTempParams().forEach(v -> {
messageBuilder.append((added.get()) ? ", " : "").append(v);
added.set(true);
});
StackTraceElement[] st = Thread.currentThread().getStackTrace();
StackTraceElement stack = st[st.length-1];
LOG.debug("DEBUG <" + stack.getFileName()+":"+stack.getLineNumber() + "> Statement: " + messageBuilder.toString());
StackTraceElement stack = st[st.length - 1];
LOG.debug("DEBUG <" + stack.getFileName() + ":" + stack.getLineNumber() + "> Statement: " + messageBuilder);
return this;
}
/**
* Adding another factors
* @param query MySQL Query
*
* @param query MySQL Query
* @param params Optional parameters for the Query
* @return this class
*/
@ -142,13 +156,19 @@ public class DataBaseSelection {
/**
* Get the current statement query
*
* @return the current statement query
*/
private String processStatement() {
StringBuilder sb = new StringBuilder();
sb.append("SELECT * FROM ").append(tableName).append(" ");
AtomicBoolean used = new AtomicBoolean(false);
whereList.forEach((k, v) -> { if (!used.get()) sb.append("WHERE ");else sb.append("AND ");used.set(true);sb.append(k).append(" = ? "); });
whereList.forEach((k, v) -> {
if (!used.get()) sb.append("WHERE ");
else sb.append("AND ");
used.set(true);
sb.append(k).append(" = ? ");
});
if (limit != 0) sb.append("LIMIT ").append(limit);
optionalQuery.forEach(v -> sb.append(" ").append(v).append(" "));
return sb.toString();

View File

@ -7,12 +7,13 @@ import java.util.concurrent.atomic.AtomicBoolean;
public class InsertManager {
private MySQLConnection connection;
private final MySQLConnection connection;
private String tableName;
private HashMap<String, Object> values;
private final HashMap<String, Object> values;
/**
* Basic constructor for the InsertManager
*
* @param connection The current connection
*/
public InsertManager(MySQLConnection connection) {
@ -22,8 +23,9 @@ public class InsertManager {
/**
* Constructor with direct tableName integration
*
* @param connection The current connection
* @param tableName The name of the table
* @param tableName The name of the table
*/
public InsertManager(MySQLConnection connection, String tableName) {
this.connection = connection;
@ -33,6 +35,7 @@ public class InsertManager {
/**
* Optionally add the tableName / Change the name of the table
*
* @param tableName The name of the table
*/
public void from(String tableName) {
@ -41,8 +44,9 @@ public class InsertManager {
/**
* Query value
*
* @param fieldName The name of the column
* @param value The value of the object you want to insert
* @param value The value of the object you want to insert
* @return this class
*/
public InsertManager value(String fieldName, Object value) {
@ -52,6 +56,7 @@ public class InsertManager {
/**
* Prepares the SQL Query
*
* @return the SQL Query
*/
public String prepareStatement() {
@ -66,7 +71,7 @@ public class InsertManager {
query.append(")");
if (values.size() > 0) query.append(" VALUES (");
AtomicBoolean used_values = new AtomicBoolean(false);
for (int i=0; i < values.size(); i++) {
for (int i = 0; i < values.size(); i++) {
if (used_values.get()) query.append(", ");
used_values.set(true);
query.append("?");
@ -77,6 +82,7 @@ public class InsertManager {
/**
* Execute the current SQL query
*
* @return this class
*/
public InsertManager execute() {

View File

@ -16,6 +16,7 @@ public class ResultManager {
/**
* Basic constructor for the ResultManager
*
* @param resultSet Existing ResultSet
*/
public ResultManager(ResultSet resultSet) {
@ -24,6 +25,7 @@ public class ResultManager {
/**
* Getting the ResultSet
*
* @return ResultSet
*/
public ResultSet getResultSet() {
@ -32,92 +34,140 @@ public class ResultManager {
/**
* Get the Object of an Result
*
* @param column Table column
* @return Object
*/
public Object getObject(String column) {
try { while (resultSet.next()) { return resultSet.getObject(column); }
} catch (Exception err) { LOG.error(err.getMessage()); }
try {
while (resultSet.next()) {
return resultSet.getObject(column);
}
} catch (Exception err) {
LOG.error(err.getMessage());
}
return null;
}
/**
* Get the String of an Result
*
* @param column Table column
* @return String
*/
public String getString(String column) {
try { while (resultSet.next()) { return resultSet.getString(column); }
} catch (Exception err) { LOG.error(err.getMessage()); }
try {
while (resultSet.next()) {
return resultSet.getString(column);
}
} catch (Exception err) {
LOG.error(err.getMessage());
}
return "";
}
/**
* Get the Integer of an Result
*
* @param column Table column
* @return Integer
*/
public Integer getInteger(String column) {
try { while (resultSet.next()) { return resultSet.getInt(column); }
} catch (Exception err) { LOG.error(err.getMessage()); }
try {
while (resultSet.next()) {
return resultSet.getInt(column);
}
} catch (Exception err) {
LOG.error(err.getMessage());
}
return 0;
}
/**
* Get the Boolean of an Result
*
* @param column Table column
* @return Boolean
*/
public Boolean getBoolean(String column) {
try { while (resultSet.next()) { return resultSet.getBoolean(column); }
} catch (Exception err) { LOG.error(err.getMessage()); }
try {
while (resultSet.next()) {
return resultSet.getBoolean(column);
}
} catch (Exception err) {
LOG.error(err.getMessage());
}
return false;
}
public Timestamp getTimestamp(String column) {
try { while (resultSet.next()) { return resultSet.getTimestamp(column); }
} catch (Exception err) { LOG.error(err.getMessage()); }
try {
while (resultSet.next()) {
return resultSet.getTimestamp(column);
}
} catch (Exception err) {
LOG.error(err.getMessage());
}
return null;
}
/**
* Get the count of the Result
*
* @return Integer (Count)
*/
public Integer getRowCount() {
int count=0;
try { while (resultSet.next()) { count++; } } catch (Exception err) { LOG.error(err.getMessage()); }
int count = 0;
try {
while (resultSet.next()) {
count++;
}
} catch (Exception err) {
LOG.error(err.getMessage());
}
return count;
}
/**
* Get a List of all Results
*
* @param column Table column
* @return ArrayList of Result
*/
public ArrayList<String> getList(String column) {
ArrayList<String> results = new ArrayList<>();
try { while (resultSet.next()) { results.add(resultSet.getString(column)); }
} catch (Exception err) { LOG.error(err.getMessage()); }
try {
while (resultSet.next()) {
results.add(resultSet.getString(column));
}
} catch (Exception err) {
LOG.error(err.getMessage());
}
return results;
}
/**
* Get a List of all Results for 2 columns
* @param column Table column #1
*
* @param column Table column #1
* @param column2 Table column #2
* @return HashMap of Result
*/
public HashMap<String, String> getMultipleList(String column, String column2) {
HashMap<String, String> results = new HashMap<>();
try { while (resultSet.next()) {
results.put(resultSet.getString(column), resultSet.getString(column2)); }
} catch (Exception err) { LOG.error(err.getMessage()); }
try {
while (resultSet.next()) {
results.put(resultSet.getString(column), resultSet.getString(column2));
}
} catch (Exception err) {
LOG.error(err.getMessage());
}
return results;
}
/**
* Get a list of all results with all columns
*
* @return ArrayList with the result
*/
public ArrayList<HashMap<String, Object>> getList() {
@ -125,13 +175,15 @@ public class ResultManager {
try {
while (resultSet.next()) {
HashMap<String, Object> result = new HashMap<>();
for (int i=0; i < resultSet.getMetaData().getColumnCount(); i++) {
String columnName = resultSet.getMetaData().getColumnName(i+1);
for (int i = 0; i < resultSet.getMetaData().getColumnCount(); i++) {
String columnName = resultSet.getMetaData().getColumnName(i + 1);
result.put(columnName, resultSet.getObject(columnName));
}
results.add(result);
}
} catch (Exception err) { LOG.error(err.getMessage()); }
} catch (Exception err) {
LOG.error(err.getMessage());
}
return results;
}

View File

@ -12,13 +12,14 @@ public class UpdateManager {
private final Logger LOG = MySQLConnection.LOG;
private MySQLConnection connection;
private final MySQLConnection connection;
private String tableName;
private HashMap<String, Object> whereList;
private HashMap<String, Object> setList;
private final HashMap<String, Object> whereList;
private final HashMap<String, Object> setList;
/**
* Basic constructor for the UpdateManager
*
* @param connection Existing MySQL connection
*/
public UpdateManager(MySQLConnection connection) {
@ -29,10 +30,11 @@ public class UpdateManager {
/**
* Basic constructor for the UpdateManager
*
* @param connection Existing MySQL connection
* @param tableName Table name
* @param tableName Table name
*/
public UpdateManager(MySQLConnection connection, String tableName){
public UpdateManager(MySQLConnection connection, String tableName) {
this.connection = connection;
toTable(tableName);
this.whereList = new HashMap<>();
@ -41,6 +43,7 @@ public class UpdateManager {
/**
* Get the parameters for the SQL statement
*
* @return the parameters
*/
private ArrayList<Object> getTempParams() {
@ -52,6 +55,7 @@ public class UpdateManager {
/**
* Get the current connection
*
* @return MySQL connection
*/
public MySQLConnection getConnection() {
@ -60,6 +64,7 @@ public class UpdateManager {
/**
* Print the deletion statement
*
* @return this class
*/
public UpdateManager printDeleteStatement() {
@ -67,15 +72,19 @@ public class UpdateManager {
messageBuilder.append(processDeleteStatement());
messageBuilder.append(" | params» ");
AtomicBoolean added = new AtomicBoolean(false);
whereList.values().forEach(v -> { messageBuilder.append((added.get()) ? ", " : "").append(v);added.set(true); });
whereList.values().forEach(v -> {
messageBuilder.append((added.get()) ? ", " : "").append(v);
added.set(true);
});
StackTraceElement[] st = Thread.currentThread().getStackTrace();
StackTraceElement stack = st[st.length-1];
LOG.debug("DEBUG <" + stack.getFileName()+":"+stack.getLineNumber() + "> Statement: " + messageBuilder.toString());
StackTraceElement stack = st[st.length - 1];
LOG.debug("DEBUG <" + stack.getFileName() + ":" + stack.getLineNumber() + "> Statement: " + messageBuilder);
return this;
}
/**
* Print the update statement
*
* @return this class
*/
public UpdateManager printUpdateStatement() {
@ -83,15 +92,19 @@ public class UpdateManager {
messageBuilder.append(processUpdateStatement());
messageBuilder.append(" | params» ");
AtomicBoolean added = new AtomicBoolean(false);
getTempParams().forEach(v -> { messageBuilder.append((added.get()) ? ", " : "").append(v);added.set(true); });
getTempParams().forEach(v -> {
messageBuilder.append((added.get()) ? ", " : "").append(v);
added.set(true);
});
StackTraceElement[] st = Thread.currentThread().getStackTrace();
StackTraceElement stack = st[st.length-1];
LOG.debug("DEBUG <" + stack.getFileName()+":"+stack.getLineNumber() + "> Statement: " + messageBuilder.toString());
StackTraceElement stack = st[st.length - 1];
LOG.debug("DEBUG <" + stack.getFileName() + ":" + stack.getLineNumber() + "> Statement: " + messageBuilder);
return this;
}
/**
* Change the tableName
*
* @param tableName New tableName
* @return this class
*/
@ -102,8 +115,9 @@ public class UpdateManager {
/**
* Add a 'where'-clause
*
* @param column Table column
* @param value Value of the column
* @param value Value of the column
* @return this class
*/
public UpdateManager where(String column, Object value) {
@ -113,8 +127,9 @@ public class UpdateManager {
/**
* Add a 'set'-clause
*
* @param column Table column
* @param value Value of the column
* @param value Value of the column
* @return this class
*/
public UpdateManager set(String column, Object value) {
@ -124,6 +139,7 @@ public class UpdateManager {
/**
* Delete the entries with your current conditions
*
* @return this class
*/
public UpdateManager delete() {
@ -133,6 +149,7 @@ public class UpdateManager {
/**
* Update the entries with your current conditions
*
* @return this class
*/
public UpdateManager update() {
@ -142,6 +159,7 @@ public class UpdateManager {
/**
* Get the Query of the 'update'-Statement
*
* @return the Statement
*/
private String processUpdateStatement() {
@ -166,6 +184,7 @@ public class UpdateManager {
/**
* Get the Query of the 'delete'-Statement
*
* @return
*/
private String processDeleteStatement() {
@ -184,6 +203,7 @@ public class UpdateManager {
/**
* Generate a new Table (with prefilled tableName)
*
* @param tableName Name of the new table
* @return the de.gnmyt.SQLToolkit.generator
*/
@ -193,6 +213,7 @@ public class UpdateManager {
/**
* Generate a new Table
*
* @return the de.gnmyt.SQLToolkit.generator
*/
public TableGenerator generateTable() {

View File

@ -8,11 +8,12 @@ import java.util.ArrayList;
public abstract class SQLTable {
private MySQLConnection connection;
private final MySQLConnection connection;
private ArrayList<TableField> tableFields;
/**
* The basic constructor of the {@link SQLTable}
*
* @param connection The mysql connection you want to use
*/
public SQLTable(MySQLConnection connection) {
@ -25,10 +26,11 @@ public abstract class SQLTable {
/**
* Adds a string to the table (without allowNull)
* @param name The name of the string you want to add
* @param length The length of the string you want to add
*
* @param name The name of the string you want to add
* @param length The length of the string you want to add
* @param defaultValue The default value of the string you want to add (leave empty if you don't want to use one)
* @param extras The extras you want to add to the string
* @param extras The extras you want to add to the string
*/
protected void string(String name, int length, String defaultValue, String... extras) {
custom(SQLType.STRING.getValue(), name, length, false, defaultValue, extras);
@ -36,11 +38,12 @@ public abstract class SQLTable {
/**
* Adds a string to the table (with allowNull)
* @param name The name of the string you want to add
* @param length The length of the string you want to add
* @param allowNull <code>true</code> if you want to allow a <b>NULL</b> in the field, otherwise <code>false</code>
*
* @param name The name of the string you want to add
* @param length The length of the string you want to add
* @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 string you want to add (leave empty if you don't want to use one)
* @param extras The extras you want to add to the string
* @param extras The extras you want to add to the string
*/
protected void string(String name, int length, boolean allowNull, String defaultValue, String... extras) {
custom(SQLType.STRING.getValue(), name, length, allowNull, defaultValue, extras);
@ -48,10 +51,11 @@ public abstract class SQLTable {
/**
* Adds an integer to the table (without allowNull)
* @param name The name of the integer you want to add
* @param length The length of the integer you want to add
*
* @param name The name of the integer you want to add
* @param length The length of the integer you want to add
* @param defaultValue The default value of the integer you want to add (leave empty if you don't want to use one)
* @param extras The extras you want to add to the integer
* @param extras The extras you want to add to the integer
*/
protected void integer(String name, int length, String defaultValue, String... extras) {
custom(SQLType.INTEGER.getValue(), name, length, false, defaultValue, extras);
@ -59,11 +63,12 @@ public abstract class SQLTable {
/**
* Adds an integer to the table (with allowNull)
* @param name The name of the integer you want to add
* @param length The length of the integer you want to add
* @param allowNull <code>true</code> if you want to allow a <b>NULL</b> in the field, otherwise <code>false</code>
*
* @param name The name of the integer you want to add
* @param length The length of the integer you want to add
* @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 integer you want to add (leave empty if you don't want to use one)
* @param extras The extras you want to add to the integer
* @param extras The extras you want to add to the integer
*/
protected void integer(String name, int length, boolean allowNull, String defaultValue, String... extras) {
custom(SQLType.INTEGER.getValue(), name, length, allowNull, defaultValue, extras);
@ -71,10 +76,11 @@ public abstract class SQLTable {
/**
* Adds a boolean to the table (without allowNull)
* @param name The name of the boolean you want to add
* @param length The length of the boolean you want to add
*
* @param name The name of the boolean you want to add
* @param length The length of the boolean you want to add
* @param defaultValue The default value of the boolean you want to add (leave empty if you don't want to use one)
* @param extras The extras you want to add to the boolean
* @param extras The extras you want to add to the boolean
*/
protected void bool(String name, int length, String defaultValue, String... extras) {
custom(SQLType.BOOLEAN.getValue(), name, length, false, defaultValue, extras);
@ -82,11 +88,12 @@ public abstract class SQLTable {
/**
* Adds a boolean to the table (with allowNull)
* @param name The name of the boolean you want to add
* @param length The length of the boolean you want to add
* @param allowNull <code>true</code> if you want to allow a <b>NULL</b> in the field, otherwise <code>false</code>
*
* @param name The name of the boolean you want to add
* @param length The length of the boolean you want to add
* @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 boolean you want to add (leave empty if you don't want to use one)
* @param extras The extras you want to add to the boolean
* @param extras The extras you want to add to the boolean
*/
protected void bool(String name, int length, boolean allowNull, String defaultValue, String... extras) {
custom(SQLType.BOOLEAN.getValue(), name, length, allowNull, defaultValue, extras);
@ -94,12 +101,13 @@ public abstract class SQLTable {
/**
* Adds a custom field to the table
* @param type The type of the field
* @param name The name of the field
* @param length The length of the field
* @param allowNull <code>true</code> if you want to allow a <b>NULL</b> in the field, otherwise <code>false</code>
*
* @param type The type of the field
* @param name The name of the field
* @param length The length of the field
* @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 extras The extras that you want to add
* @param extras The extras that you want to add
*/
protected void custom(String type, String name, int length, boolean allowNull, String defaultValue, String... extras) {
custom(new TableField(name, type, length, allowNull, defaultValue).setExtra(extras));
@ -107,6 +115,7 @@ public abstract class SQLTable {
/**
* Adds a table field to the table
*
* @param tableField The table field you want to add
*/
protected void custom(TableField tableField) {
@ -115,6 +124,7 @@ public abstract class SQLTable {
/**
* Generates the table sql
*
* @return the table sql
*/
public String generateSQL() {

View File

@ -15,6 +15,7 @@ public enum LoginParam {
/**
* Basic constructor for the LoginParam enum
*
* @param value JDBC parameter
*/
LoginParam(String value) {
@ -23,6 +24,7 @@ public enum LoginParam {
/**
* Get the JDBC value
*
* @return the value
*/
public String getValue() {

View File

@ -12,6 +12,7 @@ public enum SQLType {
/**
* Basic constructor for the SQLType enum
*
* @param value MySQL data type
*/
SQLType(String value) {
@ -20,6 +21,7 @@ public enum SQLType {
/**
* Get the value of the chosen enum
*
* @return
*/
public String getValue() {

View File

@ -11,10 +11,11 @@ public class TableField {
/**
* 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 <code>true</code> if you want to allow a <b>NULL</b> in the field, otherwise <code>false</code>
*
* @param name The name of the field
* @param type The type of the field
* @param length The length of the field
* @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
*/
public TableField(String name, String type, int length, boolean allowNull, String defaultValue) {
@ -27,6 +28,7 @@ public class TableField {
/**
* Simple constructor of the {@link TableField}
*
* @param name The name of the field
*/
public TableField(String name) {
@ -42,73 +44,16 @@ public class 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 <b>NULL</b>
* @return <code>true</code> if the field is <b>NULL</b>, otherwise <code>false</code>
*/
public boolean isAllowNull() {
return allowNull;
}
/**
* Sets the name of the field
*
* @param name The new name of the field
* @return this class
*/
@ -117,8 +62,18 @@ public class TableField {
return this;
}
/**
* Gets the type of the field
*
* @return the type of the field
*/
public String getType() {
return type;
}
/**
* Sets the type of the field
*
* @param type The new type of the field
* @return this class
*/
@ -129,6 +84,7 @@ public class TableField {
/**
* Sets the type of the field (with a sql type)
*
* @param type The new type of the field
* @return this class
*/
@ -137,8 +93,18 @@ public class TableField {
return this;
}
/**
* Gets the length of the field
*
* @return the length of the field
*/
public int getLength() {
return length != 0 ? length : 255;
}
/**
* Sets the length of the field
*
* @param length The new length of the field
* @return this class
*/
@ -148,27 +114,17 @@ public class TableField {
}
/**
* Sets the allowNull variable of the field
* @param allowNull <code>true</code> if you want to allow a <b>NULL</b> in the field, otherwise <code>false</code>
* @return this class
* Gets the extra of the field
*
* @return the extra of the field
*/
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;
public String[] getExtra() {
return extra;
}
/**
* Sets the extras of the field
*
* @param extra The new extras of the field
* @return this class
*/
@ -177,8 +133,70 @@ public class TableField {
return this;
}
/**
* 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 + "'" : "";
}
/**
* 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;
}
/**
* Checks if the field is allowed to have a <b>NULL</b>
*
* @return <code>true</code> if the field is <b>NULL</b>, otherwise <code>false</code>
*/
public boolean isAllowNull() {
return allowNull;
}
/**
* Sets the allowNull variable of the field
*
* @param allowNull <code>true</code> if you want to allow a <b>NULL</b> in the field, otherwise <code>false</code>
* @return this class
*/
public TableField setAllowNull(boolean allowNull) {
this.allowNull = allowNull;
return this;
}
/**
* Gets the generated sql row
*
* @return the generated sql row
*/
public String generateSQLRow() {