I have to write plain SQL queries inside infixes. I have a main SQL query and then add AND clauses into it based on certain conditions.
For example :
val andClause= quote { (name: String) =>
infix"""AND name = $name """.as[Query[(Int, String)]]
}
val rawQuery = quote { (id: String, name: String) =>
infix"""SELECT id, name FROM my_entity WHERE id = $id ${andClause(name)}"""
.as[Query[(Int, String)]]
}
ctx.translate(rawQuery("ramakrishna", "rk”))
and this prints
SELECT x._1, x._2 FROM (SELECT id, name FROM my_entity WHERE id = 'ramakrishna' AND name = 'rk' ) AS x
as expected.
However I need to concat the infixes conditionally like
val andClause1= quote { (name: String) =>
infix"""AND name = $name """.as[Query[(Int, String)]]
}
val andClause2= quote { (name: String) =>
infix""" """.as[Query[(Int, String)]]
}
def andClauseCondition(i: String) =
if (!i.isEmpty) {
andClause1(i)
} else {
andClause2(i)
}
And I am calling my query as
val rawQuery = quote { (id: String, name: String) =>
infix"""SELECT id, name FROM my_entity WHERE id = $id ${andClauseCondition(name)}"""
.as[Query[(Int, String)]]
}
But I get error as
Error:(231, 26) Tree 'andClauseCondition(name)' can't be parsed to 'Ast'
val rawQuery = quote { (id: String, name: String) =>
Is it possible to append one or more infixes to base query ? If yes, is it the correct way to append ?