You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
5.0 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. #
  4. # Authors: Guewen Baconnier
  5. # Copyright 2015 Camptocamp SA
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. #
  21. import re
  22. import psycopg2
  23. from openerp.osv import orm, fields
  24. from openerp.tools.translate import _
  25. # views are created with a prefix to prevent conflicts
  26. SQL_VIEW_PREFIX = u'sql_view_'
  27. # 63 chars is the length of a postgres identifier
  28. USER_NAME_SIZE = 63 - len(SQL_VIEW_PREFIX)
  29. PG_NAME_RE = re.compile(r'^[a-z_][a-z0-9_$]*$', re.I)
  30. class SQLView(orm.Model):
  31. _name = 'sql.view'
  32. def _compute_complete_sql_name(self, cr, uid, ids, name, args,
  33. context=None):
  34. res = {}
  35. for sql_view in self.browse(cr, uid, ids, context=context):
  36. res[sql_view.id] = SQL_VIEW_PREFIX + sql_view.sql_name
  37. return res
  38. _columns = {
  39. 'name': fields.char(string='View Name', required=True),
  40. 'sql_name': fields.char(string='SQL Name', required=True,
  41. size=USER_NAME_SIZE,
  42. help="Name of the view. Will be prefixed "
  43. "by %s" % (SQL_VIEW_PREFIX,)),
  44. 'complete_sql_name': fields.function(_compute_complete_sql_name,
  45. string='Complete SQL Name',
  46. readonly=True,
  47. type='char'),
  48. 'definition': fields.text(string='Definition', required=True),
  49. }
  50. def _check_sql_name(self, cr, uid, ids, context=None):
  51. records = self.browse(cr, uid, ids, context=context)
  52. return all(PG_NAME_RE.match(record.sql_name) for record in records)
  53. _constraints = [
  54. (_check_sql_name,
  55. 'The SQL name is not a valid PostgreSQL identifier',
  56. ['sql_name']),
  57. ]
  58. def _sql_view_comment(self, cr, uid, sql_view, context=None):
  59. return "%s (created by the module sql_view)" % sql_view.name
  60. def _create_or_replace_sql_view(self, cr, uid, sql_view, context=None):
  61. view_name = sql_view.complete_sql_name
  62. try:
  63. # The parenthesis around the definition are important:
  64. # they prevent to insert a semicolon and another query after
  65. # the view declaration
  66. cr.execute(
  67. "CREATE VIEW {view_name} AS ({definition})".format(
  68. view_name=view_name,
  69. definition=sql_view.definition)
  70. )
  71. except psycopg2.ProgrammingError as err:
  72. raise orm.except_orm(
  73. _('Error'),
  74. _('The definition of the view is not correct:\n\n%s') % (err,)
  75. )
  76. comment = self._sql_view_comment(cr, uid, sql_view, context=context)
  77. cr.execute(
  78. "COMMENT ON VIEW {view_name} IS %s".format(view_name=view_name),
  79. (comment,)
  80. )
  81. def _delete_sql_view(self, cr, uid, sql_view, context=None):
  82. view_name = sql_view.complete_sql_name
  83. cr.execute("DROP view IF EXISTS %s" % (view_name,))
  84. def create(self, cr, uid, vals, context=None):
  85. record_id = super(SQLView, self).create(cr, uid, vals,
  86. context=context)
  87. record = self.browse(cr, uid, record_id, context=context)
  88. self._create_or_replace_sql_view(cr, uid, record, context=context)
  89. return record_id
  90. def write(self, cr, uid, ids, vals, context=None):
  91. # If the name has been changed, we have to drop the view,
  92. # it will be created with the new name.
  93. # If the definition have been changed, we better have to delete
  94. # it and create it again otherwise we can have 'cannot drop
  95. # columns from view' errors.
  96. for record in self.browse(cr, uid, ids, context=context):
  97. self._delete_sql_view(cr, uid, record, context=context)
  98. result = super(SQLView, self).write(cr, uid, ids, vals,
  99. context=context)
  100. for record in self.browse(cr, uid, ids, context=context):
  101. self._create_or_replace_sql_view(cr, uid, record,
  102. context=context)
  103. return result
  104. def unlink(self, cr, uid, ids, context=None):
  105. for record in self.browse(cr, uid, ids, context=context):
  106. self._delete_sql_view(cr, uid, record, context=context)
  107. result = super(SQLView, self).unlink(cr, uid, ids, context=context)
  108. return result