1

Trying out the stock Doobie example, and getting an exception complaining about an "Invalid column index". My query is extremely simple: it selects two columns from one Oracle table, and I expect Doobie to map that to a sequence of case class instances with two matching properties. I have run that query in a SQL IDE, and it runs fine. I assume that there is a mismatch between the number of parameters of a PreparedStatement (created by Doobie) and the number of parameters passed in (one) - but I don't know why. This is my first contact with Doobie, so I may misunderstand something simple.

The exception is java.sql.SQLException: Invalid column index. Any idea what I am doing wrong?

import doobie._
import doobie.implicits._
import cats.effect.IO
import scala.concurrent.ExecutionContext

/**
 * @see https://tpolecat.github.io/doobie/
 */
object DoobieExampleOne {
  implicit val cs = IO.contextShift(ExecutionContext.global)
  val jdbcUrl = "jdbc:oracle:thin:@(DESCRIPTION=...)"

  val xa = Transactor.fromDriverManager[IO](
    "oracle.jdbc.driver.OracleDriver", jdbcUrl, "myUser", "myPswd"
  )

  def main(args: Array[String]): Unit = {
    find(idPrefix = "somePfx").transact(xa).unsafeRunSync
  }

  case class SomeEntity(identifier_value: String, some_id: Long)

  def find(idPrefix: String): ConnectionIO[Option[SomeEntity]] =
    // This query runs correctly on the command line
    sql"SELECT identifier_value, some_id FROM some_table WHERE identifier_value LIKE '$idPrefix'"
      .query[SomeEntity].option
}
// Exception in thread "main" java.sql.SQLException: Invalid column index
radumanolescu
  • 4,059
  • 2
  • 31
  • 44

1 Answers1

1

Found the solution by reading the stock example more carefully: the query parameter should not be enclosed in quotes. The correct syntax is shown below:

sql"SELECT identifier_value, some_id FROM some_table WHERE identifier_value LIKE $idPrefix"
radumanolescu
  • 4,059
  • 2
  • 31
  • 44