Skip to content

Commit 457244e

Browse files
committed
[IMP] core: new SQL alias builder
Update the documentation with more recent examples of usage of the Query object.
1 parent bedf2f7 commit 457244e

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

content/developer/reference/backend/orm.rst

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,9 +749,33 @@ The recommended way to build SQL queries is to use the wrapper object
749749

750750
.. autoclass:: odoo.tools.SQL
751751

752-
.. automethod:: SQL.join
753752
.. automethod:: SQL.identifier
754753

754+
The :method:`~odoo.api.Environment.execute_query` allows to flush (see later),
755+
execute and fetch results at once.
756+
To translate model fields into SQL, and build queries, a useful abstraction
757+
is provided in
758+
759+
.. autoclass:: odoo.models.Query
760+
761+
.. automethod:: Query.select
762+
.. automethod:: Query.subselect
763+
.. automethod:: Query.is_empty
764+
.. automethod:: Query.get_result_ids
765+
.. autoproperty:: Query.table
766+
767+
.. example::
768+
769+
.. code-block:: python
770+
771+
query = Query(self.env['model'])
772+
query.add_where(SQL("%s > %s", query.table.some_field, 'test'))
773+
query.select()
774+
# SELECT model.id
775+
# FROM model
776+
# WHERE model.some_field > 'test'
777+
778+
755779
One important thing to know about models is that they don't necessarily perform
756780
database updates right away. Indeed, for performance reasons, the framework
757781
delays the recomputation of fields after modifying records. And some database
@@ -781,6 +805,11 @@ when flushing.
781805

782806
.. automethod:: Model.flush_recordset
783807

808+
.. automethod:: odoo.api.Environment.execute_query
809+
810+
When using the Query builder and ``execute_query``, model flushes are done based
811+
on the metadata in the ``SQL``.
812+
784813
Because models use the same cursor and the :class:`~odoo.api.Environment`
785814
holds various caches, these caches must be invalidated when *altering* the
786815
database in raw SQL, or further uses of models may become incoherent. It is
@@ -798,6 +827,13 @@ SQL, but not ``SELECT`` (which simply reads the database).
798827
799828
# invalidate 'state' from the cache
800829
self.env['model'].invalidate_model(['state'])
830+
831+
.. code-block:: python
832+
833+
# get name and country name for all records, flushing and joins are
834+
# done automatically
835+
query = Query(self.env['model'])
836+
self.env.execute_query(query.select(SQL(query.table.name, query.table.country_id.name)))
801837
802838
Just like flushing, one can invalidate either the whole cache, the cache of all
803839
the records of a model, or the cache of specific records. One can even

content/developer/reference/backend/orm/changelog.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
Changelog
55
=========
66

7+
Odoo Online version 19.1
8+
========================
9+
10+
- New API to build SQL.
11+
See `#234156 <https://github.com/odoo/odoo/pull/234156>`_.
12+
713
Odoo version 19.0
814
=================
915

0 commit comments

Comments
 (0)