Various code simplification/fixes and a lot of typo/grammar fixes (may brake some stuff)

This commit is contained in:
2023-06-20 00:15:46 +02:00
parent c984b63cee
commit 5edd8cdfec
151 changed files with 909 additions and 983 deletions

View File

@@ -52,10 +52,10 @@ public final class DB {
}
/**
* Initialialize the table represented by the provided class.
* Initialize the table represented by the provided class.
* @param elemClass the class representing a table.
* @param <E> the type representing the table.
* @throws DBInitTableException if the table failed to initialized.
* @throws DBInitTableException if the table failed to initialize.
*/
public static synchronized <E extends SQLElement<E>> void initTable(Class<E> elemClass) throws DBInitTableException {
if (connection == null) {
@@ -111,7 +111,7 @@ public final class DB {
* @param elemClass the class representing a table.
* @return a table name.
* @param <E> the type representing the table.
* @throws DBInitTableException if the provided table had to be initialized and it failed to do so.
* @throws DBInitTableException if the provided table had to be initialized and failed to do so.
*/
public static <E extends SQLElement<E>> String getTableName(Class<E> elemClass) throws DBInitTableException {
initTable(elemClass);
@@ -130,7 +130,7 @@ public final class DB {
* @param elemClass the class representing a table.
* @return the {@code id} field of the provided table.
* @param <E> the type representing the table.
* @throws DBInitTableException if the provided table had to be initialized and it failed to do so.
* @throws DBInitTableException if the provided table had to be initialized and failed to do so.
*/
@SuppressWarnings("unchecked")
public static <E extends SQLElement<E>> SQLField<E, Integer> getSQLIdField(Class<E> elemClass) throws DBInitTableException {
@@ -226,8 +226,8 @@ public final class DB {
* @throws DBException if an error occurs when interacting with the database.
*/
public static <E extends SQLElement<E>> E getFirst(Class<E> elemClass, SQLWhere<E> where, SQLOrderBy<E> orderBy, Integer offset) throws DBException {
SQLElementList<E> elts = getAll(elemClass, where, orderBy, 1, offset);
return (elts.size() == 0) ? null : elts.get(0);
SQLElementList<E> elements = getAll(elemClass, where, orderBy, 1, offset);
return (elements.size() == 0) ? null : elements.get(0);
}
/**
@@ -294,15 +294,15 @@ public final class DB {
* @throws DBException if an error occurs when interacting with the database.
*/
public static <E extends SQLElement<E>> SQLElementList<E> getAll(Class<E> elemClass, SQLWhere<E> where, SQLOrderBy<E> orderBy, Integer limit, Integer offset) throws DBException {
SQLElementList<E> elmts = new SQLElementList<>();
forEach(elemClass, where, orderBy, limit, offset, elmts::add);
return elmts;
SQLElementList<E> elements = new SQLElementList<>();
forEach(elemClass, where, orderBy, limit, offset, elements::add);
return elements;
}
/**
* Iterate through all the entries from the provided table.
* @param elemClass the class representing a table.
* @param action the action to perform on each entries.
* @param action the action to perform on each entry.
* @param <E> the type representing the table.
* @throws DBException if an error occurs when interacting with the database.
*/
@@ -314,7 +314,7 @@ public final class DB {
* Iterate through the entries from the provided table, using the provided {@code WHERE} clause.
* @param elemClass the class representing a table.
* @param where the {@code WHERE} clause of the query.
* @param action the action to perform on each entries.
* @param action the action to perform on each entry.
* @param <E> the type representing the table.
* @throws DBException if an error occurs when interacting with the database.
*/
@@ -328,7 +328,7 @@ public final class DB {
* @param elemClass the class representing a table.
* @param where the {@code WHERE} clause of the query.
* @param orderBy the {@code ORDER BY} clause of the query.
* @param action the action to perform on each entries.
* @param action the action to perform on each entry.
* @param <E> the type representing the table.
* @throws DBException if an error occurs when interacting with the database.
*/
@@ -343,7 +343,7 @@ public final class DB {
* @param where the {@code WHERE} clause of the query.
* @param orderBy the {@code ORDER BY} clause of the query.
* @param limit the {@code LIMIT} clause of the query.
* @param action the action to perform on each entries.
* @param action the action to perform on each entry.
* @param <E> the type representing the table.
* @throws DBException if an error occurs when interacting with the database.
*/
@@ -359,7 +359,7 @@ public final class DB {
* @param orderBy the {@code ORDER BY} clause of the query.
* @param limit the {@code LIMIT} clause of the query.
* @param offset the {@code OFFSET} clause of the query.
* @param action the action to perform on each entries.
* @param action the action to perform on each entry.
* @param <E> the type representing the table.
* @throws DBException if an error occurs when interacting with the database.
*/
@@ -577,7 +577,7 @@ public final class DB {
@SuppressWarnings("unchecked")
private static <E extends SQLElement<E>> E getElementInstance(ResultSet set, Class<E> elemClass) throws DBException {
try {
E instance = Reflect.ofClass(elemClass).constructor(int.class).instanciate(set.getInt("id"));
E instance = Reflect.ofClass(elemClass).constructor(int.class).instantiate(set.getInt("id"));
int fieldCount = set.getMetaData().getColumnCount();
@@ -623,7 +623,7 @@ public final class DB {
return instance;
} catch (ReflectiveOperationException | IllegalArgumentException | SecurityException | SQLException e) {
throw new DBException("Can't instanciate " + elemClass.getName(), e);
throw new DBException("Can't instantiate " + elemClass.getName(), e);
}
}

View File

@@ -1,7 +1,7 @@
package fr.pandacube.lib.db;
/**
* Exception thrown when something bad happends when using the {@link DB} API.
* Exception thrown when something bad happens when using the {@link DB} API.
*/
public class DBException extends Exception {

View File

@@ -1,7 +1,7 @@
package fr.pandacube.lib.db;
/**
* Exception thrown when something bad happends when initializing a new table using {@link DB#initTable(Class)}.
* Exception thrown when something bad happens when initializing a new table using {@link DB#initTable(Class)}.
*/
public class DBInitTableException extends DBException {

View File

@@ -119,7 +119,7 @@ public abstract class SQLElement<E extends SQLElement<E>> {
/**
* Gets the name of the table in the database, without the prefix defined by {@link DB#init(DBConnection, String)}.
* @return The unprefixed name of the table in the database.
* @return The non-prefixed name of the table in the database.
*/
protected abstract String tableName();
@@ -133,7 +133,7 @@ public abstract class SQLElement<E extends SQLElement<E>> {
}
/**
* Fills the values of this entry that are known to be nullable or have a default value.
* Fills the entries values that are known to be nullable or have a default value.
*/
@SuppressWarnings("unchecked")
private void initDefaultValues() {
@@ -193,7 +193,7 @@ public abstract class SQLElement<E extends SQLElement<E>> {
/**
* Sets a value in this entry.
* <p>
* This is not good practice to set the {@code id} field of any entry, because its an unique auto-incremented
* This is not good practice to set the {@code id} field of any entry, because its a unique auto-incremented
* value. Use {@link #save()} and {@link #delete()} to set or unset the {@code id} instead, in consistence with the
* database.
* @param field the field to set.
@@ -255,11 +255,11 @@ public abstract class SQLElement<E extends SQLElement<E>> {
}
/**
* Gets the foreign table entry targeted by the provided foreignkey of this table.
* @param field a foreignkey of this table.
* @param <T> the type of the foreignkey field.
* Gets the foreign table entry targeted by the provided foreign key of this table.
* @param field a foreign key of this table.
* @param <T> the type of the foreign key field.
* @param <P> the targeted foreign table type.
* @return the foreign table entry targeted by the provided foreignkey of this table.
* @return the foreign table entry targeted by the provided foreign key of this table.
* @throws DBException if an error occurs when interacting with the database.
*/
public <T, P extends SQLElement<P>> P getReferencedEntry(SQLFKField<E, T, P> field) throws DBException {
@@ -271,11 +271,11 @@ public abstract class SQLElement<E extends SQLElement<E>> {
/**
* Gets the original table entry which the provided foreign key is targeting this entry, and following the provided
* {@code ORDER BY}, {@code LIMIT} and {@code OFFSET} clauses.
* @param field a foreignkey in the original table.
* @param field a foreign key in the original table.
* @param orderBy the {@code ORDER BY} clause of the query.
* @param limit the {@code LIMIT} clause of the query.
* @param offset the {@code OFFSET} clause of the query.
* @param <T> the type of the foreignkey field.
* @param <T> the type of the foreign key field.
* @param <F> the table class of the foreign key that reference a field of this entry.
* @return the original table entry which the provided foreign key is targeting this entry.
* @throws DBException if an error occurs when interacting with the database.
@@ -314,7 +314,7 @@ public abstract class SQLElement<E extends SQLElement<E>> {
/**
* Saves this entry into the database, either by updating the already existing entry in it, or by creating a new
* entry if it doesnt exist yet.
* entry if it doesn't exist yet.
* @return this.
* @throws DBException if an error occurs when interacting with the database.
*/
@@ -474,14 +474,14 @@ public abstract class SQLElement<E extends SQLElement<E>> {
* Creates a new SQL field.
* @param type the type of the field.
* @param nullable true if nullable, false if {@code NOT NULL}.
* @param autoIncr if {@code AUTO_INCREMENT}.
* @param autoIncrement if {@code AUTO_INCREMENT}.
* @param deflt a default value for this field. A null value indicate that this has no default value.
* @return the new SQL field.
* @param <E> the table type.
* @param <T> the Java type of this field.
*/
protected static <E extends SQLElement<E>, T> SQLField<E, T> field(SQLType<T> type, boolean nullable, boolean autoIncr, T deflt) {
return new SQLField<>(type, nullable, autoIncr, deflt);
protected static <E extends SQLElement<E>, T> SQLField<E, T> field(SQLType<T> type, boolean nullable, boolean autoIncrement, T deflt) {
return new SQLField<>(type, nullable, autoIncrement, deflt);
}
/**
@@ -500,13 +500,13 @@ public abstract class SQLElement<E extends SQLElement<E>> {
* Creates a new SQL field.
* @param type the type of the field.
* @param nullable true if nullable, false if {@code NOT NULL}.
* @param autoIncr if {@code AUTO_INCREMENT}.
* @param autoIncrement if {@code AUTO_INCREMENT}.
* @return the new SQL field.
* @param <E> the table type.
* @param <T> the Java type of this field.
*/
protected static <E extends SQLElement<E>, T> SQLField<E, T> field(SQLType<T> type, boolean nullable, boolean autoIncr) {
return new SQLField<>(type, nullable, autoIncr);
protected static <E extends SQLElement<E>, T> SQLField<E, T> field(SQLType<T> type, boolean nullable, boolean autoIncrement) {
return new SQLField<>(type, nullable, autoIncrement);
}
/**

View File

@@ -46,7 +46,7 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
E emptyElement = elemClass.getConstructor().newInstance();
emptyElement.set(field, value, false);
} catch (Exception e) {
throw new IllegalArgumentException("Illegal field or value or can't instanciante an empty instance of "
throw new IllegalArgumentException("Illegal field or value or can't instantiate an empty instance of "
+ elemClass.getName() + ". (the instance is only created to test validity of field and value)", e);
}
@@ -83,7 +83,7 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
@SuppressWarnings("unchecked")
private void applyNewValuesToElements(List<E> storedEl) {
// applique les valeurs dans chaques objets de la liste
// applique les valeurs dans chaque objet de la liste
for (E el : storedEl) {
for (@SuppressWarnings("rawtypes") SQLField entry : modifiedValues.keySet()) {
if (!el.isModified(entry)) {
@@ -100,7 +100,7 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
/**
* Removes all the entries of this list from the database.
* This method has the same effect as calling the {@link SQLElement#delete()} method individually on each element,
* but with only one SQL query to delete all of the entries.
* but with only one SQL query to delete all the entries.
* <p>
* If you intend to remove the entries from the database just after fetching them, call directly the
* {@link DB#delete(Class, SQLWhere)} method instead.
@@ -124,9 +124,9 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
/**
* Get all the entries targeted by the foreign key of all the entries in this list.
* @param foreignKey a foreignkey of this table.
* @param foreignKey a foreign key of this table.
* @param orderBy the {@code ORDER BY} clause of the query.
* @return a list of foreign table entries targeted by the provided foreignkey of this table.
* @return a list of foreign table entries targeted by the provided foreign key of this table.
* @param <T> the fields Java type.
* @param <P> the target table type.
* @throws DBException if an error occurs when interacting with the database.
@@ -144,7 +144,7 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
/**
* Get all the entries targeted by the foreign key of all the entries in this list, mapped from the foreign key value.
* @param foreignKey a foreignkey of this table.
* @param foreignKey a foreign key of this table.
* @return a map of the foreign key values, mapped to the foreign tables entries.
* @param <T> the fields Java type.
* @param <P> the target table type.
@@ -163,11 +163,11 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
/**
* Gets all the original tables entries which the provided foreign key is targeting the entries of this list, and
* following the provided {@code ORDER BY}, {@code LIMIT} and {@code OFFSET} clauses.
* @param foreignKey a foreignkey in the original table.
* @param foreignKey a foreign key in the original table.
* @param orderBy the {@code ORDER BY} clause of the query.
* @param limit the {@code LIMIT} clause of the query.
* @param offset the {@code OFFSET} clause of the query.
* @param <T> the type of the foreignkey field.
* @param <T> the type of the foreign key field.
* @param <F> the table class of the foreign key that reference a field of this entry.
* @return the original tables entries which the provided foreign key is targeting the entries of this list.
* @throws DBException if an error occurs when interacting with the database.
@@ -187,11 +187,11 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
* Gets all the original tables entries which the provided foreign key is targeting the entries of this list,
* following the provided {@code ORDER BY}, {@code LIMIT} and {@code OFFSET} clauses, and mapped from the foreign
* key value.
* @param foreignKey a foreignkey in the original table.
* @param foreignKey a foreign key in the original table.
* @param orderBy the {@code ORDER BY} clause of the query.
* @param limit the {@code LIMIT} clause of the query.
* @param offset the {@code OFFSET} clause of the query.
* @param <T> the type of the foreignkey field.
* @param <T> the type of the foreign key field.
* @param <F> the table class of the foreign key that reference a field of this entry.
* @return a map of the foreign key values, mapped to the orignal tables entries.
* @throws DBException if an error occurs when interacting with the database.

View File

@@ -28,7 +28,7 @@ public class SQLFKField<F extends SQLElement<F>, T, P extends SQLElement<P>> ext
SQLField<F, Integer> f = DB.getSQLIdField(fkEl);
return new SQLFKField<>(f.type, nul, deflt, fkEl, f);
} catch (DBInitTableException e) {
Log.severe("Can't create Foreign key Field targetting id field of '"+fkEl+"'", e);
Log.severe("Can't create Foreign key Field targeting id field of '"+fkEl+"'", e);
return null;
}
}
@@ -51,7 +51,7 @@ public class SQLFKField<F extends SQLElement<F>, T, P extends SQLElement<P>> ext
}
if (fkF.getSQLElementType() == null)
throw new RuntimeException("Can't initialize foreign key. The primary key in the table " + fkEl.getName() + " is not properly initialized and can't be targetted by a forein key");
throw new RuntimeException("Can't initialize foreign key. The primary key in the table " + fkEl.getName() + " is not properly initialized and can't be targeted by a foreign key");
sqlPrimaryKeyField = fkF;
sqlForeignKeyElemClass = fkEl;
}

View File

@@ -24,10 +24,10 @@ public class SQLField<E extends SQLElement<E>, T> {
/* package */ final boolean autoIncrement;
/* package */ final T defaultValue;
/* package */ SQLField(SQLType<T> type, boolean nullable, boolean autoIncr, T deflt) {
/* package */ SQLField(SQLType<T> type, boolean nullable, boolean autoIncrement, T deflt) {
this.type = type;
this.nullable = nullable;
autoIncrement = autoIncr;
this.autoIncrement = autoIncrement;
defaultValue = deflt;
}
@@ -35,8 +35,8 @@ public class SQLField<E extends SQLElement<E>, T> {
this(type, nullable, false, null);
}
/* package */ SQLField(SQLType<T> type, boolean nullable, boolean autoIncr) {
this(type, nullable, autoIncr, null);
/* package */ SQLField(SQLType<T> type, boolean nullable, boolean autoIncrement) {
this(type, nullable, autoIncrement, null);
}
/* package */ SQLField(SQLType<T> type, boolean nullable, T deflt) {