I have a really crazy query that handles one computed value with struct values, but i have seem to keep finding blockers. I want to ideally keep the struct, but have also include the computed value rownum. Basically i’m working with an older version of Oracle that seems to have a really weird way of doing pagination using various subqueries:
select o.* from (select o.*, rownum as r__ from (select * from o_order ORDER BY id) o where rownum <= 60) o where r__ >= 21;
schema "o_order" do
field :externalid, :string
belongs_to :store, MesaHub.Model.Store, foreign_key: :storeid
has_many :items, MesaHub.Model.OrderItem, foreign_key: :orderid
field :status, :string
field :statusreason, :string
field :customerid, :integer
field :createdby, :string
field :modifiedby, :string
end
The closest i’ve come to succeeding:
w1 = from o in MesaHub.Model.Order
w2 = from o in subquery(w1), where: fragment("rownum <= 60"), select: %{foo: o.id, test: o.externalid}, select_merge: %{r: fragment("rownum")}
w3 = from o in subquery(w2), where: fragment("? >= 21", o.r)
SELECT s0.foo, s0.test, s0.r FROM ((SELECT s0.id foo, s0.externalid test, rownum r FROM ((SELECT o0.id id, o0.externalid externalid, o0.storeid storeid, o0.status status, o0.statusreason statusreason, o0.customerid customerid, o0.createdby createdby, o0.modifiedby modifiedby FROM o_order o0)) s0 WHERE (rownum <= 60))) s0 WHERE (s0.r >= 21) []
Here i’ve managed to get pretty close to the actual query itself, however i find this pretty inflexible for creating a generic way to paginate objects, firstly i would need to always map all the keys of the struct and secondly i don’t have a nice list of structs at the end.