| 1 | from django.contrib.gis.db.backend import SpatialBackend |
| 2 | from django.db.models.query import insert_query |
| 3 | |
| 4 | if SpatialBackend.oracle: |
| 5 | from django.db import connection |
| 6 | from django.db.models.sql.subqueries import InsertQuery |
| 7 | |
| 8 | class GeoInsertQuery(InsertQuery): |
| 9 | def insert_values(self, insert_values, raw_values=False): |
| 10 | placeholders, values = [], [] |
| 11 | for field, val in insert_values: |
| 12 | if hasattr(field, 'get_placeholder'): |
| 13 | if hasattr(field, 'geom_type') and val is None: |
| 14 | placeholders.append('NULL') |
| 15 | else: |
| 16 | placeholders.append(field.get_placeholder(val)) |
| 17 | values.append(val) |
| 18 | else: |
| 19 | placeholders.append('%s') |
| 20 | values.append(val) |
| 21 | self.columns.append(field.column) |
| 22 | |
| 23 | if raw_values: |
| 24 | self.values.extend(values) |
| 25 | else: |
| 26 | self.params += tuple(values) |
| 27 | self.values.extend(placeholders) |
| 28 | |
| 29 | def insert_query(model, values, return_id=False, raw_values=False): |
| 30 | query = GeoInsertQuery(model, connection) |
| 31 | query.insert_values(values, raw_values) |
| 32 | return query.execute_sql(return_id) |
| 33 | |