add better control of "expression affinity"

Issue #1726 resolved
Mike Bayer repo owner created an issue

i.e. below, to avoid using literal(ip, IPV4), we can stick _type_affinity on the TypeDecorator. but that might get in trouble in other places, so a new system for just this purpose should be added, probably expression_affinity().

Also TypeDecorator should be in sqlalchemy.*, not sure what's up with that.

from sqlalchemy import *
from sqlalchemy.types import TypeDecorator

engine = create_engine('oracle://scott:tiger@172.16.248.131/xe', echo=True)

from struct import pack, unpack
from socket import inet_aton, inet_ntoa

class IPV4(TypeDecorator):
   impl = Integer

   def process_bind_param(self, dotted_quad, engine):
       return unpack('!L', inet_aton(dotted_quad))[0](0)

   def process_result_value(self, n, engine):
       return inet_ntoa(pack('!L', long(n)))

   @property
   def _type_affinity(self):
       return String

m = MetaData(engine)
network = Table("network", m, Column('ip', IPV4))
m.drop_all()
m.create_all()

network.insert().execute({'ip':'10.184.67.163', 'ip':'127.0.0.1'})

ip='10.184.67.163'
s1 = select([network.c.ip](network.c.ip), network.c.ip <= ip)

print s1

r = s1.execute()

Comments (3)

  1. Log in to comment