|
5 | 5 | to SQL for relational databases, but without the support for data definition
|
6 | 6 | operations such as creating or deleting :doc:`databases <database>`,
|
7 | 7 | :doc:`collections <collection>` or :doc:`indexes <indexes>`. For more
|
8 |
| -information, refer to `ArangoDB manual`_. |
| 8 | +information, refer to `ArangoDB Manual`_. |
9 | 9 |
|
10 |
| -.. _ArangoDB manual: https://docs.arangodb.com |
| 10 | +.. _ArangoDB Manual: https://docs.arangodb.com |
| 11 | + |
| 12 | +AQL Queries |
| 13 | +=========== |
| 14 | + |
| 15 | +AQL queries are invoked from AQL wrapper. Executing queries returns |
| 16 | +:doc:`cursors <cursor>`. |
| 17 | + |
| 18 | +**Example:** |
| 19 | + |
| 20 | +.. code-block:: python |
| 21 | +
|
| 22 | + from arangoasync import ArangoClient, AQLQueryKillError |
| 23 | + from arangoasync.auth import Auth |
| 24 | +
|
| 25 | + # Initialize the client for ArangoDB. |
| 26 | + async with ArangoClient(hosts="http://localhost:8529") as client: |
| 27 | + auth = Auth(username="root", password="passwd") |
| 28 | +
|
| 29 | + # Connect to "test" database as root user. |
| 30 | + db = await client.db("test", auth=auth) |
| 31 | +
|
| 32 | + # Get the API wrapper for "students" collection. |
| 33 | + students = db.collection("students") |
| 34 | +
|
| 35 | + # Insert some test documents into "students" collection. |
| 36 | + await students.insert_many([ |
| 37 | + {"_key": "Abby", "age": 22}, |
| 38 | + {"_key": "John", "age": 18}, |
| 39 | + {"_key": "Mary", "age": 21} |
| 40 | + ]) |
| 41 | +
|
| 42 | + # Get the AQL API wrapper. |
| 43 | + aql = db.aql |
| 44 | +
|
| 45 | + # Retrieve the execution plan without running the query. |
| 46 | + plan = await aql.explain("FOR doc IN students RETURN doc") |
| 47 | +
|
| 48 | + # Validate the query without executing it. |
| 49 | + validate = await aql.validate("FOR doc IN students RETURN doc") |
| 50 | +
|
| 51 | + # Execute the query |
| 52 | + cursor = await db.aql.execute( |
| 53 | + "FOR doc IN students FILTER doc.age < @value RETURN doc", |
| 54 | + bind_vars={"value": 19} |
| 55 | + ) |
| 56 | +
|
| 57 | + # Iterate through the result cursor |
| 58 | + student_keys = [] |
| 59 | + async for doc in cursor: |
| 60 | + student_keys.append(doc) |
| 61 | +
|
| 62 | + # List currently running queries. |
| 63 | + queries = await aql.queries() |
| 64 | +
|
| 65 | + # List any slow queries. |
| 66 | + slow_queries = await aql.slow_queries() |
| 67 | +
|
| 68 | + # Clear slow AQL queries if any. |
| 69 | + await aql.clear_slow_queries() |
| 70 | +
|
| 71 | + # Retrieve AQL query tracking properties. |
| 72 | + await aql.tracking() |
| 73 | +
|
| 74 | + # Configure AQL query tracking properties. |
| 75 | + await aql.set_tracking( |
| 76 | + max_slow_queries=10, |
| 77 | + track_bind_vars=True, |
| 78 | + track_slow_queries=True |
| 79 | + ) |
| 80 | +
|
| 81 | + # Kill a running query (this should fail due to invalid ID). |
| 82 | + try: |
| 83 | + await aql.kill("some_query_id") |
| 84 | + except AQLQueryKillError as err: |
| 85 | + assert err.http_code == 404 |
| 86 | +
|
| 87 | +See :class:`arangoasync.aql.AQL` for API specification. |
| 88 | + |
| 89 | + |
| 90 | +AQL User Functions |
| 91 | +================== |
| 92 | + |
| 93 | +**AQL User Functions** are custom functions you define in Javascript to extend |
| 94 | +AQL functionality. They are somewhat similar to SQL procedures. |
| 95 | + |
| 96 | +**Example:** |
| 97 | + |
| 98 | +.. code-block:: python |
| 99 | +
|
| 100 | + from arangoasync import ArangoClient |
| 101 | + from arangoasync.auth import Auth |
| 102 | +
|
| 103 | + # Initialize the client for ArangoDB. |
| 104 | + async with ArangoClient(hosts="http://localhost:8529") as client: |
| 105 | + auth = Auth(username="root", password="passwd") |
| 106 | +
|
| 107 | + # Connect to "test" database as root user. |
| 108 | + db = await client.db("test", auth=auth) |
| 109 | +
|
| 110 | + # Get the AQL API wrapper. |
| 111 | + aql = db.aql |
| 112 | +
|
| 113 | + # Create a new AQL user function. |
| 114 | + await aql.create_function( |
| 115 | + # Grouping by name prefix is supported. |
| 116 | + name="functions::temperature::converter", |
| 117 | + code="function (celsius) { return celsius * 1.8 + 32; }" |
| 118 | + ) |
| 119 | +
|
| 120 | + # List AQL user functions. |
| 121 | + functions = await aql.functions() |
| 122 | +
|
| 123 | + # Delete an existing AQL user function. |
| 124 | + await aql.delete_function("functions::temperature::converter") |
| 125 | +
|
| 126 | +See :class:`arangoasync.aql.AQL` for API specification. |
| 127 | + |
| 128 | + |
| 129 | +AQL Query Cache |
| 130 | +=============== |
| 131 | + |
| 132 | +**AQL Query Cache** is used to minimize redundant calculation of the same query |
| 133 | +results. It is useful when read queries are issued frequently and write queries |
| 134 | +are not. |
| 135 | + |
| 136 | +**Example:** |
| 137 | + |
| 138 | +.. code-block:: python |
| 139 | +
|
| 140 | + from arangoasync import ArangoClient |
| 141 | + from arangoasync.auth import Auth |
| 142 | +
|
| 143 | + # Initialize the client for ArangoDB. |
| 144 | + async with ArangoClient(hosts="http://localhost:8529") as client: |
| 145 | + auth = Auth(username="root", password="passwd") |
| 146 | +
|
| 147 | + # Connect to "test" database as root user. |
| 148 | + db = await client.db("test", auth=auth) |
| 149 | +
|
| 150 | + # Get the AQL API wrapper. |
| 151 | + aql = db.aql |
| 152 | +
|
| 153 | + # Retrieve AQL query cache properties. |
| 154 | + await aql.cache.properties() |
| 155 | +
|
| 156 | + # Configure AQL query cache properties. |
| 157 | + await aql.cache.configure(mode="demand", max_results=10000) |
| 158 | +
|
| 159 | + # List results cache entries. |
| 160 | + entries = await aql.cache.entries() |
| 161 | +
|
| 162 | + # List plan cache entries. |
| 163 | + plan_entries = await aql.cache.plan_entries() |
| 164 | +
|
| 165 | + # Clear results in AQL query cache. |
| 166 | + await aql.cache.clear() |
| 167 | +
|
| 168 | + # Clear results in AQL query plan cache. |
| 169 | + await aql.cache.clear_plan() |
| 170 | +
|
| 171 | +See :class:`arangoasync.aql.AQLQueryCache` for API specification. |
0 commit comments