Skip to content

Commit

Permalink
feat: select all columns by default (#509)
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev authored Sep 28, 2024
1 parent 8e0a1d4 commit ffb304f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
3 changes: 2 additions & 1 deletion postgrest/_async/request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ def select(
self,
*columns: str,
count: Optional[CountMethod] = None,
head: Optional[bool] = None,
) -> AsyncSelectRequestBuilder[_ReturnT]:
"""Run a SELECT query.
Expand All @@ -291,7 +292,7 @@ def select(
Returns:
:class:`AsyncSelectRequestBuilder`
"""
method, params, headers, json = pre_select(*columns, count=count)
method, params, headers, json = pre_select(*columns, count=count, head=head)
return AsyncSelectRequestBuilder[_ReturnT](
self.session, self.path, method, headers, params, json
)
Expand Down
3 changes: 2 additions & 1 deletion postgrest/_sync/request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ def select(
self,
*columns: str,
count: Optional[CountMethod] = None,
head: Optional[bool] = None,
) -> SyncSelectRequestBuilder[_ReturnT]:
"""Run a SELECT query.
Expand All @@ -291,7 +292,7 @@ def select(
Returns:
:class:`SyncSelectRequestBuilder`
"""
method, params, headers, json = pre_select(*columns, count=count)
method, params, headers, json = pre_select(*columns, count=count, head=head)
return SyncSelectRequestBuilder[_ReturnT](
self.session, self.path, method, headers, params, json
)
Expand Down
25 changes: 19 additions & 6 deletions postgrest/base_request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,29 @@ def _unique_columns(json: List[Dict]):
return columns


def _cleaned_columns(columns: str) -> str:
quoted = False
result = []

for c in columns:
if c.isspace() and not quoted:
continue
if c == '"':
quoted = not quoted
result.append(c)

return ",".join(result)


def pre_select(
*columns: str,
count: Optional[CountMethod] = None,
head: Optional[bool] = None,
) -> QueryArgs:
if columns:
method = RequestMethod.GET
params = QueryParams({"select": ",".join(columns)})
else:
method = RequestMethod.HEAD
params = QueryParams()
method = RequestMethod.HEAD if head else RequestMethod.GET
cleaned_columns = _cleaned_columns(columns or "*")
params = QueryParams({"select": cleaned_columns})

headers = Headers({"Prefer": f"count={count}"}) if count else Headers()
return QueryArgs(method, params, headers, {})

Expand Down
16 changes: 12 additions & 4 deletions tests/_async/test_request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ def test_select(self, request_builder: AsyncRequestBuilder):
def test_select_with_count(self, request_builder: AsyncRequestBuilder):
builder = request_builder.select(count=CountMethod.exact)

assert builder.params.get("select") is None
assert builder.params["select"] == "*"
assert builder.headers["prefer"] == "count=exact"
assert builder.http_method == "GET"
assert builder.json == {}

def test_select_with_head(self, request_builder: AsyncRequestBuilder):
builder = request_builder.select("col1", "col2", head=True)

assert builder.params.get("select") == "col1,col2"
assert builder.headers.get("prefer") is None
assert builder.http_method == "HEAD"
assert builder.json == {}

Expand Down Expand Up @@ -193,15 +201,15 @@ def test_explain_options(self, request_builder: AsyncRequestBuilder):
class TestOrder:
def test_order(self, request_builder: AsyncRequestBuilder):
builder = request_builder.select().order("country_name", desc=True)
assert str(builder.params) == "order=country_name.desc"
assert str(builder.params) == "select=%2A&order=country_name.desc"

def test_multiple_orders(self, request_builder: AsyncRequestBuilder):
builder = (
request_builder.select()
.order("country_name", desc=True)
.order("iso", desc=True)
)
assert str(builder.params) == "order=country_name.desc%2Ciso.desc"
assert str(builder.params) == "select=%2A&order=country_name.desc%2Ciso.desc"

def test_multiple_orders_on_foreign_table(self, request_builder: AsyncRequestBuilder):
foreign_table = "cities"
Expand All @@ -212,7 +220,7 @@ def test_multiple_orders_on_foreign_table(self, request_builder: AsyncRequestBui
)
assert (
str(builder.params)
== "order=cities%28city_name%29.desc%2Ccities%28id%29.desc"
== "select=%2A&order=cities%28city_name%29.desc%2Ccities%28id%29.desc"
)


Expand Down
16 changes: 12 additions & 4 deletions tests/_sync/test_request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ def test_select(self, request_builder: SyncRequestBuilder):
def test_select_with_count(self, request_builder: SyncRequestBuilder):
builder = request_builder.select(count=CountMethod.exact)

assert builder.params.get("select") is None
assert builder.params["select"] == "*"
assert builder.headers["prefer"] == "count=exact"
assert builder.http_method == "GET"
assert builder.json == {}

def test_select_with_head(self, request_builder: SyncRequestBuilder):
builder = request_builder.select("col1", "col2", head=True)

assert builder.params.get("select") == "col1,col2"
assert builder.headers.get("prefer") is None
assert builder.http_method == "HEAD"
assert builder.json == {}

Expand Down Expand Up @@ -193,15 +201,15 @@ def test_explain_options(self, request_builder: SyncRequestBuilder):
class TestOrder:
def test_order(self, request_builder: SyncRequestBuilder):
builder = request_builder.select().order("country_name", desc=True)
assert str(builder.params) == "order=country_name.desc"
assert str(builder.params) == "select=%2A&order=country_name.desc"

def test_multiple_orders(self, request_builder: SyncRequestBuilder):
builder = (
request_builder.select()
.order("country_name", desc=True)
.order("iso", desc=True)
)
assert str(builder.params) == "order=country_name.desc%2Ciso.desc"
assert str(builder.params) == "select=%2A&order=country_name.desc%2Ciso.desc"

def test_multiple_orders_on_foreign_table(self, request_builder: SyncRequestBuilder):
foreign_table = "cities"
Expand All @@ -212,7 +220,7 @@ def test_multiple_orders_on_foreign_table(self, request_builder: SyncRequestBuil
)
assert (
str(builder.params)
== "order=cities%28city_name%29.desc%2Ccities%28id%29.desc"
== "select=%2A&order=cities%28city_name%29.desc%2Ccities%28id%29.desc"
)


Expand Down

0 comments on commit ffb304f

Please sign in to comment.