简体   繁体   English

Groovy SQL多个结果集

[英]Groovy SQL Multiple ResultSets

I am calling a stored procedure from my Groovy code. 我正在从Groovy代码中调用存储过程。 The stored proc looks like this 存储的过程看起来像这样

SELECT * FROM blahblahblah

SELECT * FROM suchAndsuch

So basically, two SELECT statements and therefore two ResultSets . 因此,基本上是两个SELECT语句,因此是两个ResultSets

sql.eachRow("dbo.testing 'param1'"){ rs ->
    println rs
}

This works fine for a single ResultSet. 对于单个ResultSet来说,这很好用。 How can I get the second one (or an arbitrary number of ResultSets for that matter). 我如何获得第二个(或与此相关的任意数量的ResultSet)。

You would need callWithAllRows() or its variant. 您将需要callWithAllRows()或其变体。

The return type of this method is List<List<GroovyRowResult>> . 此方法的返回类型为List<List<GroovyRowResult>>

Use this when calling a stored procedure that utilizes both output parameters and returns multiple ResultSets. 调用使用两个输出参数并返回多个ResultSet的存储过程时,请使用此方法。

This question is kind of old, but I will answer since I came across the same requirement recently and it maybe useful for future reference for me and others. 这个问题有点陈旧,但是我会回答,因为我最近遇到了相同的要求,它可能对我和其他人将来有用。

I'm working on a Spring application with SphinxSearch. 我正在使用SphinxSearch开发Spring应用程序。 When you run a query in sphinx, you get results, you need to run a second query to get the metadata for number of records etc... 在狮身人面像中运行查询时,您会得到结果,您需要运行第二个查询以获取记录数等的元数据。

// the query
String query = """
    SELECT * FROM INDEX_NAME WHERE MATCH('SEARCHTERM')  
    LIMIT 0,25 OPTION MAX_MATCHES=25;
    SHOW META LIKE 'total_found';
"""

// create an instance of our groovy sql (sphinx doesn't use a username or password, jdbc url is all we need)
// connection can be created from java, don't have to use groovy for it
Sql sql = Sql.newInstance('jdbc:mysql://127.0.0.1:9306/?characterEncoding=utf8&maxAllowedPacket=512000&allowMultiQueries=true','sphinx','sphinx123','com.mysql.jdbc.Driver')

// create a prepared statement so we can execute multiple resultsets
PreparedStatement ps = sql.getConnection().prepareStatement(query)

// execute the prepared statement
ps.execute()

// get the first result set and pass to GroovyResultSetExtension
GroovyResultSetExtension rs1 = new GroovyResultSetExtension(ps.getResultSet())
rs1.eachRow {
    println it
}

// call getMoreResults on the prepared statement to activate the 2nd set of results
ps.getMoreResults()

// get the second result set and pass to GroovyResultSetExtension
GroovyResultSetExtension rs2 = new GroovyResultSetExtension(ps.getResultSet())
rs2.eachRow {
    println it
}

Just some test code, this needs some improving on. 只是一些测试代码,这需要一些改进。 You can loop the result sets and do whatever processing... 您可以循环结果集并执行任何处理...

Comments should be self-explanatory, hope it helps others in the future! 评论应该是不言自明的,希望将来对其他人有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM