Friday, 15 May 2015

scala - Mapper versus Record/Squeryl -



scala - Mapper versus Record/Squeryl -

i start first project in lift framework , have decide persistence library choose. utilize relational backend both mapper , record may come in play.

in case of mapper - thing miss ability command queries beingness sent rdbms - when comes more complicated queries solved joins, aggregation etc in sql. take next illustration - let's have next 2 entities:

class baseproduct extends longkeyedmapper[baseproduct] idpk { // fields } object baseproduct extends baseproduct longkeyedmetamapper[baseproduct] class myproduct extends longkeyedmapper[myproduct] idpk { // fields object base of operations extends mappedlongforeignkey(this, baseproduct) } object myproduct extends myproduct longkeyedmetamapper[myproduct]

where myproduct 1 of specialization of baseproduct entity. there one-to-one relation between these entities. best possibility i've come query exact myproduct baseproduct query this:

myproduct.findall(precache(myproduct.base))

which issues 2 queries (moreover afraid not able command fields of myproduct entity want select.

enough bad mapper library. main concern record/squeryl api fact lacks proto-classes nowadays around mapper api. there close functionality of these classes record? possible access database specific features in squeryl (eg. geometrical queries in postgresql)?

are there other pros , cons either of these layers? or there other abstraction layer deserve attending if want have decent typesafe encapsulation of communication database , provide decent command on queries (i used issuing queries straight using pdo layer in php - don't want such direct querying interface possibility of having command on queries great) integration lift framework advantage.

thank you!

we've been using squeryl lift quite while , have been happy it. based on case above, in current release version of squeryl (0.9.5), like:

class baseproduct(id:long, more fields) extends keyedentity[long] { } class myproduct(id:long, more fields) extends keyedentity[long] { }

then, have schema defines relationships (i assuming joined on id field):

val baseproducts = table[baseproduct]("base_products") val myproducts = table[myproduct]("my_products") val myproductstobaseproducts = onetomanyrelation(myproducts, baseproducts).via((mp, bp) => mp.id === bp.id)

to query both records, like:

from(myproducts, baseproducts) ( (mp, bp) => where(mp.id === bp.id , mp.id === lookupval) select(bp, mp) )

the query above homecoming tuple of (baseproduct, marketproduct) single sql select.

you can utilize relationship retrieve related item, such adding method myproduct:

def baseproduct = myproductstobaseproducts.left(this)

however, illustration mapper, issue sec query. making database specific queries, there & operator allow evaluate expressions @ server. if function not available in squeryl can create custom functions.

overall, have found squeryl flexible , great hybrid between orm , straight sql. has performed remarkably , have not found many places squeryl prohibited me getting @ database functionality needed. gets easier next version, 0.9.6 have lot more flexibility custom types.

scala lift squeryl

No comments:

Post a Comment