Search in sources :

Example 1 with PreparedSqlBuilder

use of org.seasar.doma.internal.jdbc.sql.PreparedSqlBuilder in project doma by domaframework.

the class AutoDeleteQuery method prepareSql.

protected void prepareSql() {
    Naming naming = config.getNaming();
    Dialect dialect = config.getDialect();
    PreparedSqlBuilder builder = new PreparedSqlBuilder(config, SqlKind.DELETE, sqlLogType);
    builder.appendSql("delete from ");
    builder.appendSql(entityType.getQualifiedTableName(naming::apply, dialect::applyQuote));
    boolean whereClauseAppended = false;
    if (idPropertyTypes.size() > 0) {
        builder.appendSql(" where ");
        whereClauseAppended = true;
        for (EntityPropertyType<ENTITY, ?> propertyType : idPropertyTypes) {
            Property<ENTITY, ?> property = propertyType.createProperty();
            property.load(entity);
            builder.appendSql(propertyType.getColumnName(naming::apply, dialect::applyQuote));
            builder.appendSql(" = ");
            builder.appendParameter(property.asInParameter());
            builder.appendSql(" and ");
        }
        builder.cutBackSql(5);
    }
    if (versionPropertyType != null && !versionIgnored) {
        if (whereClauseAppended) {
            builder.appendSql(" and ");
        } else {
            builder.appendSql(" where ");
            whereClauseAppended = true;
        }
        Property<ENTITY, ?> property = versionPropertyType.createProperty();
        property.load(entity);
        builder.appendSql(versionPropertyType.getColumnName(naming::apply, dialect::applyQuote));
        builder.appendSql(" = ");
        builder.appendParameter(property.asInParameter());
    }
    if (tenantIdPropertyType != null) {
        if (whereClauseAppended) {
            builder.appendSql(" and ");
        } else {
            builder.appendSql(" where ");
            // noinspection UnusedAssignment
            whereClauseAppended = true;
        }
        Property<ENTITY, ?> property = tenantIdPropertyType.createProperty();
        property.load(entity);
        builder.appendSql(tenantIdPropertyType.getColumnName(naming::apply, dialect::applyQuote));
        builder.appendSql(" = ");
        builder.appendParameter(property.asInParameter());
    }
    sql = builder.build(this::comment);
}
Also used : Dialect(org.seasar.doma.jdbc.dialect.Dialect) Naming(org.seasar.doma.jdbc.Naming) PreparedSqlBuilder(org.seasar.doma.internal.jdbc.sql.PreparedSqlBuilder)

Example 2 with PreparedSqlBuilder

use of org.seasar.doma.internal.jdbc.sql.PreparedSqlBuilder in project doma by domaframework.

the class AutoBatchDeleteQuery method prepareSql.

