On Friday, July 20, 2012 5:58:39 PM UTC-7, cult hero wrote:
>
> Just to give some background:
>
> 1. I'm working with MySQL via JDBC.
> 2. I'm running in 1.9 mode.
> 3. I'm connecting an existing schema that uses GUIDs as primary keys.
> 4. Said GUIDs are stored as binary(16) fields.
>

Yuck. :)
 

> The following code does not work and it seems like it should:
>
> m1 = Model.first => #<Model 
> @values={:id=>"\u0016\u00EF|T\u00C1a\u0011\u00E1\u008BD\u0003+\u00CC\u0081 
> \u001C">
> SQL: SELECT * FROM `models` LIMIT 1
>
> m1.id.class => Sequel::SQL::Blob
>
> m2 = Model.filter(:id => m1.id).first => nil
> SQL: SELECT * FROM `models` WHERE `id` = '?|T?a?D+́ '
>

If I had to guess, this is an encoding issue.  What is m1.id.encoding?

Can you try using the prepared_statements plugin and do Model[m1.id] (or 
just create and execute a similar prepared statement manually)?
 

> The data coming back is definitely correct:
>
> uuid = UUIDTools::UUID.parse_raw(m1.id) => #<UUID:0xc9a 
> UUID:16ef7c54-c161-11e1-8b44-032bcc81201c>
>

OK.  So you know the bytes in the string are correct.
 

> There is an existing MySQL routine for handling UUIDs that is used and it 
> works as would be expected:
>
> Model.filter(:id => :uuid2binary.sql_function(uuid.to_s)).first => #<Model 
> @values={:id=>"\u0016\u00EF|T\u00C1a\u0011\u00E1\u008BD\u0003+\u00CC\u0081 
> \u001C">\
> SQL: SELECT * FROM `models` WHERE (`id` = 
> uuid2binary('16ef7c54-c161-11e1-8b44-032bcc81201c')) LIMIT 1
>

One way to handle this is to typecast to UUID on model load, and the add a 
literalizer for UUID to use the uuid2binary function with the uuid string 
value.  That may work.
 

> Why does "m2 = Model.filter(:id => m1.id).first" fail?
>
> It appears to be come kind of escaping issue.
>

I wonder if the m1.id is encoded differently, and when you put it in a 
query string, it transcodes (because the query string has a different 
encoding), changing the bytes, which causes the lookup to fail.

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/-/OQ2NoRqskaYJ.
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