File: db_model_query.rst

package info (click to toggle)
python-neutron-lib 3.21.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 7,660 kB
  • sloc: python: 22,829; sh: 137; makefile: 24
file content (66 lines) | stat: -rw-r--r-- 2,353 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
..
      Licensed under the Apache License, Version 2.0 (the "License"); you may
      not use this file except in compliance with the License. You may obtain
      a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
      WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
      License for the specific language governing permissions and limitations
      under the License.


      Convention for heading levels in Neutron devref:
      =======  Heading 0 (reserved for the title in a document)
      -------  Heading 1
      ~~~~~~~  Heading 2
      +++++++  Heading 3
      '''''''  Heading 4
      (Avoid deeper levels because they do not render well.)


DB Model Query
==============

The implementation in ``neutron_lib.db.model_query`` is intended to be used as a
stepping stone for existing consumers using standard database models/tables.
Moving forward new database implementations should all use neutron's Versioned
Object approach, while existing model based implementations should begin
migrating to Versioned Objects.


Registering Hooks
-----------------

The ``neutron_lib.db.model_query.register_hook`` function allows hooks to be
registered for invocation during a respective database query.

Each hook has three components:

- "query": used to build the query expression
- "filter": used to build the filter expression
- "result_filters": used for final filtering on the query result

Query hooks take as input the query being built and return a transformed
query expression. For example::

    def mymodel_query_hook(context, original_model, query):
        augmented_query = ...
        return augmented_query

Filter hooks take as input the filter expression being built and return
a transformed filter expression. For example::

    def mymodel_filter_hook(context, original_model, filters):
        refined_filters = ...
        return refined_filters

Result filter hooks take as input the query expression and the filter
expression, and return a final transformed query expression. For example::

    def mymodel_result_filter_hook(query, filters):
        final_filters = ...
        return query.filter(final_filters)