2
votes

I am trying to write a configurable sql query executor using Slick. User provides a prepared statement with ? and at run time the exact query is formed by replacing ? with values.

Generally this is how one would run a plain sql query using slick.

val query = sql"#$queryString".as[(String,Int)]

In my case i would not know the result type so i want to get back a generic result type. Maybe a List of Tuples with each tuple representing a row of result SET.

Any ideas on how this would be done?

1

1 Answers

1
votes

I found a solution from one of the scala git issues. Here it is

ResultMap extends GetResult[Map[String, Any]] {
  def apply(pr: PositionedResult) = {
    val resultSet = pr.rs
    val metaData = resultSet.getMetaData();
    (1 to pr.numColumns).map { i =>
      metaData.getColumnName(i) -> resultSet.getObject(i)
    }.toMap
  }

and then we can simply do val query = sql"#$queryString".as(ResultMap)

Hope it helps!!