protected void prepareSql() {
    Naming naming = config.getNaming();
    Dialect dialect = config.getDialect();
    PreparedSqlBuilder builder = new PreparedSqlBuilder(config, SqlKind.BATCH_DELETE, sqlLogType);
    builder.appendSql("delete from ");
    builder.appendSql(entityType.getQualifiedTableName(naming::apply, dialect::applyQuote));
    boolean whereClauseAppended = false;
    if (idPropertyTypes.size() > 0) {
        builder.appendSql(" where ");
        whereClauseAppended = true;
        for (EntityPropertyType<ENTITY, ?> propertyType : idPropertyTypes) {
            Property<ENTITY, ?> property = propertyType.createProperty();
            property.load(currentEntity);
            builder.appendSql(propertyType.getColumnName(naming::apply, dialect::applyQuote));
            builder.appendSql(" = ");
            builder.appendParameter(property.asInParameter());
            builder.appendSql(" and ");
        }
        builder.cutBackSql(5);
    }
    if (versionPropertyType != null && !versionIgnored) {
        if (whereClauseAppended) {
            builder.appendSql(" and ");
        } else {
            builder.appendSql(" where ");
            whereClauseAppended = true;
        }
        Property<ENTITY, ?> property = versionPropertyType.createProperty();
        property.load(currentEntity);
        builder.appendSql(versionPropertyType.getColumnName(naming::apply, dialect::applyQuote));
        builder.appendSql(" = ");
        builder.appendParameter(property.asInParameter());
    }
    if (tenantIdPropertyType != null) {
        if (whereClauseAppended) {
            builder.appendSql(" and ");
        } else {
            builder.appendSql(" where ");
            // noinspection UnusedAssignment
            whereClauseAppended = true;
        }
        Property<ENTITY, ?> property = tenantIdPropertyType.createProperty();
        property.load(currentEntity);
        builder.appendSql(tenantIdPropertyType.getColumnName(naming::apply, dialect::applyQuote));
        builder.appendSql(" = ");
        builder.appendParameter(property.asInParameter());
    }
    PreparedSql sql = builder.build(this::comment);
    sqls.add(sql);
}
Also used : PreparedSql(org.seasar.doma.jdbc.PreparedSql) Dialect(org.seasar.doma.jdbc.dialect.Dialect) Naming(org.seasar.doma.jdbc.Naming) PreparedSqlBuilder(org.seasar.doma.internal.jdbc.sql.PreparedSqlBuilder)

Example 3 with PreparedSqlBuilder

use of org.seasar.doma.internal.jdbc.sql.PreparedSqlBuilder in project doma by domaframework.

the class AutoBatchInsertQuery method prepareSql.

protected void prepareSql() {
    Naming naming = config.getNaming();
    Dialect dialect = config.getDialect();
    PreparedSqlBuilder builder = new PreparedSqlBuilder(config, SqlKind.BATCH_INSERT, sqlLogType);
    builder.appendSql("insert into ");
    builder.appendSql(entityType.getQualifiedTableName(naming::apply, dialect::applyQuote));
    builder.appendSql(" (");
    for (EntityPropertyType<ENTITY, ?> p : targetPropertyTypes) {
        builder.appendSql(p.getColumnName(naming::apply, dialect::applyQuote));
        builder.appendSql(", ");
    }
    builder.cutBackSql(2);
    builder.appendSql(") values (");
    for (EntityPropertyType<ENTITY, ?> propertyType : targetPropertyTypes) {
        Property<ENTITY, ?> property = propertyType.createProperty();
        property.load(currentEntity);
        builder.appendParameter(property.asInParameter());
        builder.appendSql(", ");
    }
    builder.cutBackSql(2);
    builder.appendSql(")");
    PreparedSql sql = builder.build(this::comment);
    sqls.add(sql);
}
Also used : PreparedSql(org.seasar.doma.jdbc.PreparedSql) Dialect(org.seasar.doma.jdbc.dialect.Dialect) Naming(org.seasar.doma.jdbc.Naming) PreparedSqlBuilder(org.seasar.doma.internal.jdbc.sql.PreparedSqlBuilder)

Example 4 with PreparedSqlBuilder

use of org.seasar.doma.internal.jdbc.sql.PreparedSqlBuilder in project doma by domaframework.

the class AutoBatchUpdateQuery method prepareSql.

