PandaLib/pandalib-db/src/main/java/fr/pandacube/lib/db/SQLField.java

141 lines
3.6 KiB
Java
Raw Normal View History

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;
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);
}
/* package */ ParameterizedSQLString forSQLPreparedStatement() {
2021-03-21 12:41:43 +01:00
List<Object> params = new ArrayList<>(1);
if (defaultValue != null && !autoIncrement) params.add(defaultValue);
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;
}
/**
* <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() {
return forSQLPreparedStatement().sqlString().replaceFirst("\\?",
2021-03-21 12:41:43 +01:00
(defaultValue != null && !autoIncrement) ? defaultValue.toString() : "");
}
@Override
public boolean equals(Object obj) {
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();
}
public fr.pandacube.lib.db.SQLWhere<E> eq(T r) {
2021-03-21 12:41:43 +01:00
return comp(SQLComparator.EQ, r);
}
public fr.pandacube.lib.db.SQLWhere<E> geq(T r) {
2021-03-21 12:41:43 +01:00
return comp(SQLComparator.GEQ, r);
}
public fr.pandacube.lib.db.SQLWhere<E> gt(T r) {
2021-03-21 12:41:43 +01:00
return comp(SQLComparator.GT, r);
}
public fr.pandacube.lib.db.SQLWhere<E> leq(T r) {
2021-03-21 12:41:43 +01:00
return comp(SQLComparator.LEQ, r);
}
public fr.pandacube.lib.db.SQLWhere<E> lt(T r) {
2021-03-21 12:41:43 +01:00
return comp(SQLComparator.LT, r);
}
public fr.pandacube.lib.db.SQLWhere<E> neq(T r) {
2021-03-21 12:41:43 +01:00
return comp(SQLComparator.NEQ, r);
}
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);
}
public fr.pandacube.lib.db.SQLWhere<E> like(String like) {
2021-03-21 12:41:43 +01:00
return new SQLWhereLike<>(this, like);
}
public fr.pandacube.lib.db.SQLWhere<E> in(Collection<T> v) {
2021-03-21 12:41:43 +01:00
return new SQLWhereIn<>(this, v);
}
public fr.pandacube.lib.db.SQLWhere<E> isNull() {
2021-03-21 12:41:43 +01:00
return new SQLWhereNull<>(this, true);
}
public fr.pandacube.lib.db.SQLWhere<E> isNotNull() {
2021-03-21 12:41:43 +01:00
return new SQLWhereNull<>(this, false);
}
}