use of org.hibernate.SQLQuery in project candlepin by candlepin.
the class AbstractHibernateCurator method bulkSQLDelete.
/**
* Performs an SQL delete on the given table, using the given criteria map to filter rows to
* delete.
*
* @param table
* The name of the table to update
*
* @param criteria
* A mapping of criteria to apply to the deletion (column name => value); applied as a
* conjunction.
*
* @return
* the number of rows deleted as a result of this query
*/
protected int bulkSQLDelete(String table, Map<String, Object> criteria) {
StringBuilder builder = new StringBuilder("DELETE FROM ").append(table);
// Add criteria
if (criteria != null && !criteria.isEmpty()) {
boolean whereStarted = false;
int param = 0;
for (Map.Entry<String, Object> criterion : criteria.entrySet()) {
if (criterion.getValue() instanceof Collection) {
if (((Collection) criterion.getValue()).size() > 0) {
int inBlocks = (int) Math.ceil((((Collection) criterion.getValue()).size() / (float) getInBlockSize()));
builder.append(whereStarted ? " AND " : " WHERE ");
whereStarted = true;
if (inBlocks > 1) {
builder.append('(');
for (int i = 0; i < inBlocks; ++i) {
if (i != 0) {
builder.append(" OR ");
}
builder.append(criterion.getKey()).append(" IN (?").append(++param).append(')');
}
builder.append(')');
} else {
builder.append(criterion.getKey()).append(" IN (?").append(++param).append(')');
}
}
} else {
builder.append(whereStarted ? " AND " : " WHERE ");
whereStarted = true;
builder.append(criterion.getKey()).append(" = ?").append(++param);
}
}
}
SQLQuery query = this.currentSession().createSQLQuery(builder.toString());
if (criteria != null && !criteria.isEmpty()) {
int param = 0;
for (Object criterion : criteria.values()) {
if (criterion instanceof Collection) {
Iterable<List> inBlocks = this.partition((Collection) criterion);
for (List inBlock : inBlocks) {
query.setParameterList(String.valueOf(++param), inBlock);
}
} else {
query.setParameter(String.valueOf(++param), criterion);
}
}
}
return query.executeUpdate();
}
use of org.hibernate.SQLQuery in project onebusaway-application-modules by camsys.
the class GtfsArchiveTask method cleanTempTables.
private void cleanTempTables(Session session) {
SQLQuery check = session.createSQLQuery("set foreign_key_checks = 0");
check.executeUpdate();
for (String table : TMP_TABLES) {
logUpdate(session, "truncate table " + table);
}
check = session.createSQLQuery("set foreign_key_checks = 1");
check.executeUpdate();
}
use of org.hibernate.SQLQuery in project gocd by gocd.
the class PipelineRepository method updateNaturalOrderForPipeline.
public static int updateNaturalOrderForPipeline(Session session, Long pipelineId, double naturalOrder) {
String sql = "UPDATE pipelines SET naturalOrder = :naturalOrder WHERE id = :pipelineId";
SQLQuery query = session.createSQLQuery(sql);
query.setLong("pipelineId", pipelineId);
query.setDouble("naturalOrder", naturalOrder);
return query.executeUpdate();
}
Aggregations