janDro
janDro

Reputation: 1466

Passing a list to SQL each row call Groovy

I am currently rendering a list of sql rows from a database using:

Sql sql = new Sql(dataSource)
def list = []
def index = 0
params.mrnaIds.each { mrnaName ->
   sql.eachRow ("select value from patient_mrna where mrna_id=$mrnaId") { row ->
      list[index] = row.value
      index++
   }
}
render list

However I would like to avoid assigning the values to a list before rendering them.

The variable params.mrnaIds is coming from a multi select input, so it could either be a single string or a string array containing ids. Is there a way to iterate through these ids inside the eachRow method?

I would like to be able to execute something like:

render sql.eachRow ("select value from patient_mrna where mrna_id=?", params.mrnaIds) { row ->
   list[index] = row.value
   index++
}

But I'm not completely sure that there is a way to call eachRow with this functionality. If there is not, is there some other way to render the results without storing them in a list?

Upvotes: 0

Views: 1537

Answers (2)

injecteer
injecteer

Reputation: 20699

I think you can render each row:

sql.eachRow( someQuery, someParams ){ row ->
  render row as JSON
}

Upvotes: 1

cfrick
cfrick

Reputation: 37008

There is rows() to return a list instead ok working with it (like eachRow() is used for). It also shares all the different arguments. E.g.:

render sql.rows("select value from patient_mrna where mrna_id=?", params).collect{ it.value }

Upvotes: 1

Related Questions