0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

postgresqlのrefcursorの扱い方

0
Last updated at Posted at 2015-05-25

spockでPostgreSQLのストアドのテストをしようとして最初ハマッたのでメモ

sample.groovy
def sql = Sql.newInstance(省略)
// 引数1、2が文字列、引数3がOUTでInteger、引数4がOUTでrefcursorのケース
def s = "{call ストアド名(?, ?, ?, ?)}"
def paras = ["引数1", "引数2", Sql.out(Sql.INTEGER.getType()), Sql.out(Sql.OTHER.getType())]
sql.call(strSql, params) {errorCd, rows->
	rows.eachRow {rs->
		// 取得結果(hogeカラム)へのアクセス例
		def hoge = rs.hoge
	}
}

refcursorはSQLタイプとしてOTHERを指定する。
(outがtext[]の場合はARRAYを指定する)
それとrefcursorにアクセスするにはeachRowの中でアクセスしないと
ResultSetは閉じられているという実行時エラーとなる。

pgAdminでこのストアドを使おうとした場合
begin;
ストアド名('引数1', '引数2');
と書いて実行すると<unnamed portal 数値>というのが取ってこれて
fetch all "<unnamed portal 数値>";
とやらないとrefcursorにアクセスできないのと同じ原理だと思う。

ちなみにこのrsをコピーしてeachRowの外で使おうとしても
やっぱりResultSetは閉じられているっていう実行時エラーとなる。
whenで実行してデータを保持しといてthenでassertしたいのだけど
やり方がわからない(´;ω;`)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?