File: query.rst

package info (click to toggle)
librouteros 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 336 kB
  • sloc: python: 1,110; makefile: 127; sh: 8
file content (65 lines) | stat: -rw-r--r-- 1,741 bytes parent folder | download
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
Query
=====

Basic usage
-----------
Get only ``name`` and ``disabled`` keys from all interfaces.

.. code-block:: python

   from librouteros.query import Key
   # Each key must be created first in order to reference it later.
   name = Key('name')
   disabled = Key('disabled')

   for row in api.path('/interface').select(name, disabled):
      print(row)

Advanced Usage
--------------
Adding ``where()``, allows to fine tune serch criteria.
Syntax is very simmilar to a SQL query.

.. code-block:: python

   name = Key('name')
   disabled = Key('disabled')
   query = api.path('/interface').select(name, disabled).where(
           disabled == False,
           Or(
               name == 'ether2',
               name == 'wlan-lan',
               ),
           )

Above code demonstrates how to select ``name``, ``disabled`` fields where each interface is disabled
and ``name`` is equal to one of ``ether2``, ``wlan-lan``.
If you do not specify any logical operation within `where()`, them it defaults to `And()`.
Above example can be rewritten using `In` operator.

.. code-block:: python

   name = Key('name')
   disabled = Key('disabled')
   query = api.path('/interface').select(name, disabled).where(
           disabled == False,
           name.In('ether2', 'wlan-lan'),
           )

Usable operators
----------------
======== =========
operator example
======== =========
``==``   ``name == 'ether2'``
``!=``   ``name != 'ether2'``
``>``    ``mtu > 1500``
``<``    ``mtu < 1400``
``In``   ``name.In('ether1', 'ether2', 'wlan1')``
======== =========


Logical operators
-----------------
``And``, ``Or``. Ecah operator takes at least two expressions and performs a logical operation translating it to API
query equivalents.