我有2个类似的案例类:
case class ClassTeacherWrapper(
success: Boolean,
classes: List[ClassTeacher]
)
第二个:
case class ClassTeacher(
clid: String,
name: String
)
还有这样的查询:
val query =
SQL"""
SELECT
s.section_sk::text AS clid,
s.name AS name
from
********************
"""
P.S。出于安全原因,我将*替换为查询:
所以我的查询返回2个值。如何将其映射到case class ClassTeacher
目前我正在做这样的事情:
def getClassTeachersByInstructor(instructor: String, section: String): ClassTeacherWrapper = {
implicit var conn: Connection = null
try {
conn = datamartDatasourceConnectionPool.getDBConnection()
// Define query
val query =
SQL"""
SELECT
s.section_sk::text AS clid,
s.name AS name
********
"""
logger.info("Read from DB: " + query)
// create a List containing all the datasets from the resultset and return
new ClassTeacherWrapper(
success =true,
query.as(Macro.namedParser[ClassTeacher].*)
)
//Trying new approch
//val users = query.map(user => new ClassTeacherWrapper(true, user[Int]("clid"), user[String]("name")).tolist
}
catch {
case NonFatal(e) =>
logger.error("getGradebookScores: error getting/parsing data from DB", e)
throw e
}
}
随着我得到这个异常:
{
"error": "ERROR: operator does not exist: uuid = character varying\n
Hint: No operator matches the given name and argument type(s). You
might need to add explicit type casts.\n Position: 324"
}
任何人都可以帮我解决问题。我是Scala和Anorm的新手
我应该在代码的query.as
部分
答案 0 :(得分:1)
您需要成功字段吗?通常,空列表就足够了吗?
我发现解析器非常有用(并且可重复使用),因此在ClassTeacher单例(或类似位置)中类似以下内容:
val fields = "s.section_sk::text AS clid, s.name"
val classTeacherP =
get[Int]("clid") ~
get[String]("name") map {
case clid ~ name =>
ClassTeacher(clid,name)
}
def allForInstructorSection(instructor: String, section: String):List[ClassTeacher] =
DB.withConnection { implicit c => //-- or injected db
SQL(s"""select $fields from ******""")
.on('instructor -> instructor, 'section -> section)
.as(classTeacherP *)
}