Revision: 1c79c28369d8
Author:   paul cannon <[email protected]>
Date:     Sat Sep  8 20:00:13 2012
Log:      Support for inet data type

http://code.google.com/a/apache-extras.org/p/cassandra-dbapi2/source/detail?r=1c79c28369d8

Modified:
 /cql/cqltypes.py
 /test/test_marshalling.py

=======================================
--- /cql/cqltypes.py    Thu Aug 30 21:01:20 2012
+++ /cql/cqltypes.py    Sat Sep  8 20:00:13 2012
@@ -35,6 +35,7 @@
                          varint_pack, varint_unpack)
 from decimal import Decimal
 import time
+import socket
 import calendar
 import re

@@ -406,6 +407,25 @@
     deserialize = staticmethod(varint_unpack)
     serialize = staticmethod(varint_pack)

+class InetAddressType(_CassandraType):
+    typename = 'inet'
+
+    @staticmethod
+    def deserialize(byts):
+        if len(byts) == 16:
+            fam = socket.AF_INET6
+        else:
+            fam = socket.AF_INET
+        return socket.inet_ntop(fam, byts)
+
+    @staticmethod
+    def serialize(addr):
+        if ':' in addr:
+            fam = socket.AF_INET6
+        else:
+            fam = socket.AF_INET
+        return socket.inet_pton(fam, addr)
+
 class CounterColumnType(_CassandraType):
     typename = 'counter'

=======================================
--- /test/test_marshalling.py   Sun Aug 19 13:43:08 2012
+++ /test/test_marshalling.py   Sat Sep  8 20:00:13 2012
@@ -56,6 +56,9 @@
     ('\x7f\xff\xff\xff\xff\xff\xff\xff', 'LongType',  9223372036854775807),
     ('\x80\x00\x00\x00\x00\x00\x00\x00', 'LongType', -9223372036854775808),
     ('', 'LongType', None),
+    ('', 'InetAddressType', None),
+    ('A46\xa9', 'InetAddressType', '65.52.54.169'),
+ ('*\x00\x13(\xe1\x02\xcc\xc0\x00\x00\x00\x00\x00\x00\x01"', 'InetAddressType', '2a00:1328:e102:ccc0::122'), ('\xe3\x81\xbe\xe3\x81\x97\xe3\x81\xa6', 'UTF8Type', u'\u307e\u3057\u3066'), ('\xe3\x81\xbe\xe3\x81\x97\xe3\x81\xa6' * 1000, 'UTF8Type', u'\u307e\u3057\u3066' * 1000),
     ('', 'UTF8Type', u''),

Reply via email to