SQL expressions can't be called from within the bind processor of  
types.  The bind processor only affects the values sent as parameters  
to the DBAPI execute() and executemany() functions.    There is a  
feature request that is as yet unimplemented to provide this feature,  
however.

An approach towards embedding geometry functions has been addressed in  
a SQLAlchemy example which you can find in the source distribution in  
examples/postgis/ .    This example builds upon the ORM to provide the  
embedding so might not work for your needs.

A full blown package for GIS integration is GeoAlchemy:  http://geoalchemy.org/ 
   , which supports three different spatial databases.   Oracle's  
backend could be added to this package as well if you were in it for  
the long haul and could contribute to them.   I am not entirely  
certain but I think this package may be building off of the examples  
in the Postgis example.

Here's yet another approach which uses the compiler extension.  This  
is at the SQL level and may be enough to suit your needs, and also  
adds bind parameter support to your function.  It requires the usage  
of a custom SQL element for the "value" side of an expression (it is  
essentially a more explicit version of how the aforementioned feature  
improvement would be implemented):

from sqlalchemy import *
from sqlalchemy.sql import ClauseElement, table, column
from sqlalchemy.ext.compiler import compiles

m = MetaData()

t = table('mytable',
     column('some_geom')
)

class GeomValue(ClauseElement):
     def __init__(self, wkt, srid):
         self.wkt = literal(wkt)
         self.srid = srid

@compiles(GeomValue)
def compile_geom(element, compiler, **kw):
     return "SDO_GEOMETRY(%s,%s)" % (compiler.process(element.wkt),  
element.srid)


print t.insert().values(some_geom=GeomValue("foo", "bar"))

print t.select().where(t.c.some_geom==GeomValue("foo", "bar"))



On Sep 7, 2009, at 7:39 AM, Nicolas wrote:

>
> Hi list,
>
> While trying to build a type for spatial geometry in Oracle Spatial
> ( SDO_GEOMETRY) , i try to use the build-in function SDO_GEOMETRY from
> oracle in the bind_processor for converting WKT (text representation
> for spatial geometry) to SDO_GEOM.
>
> The problem is that sqlalchemy place quotes around the SDO_GEOMETRY
> function call and then i have the following error :
>
> DatabaseError: (DatabaseError) ORA-00932: inconsistent datatypes:
> expected MDSYS.SDO_GEOMETRY got CHAR
>
> I there a  way to call functions in the bind_processor ???
>
> Sample code :
>
>    def bind_processor(self, dialect):
>        """convert value from a geometry object to database"""
>        def convert(value):
>            if value is None:
>                return None
>            else:
> ------>        return "SDO_GEOMETRY('%s',%s)" % (value.wkt, self.srid)
>
>        return convert
>
>    def result_processor(self, dialect):
>        """convert value from database to a geometry object"""
>        def convert(value):
>            if value is None:
>                return None
>            else:
>                return asShape(sdo.Geometry(value))
>
>        return convert
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to