Browse Source

bi_view_editor: window functions without ORDER BY clause do not have reliable behavior

12.0
SimoRubi 5 years ago
parent
commit
acb5cabccd
  1. 18
      bi_view_editor/models/bve_view.py
  2. 1
      bi_view_editor/readme/ROADMAP.rst
  3. 3
      bi_view_editor/readme/USAGE.rst
  4. 3
      bi_view_editor/views/bve_view.xml

18
bi_view_editor/models/bve_view.py

@ -98,6 +98,19 @@ class BveView(models.Model):
compute='_compute_users', compute='_compute_users',
store=True) store=True)
query = fields.Text(compute='_compute_sql_query') query = fields.Text(compute='_compute_sql_query')
over_condition = fields.Text(
states={
'draft': [
('readonly', False),
],
},
readonly=True,
help="Condition to be inserted in the OVER part "
"of the ID's row_number function.\n"
"For instance, 'ORDER BY t1.id' would create "
"IDs ordered in the same way as t1's IDs; otherwise "
"IDs are assigned with no specific order.",
)
er_diagram_image = fields.Binary(compute='_compute_er_diagram_image') er_diagram_image = fields.Binary(compute='_compute_er_diagram_image')
_sql_constraints = [ _sql_constraints = [
@ -285,11 +298,12 @@ class BveView(models.Model):
self.env.cr.execute('CREATE or REPLACE VIEW %s as (%s)', ( self.env.cr.execute('CREATE or REPLACE VIEW %s as (%s)', (
AsIs(view_name), AsIs(query), )) AsIs(view_name), AsIs(query), ))
@api.depends('line_ids', 'state')
@api.depends('line_ids', 'state', 'over_condition')
def _compute_sql_query(self): def _compute_sql_query(self):
for bve_view in self: for bve_view in self:
tables_map = {} tables_map = {}
select_str = '\n CAST(row_number() OVER () as integer) AS id'
select_str = '\n CAST(row_number() OVER ({}) as integer) AS id' \
.format(bve_view.over_condition or '')
for line in bve_view.field_ids: for line in bve_view.field_ids:
table = line.table_alias table = line.table_alias
select = line.field_id.name select = line.field_id.name

1
bi_view_editor/readme/ROADMAP.rst

@ -5,3 +5,4 @@
* Data the user has no access to (e.g. in a multi company situation) can be * Data the user has no access to (e.g. in a multi company situation) can be
viewed by making a view. Would be nice if models available to select when viewed by making a view. Would be nice if models available to select when
creating a view are limited to the ones that have intersecting groups. creating a view are limited to the ones that have intersecting groups.
* Raise a warning in case the SQL query is not correct.

3
bi_view_editor/readme/USAGE.rst

@ -21,3 +21,6 @@ To access the created BI View with a dedicated menu:
A more advanced UI is also available under the "Details" tab. It provides extra A more advanced UI is also available under the "Details" tab. It provides extra
possibilities for more advanced users, like to use LEFT JOIN instead of the possibilities for more advanced users, like to use LEFT JOIN instead of the
default INNER JOIN. default INNER JOIN.
It also possible to improve the IDs generation for new views by adding an `Over Condition` in the "SQL" tab, see https://www.postgresql.org/docs/current/sql-expressions.html#SYNTAX-WINDOW-FUNCTIONS for further details.
For instance, an ORDER BY clause helps preventing unreliable behavior when filtering the generated views.

3
bi_view_editor/views/bve_view.xml

@ -85,8 +85,9 @@
</group> </group>
</page> </page>
<page string="SQL" groups="base.group_no_one"> <page string="SQL" groups="base.group_no_one">
<field name="query"/>
<group> <group>
<field name="query" nolabel="1" />
<field name="over_condition"/>
</group> </group>
</page> </page>
<page string="Security"> <page string="Security">

Loading…
Cancel
Save