mkjellman / perlcassa

a Perl client for Apache Cassandra
Apache License 2.0
20 stars 12 forks source link

CQL UUID unpack only takes first 2 bytes #26

Closed MartinHerren closed 11 years ago

MartinHerren commented 11 years ago

When using CQL and doing a SELECT on an UUIDType column, it returns a short int containing only the first 2 bytes of the UUID.

Below a solution proposal returning a formated UUID string. Adding the CQL3 TimeUUIDType by the way. An other solution would be to return some sort of 128 bit binary structure.


diff --git a/lib/perlcassa/Decoder.pm b/lib/perlcassa/Decoder.pm
index aee25ef..fe56cf2 100644
--- a/lib/perlcassa/Decoder.pm
+++ b/lib/perlcassa/Decoder.pm
@@ -26,7 +26,6 @@ our %simple_unpack = (
        'Int32Type'             => 'l>',
        'LongType'              => 'q>',
        'UTF8Type'              => 'a*',
-       'UUIDType'              => 'S',
        'CounterColumnType'     => 'q>'
 );

@@ -34,6 +33,8 @@ our %complicated_unpack = (
        'IntegerType'           => \&unpack_IntegerType,
        'DecimalType'           => \&unpack_DecimalType,
        'InetAddressType'       => \&unpack_ipaddress,
+       'UUIDType'                      => \&unpack_uuid,
+       'TimeUUIDType'          => \&unpack_uuid
 );

 sub new {
@@ -198,6 +199,21 @@ sub unpack_ipaddress {
     return $ret;
 }

+# Unpack uuid/uuidtime type
+# Returns a string
+sub unpack_uuid {
+    my $packed_value = shift;
+    my $data_type = shift;
+    my $len = length($packed_value);
+    my $ret;
+    if ($len ==16) {
+               $ret = unpack('H8', $packed_value).'-'.unpack('x4H4', $packed_value).'-'.unpack('x6H4', $packed_value).'-'.unpack('x8H4', $packed_value).'-'.unpack('x10H12', $packed_value);
+       } else {
+        die("[ERROR] Invalid uuid type.");
+    }
+    return $ret;
+}
+
 # Unpack a collection type. List, Map, or Set
 # Returns:
 #   array - for list
mkjellman commented 11 years ago

want to submit a pull request and we can get this tested out? the diff pasted has got some crazy formatting issues going on.

MartinHerren commented 11 years ago

I just fixed the formating

MartinHerren commented 11 years ago

Ok, submitted a pull request.