On Monday, July 23, 2012 12:41:12 PM UTC-7, binarypaladin wrote:
>
>
>   class Model
>>>>     plugin :typecast_on_load, :id
>>>>     def id=(v)
>>>>       v.is_a?(String) ? super(UUIDTools::UUID.parse_raw(v) : super
>>>>     end
>>>>   end
>>>>   class UUID
>>>>     def sql_literal(ds)
>>>>       "uuid2binary(#{ds.literal(to_s)})"
>>>>     end
>>>>   end
>>>>
>>>
> I've actually had time to attempt implementing this. The problem 
> with typecast_on_load is that I end up with this:
>
> m1.id => "db7ee4c0-cac5-11e1-85e9-b8f6b1134127"
> m1.id.class => Sequel::SQL::Blob
>
> It's still being converted back into Sequel::SQL::Blob and as such the 
> UUID literalizer doesn't end up doing anything.
>

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?
 

> I'm halfway temped to override or create a literalizer 
> for Sequel::SQL::Blob since the only binary fields in use are these.
>

Certainly that's an option.  You'll have to override 
Dataset#literal_blob_append if you want to do that.
 

> I pointed out the uuid2binary, but we also have binary2uuid which is 
> probably self explanatory. What could I do to force all binary(16) fields 
> to use those functions transparently in the models?
>

If you want to do that in the query that retrieves the row:

   Model.dataset = Model.dataset.select(Sequel.function(:binary2uuid, 
:id).as(:id), ...)
 
I don't think that's a good option, though, since then it should show up as 
a normal string.

Even using prepared statements, when I try and do an association that uses 
> a UUID key, I run into the same troubles we mentioned before:
>
> m1.assoc_models => nil
> SELECT * FROM `assoc_models` WHERE (`assoc_models`.`model_id` = '?|T?a?D+́ 
> ')
>

Are you also using the prepared_statements_associations plugin?

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/-/0fBwOEoFjCQJ.
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.

Reply via email to