package fr.pandacube.lib.db; import java.sql.PreparedStatement; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import fr.pandacube.lib.util.Log; /** * Builder for a SQL {@code UPDATE} query. * @param the type of te table affected by this update. */ public class SQLUpdateBuilder> { private final Class elemClass; private final SQLWhere where; private final Map, Object> values; /* package */ SQLUpdateBuilder(Class el, SQLWhere w) { elemClass = el; where = w; values = new HashMap<>(); } /* package */ SQLUpdateBuilder(Class el, SQLWhere w, Map, Object> v) { elemClass = el; where = w; values = v; } /** * Sets the value for the specified field. * @param field the field to set. * @param value the value to put in the field. * @return this. * @param the type of the value */ public SQLUpdateBuilder set(SQLField field, T value) { values.put(field, value); return this; } /** * Sets the value for the specified field, without statically checking the value type. * This method is not safe to use. Use {@link #set(SQLField, Object)} instead when possible. * @param field the field to set. * @param value the value to put in the field. * @return this. */ public SQLUpdateBuilder setUnsafe(SQLField field, Object value) { values.put(field, value); return this; } /** * Executes the SQL {@code UPDATE} query. * @return the value returned by {@link PreparedStatement#executeUpdate()}. * @throws DBException if an error occurs when interacting with the database. */ public int execute() throws DBException { if (values.isEmpty()) { Log.warning(new DBException("Trying to do an UPDATE with no values to SET. Query aborted.")); return 0; } StringBuilder sql = new StringBuilder("UPDATE " + DB.getTableName(elemClass) + " SET "); List params = new ArrayList<>(); boolean first = true; for (Map.Entry, Object> entry : values.entrySet()) { if (!first) sql.append(", "); sql.append("`").append(entry.getKey().getName()).append("` = ? "); SQLElement.addValueToSQLObjectList(params, entry.getKey(), entry.getValue()); first = false; } if (where != null) { ParameterizedSQLString ret = where.toSQL(); sql.append(" WHERE ").append(ret.sqlString()); params.addAll(ret.parameters()); } sql.append(";"); return DB.customUpdateStatement(sql.toString(), params); } }