3

我需要使用一个相当复杂的排序函数,它涉及多个连接和一些数学运算。因此,无法将其合并到标准 DataMapper 语法(例如User.all(:order => ...))中。目前我有类似的东西

repository(:default).adapter.select('
SELECT users.id, ( (count(table1.*) - count(table2.*))/((EXTRACT(EPOCH FROM current_timestamp)) ) as sort FROM users
LEFT JOIN table1
ON table1.user_id = users.id
LEFT JOIN table2
ON ...
AND ...
GROUP BY users.id
ORDER BY sort DESC')

但是,我想将它集成到 DataMapper 中,以便我可以在任何 datamapper 集合上调用它

例如,User.all(:name => 'bob').crazy_sort_function返回按我的特殊函数排序的名为“bob”的用户,或者可能User.all(:order => crazy_sort),等等......无论什么公式都尽可能地将其无缝集成到数据映射器 ORM 中。

注意:由于 current_timestamp 是查询的一部分,因此我不能定期为所有行编译并放入一列中。

4

0 回答 0