
net.sf.aguacate.context.spi.sql.impl.SentenceSqlupdate Maven / Gradle / Ivy
package net.sf.aguacate.context.spi.sql.impl;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import net.sf.aguacate.context.spi.sql.SentenceSqlType;
public class SentenceSqlupdate extends AbstractSentenceSqlUpdate {
public SentenceSqlupdate(String name, String table, List methods, List required,
List optional) {
super(name, table, methods, required, optional, null);
}
@Override
public SentenceSqlType getType() {
return SentenceSqlType.UPDATE;
}
@Override
String buildSqlSentence(String table, Map context, String[] required, String[] optional) {
StringBuilder principal = new StringBuilder();
StringBuilder auxiliar = new StringBuilder();
principal.append("UPDATE ").append(table).append(" SET ");
auxiliar.append(" WHERE ");
for (String parameter : required) {
if (context.containsKey(parameter)) {
auxiliar.append(parameter).append("=? AND ");
} else {
return null;
}
}
if (auxiliar.toString().endsWith(" AND ")) {
auxiliar.setLength(auxiliar.length() - " AND ".length());
} else {
return null;
}
for (String parameter : optional) {
if (context.containsKey(parameter)) {
principal.append(parameter).append("=?,");
}
}
int position = principal.length() - 1;
if (principal.charAt(position) == ',') {
principal.setLength(position);
} else {
return null;
}
return principal.append(auxiliar).toString();
}
@Override
void parameters(PreparedStatement statement, Map context, String[] required,
String[] optional) throws SQLException {
int position = 1;
for (String parameter : optional) {
if (context.containsKey(parameter)) {
statement.setObject(position, context.get(parameter));
position += 1;
}
}
for (String parameter : required) {
if (context.containsKey(parameter)) {
statement.setObject(position, context.get(parameter));
position += 1;
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy