
com.j256.ormlite.stmt.mapped.MappedDeleteCollection Maven / Gradle / Ivy
package com.j256.ormlite.stmt.mapped;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.j256.ormlite.db.DatabaseType;
import com.j256.ormlite.field.FieldType;
import com.j256.ormlite.misc.SqlExceptionUtil;
import com.j256.ormlite.support.JdbcTemplate;
import com.j256.ormlite.table.TableInfo;
/**
* A mapped statement for deleting objects that correspond to a collection of IDs.
*
* @author graywatson
*/
public class MappedDeleteCollection extends BaseMappedStatement {
private MappedDeleteCollection(TableInfo tableInfo, String statement, List argFieldTypeList) {
super(tableInfo, statement, argFieldTypeList);
}
/**
* Delete all of the objects in the collection. This builds a {@link MappedDeleteCollection} on the fly because the
* datas could be variable sized.
*/
public static int deleteObjects(DatabaseType databaseType, TableInfo tableInfo, JdbcTemplate template,
Collection datas) throws SQLException {
MappedDeleteCollection deleteCollection =
MappedDeleteCollection.build(databaseType, tableInfo, datas.size());
Object[] fieldObjects = new Object[datas.size()];
int objC = 0;
for (T data : datas) {
fieldObjects[objC] = tableInfo.getIdField().getConvertedFieldValue(data);
objC++;
}
return updateRows(template, deleteCollection, fieldObjects);
}
/**
* Delete all of the objects in the collection. This builds a {@link MappedDeleteCollection} on the fly because the
* ids could be variable sized.
*/
public static int deleteIds(DatabaseType databaseType, TableInfo tableInfo, JdbcTemplate template,
Collection ids) throws SQLException {
MappedDeleteCollection deleteCollection =
MappedDeleteCollection.build(databaseType, tableInfo, ids.size());
Object[] idsArray = ids.toArray(new Object[ids.size()]);
return updateRows(template, deleteCollection, idsArray);
}
/**
* This is private because the execute is the only method that should be called here.
*/
private static MappedDeleteCollection build(DatabaseType databaseType, TableInfo tableInfo,
int dataSize) {
FieldType idField = tableInfo.getIdField();
if (idField == null) {
throw new IllegalArgumentException("Cannot delete " + tableInfo.getDataClass()
+ " because it doesn't have an id field defined");
}
StringBuilder sb = new StringBuilder();
List argFieldTypeList = new ArrayList();
appendTableName(databaseType, sb, "DELETE FROM ", tableInfo.getTableName());
appendWhereIds(databaseType, idField, sb, dataSize, argFieldTypeList);
return new MappedDeleteCollection(tableInfo, sb.toString(), argFieldTypeList);
}
private static int updateRows(JdbcTemplate template, MappedDeleteCollection deleteCollection,
Object[] args) throws SQLException {
try {
int rowC = template.update(deleteCollection.statement, args, deleteCollection.argFieldTypeVals);
logger.debug("delete-collection with statement '{}' and {} args, changed {} rows",
deleteCollection.statement, args.length, rowC);
if (args.length > 0) {
// need to do the (Object) cast to force args to be a single object
logger.trace("delete-collection arguments: {}", (Object) args);
}
return rowC;
} catch (SQLException e) {
throw SqlExceptionUtil.create("Unable to run delete collection stmt: " + deleteCollection.statement, e);
}
}
private static void appendWhereIds(DatabaseType databaseType, FieldType idField, StringBuilder sb, int numDatas,
List fieldTypeList) {
sb.append("WHERE ");
databaseType.appendEscapedEntityName(sb, idField.getDbColumnName());
sb.append(" IN (");
boolean first = true;
for (int i = 0; i < numDatas; i++) {
if (first) {
first = false;
} else {
sb.append(',');
}
sb.append('?');
if (fieldTypeList != null) {
fieldTypeList.add(idField);
}
}
sb.append(") ");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy