I had to override the "output_identifier" as well to avoid "multiple
assignments to same column" error.
I created a plugin to solve the whole thing. It works for me, but any
comment would be appreciated.
module Sequel
module Plugins
module ColumnAlias
def self.configure(model, alias_mappings)
model.instance_eval do
(@alias_mappings = alias_mappings).each do |column_alias,
column_name|
def_column_alias column_alias, column_name
end
end
end
module ClassMethods
attr_reader :alias_mappings
end
module DatasetMethods
def input_identifier(value)
model.alias_mappings[value.to_sym]&.to_sym || super
end
def output_identifier(value)
model.alias_mappings[value.to_sym] || super
end
end
end
end
end
And then, in the model:
plugin :column_alias, { campaign_id: :krptk89 }
Thanks,
Juan
On Fri, Jul 23, 2021 at 2:33 PM Jeremy Evans <[email protected]> wrote:
> 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
> <https://groups.google.com/d/msgid/sequel-talk/CADGZSSd%3DMwFpTtZK%3D%3DGnB24Lj0fnm6NG2vKzbvPqioqSxhwmVQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
--
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/CALXCfb3CBS139r8JkOx2_6vHL10-dOGO2LXD%3DgzmES9Rn7UKaQ%40mail.gmail.com.