Skip to content

Commit 5be79ec

Browse files
authored
feat explain (#241)
1 parent b319f64 commit 5be79ec

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

postgrest/base_request_builder.py

+19
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,25 @@ def __init__(
402402
) -> None:
403403
BaseFilterRequestBuilder.__init__(self, session, headers, params)
404404

405+
def explain(
406+
self: _FilterT,
407+
analyze: bool = False,
408+
verbose: bool = False,
409+
settings: bool = False,
410+
buffers: bool = False,
411+
wal: bool = False,
412+
format: str = "",
413+
) -> _FilterT:
414+
options = [
415+
key
416+
for key, value in locals().items()
417+
if key not in ["self", "format"] and value
418+
]
419+
options_str = "|".join(options)
420+
options_str = f'options="{options_str};"' if options_str else ""
421+
self.headers["Accept"] = f"application/vnd.pgrst.plan+{format}; {options_str}"
422+
return self
423+
405424
def order(
406425
self: _FilterT,
407426
column: str,

tests/_async/test_request_builder.py

+17
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,23 @@ def test_text_search(self, request_builder: AsyncRequestBuilder):
129129
)
130130

131131

132+
class TestExplain:
133+
def test_explain_plain(self, request_builder: AsyncRequestBuilder):
134+
builder = request_builder.select("*").explain()
135+
assert builder.params["select"] == "*"
136+
assert "application/vnd.pgrst.plan+" in str(builder.headers.get("accept"))
137+
138+
def test_explain_options(self, request_builder: AsyncRequestBuilder):
139+
builder = request_builder.select("*").explain(
140+
format="json", analyze=True, verbose=True, buffers=True, wal=True
141+
)
142+
assert builder.params["select"] == "*"
143+
assert "application/vnd.pgrst.plan+json" in str(builder.headers.get("accept"))
144+
assert 'options="analyze|verbose|buffers|wal;' in str(
145+
builder.headers.get("accept")
146+
)
147+
148+
132149
@pytest.fixture
133150
def api_response_with_error() -> Dict[str, Any]:
134151
return {

0 commit comments

Comments
 (0)