目次

Version 1, last updated by nafg at Nov 02 05:28 UTC

The In QueryParam

The In QueryParam can be used in places where the SQL IN keyword might be. Here is an example:

Suppose you want to find all Posts whose author’s last name is Smith. There are many Smiths who have authored posts, and you want to find all those posts. Your Post Mapper might look like this:
class Post extends LongMappedMapper[Post] with IdPK {
object author extends LongMappedForeignKey(this, Author)
}

You could write:
val posts = Post.findAll(In(Post.author, Author.id, By(Author.lastName, “Smith”)))

The result might be equivalent to the SQL query:
SELECT * FROM POST WHERE POST.AUTHOR IN (SELECT ID FROM AUTHOR WHERE AUTHOR.LASTNAME=“Smith”);

In other words, there is a subquery selecting a subset of Author ids, and that list of ids is being matched against the Post.author field.

So the first argument to In (Post.author) corresponds to the field between the first WHERE and the IN.
The second argument is the field to return from the subquery.
Following those are any QueryParams to apply to the subquery.