LEFT | RIGHT |
(no file at all) | |
| 1 # This file is part of trytond_backend_gis. The COPYRIGHT file at the top |
| 2 # level of this repository contains the full copyright notices and license |
| 3 # terms. |
| 4 import logging |
| 5 |
| 6 from trytond.config import config |
| 7 from trytond.transaction import Transaction |
| 8 from trytond.backend.postgresql.table import TableHandler as PGTableHandler |
| 9 |
| 10 from trytond_gis.const import GIS_SQL_TYPE_RE, WGS_84 |
| 11 |
| 12 __all__ = ['TableHandler'] |
| 13 logger = logging.getLogger(__name__) |
| 14 |
| 15 |
| 16 class TableHandler(PGTableHandler): |
| 17 |
| 18 def add_column(self, column_name, abstract_type, default=None, comment=''): |
| 19 if abstract_type.startswith('GIS_'): |
| 20 column_adder = self.add_geometry_column |
| 21 else: |
| 22 column_adder = super(TableHandler, self).add_column |
| 23 column_adder(column_name, abstract_type, default, comment) |
| 24 |
| 25 def add_geometry_column(self, column_name, abstract_type, default_fun=None, |
| 26 fill_default=True, comment=''): |
| 27 cursor = Transaction().connection.cursor() |
| 28 |
| 29 match = GIS_SQL_TYPE_RE.match(abstract_type) |
| 30 assert match is not None, \ |
| 31 'The abstract type %s is not supported' % abstract_type |
| 32 type_ = match.group('type') |
| 33 srid = config.getint('database', 'srid', default=WGS_84) |
| 34 dimension = int(match.group('dimension')) |
| 35 |
| 36 if self.column_exist(column_name): |
| 37 cursor.execute(''' |
| 38 SELECT type, srid, coord_dimension |
| 39 FROM geometry_columns |
| 40 WHERE f_table_name=%s and f_geometry_column=%s''', |
| 41 (self.table_name, column_name)) |
| 42 column_info = cursor.fetchone() |
| 43 if not column_info: |
| 44 logger.warning( |
| 45 'Unable to migrate column %s on table %s ' |
| 46 'column is not a geometric type.', |
| 47 column_name, self.table_name) |
| 48 return |
| 49 |
| 50 prev_type, prev_srid, prev_dimension = column_info |
| 51 if prev_type.upper() != type.upper(): |
| 52 logger.warning( |
| 53 'Unable to migrate column %s on table %s ' |
| 54 'from %s to %s.', |
| 55 column_name, self.table_name, |
| 56 prev_type, type_) |
| 57 elif prev_srid != srid: |
| 58 logger.warning( |
| 59 'Unable to migrate column %s on table %s ' |
| 60 'from SRID %s to %s.', |
| 61 column_name, self.table_name, |
| 62 prev_srid, srid) |
| 63 elif prev_dimension != dimension: |
| 64 logger.warning( |
| 65 'Unable to migrate column %s on table %s ' |
| 66 'from dimension %s to %s.', |
| 67 column_name, self.table_name, |
| 68 prev_dimension, dimension) |
| 69 return |
| 70 |
| 71 cursor.execute('SELECT AddGeometryColumn(%s, %s, %s, %s, %s)', |
| 72 (self.table_name, column_name, srid, type_, dimension)) |
| 73 self._update_definitions(columns=True) |
LEFT | RIGHT |