On Fri, Jul 23, 2021 at 4:33 AM Leo Arnold <[email protected]>
wrote:
> Hi there,
>
> I am using Sequel to access database tables with cryptic column names and
> for the sake of my own sanity I would like to do something like
>
> ```
> class ShoppingCart < Sequel::Model(:shopping_carts)
> column :krptk89, as: :campaign_id
> end
>
> ShoppingCart.where(campaign_id: 3).sql
> # Expected: SELECT * FROM shopping_carts WHERE krptk89 = 3;
> ```
>
> Does Sequel offer something like that?
>
Sequel offers column aliases at the model level:
class ShoppingCart < Sequel::Model(:shopping_carts)
def_column_alias(:campaign_id, :krptk89)
end
sc = ShoppingCart.new(:campaign_id=>3)
sc.krptk89 # => 3
However, this doesn't extend to the dataset level as in your example. You
can use custom dataset methods, as you showed in the later email. You can
also override Dataset#input_identifier:
SHOPPING_CART_MAPPING = {"campaign_id"=>'krptk89'}
ds = DB[:shopping_carts].with_extend do
def input_identifier(v)
SHOPPING_CART_MAPPING[v] || super
end
end
class ShoppingCart < Sequel::Model(ds)
def_column_alias(:campaign_id, :krptk89)
end
ShoppingCart.where(campaign_id: 3).sql
"SELECT * FROM `shopping_carts` WHERE (`krptk89` = 3)"
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sequel-talk/CADGZSSd%3DMwFpTtZK%3D%3DGnB24Lj0fnm6NG2vKzbvPqioqSxhwmVQ%40mail.gmail.com.