Commit Graph
14 Commits
Author SHA1 Message Date
Raphael Collet c041919f1b [IMP] core: introduce _order_to_sql() to replace _generate_order_by()
The new method should be used to generate an SQL object that represents
how to order by a field in an SQL query.  We introduced the auxiliary
method _order_field_to_sql() so that one can specify some SQL for
ordering by a given field with a simple method override.

Part-of: odoo/odoo#138019
2023-10-20 09:35:18 +00:00
Raphael Collet 0627e9940d [IMP] core: type annotations in SQL wrapper and Query
Part-of: odoo/odoo#138019
2023-10-20 09:35:17 +00:00
Raphael Collet 758780c03b [IMP] core: remove parameter 'extra' from Query.join() and Query.left_join()
Part-of: odoo/odoo#134677
2023-09-27 03:01:45 +00:00
Raphael Collet 66a0cdd637 [IMP] core: make an API to get rid of Query._raw_joins
Part-of: odoo/odoo#134677
2023-09-27 03:01:45 +00:00
Raphael Collet 6a61d2b322 [IMP] core: use SQL wrapper in Query
Part-of: odoo/odoo#134677
2023-09-27 03:01:45 +00:00
std-odoo 15353f06d7 [IMP] base, web: allow to group by properties
Purpose
=======
Allow to group records by their property values,
like we can do with normal fields.

Technical
=========
Because there's no foreign key, when we group by a relational property
we need to check the existence of the ids in the query (and same for
selection and tags, because we might have value of a deleted
option / tag in database).

Task-3032464

Part-of: odoo/odoo#103510
2023-09-08 09:46:56 +00:00
Raphael Collet e962860c6f [IMP] core: introduce search_fetch() and fetch()
This fulfills the goal of searching and fetching fields in a single SQL
query.  We introduce the new method search_fetch() for that purpose.
Also introduce method fetch() to fetch some fields for a recordset if
they are not in cache yet.

The call graph is as follows:

    search()        calls   search_fetch()
    search_read()   calls   search_fetch() and _read_format()
    read()          calls   fetch() and _read_format()

    search_count()  calls   _search()
    search_fetch()  calls   _search() and _fetch_query()
    fetch()         calls   _search() and _fetch_query()

The methods _search() and _fetch_query() are usually the ones to
override to implement business-specific logic.  The method _search()
returns a Query object to retrieve the records that satisfy the given
domain and are accessible for reading.  The method _fetch_query() uses a
Query object to retrieve fields from the database and store them in
cache.

Also use search_fetch() to save one query in search_read() and the
reading of one2many fields.

Part-of: odoo/odoo#112126
2023-03-05 15:12:55 +01:00
Raphael Collet 46c23fd64d [IMP] *: _search() always returns a Query
Goal: make _search() always return a Query object, in order to make
search_read() in a single query when possible

Adapt the overrides of _search() towards the given goal.

Part-of: odoo/odoo#112126
2023-03-05 15:12:55 +01:00
Raphael Collet 136eb34f07 [IMP] core: _search() no longer uses a default order
This simplifies the use of subqueries by avoiding some costly default
order on the model or the idiotic order='id'.  Method _flush_search()
has been adapted accordingly.

Part-of: odoo/odoo#112126
2023-03-05 15:12:54 +01:00
Raphael Collet 7e6cff5479 [IMP] core: search() and _search() no longer have parameter count
The parameter in search() is redundant with method search_count(), and
was making the calls less readable.

The method _search() is aimed at always returning a Query object.  The
method can therefore never return an integer, hence the removal of the
parameter.  This does not actually remove any functionality from the
method; counting result is simply given by using it differently.

Part-of: odoo/odoo#112126
2023-03-05 15:12:54 +01:00
Raphael Collet 789c643925 [IMP] core: improve Query for subqueries
The goal is to be able to use Query objects for both subqueries and
known ids tuples.  This provides a single API for injecting either a
subquery or its resulting ids into another query.

Part-of: odoo/odoo#112126
2023-03-05 15:12:53 +01:00
Raphael Collet 537a60ffd1 [IMP] core: quote field "id" in SQL queries
Part-of: odoo/odoo#112126
2023-03-05 15:12:53 +01:00
Rémy Voet (ryv) 0787f150b1 [FIX] core: make index naming without conflicts
There are two issues with the index naming convention used by the ORM:

Problem 1: it is possible to have naming conflict for indexes.  For
instance, the name 'slide_channel_tag_group_sequence_index' is used for
both fields slide.channel.tag.group.sequence and
slide.channel.tag.group_sequence.  Only the first index will be created.

Solution 1: we separate the model and field names with a double
underscore instead of a single one, which is the same strategy as with
LEFT JOIN aliases.  This is correct because model names don't contain
such double underscores or underscores as prefix or suffix (it is not
forbiden but model name should follow the 'dot notation'.)

Problem 2: index names can be longer than 63 chars, but PostgreSQL
silently truncates it.  This doesn't actually break anything (PostgreSQL
also truncates values when we check the existence of indexes) but it can
lead to using the same name twice.  There is hopefully not any example
in our code.

Solution 2: if the name is too large, we truncate it and pad it with a
hash of the complete name to match 63 characters, which is also the
strategy used for LEFT JOIN aliases.

task-2984730

closes odoo/odoo#100736

Related: odoo/upgrade#3957
Signed-off-by: Raphael Collet <rco@odoo.com>
2022-11-07 15:57:28 +01:00
Raphael Collet d86e582283 [IMP] core: move class Query to odoo.tools
Move the definition of class Query to odoo.tools, in order to avoid
circular imports when importing Query in core Odoo modules.

Also reorganize imports in the impacted modules.

Part-of: odoo/odoo#66938
2022-07-05 11:34:59 +02:00