On Feb 13, 8:16 am, Michael Gliwinski <Michael.Gliwin...@henderson-
group.com> wrote:
> I'm working with a legacy MSSQL database which is primarily updated by a 3rd
> party app which I have no control over. This 3rd party app likes to insert
> string values padded with spaces, which in turn forces me to call .strip on
> most values I access. I was looking for a way to make this automatic.
>
> I found I can just override getters on the module that just do `super.strip'
> but since there are many such columns on different models, this does not seem
> very optimal. Is there a way to override getters en masse (e.g. for a list of
> columns, or for columns of particular type)?
Assuming you are using models, this may work as a global setting:
class Sequel::Model
def [](v)
case s = super
when String
s.strip
else
s
end
end
end
> Also, I was searching through the list archives and found I could probably
> (ab)use typecasting for that. E.g. I've seen typecast_value_string may be the
> place to do it, but I'm not sure if this is a good idea? If it is, what would
> be a good way to extend it?
Typecasting is only going to be used for model setter methods, so if a
third party is inserting extra spaces, that's not the correct place to
handle it.
> Or would it be even better to do the transformations on database side, e.g.
> replacing columns in select with e.g. `replace(tbl.col, ' ', '') as col'?
That is another option. You could also patch the Sequel adapter or
driver to do the stripping.
> Also a related question, if a database uses e.g. type char for some columns,
> but they really are integers, and I'd prefer to work with them as integers,
> what are my options?
If there aren't too many of such columns, replacing the columns with
"cast(tbl.col AS integer) AS col" may work. Alternatively, you could
just override the accessor:
class Foo < Sequel::Model
def col
s = super
Integer(s) if s
end
end
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en.