Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 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
16 changes: 16 additions & 0 deletions Sources/StructuredQueriesCore/AggregateFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,22 @@ public struct AggregateFunction<QueryValue>: QueryExpression, Sendable {
var order: QueryFragment?
var filter: QueryFragment?

public init<each Argument: QueryExpression>(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this public, should we rename AggregateFunction to AggregateFunctionExpression?

Should we also rename QueryFunction to FunctionExpression (or ScalarFunctionExpression) and publicize its initializer?

_ name: String,
distinct isDistinct: Bool = false,
_ arguments: repeat each Argument,
order: (some QueryExpression)? = Bool?.none,
filter: (some QueryExpression<Bool>)? = Bool?.none
) {
self.init(
QueryFragment(quote: name),
isDistinct: false,
Array(repeat each arguments),
order: order?.queryFragment,
filter: filter?.queryFragment
)
}

package init(
_ name: QueryFragment,
isDistinct: Bool = false,
Expand Down
22 changes: 22 additions & 0 deletions Sources/StructuredQueriesSQLite/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,25 @@ public macro DatabaseFunction<each T: QueryRepresentable & QueryExpression>(
module: "StructuredQueriesSQLiteMacros",
type: "DatabaseFunctionMacro"
)

@attached(peer, names: overloaded, prefixed(`$`))
public macro DatabaseFunction<each T: QueryRepresentable & QueryExpression, R: QueryBindable>(
_ name: String = "",
as representableFunctionType: ((any Sequence<(repeat each T)>) -> R).Type,
isDeterministic: Bool = false
) =
#externalMacro(
module: "StructuredQueriesSQLiteMacros",
type: "DatabaseFunctionMacro"
)

@attached(peer, names: overloaded, prefixed(`$`))
public macro DatabaseFunction<each T: QueryRepresentable & QueryExpression>(
_ name: String = "",
as representableFunctionType: ((any Sequence<(repeat each T)>) -> Void).Type,
isDeterministic: Bool = false
) =
#externalMacro(
module: "StructuredQueriesSQLiteMacros",
type: "DatabaseFunctionMacro"
)
44 changes: 44 additions & 0 deletions Sources/StructuredQueriesSQLiteCore/DatabaseFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,47 @@ extension ScalarDatabaseFunction {
}
}
}

/// A type representing an aggregate database function.
///
/// Don't conform to this protocol directly. Instead, use the `@DatabaseFunction` macro to generate
/// a conformance.
public protocol AggregateDatabaseFunction<Input, Output>: DatabaseFunction {
/// A type representing one row of input to the aggregate function.
associatedtype Element = Input

/// Decodes a row into an element to aggregate a result from.
///
/// - Parameter decoder: A query decoder.
/// - Returns: An element to append to the sequence sent to the aggregate function.
func step(_ decoder: inout some QueryDecoder) throws -> Element

/// Aggregates elements into a bindable value.
///
/// - Parameter arguments: A sequence of elements to aggregate from.
/// - Returns: A binding returned from the aggregate function.
func invoke(_ arguments: some Sequence<Element>) throws -> QueryBinding
}

extension AggregateDatabaseFunction {
/// A function call expression.
///
/// - Parameter input: Expressions representing the arguments of the function.
/// - Returns: An expression representing the function call.
@_disfavoredOverload
public func callAsFunction<each T: QueryExpression>(
_ input: repeat each T,
order: (some QueryExpression)? = Bool?.none,
filter: (some QueryExpression<Bool>)? = Bool?.none
) -> some QueryExpression<Output>
where Input == (repeat (each T).QueryValue) {
$_isSelecting.withValue(false) {
AggregateFunction(
QueryFragment(quote: name),
Array(repeat each input),
order: order?.queryFragment,
filter: filter?.queryFragment
)
}
}
}
Loading