LEFT | RIGHT |
1 # This file is part of trytond_backend_gis. The COPYRIGHT file at the top | 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 | 2 # level of this repository contains the full copyright notices and license |
3 # terms. | 3 # terms. |
| 4 import re |
| 5 |
4 from trytond.transaction import Transaction | 6 from trytond.transaction import Transaction |
5 from trytond.backend.postgresql.table import TableHandler as PGTableHandler | 7 from trytond.backend.postgresql.table import TableHandler as PGTableHandler |
6 | 8 |
7 from trytond_gis.fields import GIS_RE | 9 __all__ = ['TableHandler'] |
8 | 10 |
9 __all__ = ['TableHandler'] | 11 GIS_RE = re.compile( |
| 12 r'GIS_(?P<type>[A-Z]+)\((?P<srid>[0-9]+), (?P<dimension>[0-9]+)\)') |
10 | 13 |
11 | 14 |
12 class TableHandler(PGTableHandler): | 15 class TableHandler(PGTableHandler): |
13 | 16 |
14 def add_column(self, column_name, abstract_type, default_fun=None, | 17 def add_column(self, column_name, abstract_type, default=None, comment=''): |
15 fill_default=True, comment=''): | |
16 if abstract_type.startswith('GIS_'): | 18 if abstract_type.startswith('GIS_'): |
17 column_adder = self.add_geometry_column | 19 column_adder = self.add_geometry_column |
18 else: | 20 else: |
19 column_adder = super(TableHandler, self).add_column | 21 column_adder = super(TableHandler, self).add_column |
20 column_adder(column_name, abstract_type, default_fun, fill_default, | 22 column_adder(column_name, abstract_type, default, comment) |
21 comment) | |
22 | 23 |
23 def add_geometry_column(self, column_name, abstract_type, default_fun=None, | 24 def add_geometry_column(self, column_name, abstract_type, default_fun=None, |
24 fill_default=True, comment=''): | 25 fill_default=True, comment=''): |
25 cursor = Transaction().connection.cursor() | 26 cursor = Transaction().connection.cursor() |
26 | 27 |
27 if self.column_exist(column_name): | 28 if self.column_exist(column_name): |
28 # TODO check for type and eventual migration | 29 # TODO check for type and eventual migration |
29 return | 30 return |
30 | 31 |
31 match = GIS_RE.match(abstract_type) | 32 match = GIS_RE.match(abstract_type) |
32 assert match is not None, \ | 33 assert match is not None, \ |
33 'The abstract type %s is not supported' % abstract_type | 34 'The abstract type %s is not supported' % abstract_type |
34 type_ = match.group('type') | 35 type_ = match.group('type') |
35 srid, dimension = map(int, match.group('srid', 'dimension')) | 36 srid, dimension = map(int, match.group('srid', 'dimension')) |
36 cursor.execute('SELECT AddGeometryColumn(%s, %s, %s, %s, %s)', | 37 cursor.execute('SELECT AddGeometryColumn(%s, %s, %s, %s, %s)', |
37 (self.table_name, column_name, srid, type_, dimension)) | 38 (self.table_name, column_name, srid, type_, dimension)) |
38 self._update_definitions(columns=True) | 39 self._update_definitions(columns=True) |
LEFT | RIGHT |