前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在SQL GString Query中使用扩展变量

在SQL GString Query中使用扩展变量

作者头像
白石
发布2019-08-23 10:00:22
1K0
发布2019-08-23 10:00:22
举报
文章被收录于专栏:白石白石

在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以及何时不需要:

代码语言:javascript
复制
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编写。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 在SQL GString Query中使用扩展变量
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档