Reputation: 4139
I typed a couple of lines on Single / Optional Result for Query @ ScalikeJDBC Docs:
package org.mysample
import scalikejdbc._
// define a class to map the result
case class Emp(id: String, name: String)
// QueryDSL
object Emp extends SQLSyntaxSupport[Emp] {
def apply(e: ResultName[Emp])(rs: WrappedResultSet): Emp =
new Emp(id = rs.get(e.id), name = rs.get(e.name))
}
class TestClass {
val emp: Option[Emp] = DB readOnly { implicit session =>
sql"SELECT id, name FROM emp WHERE id = ${id}"
.map(rs => Emp(rs.string("id"), rs.string("name"))).single.apply()
}
val e = Emp.syntax("e")
val id = 123
val emp2: Option[Emp] = DB readOnly { implicit session =>
withSQL {
select.from(Emp as e).where.eq(e.id, id)
}.map(Emp(e)).single.apply()
}
}
But this snippet has an compile error:
[error] /Users/pj/git/sirloin/src/main/scala/org/mysample/emp.scala:25: type mismatch;
[error] found : scalikejdbc.QuerySQLSyntaxProvider[scalikejdbc.SQLSyntaxSupport[org.mysample.Emp],org.mysample.Emp]
[error] required: scalikejdbc.ResultName[org.mysample.Emp]
[error] (which expands to) scalikejdbc.ResultNameSQLSyntaxProvider[scalikejdbc.SQLSyntaxSupport[org.mysample.Emp],org.mysample.Emp]
[error] }.map(Emp(e)).single.apply()
[error] ^
I cleaned and recompiled in sbt, but still failed to compile. What is wrong with this code?
Thanks for any helps.
Upvotes: 0
Views: 735
Reputation: 1877
Sorry for bothering you. Just specify map(Emp(e.resultName))
instead. or update Emp companion object like this:
object Emp extends SQLSyntaxSupport[Emp] {
def apply(e: ResultName[Emp])(rs: WrappedResultSet): Emp =
new Emp(id = rs.get(e.id), name = rs.get(e.name))
def apply(e: SyntaxProvider[Emp])(rs: WrappedResultSet): Emp = apply(e.resultName)(rs)
}
Upvotes: 2