Search in sources :

Example 1 with Condition

use of cn.hutool.db.sql.Condition in project hutool by looly.

the class AnsiSqlDialect method psForUpdate.

@Override
public PreparedStatement psForUpdate(Connection conn, Entity entity, Query query) throws SQLException {
    Assert.notNull(query, "query must be not null !");
    final Condition[] where = query.getWhere();
    if (ArrayUtil.isEmpty(where)) {
        // 对于无条件的删除语句直接抛出异常禁止,防止误删除
        throw new SQLException("No 'WHERE' condition, we can't prepare statement for update everything.");
    }
    final SqlBuilder update = SqlBuilder.create(wrapper).update(entity).where(where);
    return StatementUtil.prepareStatement(conn, update);
}
Also used : Condition(cn.hutool.db.sql.Condition) SQLException(java.sql.SQLException) SqlBuilder(cn.hutool.db.sql.SqlBuilder)

Example 2 with Condition

use of cn.hutool.db.sql.Condition in project hutool by looly.

the class DbUtil method buildConditions.

/**
 * 通过实体对象构建条件对象
 *
 * @param entity 实体对象
 * @return 条件对象
 */
public static Condition[] buildConditions(Entity entity) {
    if (null == entity || entity.isEmpty()) {
        return null;
    }
    final Condition[] conditions = new Condition[entity.size()];
    int i = 0;
    Object value;
    for (Entry<String, Object> entry : entity.entrySet()) {
        value = entry.getValue();
        if (value instanceof Condition) {
            conditions[i++] = (Condition) value;
        } else {
            conditions[i++] = new Condition(entry.getKey(), value);
        }
    }
    return conditions;
}
Also used : Condition(cn.hutool.db.sql.Condition)

Example 3 with Condition

use of cn.hutool.db.sql.Condition in project hutool by looly.

the class AnsiSqlDialect method psForDelete.

@Override
public PreparedStatement psForDelete(Connection conn, Query query) throws SQLException {
    Assert.notNull(query, "query must be not null !");
    final Condition[] where = query.getWhere();
    if (ArrayUtil.isEmpty(where)) {
        // 对于无条件的删除语句直接抛出异常禁止,防止误删除
        throw new SQLException("No 'WHERE' condition, we can't prepared statement for delete everything.");
    }
    final SqlBuilder delete = SqlBuilder.create(wrapper).delete(query.getFirstTableName()).where(where);
    return StatementUtil.prepareStatement(conn, delete);
}
Also used : Condition(cn.hutool.db.sql.Condition) SQLException(java.sql.SQLException) SqlBuilder(cn.hutool.db.sql.SqlBuilder)

Aggregations

Condition (cn.hutool.db.sql.Condition)3 SqlBuilder (cn.hutool.db.sql.SqlBuilder)2 SQLException (java.sql.SQLException)2