File: query.rst

package info (click to toggle)
librouteros 3.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 376 kB
  • sloc: python: 1,579; makefile: 141; sh: 4
file content (76 lines) | stat: -rw-r--r-- 1,982 bytes parent folder | download | duplicates (2)
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
67
68
69
70
71
72
73
74
75
76
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 search criteria.
Syntax is very similar 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'),
           )

Select all keys/fields.

.. code-block:: python

   name = Key('name')
   disabled = Key('disabled')
   query = api.path('/interface').select().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``. Each operator takes at least two expressions and performs a logical operation translating it to API
query equivalents.