Search in sources :

Example 56 with SQLQuery

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();
}
Also used : Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) SQLQuery(org.hibernate.SQLQuery) Map(java.util.Map) SortedMap(java.util.SortedMap) TreeMap(java.util.TreeMap)

Example 57 with SQLQuery

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();
}
Also used : SQLQuery(org.hibernate.SQLQuery)

Example 58 with SQLQuery

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();
}
Also used : SQLQuery(org.hibernate.SQLQuery)

Aggregations

SQLQuery (org.hibernate.SQLQuery)58 Session (org.hibernate.Session)26 Test (org.junit.Test)16 List (java.util.List)13 Map (java.util.Map)9 ArrayList (java.util.ArrayList)7 Collection (java.util.Collection)5 Paint (javafx.scene.paint.Paint)5 Cohort (org.openmrs.Cohort)5 EvaluatedCohort (org.openmrs.module.reporting.cohort.EvaluatedCohort)5 HibernateException (org.hibernate.HibernateException)4 JFXButton (com.jfoenix.controls.JFXButton)3 MaterialDesignIconView (de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIconView)3 MaterialIconView (de.jensd.fx.glyphs.materialicons.MaterialIconView)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 Insets (javafx.geometry.Insets)3 ScrollPane (javafx.scene.control.ScrollPane)3 Tooltip (javafx.scene.control.Tooltip)3 GridPane (javafx.scene.layout.GridPane)3