Skip to content

Added select with foreign table #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions Sources/PostgREST/PostgrestQueryBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,67 @@
public final class PostgrestQueryBuilder: PostgrestBuilder {
/// Performs a vertical filtering with SELECT.
/// - Parameters:
/// - columns: The columns to retrieve, separated by commas.
/// - head: When set to true, select will void data.
/// - count: Count algorithm to use to count rows in a table.
/// - foreignTable: The foreign table or view name to query.
/// - foreignTableColum: The columns associated with to retrieve, separated by commas
public func select(
columns: String = "*",
foreignTable: String? = nil,
foreignTableColum: String = "*",
head: Bool = false,
count: CountOption? = nil
) -> PostgrestFilterBuilder {
method = "GET"
// remove whitespaces except when quoted.
var quoted = false
var selectQueryValue = ""
let cleanedColumns = columns.compactMap { char -> String? in
if char.isWhitespace, !quoted {
return nil
}
if char == "\"" {
quoted = !quoted
}
return String(char)
}
.joined(separator: "")
selectQueryValue += cleanedColumns
if let foreignTable = foreignTable {
let cleanedForeignTable = foreignTable.compactMap { char -> String? in
if char.isWhitespace, !quoted {
return nil
}
if char == "\"" {
quoted = !quoted
}
return String(char)
}
.joined(separator: "")

let cleanedForeignTableColum = foreignTableColum.compactMap { char -> String? in
if char.isWhitespace, !quoted {
return nil
}
if char == "\"" {
quoted = !quoted
}
return String(char)
}
.joined(separator: "")
selectQueryValue += ",\(cleanedForeignTable)(\(cleanedForeignTableColum))"
}
appendSearchParams(name: "select", value: selectQueryValue)
if let count = count {
headers["Prefer"] = "count=\(count.rawValue)"
}
if head {
method = "HEAD"
}
return PostgrestFilterBuilder(self)
}

/// Performs a vertical filtering with SELECT.
/// - Parameters:
/// - columns: The columns to retrieve, separated by commas.
Expand Down
5 changes: 5 additions & 0 deletions Tests/PostgRESTTests/BuildURLRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@
.select()
.gt(column: "received_at", value: "2023-03-23T15:50:30.511743+00:00")
.order(column: "received_at")
},
TestCase(name: "query with foreign table") { client in
client.from("user")
.select(foreignTable: "tasks")
.eq(column: "id", value: "Cigányka-ér (0+400 cskm) vízrajzi állomás")
}
]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
curl \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
"https://example.supabase.co/user?id=eq.Cig%C3%A1nyka-%C3%A9r%20(0+400%20cskm)%20v%C3%ADzrajzi%20%C3%A1llom%C3%A1s&select=tasks%5C(*%5C)"