-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathquery.py
More file actions
211 lines (161 loc) · 7.8 KB
/
query.py
File metadata and controls
211 lines (161 loc) · 7.8 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
"""Query operations namespace for the Dataverse SDK."""
from __future__ import annotations
from typing import Iterable, List, Optional, TYPE_CHECKING
from ..models.record import Record
from ..models.query_builder import QueryBuilder
if TYPE_CHECKING:
from ..client import DataverseClient
__all__ = ["QueryOperations"]
class QueryOperations:
"""Namespace for query operations.
Accessed via ``client.query``. Provides query and search operations
against Dataverse tables.
:param client: The parent :class:`~PowerPlatform.Dataverse.client.DataverseClient` instance.
:type client: ~PowerPlatform.Dataverse.client.DataverseClient
Example::
client = DataverseClient(base_url, credential)
# Fluent query builder (recommended)
for record in (client.query.builder("account")
.select("name", "revenue")
.filter_eq("statecode", 0)
.order_by("revenue", descending=True)
.top(100)
.execute()):
print(record["name"])
# SQL query
rows = client.query.sql("SELECT TOP 10 name FROM account ORDER BY name")
for row in rows:
print(row["name"])
"""
def __init__(self, client: DataverseClient) -> None:
self._client = client
# ----------------------------------------------------------------- builder
def builder(self, table: str) -> QueryBuilder:
"""Create a fluent query builder for the specified table.
Returns a :class:`~PowerPlatform.Dataverse.models.query_builder.QueryBuilder`
that can be chained with filter, select, and order methods, then
executed directly via ``.execute()``.
:param table: Table schema name (e.g. ``"account"``).
:type table: :class:`str`
:return: A QueryBuilder instance bound to this client.
:rtype: ~PowerPlatform.Dataverse.models.query_builder.QueryBuilder
Example:
Build and execute a query fluently::
for record in (client.query.builder("account")
.select("name", "revenue")
.filter_eq("statecode", 0)
.filter_gt("revenue", 1000000)
.order_by("revenue", descending=True)
.top(100)
.page_size(50)
.execute()):
print(record["name"])
With composable expression tree::
from PowerPlatform.Dataverse.models.filters import eq, gt
for record in (client.query.builder("account")
.where((eq("statecode", 0) | eq("statecode", 1))
& gt("revenue", 100000))
.execute()):
print(record["name"])
"""
qb = QueryBuilder(table)
qb._query_ops = self
return qb
# -------------------------------------------------------------------- sql
def sql(self, sql: str) -> List[Record]:
"""Execute a read-only SQL query using the Dataverse Web API.
The SQL query must follow the supported subset: a single SELECT
statement with optional WHERE, TOP (integer literal), ORDER BY (column
names only), and a simple table alias after FROM.
:param sql: Supported SQL SELECT statement.
:type sql: :class:`str`
:return: List of :class:`~PowerPlatform.Dataverse.models.record.Record`
objects. Returns an empty list when no rows match.
:rtype: list[~PowerPlatform.Dataverse.models.record.Record]
:raises ~PowerPlatform.Dataverse.core.errors.ValidationError:
If ``sql`` is not a string or is empty.
Example:
Basic SQL query::
rows = client.query.sql(
"SELECT TOP 10 accountid, name FROM account "
"WHERE name LIKE 'C%' ORDER BY name"
)
for row in rows:
print(row["name"])
Query with alias::
rows = client.query.sql(
"SELECT a.name, a.telephone1 FROM account AS a "
"WHERE a.statecode = 0"
)
"""
with self._client._scoped_odata() as od:
rows = od._query_sql(sql)
return [Record.from_api_response("", row) for row in rows]
# -------------------------------------------------------------------- fetchxml
def fetchxml(
self,
fetchxml: str,
*,
page_size: Optional[int] = None,
) -> Iterable[List[Record]]:
"""Execute a FetchXML query with automatic paging cookie handling.
Executes a FetchXML query against Dataverse via the Web API. The FetchXML
string must contain a root ``<fetch>`` element with a child ``<entity name='...'>``
element. Results are yielded as pages (lists of
:class:`~PowerPlatform.Dataverse.models.record.Record` objects).
:param fetchxml: Raw FetchXML string (e.g. ``<fetch><entity name='account'>...</entity></fetch>``).
:type fetchxml: :class:`str`
:param page_size: Optional page size override. Sets the ``count`` attribute on
``<fetch>`` if not already present.
:type page_size: :class:`int` | ``None``
.. note::
The ``page_size`` parameter is always validated, but only applied (sets the
``count`` attribute) when the FetchXML does not already contain one.
If the FetchXML already has a ``count`` attribute, it takes precedence.
.. note::
Raw FetchXML input is limited to 16,000 characters (security cap before XML
parsing). The final encoded request URL is limited to 32,768 bytes. Paging
cookies can increase URL size on subsequent pages.
:return: Generator yielding pages, where each page is a list of
:class:`~PowerPlatform.Dataverse.models.record.Record` objects.
:rtype: :class:`~typing.Iterable` of :class:`list` of
:class:`~PowerPlatform.Dataverse.models.record.Record`
:raises ~PowerPlatform.Dataverse.core.errors.ValidationError:
If ``fetchxml`` is not a string, is empty, or has invalid structure.
Example:
Basic FetchXML query::
fetchxml = '''
<fetch top='5'>
<entity name='account'>
<attribute name='name' />
</entity>
</fetch>
'''
for page in client.query.fetchxml(fetchxml):
for record in page:
print(record["name"])
Paginated query with page size::
fetchxml = '''
<fetch>
<entity name='contact'>
<attribute name='fullname' />
<filter type='and'>
<condition attribute='statecode' operator='eq' value='0' />
</filter>
</entity>
</fetch>
'''
for page in client.query.fetchxml(fetchxml, page_size=50):
for record in page:
print(record["fullname"])
"""
def _paged() -> Iterable[List[Record]]:
with self._client._scoped_odata() as od:
entity_name = ""
for page in od._query_fetchxml(fetchxml, page_size=page_size):
if not entity_name:
entity_name = od._extract_entity_from_fetchxml(fetchxml)
yield [Record.from_api_response(entity_name, row) for row in page]
return _paged()