Changes between Initial Version and Version 1 of Ticket #25446, comment 1


Ignore:
Timestamp:
Sep 22, 2015, 1:23:43 PM (9 years ago)
Author:
Evan Heidtmann

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #25446, comment 1

    initial v1  
    99        from django.contrib.gis.db.backends.postgis.adapter import PostGISAdapter
    1010
    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(
    1212            line_serialized=str(PostGISAdapter(line)))
    1313
    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()
    1521
    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)
    2126        else:
    22             return (False, segments[0])
     27            return (cls.objects.get(id=ids[0][0]), False)
     28
    2329
    2430}}}
Back to Top