Commit 5a395ec
Add QueryBuilder with fluent API and composable filter expressions (microsoft#118)
## Summary
Implements the QueryBuilder feature from the SDK redesign design doc
(ADO PR 1504429):
- **Fluent query builder** via `client.query.builder("table")` with 20
chainable methods including `select`, `filter_eq/ne/gt/ge/lt/le`,
`filter_contains/startswith/endswith`, `filter_in`, `filter_between`,
`filter_null/not_null`, `filter_raw`, `where`, `order_by`, `top`,
`page_size`, `expand`, and `execute`
- **Composable filter expression tree** (`models/filters.py`) with
Python operator overloads (`&`, `|`, `~`) for AND, OR, NOT composition
- **Value auto-formatting** for `str`, `int`, `float`, `bool`, `None`,
`datetime`, `date`, `uuid.UUID`
- 126 new unit tests (57 filters + 69 query builder), 309 total passing
### Usage examples
```python
# Fluent builder
for page 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()):
for record in page:
print(record["name"])
# Composable expression tree with where()
from PowerPlatform.Dataverse.models.filters import eq, gt, filter_in
for page in (client.query.builder("account")
.where((eq("statecode", 0) | eq("statecode", 1))
& gt("revenue", 100000))
.execute()):
for record in page:
print(record["name"])
```
### Design decisions
- **Regular class, not dataclass** — prevents leaking internal state as
constructor params
- **Unified `_filter_parts` list** — preserves call order when mixing
`filter_*()` and `where()`
- **`execute()` calls `build()` internally** — single source of truth
for filter compilation
- **No public `get()` on QueryOperations** — only `builder()` added;
paginated queries remain on `records.get()`
- **Parenthesized `filter_between`** — `(col ge low and col le high)`
for correct precedence
### Files changed
| File | Description |
|------|-------------|
| `src/.../models/filters.py` | **NEW** — Composable expression tree |
| `src/.../models/query_builder.py` | **NEW** — Fluent QueryBuilder
class |
| `src/.../operations/query.py` | Add `builder()` to QueryOperations |
| `src/.../models/__init__.py` | Updated docstring |
| `tests/.../models/test_filters.py` | **NEW** — 57 filter tests |
| `tests/.../models/test_query_builder.py` | **NEW** — 69 builder tests
|
| `tests/.../test_query_operations.py` | 6 new integration tests |
### Merge conflict note
`operations/query.py` may conflict with PR microsoft#115 (typed return models) —
resolution is straightforward since we only add a `builder()` method.
## Test plan
- [x] `pytest tests/unit/models/test_filters.py` — 57 passed
- [x] `pytest tests/unit/models/test_query_builder.py` — 69 passed
- [x] `pytest tests/unit/test_query_operations.py` — 9 passed
- [x] `pytest tests/` — 309 passed, 0 failed
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: tpellissier <tpellissier@microsoft.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Abel Milash <abelmilash@microsoft.com>
Co-authored-by: Saurabh Badenkal <sbadenkal@microsoft.com>
Co-authored-by: Saurabh Ravindra Badenkal <32964911+saurabhrb@users.noreply.github.com>1 parent 9788cbb commit 5a395ec
File tree
17 files changed
+3388
-39
lines changed- examples/advanced
- src/PowerPlatform/Dataverse
- data
- models
- operations
- tests/unit
- models
17 files changed
+3388
-39
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
| 40 | + | |
| 41 | + | |
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
| |||
116 | 117 | | |
117 | 118 | | |
118 | 119 | | |
119 | | - | |
| 120 | + | |
120 | 121 | | |
121 | 122 | | |
122 | 123 | | |
| |||
272 | 273 | | |
273 | 274 | | |
274 | 275 | | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
275 | 377 | | |
276 | | - | |
277 | 378 | | |
278 | 379 | | |
279 | 380 | | |
280 | 381 | | |
281 | 382 | | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
282 | 386 | | |
283 | | - | |
284 | | - | |
| 387 | + | |
285 | 388 | | |
286 | 389 | | |
287 | | - | |
288 | | - | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
289 | 393 | | |
290 | 394 | | |
291 | 395 | | |
292 | 396 | | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | 397 | | |
305 | 398 | | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | 399 | | |
312 | 400 | | |
313 | 401 | | |
| |||
0 commit comments