Search code examples
scalasorm

Fetch object by plain SQL query with SORM


Is it possible to fetch items by plain SQL query instead of building query by DSL using SORM?

For example is there an API for making something like

val metallica = Db.query[Artist].fromString("SELECT * FROM artist WHERE name = ?", "Metallica").fetchOne() // Option[Artist]

instead of

val metallica = Db.query[Artist].whereEqual("name", "Metallica").fetchOne() // Option[Artist]

Solution

  • Since populating an entity with collections and other structured values involves fetching data from multiple tables in an unjoinable way, the API for fetching it directly will most probably never get exposed. However another approach to this problem is currently being considered.

    Here's how it could be implemented:

    val artists : Seq[Artist] 
      = Db.fetchWithSql[Artist]("SELECT id FROM artist WHERE name = ?", "Metallica")
    

    If this issue gets a notable support either here or, even better, here, it will probably get implemented in the next minor release.

    Update

    implemented in 0.3.1