2022-07-20 13:18:57 +02:00
|
|
|
package fr.pandacube.lib.db;
|
2021-03-21 12:41:43 +01:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
|
|
|
|
2022-07-20 13:18:57 +02:00
|
|
|
import fr.pandacube.lib.db.SQLWhere.SQLWhereComp;
|
|
|
|
import fr.pandacube.lib.db.SQLWhere.SQLWhereComp.SQLComparator;
|
|
|
|
import fr.pandacube.lib.db.SQLWhere.SQLWhereIn;
|
|
|
|
import fr.pandacube.lib.db.SQLWhere.SQLWhereLike;
|
|
|
|
import fr.pandacube.lib.db.SQLWhere.SQLWhereNull;
|
2021-03-21 12:41:43 +01:00
|
|
|
|
|
|
|
public class SQLField<E extends SQLElement<E>, T> {
|
|
|
|
|
|
|
|
private Class<E> sqlElemClass;
|
|
|
|
private String name = null;
|
|
|
|
public final SQLType<T> type;
|
|
|
|
public final boolean canBeNull;
|
|
|
|
public final boolean autoIncrement;
|
|
|
|
/* package */ final T defaultValue;
|
|
|
|
|
|
|
|
/* package */ SQLField(SQLType<T> t, boolean nul, boolean autoIncr, T deflt) {
|
|
|
|
type = t;
|
|
|
|
canBeNull = nul;
|
|
|
|
autoIncrement = autoIncr;
|
|
|
|
defaultValue = deflt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* package */ SQLField(SQLType<T> t, boolean nul) {
|
|
|
|
this(t, nul, false, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* package */ SQLField(SQLType<T> t, boolean nul, boolean autoIncr) {
|
|
|
|
this(t, nul, autoIncr, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* package */ SQLField(SQLType<T> t, boolean nul, T deflt) {
|
|
|
|
this(t, nul, false, deflt);
|
|
|
|
}
|
|
|
|
|
2021-06-18 02:41:48 +02:00
|
|
|
/* package */ ParameterizedSQLString forSQLPreparedStatement() {
|
2021-03-21 12:41:43 +01:00
|
|
|
List<Object> params = new ArrayList<>(1);
|
|
|
|
if (defaultValue != null && !autoIncrement) params.add(defaultValue);
|
2021-06-18 02:41:48 +02:00
|
|
|
return new ParameterizedSQLString("`" + getName() + "` " + type.toString() + (canBeNull ? " NULL" : " NOT NULL")
|
2021-03-21 12:41:43 +01:00
|
|
|
+ (autoIncrement ? " AUTO_INCREMENT" : "")
|
|
|
|
+ ((defaultValue == null || autoIncrement) ? "" : " DEFAULT ?"), params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* package */ void setSQLElementType(Class<E> elemClass) {
|
|
|
|
sqlElemClass = elemClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Class<E> getSQLElementType() {
|
|
|
|
return sqlElemClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* package */ void setName(String n) {
|
|
|
|
name = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-10 00:55:56 +02:00
|
|
|
* <b>Don't use this {@code toString()} method in a SQL query, because
|
2021-03-21 12:41:43 +01:00
|
|
|
* the default value is not escaped correctly</b>
|
|
|
|
*
|
|
|
|
* @see java.lang.Object#toString()
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2021-06-18 02:41:48 +02:00
|
|
|
return forSQLPreparedStatement().sqlString().replaceFirst("\\?",
|
2021-03-21 12:41:43 +01:00
|
|
|
(defaultValue != null && !autoIncrement) ? defaultValue.toString() : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
2022-07-10 00:55:56 +02:00
|
|
|
return obj instanceof SQLField<?, ?> f
|
|
|
|
&& f.getName().equals(getName())
|
|
|
|
&& f.sqlElemClass.equals(sqlElemClass);
|
2021-03-21 12:41:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return getName().hashCode() + sqlElemClass.hashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-07-20 13:18:57 +02:00
|
|
|
public fr.pandacube.lib.db.SQLWhere<E> eq(T r) {
|
2021-03-21 12:41:43 +01:00
|
|
|
return comp(SQLComparator.EQ, r);
|
|
|
|
}
|
2022-07-20 13:18:57 +02:00
|
|
|
public fr.pandacube.lib.db.SQLWhere<E> geq(T r) {
|
2021-03-21 12:41:43 +01:00
|
|
|
return comp(SQLComparator.GEQ, r);
|
|
|
|
}
|
2022-07-20 13:18:57 +02:00
|
|
|
public fr.pandacube.lib.db.SQLWhere<E> gt(T r) {
|
2021-03-21 12:41:43 +01:00
|
|
|
return comp(SQLComparator.GT, r);
|
|
|
|
}
|
2022-07-20 13:18:57 +02:00
|
|
|
public fr.pandacube.lib.db.SQLWhere<E> leq(T r) {
|
2021-03-21 12:41:43 +01:00
|
|
|
return comp(SQLComparator.LEQ, r);
|
|
|
|
}
|
2022-07-20 13:18:57 +02:00
|
|
|
public fr.pandacube.lib.db.SQLWhere<E> lt(T r) {
|
2021-03-21 12:41:43 +01:00
|
|
|
return comp(SQLComparator.LT, r);
|
|
|
|
}
|
2022-07-20 13:18:57 +02:00
|
|
|
public fr.pandacube.lib.db.SQLWhere<E> neq(T r) {
|
2021-03-21 12:41:43 +01:00
|
|
|
return comp(SQLComparator.NEQ, r);
|
|
|
|
}
|
|
|
|
|
2022-07-20 13:18:57 +02:00
|
|
|
private fr.pandacube.lib.db.SQLWhere<E> comp(SQLComparator c, T r) {
|
2021-03-21 12:41:43 +01:00
|
|
|
if (r == null)
|
|
|
|
throw new IllegalArgumentException("The value cannot be null. Use SQLField#isNull(value) or SQLField#isNotNull(value) to check for null values");
|
|
|
|
return new SQLWhereComp<>(this, c, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-20 13:18:57 +02:00
|
|
|
public fr.pandacube.lib.db.SQLWhere<E> like(String like) {
|
2021-03-21 12:41:43 +01:00
|
|
|
return new SQLWhereLike<>(this, like);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-07-20 13:18:57 +02:00
|
|
|
public fr.pandacube.lib.db.SQLWhere<E> in(Collection<T> v) {
|
2021-03-21 12:41:43 +01:00
|
|
|
return new SQLWhereIn<>(this, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-07-20 13:18:57 +02:00
|
|
|
public fr.pandacube.lib.db.SQLWhere<E> isNull() {
|
2021-03-21 12:41:43 +01:00
|
|
|
return new SQLWhereNull<>(this, true);
|
|
|
|
}
|
|
|
|
|
2022-07-20 13:18:57 +02:00
|
|
|
public fr.pandacube.lib.db.SQLWhere<E> isNotNull() {
|
2021-03-21 12:41:43 +01:00
|
|
|
return new SQLWhereNull<>(this, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|