J. Cliff Dyer wrote:
>
> I'm trying to create a custom geometry type for MySQL, but I'm running
> up against my limited knowledge of SQLAlchemy.
>
>
> My goal is to be able to pass in a geos.Point object on INSERT and
> UPDATE, convert it to WKT representation ('POINT (1.0000000000000000
> 2.0000000000000000)'), and pass that to the database, using MySQL's
> GeomFromText() function.
>
> When I get it out of the database, I would like to be able to have my
> SELECT query ask for AsText(column) rather than just column, and then on
> the python side, pass that through geos.from_str() to get a geos.Point
> object back.
>
> I'm basing my work on the code here:
> http://trac.bycycle.org/browser/Core/trunk/bycycle/core/model/data/sqltypes.py?rev=1180
>
> I'm pretty sure I can handle everything on the python side using
> bind_processor() and result_processor(), but I'm not sure how to pass my
> WKT object through GeomFromText() on the way into the database.
>
> I tried writing a method like the following on my Geometry(TypeEngine)
> class:
>
>
>     def bind_processor(self, dialect):
>         """Convert from Python type to database type."""
>
>         def process(value):
>             if value is None:
>                 return None
>             else:
>                 if isinstance(value, geos.base.GEOSGeometry):
>                     return sqlalchemy.func.GeomFromText(value.wkt)
>                 else:
>                     return sqlalchemy.func.GeomFromText(value)
>         return process

bind_processor processes the value of bind parameters sent to the
database, at the DBAPI level they are in the params argument to
cursor.execute(), and occur after the statement has been rendered.  SQL
expressions like functions cannot be rendered within them.  The rendering
of func.GeomFromText() has to take place at the level of statement
construction.

Theres a comprehensive ORM-centric example of how to work with
GeomFromText as it exists in postgis, in examples/postgis/postgis.py .  
there, the ORM level attributes are given special comparison behavior that
renders the GeomFromText function automatically.   but as always you can
render SQL expressions directly as long as you call func.GeomFromText() in
the columns clause of select() constructs.


--~--~---------~--~----~------------~-------~--~----~
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