Search in sources :

Example 6 with SqlBuilder

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

the class AnsiSqlDialect method psForInsertBatch.

@Override
public PreparedStatement psForInsertBatch(Connection conn, Entity... entities) throws SQLException {
    if (ArrayUtil.isEmpty(entities)) {
        throw new DbRuntimeException("Entities for batch insert is empty !");
    }
    // 批量,根据第一行数据结构生成SQL占位符
    final SqlBuilder insert = SqlBuilder.create(wrapper).insert(entities[0], this.dialectName());
    final Set<String> fields = CollUtil.filter(entities[0].keySet(), StrUtil::isNotBlank);
    return StatementUtil.prepareStatementForBatch(conn, insert.build(), fields, entities);
}
Also used : StrUtil(cn.hutool.core.util.StrUtil) SqlBuilder(cn.hutool.db.sql.SqlBuilder) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Example 7 with SqlBuilder

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

the class AnsiSqlDialect method psForPage.

@Override
public PreparedStatement psForPage(Connection conn, Query query) throws SQLException {
    Assert.notNull(query, "query must be not null !");
    if (StrUtil.hasBlank(query.getTableNames())) {
        throw new DbRuntimeException("Table name must be not empty !");
    }
    final SqlBuilder find = SqlBuilder.create(wrapper).query(query);
    return psForPage(conn, find, query.getPage());
}
Also used : SqlBuilder(cn.hutool.db.sql.SqlBuilder) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Example 8 with SqlBuilder

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

the class H2Dialect method psForUpsert.

@Override
public PreparedStatement psForUpsert(Connection conn, Entity entity, String... keys) throws SQLException {
    Assert.notEmpty(keys, "Keys must be not empty for H2 MERGE SQL.");
    SqlBuilder.validateEntity(entity);
    final SqlBuilder builder = SqlBuilder.create(wrapper);
    final StringBuilder fieldsPart = new StringBuilder();
    final StringBuilder placeHolder = new StringBuilder();
    // 构建字段部分和参数占位符部分
    entity.forEach((field, value) -> {
        if (StrUtil.isNotBlank(field)) {
            if (fieldsPart.length() > 0) {
                // 非第一个参数,追加逗号
                fieldsPart.append(", ");
                placeHolder.append(", ");
            }
            fieldsPart.append((null != wrapper) ? wrapper.wrap(field) : field);
            placeHolder.append("?");
            builder.addParams(value);
        }
    });
    String tableName = entity.getTableName();
    if (null != this.wrapper) {
        tableName = this.wrapper.wrap(tableName);
    }
    builder.append("MERGE INTO ").append(tableName).append(" (").append(fieldsPart).append(") KEY(").append(ArrayUtil.join(keys, ", ")).append(") VALUES (").append(placeHolder).append(")");
    return StatementUtil.prepareStatement(conn, builder);
}
Also used : SqlBuilder(cn.hutool.db.sql.SqlBuilder)

Example 9 with SqlBuilder

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

the class PostgresqlDialect method psForUpsert.

@Override
public PreparedStatement psForUpsert(Connection conn, Entity entity, String... keys) throws SQLException {
    Assert.notEmpty(keys, "Keys must be not empty for Postgres.");
    SqlBuilder.validateEntity(entity);
    final SqlBuilder builder = SqlBuilder.create(wrapper);
    final StringBuilder fieldsPart = new StringBuilder();
    final StringBuilder placeHolder = new StringBuilder();
    final StringBuilder updateHolder = new StringBuilder();
    // 构建字段部分和参数占位符部分
    entity.forEach((field, value) -> {
        if (StrUtil.isNotBlank(field)) {
            if (fieldsPart.length() > 0) {
                // 非第一个参数,追加逗号
                fieldsPart.append(", ");
                placeHolder.append(", ");
                updateHolder.append(", ");
            }
            final String wrapedField = (null != wrapper) ? wrapper.wrap(field) : field;
            fieldsPart.append(wrapedField);
            updateHolder.append(wrapedField).append("=EXCLUDED.").append(field);
            placeHolder.append("?");
            builder.addParams(value);
        }
    });
    String tableName = entity.getTableName();
    if (null != this.wrapper) {
        tableName = this.wrapper.wrap(tableName);
    }
    builder.append("INSERT INTO ").append(tableName).append(" (").append(fieldsPart).append(") VALUES (").append(placeHolder).append(") ON CONFLICT (").append(ArrayUtil.join(keys, ", ")).append(") DO UPDATE SET ").append(updateHolder);
    return StatementUtil.prepareStatement(conn, builder);
}
Also used : SqlBuilder(cn.hutool.db.sql.SqlBuilder)

Aggregations

SqlBuilder (cn.hutool.db.sql.SqlBuilder)9 DbRuntimeException (cn.hutool.db.DbRuntimeException)2 Condition (cn.hutool.db.sql.Condition)2 SQLException (java.sql.SQLException)2 StrUtil (cn.hutool.core.util.StrUtil)1 Query (cn.hutool.db.sql.Query)1 Test (org.junit.Test)1