Changes between Initial Version and Version 1 of Ticket #25446, comment 1
- Timestamp:
- Sep 22, 2015, 1:23:43 PM (9 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #25446, comment 1
initial v1 9 9 from django.contrib.gis.db.backends.postgis.adapter import PostGISAdapter 10 10 11 sql = 'SELECT *FROM ride_segment WHERE line ~= {line_serialized} AND ST_OrderingEquals(line, {line_serialized})'.format(11 sql = 'SELECT id FROM ride_segment WHERE line ~= {line_serialized} AND ST_OrderingEquals(line, {line_serialized})'.format( 12 12 line_serialized=str(PostGISAdapter(line))) 13 13 14 segments = list(cls.objects.raw(sql)) 14 # We have to run a query outside of Manager.raw() because Manager.raw() 15 # always supplies a params argument to cursor.execute(), and sometimes 16 # the binary form of the line contains a percent sign. 17 from django.db import connection 18 with connection.cursor() as cursor: 19 cursor.execute(sql) 20 ids = cursor.fetchmany() 15 21 16 if len(segments) > 1: 17 from django.db import IntegrityError 18 raise IntegrityError('Multiple segments for line {}'.format(line.ewkt)) 19 elif len(segments) == 0: 20 return (True, cls.objects.create(line=line)) 22 if len(ids) > 1: 23 raise cls.MultipleObjectsReturned('Multiple segments for line {}'.format(line.ewkt)) 24 elif len(ids) == 0: 25 return (cls.objects.create(line=line), True) 21 26 else: 22 return (False, segments[0]) 27 return (cls.objects.get(id=ids[0][0]), False) 28 23 29 24 30 }}}