On Monday, July 23, 2012 2:20:09 PM UTC-7, binarypaladin wrote:
>
>
> Well, Sequel::SQL::Blob is a String subclass, so it should still get
>>> wrapped in a UUID. Can you post the code you are using?
>>>
>>
> class Model < Sequel::Model(:model)
> plugin :prepared_statements
> plugin :prepared_statements_associations
> plugin :typecast_on_load, :id
>
> one_to_many :assocs
>
> def id=(v)
> super(UUIDTools::UUID.parse_raw(v))
> end
> end
>
> class UUIDTools::UUID
> def sql_literal(ds)
> "uuid2binary(#{ds.literal(to_s)})"
> end
> end
>
> m1 = Model.first => #<Model
> @values={:id=>"\u00DB~\u00E4\u00C0\u00CA\u00C5\u0011\u00E1\u0085\u00E9\u00B8\u00F6\u00B1\u0013A'"}>
> m1.id => "db7ee4c0-cac5-11e1-85e9-b8f6b1134127"
> m1.id.class => Sequel::SQL::Blob
>
That's odd. Here's an example that works here:
require 'sequel'
Sequel.sqlite.create_table(:as){Integer :id}
class A < Sequel::Model
insert(1)
plugin :typecast_on_load, :id
def id=(v)
super(v*2)
end
p first
end
Output is what I would expect:
#<A @values={:id=>2}>
> When I added this:
>
> class Model
> def id
> UUIDTools::UUID.parse_raw(super)
> end
> end
>
> Obviously, I get a UUID and the literalizer works as expected. Perhaps I
> am misunderstanding what typecast_on_load is actually for.
>
That works as it does the conversion on each method call, leaving the
stored value alone. typecast_on_load is called when the model is loaded,
and calls the setter method with the value given by the adapter.
If you can provide a self-contained example showing the your problem, I'll
be happy to look into it.
> Shouldn't m1.id.class be UUIDTools::UUID? Or does calling "super" then
> force it to do to_s (that's what it seems like it's doing)?
>
That's probably the problem. Sequel::Model probably thinks the column is a
blob, so it does a type conversion to blob inside super. You can do the
following:
Model.db_schema[:id][:type] = nil
That will turn off typecasting for Model's id column.
> Are you also using the prepared_statements_associations plugin?
>>
>
> No, because apparently I'm a doofus. That solves a large part of the
> association dilemma. If I want to do any sort of complex querying through
> the dataset directly, I can pass in an sql_function which should do
> basically everything want. This solves a bunch of things.
>
Good. I think the above typecasting workaround will get the typecast on
load plugin working for you, and hopefully a combination of prepared
statements and/or typecast_on_load will fix any issues.
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/sequel-talk/-/Uc4tLz5MM0wJ.
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.