在SQL GString Query中使用扩展变量

在SQL GString Query中使用扩展变量

使用groovy.sql.Sql类可以很容易地使用Groovy代码中的SQL数据库。 该类有几种方法来执行SQL查询,但是如果我们使用Sql中带有GString参数的方法,我们必须特别小心。Groovy将提取所有变量表达式,并将它们用作从SQL查询构造的PreparedStatement中占位符的值。 如果我们有变量表达式不应该被提取为PreparedStatement的参数,我们必须使用Sql.expand方法。 此方法将使变量表达式成为groovy.sql.ExpandedVariable对象。 此对象不用作PreparedStatement查询的参数,但该值被评估为GString变量表达式。

在下面的示例中,我们有一个类,它使用GString查询值调用Sql对象的几个方法。 我们可以看到何时使用Sql.expand以及何时不需要:

package mrhaki
 
import groovy.sql.*
 
class SampleDAO {
    private static final String TABLE_NAME = 'sample'
    private static final String COLUMN_ID = 'id'
    private static final String COLUMN_NAME = 'name'
    private static final String COLUMN_DESCRIPTION = 'description'
 
    private final Sql sql =
        Sql.newInstance(
            'jdbc:h2:test', 'sa', 'sa', 'org.h2.Driver')
 
    Long create() {
        // We need to use Sql.expand() in our GString query.
        // If we don't use it the GString variable expressions are interpreted
        // as a placeholder in a SQL prepared statement, but we don't
        // that here.
        final query =
            """
            INSERT INTO ${Sql.expand(TABLE_NAME)} DEFAULT VALUES
            """
 
        final insertedKeys = sql.executeInsert(query)
        return insertedKeys[0][0]
    }
 
    void updateDescription(final Long id, final String description) {
        // In the following GString SQL we need
        // Sql.expand(), because we use executeUpdate
        // with only the GString argument.
        // Groovy will extract all variable expressions and
        // use them as the placeholders
        // for the SQL prepared statement.
        // So to make sure only description and id are
        // placeholders for the prepared statement we use
        // Sql.expand() for the other variables.
        final query =
            """
            UPDATE ${Sql.expand(TABLE_NAME)}
            SET ${Sql.expand(COLUMN_DESCRIPTION)} = ${description}
            WHERE ${Sql.expand(COLUMN_ID)} = ${id}
            """
        sql.executeUpdate(query)
    }
 
    void updateName(final Long id, final String name) {
        // In the following GString SQL we don't need
        // Sql.expand(), because we use the executeUpdate
        // method with GString argument AND argument
        // with values for the placeholders.
        final query =
            """
            UPDATE ${TABLE_NAME}
            SET ${COLUMN_NAME} = :nameValue
            WHERE ${COLUMN_ID} = :idValue
            """
        sql.executeUpdate(query, nameValue: name, idValue: id)
    }
}

用Groovy 2.5.4编写。

转载于:https://my.oschina.net/wstone/blog/3094453

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值