How to dynamic assign special field from method parameter when extract method to perform query

15 views
Skip to first unread message

Mike Chen

unread,
Dec 23, 2015, 11:56:22 AM12/23/15
to Squeryl
Hi, I have a problem when I want to extract duplicate logic to a method:

this is my origin code that to perform a statistic to update the most common value from a table field :
val goods = getFromDb(goodsid)

//for cost field
from(yhGoodsShops)(gs =>
   
where(gs.venderid === venderid and gs.goodsid === goods.goodsid)
            groupBy
(gs.cost)
            compute
(count())
            orderBy
(count() desc)
).page(0, 1).toList.headOption.foreach(row => {
   
if (goods.cost != row.key) {        
        goods
.cost = row.key        
   
}
})

//for price field
from(yhGoodsShops)(gs =>
   
where(gs.venderid === venderid and gs.goodsid === goods.goodsid)
            groupBy
(gs.price)
            compute
(count())
            orderBy
(count() desc)
).page(0, 1).toList.headOption.foreach(row => {
   
if (goods.price != row.key) {        
        goods
.price = row.key        
   
}
})

I want to extract a method  to do the same job like this::

goods.cost = newMethod("cost", goods.cost)
goods
.price = newMethod("price", goods.price)

how I to define the newMethod ?

Thanks.
Reply all
Reply to author
Forward
0 new messages