【问题标题】:Scala anorm running query to postgreSQL with set role commandScala anorm 使用 set role 命令运行对 postgreSQL 的查询
【发布时间】:2021-06-23 18:50:48
【问题描述】:

我需要使用 anorm 在 PostgreSQL 中执行查询。为了处理授权,我必须设置角色和查询。我的查询如下。 set role myuser; select country from country_list 当直接在 postgress 中执行时,这工作正常。但是当通过 anorm 尝试时没有发现异常PSQLException: No results were returned by the query. 但是当 set role 命令被删除时,这工作正常。

代码运行良好

import anorm.SqlParser._
SQL("select country from country_list").as(str("name").*)

代码抛出异常

import anorm.SqlParser._
SQL("set role myuser; select country from country_list").as(str("name").*)

例外

org.postgresql.util.PSQLException: No results were returned by the query.
        at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:115)
        at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52)
        at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java)
        at anorm.Sql.$anonfun$resultSet$2(Anorm.scala:78)

【问题讨论】:

    标签: scala anorm


    【解决方案1】:

    您是否尝试过依次运行 2 个查询?通常像 Anorm 这样的库一次只需要一个查询。

    SQL("set role myuser").execute()
    SQL("select country from country_list").as(str("name").*)
    

    【讨论】:

    • 异常不是 ORM
    • @GaëlJ - 我不能。我正在尝试从我的 API 进行用户模拟,以便在 DB 层进行授权。我必须通过更改角色来进行查询。
    猜你喜欢
    • 2013-11-09
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-07
    相关资源
    最近更新 更多