Improve Java/SQL ORM
- Support for custom Java types and conversion for database storage - Enums are considered custom types - SQLField.name is now encapsulated as encouraged by OOP principles - SQLField.name is now auto deducted from the Java field's name in the SQLElement subclass (no more field name in the constructor) - No need to precise the SQLType of an SQLFKField (auto deducted from the target SQLField's type) via static method SQLFKField.customFK() - Support of Java type UUID (stored as CHAR(36) in database. No need to use custom getter and setters for SQLElements using UUID fields
This commit is contained in:
parent
706ae682ec
commit
0391b7a9a0
@ -7,13 +7,12 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import fr.pandacube.java.util.EnumUtil;
|
||||
import org.javatuples.Pair;
|
||||
|
||||
import fr.pandacube.java.util.Log;
|
||||
import fr.pandacube.java.util.orm.SQLWhereChain.SQLBoolOp;
|
||||
import fr.pandacube.java.util.orm.SQLWhereComp.SQLComparator;
|
||||
|
||||
import org.javatuples.Pair;
|
||||
|
||||
/**
|
||||
* <b>ORM = Object-Relational Mapping</b>
|
||||
*
|
||||
@ -99,12 +98,12 @@ public final class ORM {
|
||||
return (SQLField<E, Integer>) SQLElement.fieldsCache.get(elemClass).get("id");
|
||||
}
|
||||
|
||||
public static <E extends SQLElement<E>> List<E> getByIds(Class<E> elemClass, Collection<Integer> ids)
|
||||
public static <E extends SQLElement<E>> SQLElementList<E> getByIds(Class<E> elemClass, Collection<Integer> ids)
|
||||
throws ORMException {
|
||||
return getByIds(elemClass, ids.toArray(new Integer[ids.size()]));
|
||||
}
|
||||
|
||||
public static <E extends SQLElement<E>> List<E> getByIds(Class<E> elemClass, Integer... ids) throws ORMException {
|
||||
public static <E extends SQLElement<E>> SQLElementList<E> getByIds(Class<E> elemClass, Integer... ids) throws ORMException {
|
||||
SQLField<E, Integer> idField = getSQLIdField(elemClass);
|
||||
SQLWhereChain where = new SQLWhereChain(SQLBoolOp.OR);
|
||||
for (Integer id : ids)
|
||||
@ -176,6 +175,7 @@ public final class ORM {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <E extends SQLElement<E>> E getElementInstance(ResultSet set, Class<E> elemClass) throws ORMException {
|
||||
try {
|
||||
E instance = elemClass.getConstructor(int.class).newInstance(set.getInt("id"));
|
||||
@ -184,33 +184,38 @@ public final class ORM {
|
||||
|
||||
for (int c = 1; c <= fieldCount; c++) {
|
||||
String fieldName = set.getMetaData().getColumnLabel(c);
|
||||
if (!instance.getFields().containsKey(fieldName)) continue;
|
||||
|
||||
// ignore when field is present in database but not handled by SQLElement instance
|
||||
@SuppressWarnings("unchecked")
|
||||
if (!instance.getFields().containsKey(fieldName)) continue;
|
||||
|
||||
SQLField<E, Object> sqlField = (SQLField<E, Object>) instance.getFields().get(fieldName);
|
||||
if (sqlField.type.getJavaType().isEnum()) {
|
||||
// JDBC ne supporte pas les enums
|
||||
String enumStrValue = set.getString(c);
|
||||
if (enumStrValue == null || set.wasNull()) instance.set(sqlField, null, false);
|
||||
else {
|
||||
Enum<?> enumValue = EnumUtil.searchUncheckedEnum(sqlField.type.getJavaType(), enumStrValue);
|
||||
if (enumValue == null) throw new ORMException("The enum constant '" + enumStrValue
|
||||
+ "' is not found in enum class " + sqlField.type.getJavaType().getName());
|
||||
instance.set(sqlField, enumValue, false);
|
||||
}
|
||||
|
||||
boolean customType = sqlField.type instanceof SQLCustomType;
|
||||
|
||||
Object val = set.getObject(c,
|
||||
(Class<?>)(customType ? ((SQLCustomType<?, ?>)sqlField.type).intermediateJavaType
|
||||
: sqlField.type.getJavaType()));
|
||||
|
||||
if (val == null || set.wasNull()) {
|
||||
instance.set(sqlField, null, false);
|
||||
}
|
||||
else {
|
||||
Object val = set.getObject(c, sqlField.type.getJavaType());
|
||||
if (val == null || set.wasNull()) instance.set(sqlField, null, false);
|
||||
else
|
||||
instance.set(sqlField, val, false);
|
||||
}
|
||||
if (customType) {
|
||||
try {
|
||||
val = ((SQLCustomType<Object, Object>)sqlField.type).dbToJavaConv.apply(val);
|
||||
} catch (Exception e) {
|
||||
throw new ORMException("Error while converting value of field '"+sqlField.getName()+"' with SQLCustomType from "+((SQLCustomType<Object, Object>)sqlField.type).intermediateJavaType
|
||||
+"(jdbc source) to "+sqlField.type.getJavaType()+"(java destination). The original value is '"+val.toString()+"'", e);
|
||||
}
|
||||
}
|
||||
|
||||
// la valeur venant de la BDD est marqué comme "non modifié"
|
||||
// dans l'instance
|
||||
// car le constructeur de l'instance met tout les champs comme
|
||||
// modifiés
|
||||
instance.modifiedSinceLastSave.remove(sqlField.name);
|
||||
instance.set(sqlField, val, false);
|
||||
// la valeur venant de la BDD est marqué comme "non modifié"
|
||||
// dans l'instance car le constructeur de l'instance met
|
||||
// tout les champs comme modifiés
|
||||
instance.modifiedSinceLastSave.remove(sqlField.getName());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (!instance.isValidForSave()) throw new ORMException(
|
||||
|
32
src/main/java/fr/pandacube/java/util/orm/SQLCustomType.java
Normal file
32
src/main/java/fr/pandacube/java/util/orm/SQLCustomType.java
Normal file
@ -0,0 +1,32 @@
|
||||
package fr.pandacube.java.util.orm;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Marc
|
||||
*
|
||||
* @param <IT> intermediate type, the type of the value transmitted to the JDBC
|
||||
* @param <JT> Java type
|
||||
*/
|
||||
public class SQLCustomType<IT, JT> extends SQLType<JT> {
|
||||
|
||||
public final Class<IT> intermediateJavaType;
|
||||
public final Function<IT, JT> dbToJavaConv;
|
||||
public final Function<JT, IT> javaToDbConv;
|
||||
|
||||
protected SQLCustomType(SQLType<IT> type, Class<JT> javaT, Function<IT, JT> dbToJava, Function<JT, IT> javaToDb) {
|
||||
this(type.sqlDeclaration, type.getJavaType(), javaT, dbToJava, javaToDb);
|
||||
}
|
||||
|
||||
protected SQLCustomType(String sqlD, Class<IT> intermediateJavaT, Class<JT> javaT, Function<IT, JT> dbToJava, Function<JT, IT> javaToDb) {
|
||||
super(sqlD, javaT);
|
||||
intermediateJavaType = intermediateJavaT;
|
||||
dbToJavaConv = dbToJava;
|
||||
javaToDbConv = javaToDb;
|
||||
}
|
||||
|
||||
|
||||
// tester en local
|
||||
|
||||
}
|
@ -52,7 +52,9 @@ public abstract class SQLElement<E extends SQLElement<E>> {
|
||||
fields = new SQLFieldMap<>((Class<E>)getClass());
|
||||
|
||||
// le champ id commun à toutes les tables
|
||||
fields.addField(new SQLField<>("id", SQLType.INT, false, true, 0));
|
||||
SQLField<E, Integer> idF = new SQLField<>(SQLType.INT, false, true, 0);
|
||||
idF.setName("id");
|
||||
fields.addField(idF);
|
||||
|
||||
generateFields(fields);
|
||||
fieldsCache.put((Class<E>)getClass(), fields);
|
||||
@ -91,6 +93,7 @@ public abstract class SQLElement<E extends SQLElement<E>> {
|
||||
else if (f.canBeNull || (f.autoIncrement && !stored)) set(f, null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void generateFields(SQLFieldMap<E> listToFill) {
|
||||
|
||||
java.lang.reflect.Field[] declaredFields = getClass().getDeclaredFields();
|
||||
@ -100,7 +103,11 @@ public abstract class SQLElement<E extends SQLElement<E>> {
|
||||
try {
|
||||
Object val = field.get(null);
|
||||
if (val == null || !(val instanceof SQLField)) continue;
|
||||
|
||||
SQLField<E, ?> checkedF = (SQLField<E, ?>) val;
|
||||
checkedF.setName(field.getName());
|
||||
if (listToFill.containsKey(checkedF.getName())) throw new IllegalArgumentException(
|
||||
"SQLField " + checkedF.getName() + " already exist in " + getClass().getName());
|
||||
checkedF.setSQLElementType((Class<E>) getClass());
|
||||
listToFill.addField((SQLField<?, ?>) val);
|
||||
} catch (IllegalArgumentException | IllegalAccessException e) {
|
||||
Log.severe("Can't get value of static field " + field.toString(), e);
|
||||
@ -124,30 +131,30 @@ public abstract class SQLElement<E extends SQLElement<E>> {
|
||||
/* package */ <T> void set(SQLField<E, T> sqlField, T value, boolean setModified) {
|
||||
if (sqlField == null) throw new IllegalArgumentException("sqlField can't be null");
|
||||
if (!fields.containsValue(sqlField))
|
||||
throw new IllegalArgumentException(sqlField.getSQLElementType().getName()+sqlField.name + " is not a SQLField of " + getClass().getName());
|
||||
throw new IllegalArgumentException(sqlField.getSQLElementType().getName()+sqlField.getName() + " is not a SQLField of " + getClass().getName());
|
||||
|
||||
boolean modify = false;
|
||||
if (value == null) {
|
||||
if (sqlField.canBeNull || (sqlField.autoIncrement && !stored)) modify = true;
|
||||
else
|
||||
throw new IllegalArgumentException(
|
||||
"SQLField '" + sqlField.name + "' of " + getClass().getName() + " is a NOT NULL field");
|
||||
"SQLField '" + sqlField.getName() + "' of " + getClass().getName() + " is a NOT NULL field");
|
||||
}
|
||||
else if (sqlField.type.isAssignableFrom(value)) modify = true;
|
||||
else
|
||||
throw new IllegalArgumentException("SQLField '" + sqlField.name + "' of " + getClass().getName()
|
||||
throw new IllegalArgumentException("SQLField '" + sqlField.getName() + "' of " + getClass().getName()
|
||||
+ " type is '" + sqlField.type.toString() + "' and can't accept values of type "
|
||||
+ value.getClass().getName());
|
||||
|
||||
if (modify) if (!values.containsKey(sqlField)) {
|
||||
values.put(sqlField, value);
|
||||
if (setModified) modifiedSinceLastSave.add(sqlField.name);
|
||||
if (setModified) modifiedSinceLastSave.add(sqlField.getName());
|
||||
}
|
||||
else {
|
||||
Object oldVal = values.get(sqlField);
|
||||
if (!Objects.equals(oldVal, value)) {
|
||||
values.put(sqlField, value);
|
||||
if (setModified) modifiedSinceLastSave.add(sqlField.name);
|
||||
if (setModified) modifiedSinceLastSave.add(sqlField.getName());
|
||||
}
|
||||
// sinon, rien n'est modifié
|
||||
}
|
||||
@ -161,7 +168,7 @@ public abstract class SQLElement<E extends SQLElement<E>> {
|
||||
T val = (T) values.get(field);
|
||||
return val;
|
||||
}
|
||||
throw new IllegalArgumentException("The field '" + field.name + "' in this instance of " + getClass().getName()
|
||||
throw new IllegalArgumentException("The field '" + field.getName() + "' in this instance of " + getClass().getName()
|
||||
+ " does not exist or is not set");
|
||||
}
|
||||
|
||||
@ -179,13 +186,13 @@ public abstract class SQLElement<E extends SQLElement<E>> {
|
||||
private Map<SQLField<E, ?>, Object> getOnlyModifiedValues() {
|
||||
Map<SQLField<E, ?>, Object> modifiedValues = new LinkedHashMap<>();
|
||||
values.forEach((k, v) -> {
|
||||
if (modifiedSinceLastSave.contains(k.name)) modifiedValues.put(k, v);
|
||||
if (modifiedSinceLastSave.contains(k.getName())) modifiedValues.put(k, v);
|
||||
});
|
||||
return modifiedValues;
|
||||
}
|
||||
|
||||
public boolean isModified(SQLField<E, ?> field) {
|
||||
return modifiedSinceLastSave.contains(field.name);
|
||||
return modifiedSinceLastSave.contains(field.getName());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@ -214,16 +221,8 @@ public abstract class SQLElement<E extends SQLElement<E>> {
|
||||
List<Object> psValues = new ArrayList<>();
|
||||
|
||||
for (Map.Entry<SQLField<E, ?>, Object> entry : modifiedValues.entrySet()) {
|
||||
sql += entry.getKey().name + " = ? ,";
|
||||
if (entry.getKey().type.getJavaType().isEnum()) // prise en
|
||||
// charge
|
||||
// enum (non
|
||||
// prise en
|
||||
// charge
|
||||
// par JDBC)
|
||||
psValues.add(((Enum<?>) entry.getValue()).name());
|
||||
else
|
||||
psValues.add(entry.getValue());
|
||||
sql += entry.getKey().getName() + " = ? ,";
|
||||
addValueToSQLObjectList(psValues, entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
if (sql.length() > 0) sql = sql.substring(0, sql.length() - 1);
|
||||
@ -260,16 +259,8 @@ public abstract class SQLElement<E extends SQLElement<E>> {
|
||||
}
|
||||
first = false;
|
||||
concat_vals += " ? ";
|
||||
concat_fields += entry.getKey().name;
|
||||
if (entry.getKey().type.getJavaType().isEnum()) // prise en
|
||||
// charge
|
||||
// enum (non
|
||||
// prise en
|
||||
// charge
|
||||
// par JDBC)
|
||||
psValues.add(((Enum<?>) entry.getValue()).name());
|
||||
else
|
||||
psValues.add(entry.getValue());
|
||||
concat_fields += entry.getKey().getName();
|
||||
addValueToSQLObjectList(psValues, entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
PreparedStatement ps = conn.prepareStatement(
|
||||
@ -305,6 +296,20 @@ public abstract class SQLElement<E extends SQLElement<E>> {
|
||||
Log.debug(toStringStatement);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
protected static <E extends SQLElement<E>> void addValueToSQLObjectList(List<Object> list, SQLField<E, ?> field, Object jValue) throws ORMException {
|
||||
if (jValue != null && field.type instanceof SQLCustomType) {
|
||||
try {
|
||||
jValue = ((SQLCustomType)field.type).javaToDbConv.apply(jValue);
|
||||
} catch (Exception e) {
|
||||
throw new ORMException("Error while converting value of field '"+field.getName()+"' with SQLCustomType from "+field.type.getJavaType()
|
||||
+"(java source) to "+((SQLCustomType<?, ?>)field.type).intermediateJavaType+"(jdbc destination). The original value is '"+jValue.toString()+"'", e);
|
||||
}
|
||||
}
|
||||
list.add(jValue);
|
||||
}
|
||||
|
||||
public boolean isStored() {
|
||||
return stored;
|
||||
}
|
||||
@ -346,7 +351,7 @@ public abstract class SQLElement<E extends SQLElement<E>> {
|
||||
stored = false;
|
||||
id = 0;
|
||||
modifiedSinceLastSave.clear();
|
||||
values.forEach((k, v) -> modifiedSinceLastSave.add(k.name));
|
||||
values.forEach((k, v) -> modifiedSinceLastSave.add(k.getName()));
|
||||
}
|
||||
|
||||
protected static class SQLFieldMap<E extends SQLElement<E>> extends LinkedHashMap<String, SQLField<E, ?>> {
|
||||
@ -360,12 +365,12 @@ public abstract class SQLElement<E extends SQLElement<E>> {
|
||||
|
||||
private void addField(SQLField<?, ?> f) {
|
||||
if (f == null) return;
|
||||
if (containsKey(f.name)) throw new IllegalArgumentException(
|
||||
"SQLField " + f.name + " already exist in " + sqlElemClass.getName());
|
||||
if (containsKey(f.getName())) throw new IllegalArgumentException(
|
||||
"SQLField " + f.getName() + " already exist in " + sqlElemClass.getName());
|
||||
@SuppressWarnings("unchecked")
|
||||
SQLField<E, ?> checkedF = (SQLField<E, ?>) f;
|
||||
checkedF.setSQLElementType(sqlElemClass);
|
||||
put(checkedF.name, checkedF);
|
||||
put(checkedF.getName(), checkedF);
|
||||
}
|
||||
|
||||
}
|
||||
@ -376,9 +381,9 @@ public abstract class SQLElement<E extends SQLElement<E>> {
|
||||
|
||||
for (SQLField<E, ?> f : fields.values())
|
||||
try {
|
||||
b.append(f.name, get(f));
|
||||
b.append(f.getName(), get(f));
|
||||
} catch (IllegalArgumentException e) {
|
||||
b.append(f.name, "(Undefined)");
|
||||
b.append(f.getName(), "(Undefined)");
|
||||
}
|
||||
|
||||
return b.toString();
|
||||
@ -402,7 +407,7 @@ public abstract class SQLElement<E extends SQLElement<E>> {
|
||||
public JsonObject asJsonObject() {
|
||||
JsonObject json = new JsonObject();
|
||||
for (SQLField<E, ?> f : getFields().values()) {
|
||||
json.add(f.name, new Gson().toJsonTree(get(f)));
|
||||
json.add(f.getName(), new Gson().toJsonTree(get(f)));
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
|
||||
public synchronized <T> void setCommon(SQLField<E, T> field, T value) {
|
||||
if (field == null)
|
||||
throw new IllegalArgumentException("field can't be null");
|
||||
if (field.name == "id")
|
||||
if (field.getName() == "id")
|
||||
throw new IllegalArgumentException("Can't modify id field in a SQLElementList");
|
||||
|
||||
Class<E> elemClass = field.getSQLElementType();
|
||||
@ -78,7 +78,7 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
|
||||
*
|
||||
* @throws SQLException
|
||||
*/
|
||||
public synchronized void saveCommon() throws SQLException {
|
||||
public synchronized void saveCommon() throws ORMException {
|
||||
List<E> storedEl = getStoredEl();
|
||||
if (storedEl.isEmpty()) return;
|
||||
|
||||
@ -86,12 +86,12 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
|
||||
List<Object> psValues = new ArrayList<>();
|
||||
|
||||
for (Map.Entry<SQLField<E, ?>, Object> entry : modifiedValues.entrySet()) {
|
||||
sqlSet += entry.getKey().name + " = ? ,";
|
||||
if (entry.getKey().type.getJavaType().isEnum()) // prise en charge
|
||||
// enum (non prise
|
||||
// en charge par
|
||||
// JDBC)
|
||||
sqlSet += entry.getKey().getName() + " = ? ,";
|
||||
SQLElement.addValueToSQLObjectList(psValues, entry.getKey(), entry.getValue());
|
||||
if (entry.getKey().type.getJavaType().isEnum()) {
|
||||
// prise en charge enum (non prise en charge par JDBC)
|
||||
psValues.add(((Enum<?>) entry.getValue()).name());
|
||||
}
|
||||
else
|
||||
psValues.add(entry.getValue());
|
||||
}
|
||||
@ -106,9 +106,9 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
|
||||
sqlWhere += "id = " + el.getId();
|
||||
}
|
||||
|
||||
PreparedStatement ps = ORM.getConnection().getNativeConnection()
|
||||
.prepareStatement("UPDATE " + storedEl.get(0).tableName() + " SET " + sqlSet + " WHERE " + sqlWhere);
|
||||
try {
|
||||
try(PreparedStatement ps = ORM.getConnection().getNativeConnection()
|
||||
.prepareStatement("UPDATE " + storedEl.get(0).tableName() + " SET " + sqlSet + " WHERE " + sqlWhere);) {
|
||||
|
||||
|
||||
int i = 1;
|
||||
for (Object val : psValues)
|
||||
@ -118,8 +118,8 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
|
||||
ps.executeUpdate();
|
||||
|
||||
applyNewValuesToElements(storedEl);
|
||||
} finally {
|
||||
ps.close();
|
||||
} catch (SQLException e) {
|
||||
throw new ORMException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,36 +7,33 @@ public class SQLFKField<E extends SQLElement<E>, T, F extends SQLElement<F>> ext
|
||||
private SQLField<F, T> sqlForeignKeyField;
|
||||
private Class<F> sqlForeignKeyElemClass;
|
||||
|
||||
public SQLFKField(String n, SQLType<T> t, boolean nul, Class<F> fkEl, SQLField<F, T> fkF) {
|
||||
super(n, t, nul);
|
||||
protected SQLFKField(SQLType<T> t, boolean nul, T deflt, Class<F> fkEl, SQLField<F, T> fkF) {
|
||||
super(t, nul, deflt);
|
||||
construct(fkEl, fkF);
|
||||
}
|
||||
|
||||
public SQLFKField(String n, SQLType<T> t, boolean nul, T deflt, Class<F> fkEl, SQLField<F, T> fkF) {
|
||||
super(n, t, nul, deflt);
|
||||
construct(fkEl, fkF);
|
||||
public static <E extends SQLElement<E>, F extends SQLElement<F>> SQLFKField<E, Integer, F> idFK(boolean nul, Class<F> fkEl) {
|
||||
return idFK(nul, null, fkEl);
|
||||
}
|
||||
|
||||
public static <E extends SQLElement<E>, F extends SQLElement<F>> SQLFKField<E, Integer, F> idFK(String n, SQLType<Integer> t, boolean nul,
|
||||
Class<F> fkEl) {
|
||||
public static <E extends SQLElement<E>, F extends SQLElement<F>> SQLFKField<E, Integer, F> idFK(boolean nul, Integer deflt, Class<F> fkEl) {
|
||||
if (fkEl == null) throw new IllegalArgumentException("foreignKeyElement can't be null");
|
||||
try {
|
||||
return new SQLFKField<>(n, t, nul, fkEl, ORM.getSQLIdField(fkEl));
|
||||
SQLField<F, Integer> f = ORM.getSQLIdField(fkEl);
|
||||
return new SQLFKField<>(f.type, nul, deflt, fkEl, f);
|
||||
} catch (ORMInitTableException e) {
|
||||
Log.severe("Can't create Foreign key Field called '" + n + "'", e);
|
||||
Log.severe("Can't create Foreign key Field targetting id field of '"+fkEl+"'", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static <E extends SQLElement<E>, F extends SQLElement<F>> SQLFKField<E, Integer, F> idFKField(String n, SQLType<Integer> t, boolean nul,
|
||||
Integer deflt, Class<F> fkEl) {
|
||||
public static <E extends SQLElement<E>, T, F extends SQLElement<F>> SQLFKField<E, T, F> customFK(boolean nul, Class<F> fkEl, SQLField<F, T> fkF) {
|
||||
return customFK(nul, null, fkEl, fkF);
|
||||
}
|
||||
|
||||
public static <E extends SQLElement<E>, T, F extends SQLElement<F>> SQLFKField<E, T, F> customFK(boolean nul, T deflt, Class<F> fkEl, SQLField<F, T> fkF) {
|
||||
if (fkEl == null) throw new IllegalArgumentException("foreignKeyElement can't be null");
|
||||
try {
|
||||
return new SQLFKField<>(n, t, nul, deflt, fkEl, ORM.getSQLIdField(fkEl));
|
||||
} catch (ORMInitTableException e) {
|
||||
Log.severe("Can't create Foreign key Field called '" + n + "'", e);
|
||||
return null;
|
||||
}
|
||||
return new SQLFKField<>(fkF.type, nul, deflt, fkEl, fkF);
|
||||
}
|
||||
|
||||
private void construct(Class<F> fkEl, SQLField<F, T> fkF) {
|
||||
|
@ -8,36 +8,35 @@ import org.javatuples.Pair;
|
||||
public class SQLField<E extends SQLElement<E>, T> {
|
||||
|
||||
private Class<E> sqlElemClass;
|
||||
public final String name;
|
||||
private String name = null;
|
||||
public final SQLType<T> type;
|
||||
public final boolean canBeNull;
|
||||
public final boolean autoIncrement;
|
||||
/* package */ final T defaultValue;
|
||||
|
||||
public SQLField(String n, SQLType<T> t, boolean nul, boolean autoIncr, T deflt) {
|
||||
name = n;
|
||||
public SQLField(SQLType<T> t, boolean nul, boolean autoIncr, T deflt) {
|
||||
type = t;
|
||||
canBeNull = nul;
|
||||
autoIncrement = autoIncr;
|
||||
defaultValue = deflt;
|
||||
}
|
||||
|
||||
public SQLField(String n, SQLType<T> t, boolean nul) {
|
||||
this(n, t, nul, false, null);
|
||||
public SQLField(SQLType<T> t, boolean nul) {
|
||||
this(t, nul, false, null);
|
||||
}
|
||||
|
||||
public SQLField(String n, SQLType<T> t, boolean nul, boolean autoIncr) {
|
||||
this(n, t, nul, autoIncr, null);
|
||||
public SQLField(SQLType<T> t, boolean nul, boolean autoIncr) {
|
||||
this(t, nul, autoIncr, null);
|
||||
}
|
||||
|
||||
public SQLField(String n, SQLType<T> t, boolean nul, T deflt) {
|
||||
this(n, t, nul, false, deflt);
|
||||
public SQLField(SQLType<T> t, boolean nul, T deflt) {
|
||||
this(t, nul, false, deflt);
|
||||
}
|
||||
|
||||
/* package */ Pair<String, List<Object>> forSQLPreparedStatement() {
|
||||
List<Object> params = new ArrayList<>(1);
|
||||
if (defaultValue != null && !autoIncrement) params.add(defaultValue);
|
||||
return new Pair<>(name + " " + type.toString() + (canBeNull ? " NULL" : " NOT NULL")
|
||||
return new Pair<>(getName() + " " + type.toString() + (canBeNull ? " NULL" : " NOT NULL")
|
||||
+ (autoIncrement ? " AUTO_INCREMENT" : "")
|
||||
+ ((defaultValue == null || autoIncrement) ? "" : " DEFAULT ?"), params);
|
||||
}
|
||||
@ -50,6 +49,14 @@ public class SQLField<E extends SQLElement<E>, T> {
|
||||
return sqlElemClass;
|
||||
}
|
||||
|
||||
/* package */ void setName(String n) {
|
||||
name = n;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* <b>Don't use this {@link #toString()} method in a SQL query, because
|
||||
* the default value is not escaped correctly</b>
|
||||
@ -67,14 +74,14 @@ public class SQLField<E extends SQLElement<E>, T> {
|
||||
if (obj == null) return false;
|
||||
if (!(obj instanceof SQLField)) return false;
|
||||
SQLField<?, ?> f = (SQLField<?, ?>) obj;
|
||||
if (!f.name.equals(name)) return false;
|
||||
if (!f.getName().equals(getName())) return false;
|
||||
if (!f.sqlElemClass.equals(sqlElemClass)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return name.hashCode() + sqlElemClass.hashCode();
|
||||
return getName().hashCode() + sqlElemClass.hashCode();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ public class SQLOrderBy {
|
||||
for (OBField f : orderByFields) {
|
||||
if (!first) ret += ", ";
|
||||
first = false;
|
||||
ret += f.field.name + " " + f.direction.name();
|
||||
ret += f.field.getName() + " " + f.direction.name();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
@ -1,22 +1,23 @@
|
||||
package fr.pandacube.java.util.orm;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
import fr.pandacube.java.util.EnumUtil;
|
||||
|
||||
public class SQLType<T> {
|
||||
|
||||
private final String sqlType;
|
||||
private final String sqlTypeParam;
|
||||
protected final String sqlDeclaration;
|
||||
private final Class<T> javaTypes;
|
||||
|
||||
public SQLType(String sqlT, String sqlP, Class<T> javaT) {
|
||||
sqlType = sqlT;
|
||||
sqlTypeParam = sqlP;
|
||||
protected SQLType(String sqlD, Class<T> javaT) {
|
||||
sqlDeclaration = sqlD;
|
||||
javaTypes = javaT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return sqlType + sqlTypeParam;
|
||||
return sqlDeclaration;
|
||||
}
|
||||
|
||||
public boolean isAssignableFrom(Object val) {
|
||||
@ -39,37 +40,38 @@ public class SQLType<T> {
|
||||
return javaTypes;
|
||||
}
|
||||
|
||||
public static final SQLType<Boolean> BOOLEAN = new SQLType<>("BOOLEAN", "", Boolean.class);
|
||||
public static final SQLType<Boolean> BOOLEAN = new SQLType<>("BOOLEAN", Boolean.class);
|
||||
|
||||
public static final SQLType<Byte> TINYINT = new SQLType<>("TINYINT", "", Byte.class);
|
||||
public static final SQLType<Byte> TINYINT = new SQLType<>("TINYINT", Byte.class);
|
||||
public static final SQLType<Byte> BYTE = TINYINT;
|
||||
|
||||
public static final SQLType<Short> SMALLINT = new SQLType<>("SMALLINT", "", Short.class);
|
||||
public static final SQLType<Short> SMALLINT = new SQLType<>("SMALLINT", Short.class);
|
||||
public static final SQLType<Short> SHORT = SMALLINT;
|
||||
|
||||
public static final SQLType<Integer> INT = new SQLType<>("INT", "", Integer.class);
|
||||
public static final SQLType<Integer> INT = new SQLType<>("INT", Integer.class);
|
||||
public static final SQLType<Integer> INTEGER = INT;
|
||||
|
||||
public static final SQLType<Long> BIGINT = new SQLType<>("BIGINT", "", Long.class);
|
||||
public static final SQLType<Long> BIGINT = new SQLType<>("BIGINT", Long.class);
|
||||
public static final SQLType<Long> LONG = BIGINT;
|
||||
|
||||
public static final SQLType<Date> DATE = new SQLType<>("DATE", "", Date.class);
|
||||
public static final SQLType<Date> DATE = new SQLType<>("DATE", Date.class);
|
||||
|
||||
public static final SQLType<Float> FLOAT = new SQLType<>("FLOAT", "", Float.class);
|
||||
public static final SQLType<Float> FLOAT = new SQLType<>("FLOAT", Float.class);
|
||||
|
||||
public static final SQLType<Double> DOUBLE = new SQLType<>("DOUBLE", "", Double.class);
|
||||
public static final SQLType<Double> DOUBLE = new SQLType<>("DOUBLE", Double.class);
|
||||
|
||||
@Deprecated
|
||||
public static final SQLType<String> CHAR(int charCount) {
|
||||
if (charCount <= 0) throw new IllegalArgumentException("charCount must be positive.");
|
||||
return new SQLType<>("CHAR", "(" + charCount + ")", String.class);
|
||||
return new SQLType<>("CHAR(" + charCount + ")", String.class);
|
||||
}
|
||||
|
||||
public static final SQLType<String> VARCHAR(int charCount) {
|
||||
if (charCount <= 0) throw new IllegalArgumentException("charCount must be positive.");
|
||||
return new SQLType<>("VARCHAR", "(" + charCount + ")", String.class);
|
||||
return new SQLType<>("VARCHAR(" + charCount + ")", String.class);
|
||||
}
|
||||
|
||||
public static final SQLType<String> TEXT = new SQLType<>("TEXT", "", String.class);
|
||||
public static final SQLType<String> TEXT = new SQLType<>("TEXT", String.class);
|
||||
public static final SQLType<String> STRING = TEXT;
|
||||
|
||||
public static final <T extends Enum<T>> SQLType<T> ENUM(Class<T> enumType) {
|
||||
@ -84,7 +86,10 @@ public class SQLType<T> {
|
||||
}
|
||||
enumStr += "'";
|
||||
|
||||
return new SQLType<>("VARCHAR", "(" + enumStr + ")", enumType);
|
||||
return new SQLCustomType<>("VARCHAR(" + enumStr + ")", String.class, enumType, s -> EnumUtil.searchEnum(enumType, s), Enum::name);
|
||||
}
|
||||
|
||||
|
||||
public static final SQLType<UUID> CHAR36_UUID = new SQLCustomType<>(SQLType.CHAR(36), UUID.class, UUID::fromString, UUID::toString);
|
||||
|
||||
}
|
||||
|
@ -4,13 +4,20 @@ import java.util.List;
|
||||
|
||||
import org.javatuples.Pair;
|
||||
|
||||
import fr.pandacube.java.util.Log;
|
||||
|
||||
public abstract class SQLWhere {
|
||||
|
||||
public abstract Pair<String, List<Object>> toSQL();
|
||||
public abstract Pair<String, List<Object>> toSQL() throws ORMException;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toSQL().getValue0();
|
||||
try {
|
||||
return toSQL().getValue0();
|
||||
} catch (ORMException e) {
|
||||
Log.warning(e);
|
||||
return "[SQLWhere.toString() error (see logs)]";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ public class SQLWhereChain extends SQLWhere {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<String, List<Object>> toSQL() {
|
||||
public Pair<String, List<Object>> toSQL() throws ORMException {
|
||||
String sql = "";
|
||||
List<Object> params = new ArrayList<>();
|
||||
boolean first = true;
|
||||
|
@ -27,19 +27,24 @@ public class SQLWhereComp extends SQLWhere {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<String, List<Object>> toSQL() {
|
||||
public Pair<String, List<Object>> toSQL() throws ORMException {
|
||||
List<Object> params = new ArrayList<>();
|
||||
params.add(right);
|
||||
return new Pair<>(left.name + " " + comp.sql + " ? ", params);
|
||||
SQLElement.addValueToSQLObjectList(params, left, right);
|
||||
return new Pair<>(left.getName() + " " + comp.sql + " ? ", params);
|
||||
}
|
||||
|
||||
public enum SQLComparator {
|
||||
/** Equivalent to SQL "<code>=</code>" */
|
||||
EQ("="), /** Equivalent to SQL "<code>></code>" */
|
||||
GT(">"), /** Equivalent to SQL "<code>>=</code>" */
|
||||
GEQ(">="), /** Equivalent to SQL "<code><</code>" */
|
||||
LT("<"), /** Equivalent to SQL "<code><=</code>" */
|
||||
LEQ("<="), /** Equivalent to SQL "<code>!=</code>" */
|
||||
EQ("="),
|
||||
/** Equivalent to SQL "<code>></code>" */
|
||||
GT(">"),
|
||||
/** Equivalent to SQL "<code>>=</code>" */
|
||||
GEQ(">="),
|
||||
/** Equivalent to SQL "<code><</code>" */
|
||||
LT("<"),
|
||||
/** Equivalent to SQL "<code><=</code>" */
|
||||
LEQ("<="),
|
||||
/** Equivalent to SQL "<code>!=</code>" */
|
||||
NEQ("!=");
|
||||
|
||||
public final String sql;
|
||||
|
@ -27,7 +27,7 @@ public class SQLWhereLike extends SQLWhere {
|
||||
public Pair<String, List<Object>> toSQL() {
|
||||
ArrayList<Object> params = new ArrayList<>();
|
||||
params.add(likeExpr);
|
||||
return new Pair<>(field.name + " LIKE ? ", params);
|
||||
return new Pair<>(field.getName() + " LIKE ? ", params);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -23,14 +23,14 @@ public class SQLWhereNull extends SQLWhere {
|
||||
if (field == null) throw new IllegalArgumentException("field can't be null");
|
||||
if (!field.canBeNull) Log.getLogger().log(Level.WARNING,
|
||||
"Useless : Trying to check IS [NOT] NULL on the field " + field.getSQLElementType().getName() + "#"
|
||||
+ field.name + " which is declared in the ORM as 'can't be null'");
|
||||
+ field.getName() + " which is declared in the ORM as 'can't be null'");
|
||||
fild = field;
|
||||
nulll = isNull;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<String, List<Object>> toSQL() {
|
||||
return new Pair<>(fild.name + " IS" + ((nulll) ? " NULL" : " NOT NULL"), new ArrayList<>());
|
||||
return new Pair<>(fild.getName() + ((nulll) ? " IS NULL" : " IS NOT NULL"), new ArrayList<>());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user