- Timestamp:
- 02/10/08 20:25:01 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/gis/django/contrib/gis/db/backend/mysql/field.py
r6527 r7104 1 import re2 from types import StringType, UnicodeType3 1 from django.db import connection 4 2 from django.db.models.fields import Field # Django base Field class 5 from django.contrib.gis.geos import GEOSGeometry 6 from django.contrib.gis.db.backend.util import GeoFieldSQL 7 from django.contrib.gis.db.backend.mysql.query import MYSQL_GIS_TERMS, GEOM_FROM_TEXT 3 from django.contrib.gis.db.backend.mysql.query import GEOM_FROM_TEXT 8 4 9 5 # Quotename & geographic quotename, respectively. 10 6 qn = connection.ops.quote_name 11 def gqn(value):12 if isinstance(value, UnicodeType): value = value.encode('ascii')13 return "'%s'" % value14 7 15 8 class MySQLGeoField(Field): … … 24 17 Thus, for best spatial performance, you should use MyISAM tables 25 18 (which do not support transactions). For more information, see Ch. 26 1 7.6.1 of the MySQL 5.0 documentation.19 16.6.1 of the MySQL 5.0 documentation. 27 20 """ 28 21 … … 51 44 "The OpenGIS name is returned for the MySQL database column type." 52 45 return self._geom 53 54 def get_db_prep_lookup(self, lookup_type, value):55 """56 Returns field's value prepared for database lookup, accepts WKT and57 GEOS Geometries for the value.58 """59 if lookup_type in MYSQL_GIS_TERMS:60 # special case for isnull lookup61 if lookup_type == 'isnull': return GeoFieldSQL([], [])62 63 # When the input is not a GEOS geometry, attempt to construct one64 # from the given string input.65 if isinstance(value, GEOSGeometry):66 pass67 elif isinstance(value, (StringType, UnicodeType)):68 try:69 value = GEOSGeometry(value)70 except GEOSException:71 raise TypeError("Could not create geometry from lookup value: %s" % str(value))72 else:73 raise TypeError('Cannot use parameter of %s type as lookup parameter.' % type(value))74 75 return GeoFieldSQL(['%s(%%s)' % GEOM_FROM_TEXT], [value])76 77 else:78 raise TypeError("Field has invalid lookup: %s" % lookup_type)79 80 def get_db_prep_save(self, value):81 "Prepares the value for saving in the database."82 if not bool(value): return None83 if isinstance(value, GEOSGeometry):84 return value85 else:86 raise TypeError('Geometry Proxy should only return GEOSGeometry objects.')87 46 88 47 def get_placeholder(self, value): 89 48 """ 90 Nothing special happens here because MySQL does not support transformations. 49 The placeholder here has to include MySQL's WKT constructor. Because 50 MySQL does not support spatial transformations, there is no need to 51 modify the placeholder based on the contents of the given value. 91 52 """ 92 53 return '%s(%%s)' % GEOM_FROM_TEXT
