QueryOperations 類別
用於查詢操作的命名空間。
透過 client.query 存取。 提供對 Dataverse 資料表的查詢與搜尋操作。
範例:
from PowerPlatform.Dataverse.models.filters import col
client = DataverseClient(base_url, credential)
# Fluent query builder (recommended)
for record in (client.query.builder("account")
.select("name", "revenue")
.where(col("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"])
方法
| builder |
為指定的表格建立一個流暢的查詢建構器。 回傳 a QueryBuilder ,可透過過濾、選擇及排序方法串聯,然後直接透過 |
| fetchxml |
退回一個惰性 FetchXmlQuery 物體。 在回傳物件之前executeexecute_pages,不會有 HTTP 請求。 用於 SQL-JOIN 情境、彙總查詢或其他 OData 建構端點無法表達的操作。 範例:
|
| odata_bind |
建立 自動從元資料中發現導航屬性名稱與實體集合名稱。 回傳一個單一項目的指令,可合併成建立或更新的有效載荷。 範例:
|
| odata_expand |
將導航屬性名稱 透過關係元資料發現。 回傳參數的 範例:
|
| odata_expands |
從表格中發現所有 回傳每次出線查詢的條目(單值導航屬性)。 每個項目包含 和 範例:
|
| odata_select |
回傳一份適用於 可以直接傳遞到 範例:
|
| sql |
使用 Dataverse Web API 執行唯讀 SQL 查詢。 Dataverse SQL 端點支援廣泛的 T-SQL 子集:
不支援:SELECT >>*<<、子查詢、CTE、HAVING、UNION、右/滿/交叉連接、case、coalesce、視窗函式、字串/日期/數學函式、INSERT/UPDATE/DELETE。 寫入時,使用 |
| sql_columns |
回傳一份簡化的 SQL 可用欄位清單,用於資料表。 每個字典包含 範例:
|
builder
為指定的表格建立一個流暢的查詢建構器。
回傳 a QueryBuilder ,可透過過濾、選擇及排序方法串聯,然後直接透過 .execute()執行。
builder(table: str) -> QueryBuilder
參數
| 名稱 | Description |
|---|---|
|
table
必要
|
資料表結構名稱(例如 |
傳回
| 類型 | Description |
|---|---|
|
一個綁定到這個客戶端的 QueryBuilder 實例。 |
範例
流暢地建立並執行查詢:
from PowerPlatform.Dataverse.models.filters import col
for record in (client.query.builder("account")
.select("name", "revenue")
.where(col("statecode") == 0)
.where(col("revenue") > 1_000_000)
.order_by("revenue", descending=True)
.top(100)
.page_size(50)
.execute()):
print(record["name"])
使用可組合表達式樹:
from PowerPlatform.Dataverse.models.filters import col
for record in (client.query.builder("account")
.where((col("statecode") == 0) | (col("statecode") == 1))
.where(col("revenue") > 100_000)
.execute()):
print(record["name"])
fetchxml
退回一個惰性 FetchXmlQuery 物體。
在回傳物件之前executeexecute_pages,不會有 HTTP 請求。
用於 SQL-JOIN 情境、彙總查詢或其他 OData 建構端點無法表達的操作。
範例:
query = client.query.fetchxml("""
<fetch top="50">
<entity name="account">
<attribute name="name" />
<link-entity name="contact" from="parentcustomerid"
to="accountid" alias="c" link-type="inner">
<attribute name="fullname" />
</link-entity>
</entity>
</fetch>
""")
# Eager — collect all pages:
result = query.execute()
df = result.to_dataframe()
# Lazy — process one page at a time:
for page in query.execute_pages():
process(page.to_dataframe())
fetchxml(xml: str) -> FetchXmlQuery
參數
| 名稱 | Description |
|---|---|
|
xml
必要
|
格式良好的 FetchXML 查詢字串。 根 |
傳回
| 類型 | Description |
|---|---|
|
帶有 |
例外狀況
| 類型 | Description |
|---|---|
|
如果 FetchXML 缺少根 |
odata_bind
建立 @odata.bind 一個欄位來設定查找欄位。
自動從元資料中發現導航屬性名稱與實體集合名稱。 回傳一個單一項目的指令,可合併成建立或更新的有效載荷。
範例:
# Instead of manually constructing:
# {"parentcustomerid_account@odata.bind": "/accounts(guid)"}
# Just do:
bind = client.query.odata_bind("contact", "account", acct_id)
client.records.create("contact", {
"firstname": "Jane",
"lastname": "Doe",
**bind,
})
odata_bind(from_table: str, to_table: str, target_id: str) -> Dict[str, str]
參數
| 名稱 | Description |
|---|---|
|
from_table
必要
|
正在建立/更新實體的結構名稱。 |
|
to_table
必要
|
查詢所指向的目標實體的結構名稱。 |
|
target_id
必要
|
目標紀錄的 GUID。 |
傳回
| 類型 | Description |
|---|---|
|
像 |
例外狀況
| 類型 | Description |
|---|---|
|
如果表格間找不到關聯。 |
odata_expand
將導航屬性名稱 $expand 從一個資料表回傳到另一個資料表。
透過關係元資料發現。 回傳參數的 expand= 精確 PascalCase 字串。
範例:
nav = client.query.odata_expand("contact", "account")
# Returns e.g. "parentcustomerid_account"
for page in client.records.get("contact",
select=["fullname"],
expand=[nav],
top=5):
for r in page:
acct = r.get(nav) or {}
print(f"{r['fullname']} -> {acct.get('name', 'N/A')}")
odata_expand(from_table: str, to_table: str) -> str
參數
| 名稱 | Description |
|---|---|
|
from_table
必要
|
來源資料表的結構名稱(例如 |
|
to_table
必要
|
目標資料表的結構名稱(例如 |
傳回
| 類型 | Description |
|---|---|
|
導航屬性名稱(PascalCase)。 |
例外狀況
| 類型 | Description |
|---|---|
|
若未找到目標的導航屬性。 |
odata_expands
從表格中發現所有 $expand 導航屬性。
回傳每次出線查詢的條目(單值導航屬性)。 每個項目包含 和 @odata.bind所需的$expand精確 PascalCase 導航屬性名稱,以及目標實體集合名稱。
範例:
expands = client.query.odata_expands("contact")
for e in expands:
print(f"expand={e['nav_property']} -> {e['target_table']}")
# Use in a query
e = next(e for e in expands if e['target_table'] == 'account')
for page in client.records.get("contact",
select=["fullname"],
expand=[e['nav_property']]):
...
odata_expands(table: str) -> List[Dict[str, Any]]
參數
| 名稱 | Description |
|---|---|
|
table
必要
|
表格的結構名稱(例如 |
傳回
| 類型 | Description |
|---|---|
|
指令列表,每個包含:
|
odata_select
回傳一份適用於 $select的欄位邏輯名稱清單。
可以直接傳遞到 client.records.get(table, select=...)。
範例:
cols = client.query.odata_select("account")
for page in client.records.get("account", select=cols, top=10):
for r in page:
print(r)
odata_select(table: str, *, include_system: bool = False) -> List[str]
參數
| 名稱 | Description |
|---|---|
|
table
必要
|
表格的結構名稱(例如 |
|
include_system
必要
|
包含系統欄位(預設 |
僅限關鍵字的參數
| 名稱 | Description |
|---|---|
|
include_system
|
預設值: False
|
傳回
| 類型 | Description |
|---|---|
|
小寫欄位邏輯名稱列表。 |
sql
使用 Dataverse Web API 執行唯讀 SQL 查詢。
Dataverse SQL 端點支援廣泛的 T-SQL 子集:
SELECT / SELECT DISTINCT / SELECT TOP N (0-5000)
FROM table [alias]
INNER JOIN / LEFT JOIN (multi-table, no depth limit)
WHERE (=, !=, >, <, >=, <=, LIKE, IN, NOT IN, IS NULL,
IS NOT NULL, BETWEEN, AND, OR, nested parentheses)
GROUP BY column
ORDER BY column [ASC|DESC]
OFFSET n ROWS FETCH NEXT m ROWS ONLY
COUNT(*), SUM(), AVG(), MIN(), MAX()
SELECT * 不支援 — 請明確指定欄位名稱。
用 sql_columns 來發現資料表可用的欄位名稱。
不支援:SELECT >>*<<、子查詢、CTE、HAVING、UNION、右/滿/交叉連接、case、coalesce、視窗函式、字串/日期/數學函式、INSERT/UPDATE/DELETE。 寫入時,使用 client.records 方法。
sql(sql: str) -> List[Record]
參數
| 名稱 | Description |
|---|---|
|
sql
必要
|
支援 SQL SELECT 陳述式。 |
傳回
| 類型 | Description |
|---|---|
|
物品清單 Record 。 當沒有資料列相符時,會回傳一個空清單。 |
例外狀況
| 類型 | Description |
|---|---|
|
如果 |
範例
基本查詢:
rows = client.query.sql(
"SELECT TOP 10 name FROM account ORDER BY name"
)
加入並彙整:
rows = client.query.sql(
"SELECT a.name, COUNT(c.contactid) as cnt "
"FROM account a "
"JOIN contact c ON a.accountid = c.parentcustomerid "
"GROUP BY a.name"
)
sql_columns
回傳一份簡化的 SQL 可用欄位清單,用於資料表。
每個字典包含 name (SQL 的邏輯名稱)、 type (Dataverse 屬性類型)、 is_pk (主鍵標誌)和 label (顯示名稱)。 虛擬欄位總是被排除,因為 SQL 端點無法查詢它們。
範例:
cols = client.query.sql_columns("account")
for c in cols:
print(f"{c['name']:30s} {c['type']:20s} PK={c['is_pk']}")
sql_columns(table: str, *, include_system: bool = False) -> List[Dict[str, Any]]
參數
| 名稱 | Description |
|---|---|
|
table
必要
|
表格的結構名稱(例如 |
|
include_system
必要
|
當(預設)時 |
僅限關鍵字的參數
| 名稱 | Description |
|---|---|
|
include_system
|
預設值: False
|
傳回
| 類型 | Description |
|---|---|
|
欄位元資料指令列表。 |