protected void prepareSql() {
    Naming naming = config.getNaming();
    Dialect dialect = config.getDialect();
    PreparedSqlBuilder builder = new PreparedSqlBuilder(config, SqlKind.BATCH_UPDATE, sqlLogType);
    builder.appendSql("update ");
    builder.appendSql(entityType.getQualifiedTableName(naming::apply, dialect::applyQuote));
    builder.appendSql(" set ");
    helper.populateValues(currentEntity, targetPropertyTypes, versionPropertyType, builder);
    boolean whereClauseAppended = false;
    if (idPropertyTypes.size() > 0) {
        builder.appendSql(" where ");
        whereClauseAppended = true;
        for (EntityPropertyType<ENTITY, ?> propertyType : idPropertyTypes) {
            Property<ENTITY, ?> property = propertyType.createProperty();
            property.load(currentEntity);
            builder.appendSql(propertyType.getColumnName(naming::apply, dialect::applyQuote));
            builder.appendSql(" = ");
            builder.appendParameter(property.asInParameter());
            builder.appendSql(" and ");
        }
        builder.cutBackSql(5);
    }
    if (versionPropertyType != null && !versionIgnored) {
        if (whereClauseAppended) {
            builder.appendSql(" and ");
        } else {
            builder.appendSql(" where ");
            whereClauseAppended = true;
        }
        Property<ENTITY, ?> property = versionPropertyType.createProperty();
        property.load(currentEntity);
        builder.appendSql(versionPropertyType.getColumnName(naming::apply, dialect::applyQuote));
        builder.appendSql(" = ");
        builder.appendParameter(property.asInParameter());
    }
    if (tenantIdPropertyType != null) {
        if (whereClauseAppended) {
            builder.appendSql(" and ");
        } else {
            builder.appendSql(" where ");
            // noinspection UnusedAssignment
            whereClauseAppended = true;
        }
        Property<ENTITY, ?> property = tenantIdPropertyType.createProperty();
        property.load(currentEntity);
        builder.appendSql(tenantIdPropertyType.getColumnName(naming::apply, dialect::applyQuote));
        builder.appendSql(" = ");
        builder.appendParameter(property.asInParameter());
    }
    PreparedSql sql = builder.build(this::comment);
    sqls.add(sql);
}
Also used : PreparedSql(org.seasar.doma.jdbc.PreparedSql) Dialect(org.seasar.doma.jdbc.dialect.Dialect) Naming(org.seasar.doma.jdbc.Naming) PreparedSqlBuilder(org.seasar.doma.internal.jdbc.sql.PreparedSqlBuilder)

Example 5 with PreparedSqlBuilder

use of org.seasar.doma.internal.jdbc.sql.PreparedSqlBuilder in project doma by domaframework.

the class AutoInsertQuery method prepareSql.

protected void prepareSql() {
    Naming naming = config.getNaming();
    Dialect dialect = config.getDialect();
    PreparedSqlBuilder builder = new PreparedSqlBuilder(config, SqlKind.INSERT, sqlLogType);
    builder.appendSql("insert into ");
    builder.appendSql(entityType.getQualifiedTableName(naming::apply, dialect::applyQuote));
    builder.appendSql(" (");
    if (!targetPropertyTypes.isEmpty()) {
        for (EntityPropertyType<ENTITY, ?> propertyType : targetPropertyTypes) {
            builder.appendSql(propertyType.getColumnName(naming::apply, dialect::applyQuote));
            builder.appendSql(", ");
        }
        builder.cutBackSql(2);
    }
    builder.appendSql(") values (");
    if (!targetPropertyTypes.isEmpty()) {
        for (EntityPropertyType<ENTITY, ?> propertyType : targetPropertyTypes) {
            Property<ENTITY, ?> property = propertyType.createProperty();
            property.load(entity);
            builder.appendParameter(property.asInParameter());
            builder.appendSql(", ");
        }
        builder.cutBackSql(2);
    }
    builder.appendSql(")");
    sql = builder.build(this::comment);
}
Also used : Dialect(org.seasar.doma.jdbc.dialect.Dialect) Naming(org.seasar.doma.jdbc.Naming) PreparedSqlBuilder(org.seasar.doma.internal.jdbc.sql.PreparedSqlBuilder)

Aggregations

PreparedSqlBuilder (org.seasar.doma.internal.jdbc.sql.PreparedSqlBuilder)6 Naming (org.seasar.doma.jdbc.Naming)6 Dialect (org.seasar.doma.jdbc.dialect.Dialect)6 PreparedSql (org.seasar.doma.jdbc.PreparedSql)3