Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch object by plain SQL query with SORM

Tags:

scala

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]
like image 644
Andrey Kuznetsov Avatar asked Nov 23 '12 10:11

Andrey Kuznetsov


1 Answers

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

like image 134
Nikita Volkov Avatar answered Sep 21 '22 15:09

Nikita Volkov