Skip to content

Commit b5b5a9e

Browse files
authored
More flexible insert builder (#135)
* Full-text search * wip * wip * Update FTS5.swift * wip * wip * wip * Make inserts more flexible * wip * wip * wip * wip * wip * wip * fix * wip
1 parent eb8c86d commit b5b5a9e

File tree

10 files changed

+274
-291
lines changed

10 files changed

+274
-291
lines changed

Sources/StructuredQueries/Macros.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public macro Ephemeral() =
9191
/// or common table expression.
9292
@attached(
9393
extension,
94-
conformances: QueryRepresentable,
94+
conformances: _Selection,
9595
names: named(Columns),
9696
named(init(decoder:))
9797
)

Sources/StructuredQueriesCore/Internal/Deprecations.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ extension Table {
6565
public static func insert(
6666
or conflictResolution: ConflictResolution? = nil,
6767
_ columns: (TableColumns) -> TableColumns = { $0 },
68-
@InsertValuesBuilder<Self> values: () -> [Self],
68+
@InsertValuesBuilder<Self> values: () -> [[QueryFragment]],
6969
onConflict updates: ((inout Updates<Self>) -> Void)?
7070
) -> InsertOf<Self> {
7171
insert(or: conflictResolution, columns, values: values, onConflictDoUpdate: updates)
@@ -75,8 +75,8 @@ extension Table {
7575
public static func insert<V1, each V2>(
7676
or conflictResolution: ConflictResolution? = nil,
7777
_ columns: (TableColumns) -> (TableColumn<Self, V1>, repeat TableColumn<Self, each V2>),
78-
@InsertValuesBuilder<(V1.QueryOutput, repeat (each V2).QueryOutput)>
79-
values: () -> [(V1.QueryOutput, repeat (each V2).QueryOutput)],
78+
@InsertValuesBuilder<(V1, repeat each V2)>
79+
values: () -> [[QueryFragment]],
8080
onConflict updates: ((inout Updates<Self>) -> Void)?
8181
) -> InsertOf<Self> {
8282
insert(or: conflictResolution, columns, values: values, onConflictDoUpdate: updates)

Sources/StructuredQueriesCore/Seeds.swift

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public struct Seeds: Sequence {
6767
/// [SharingGRDB]: https://github.com/pointfreeco/sharing-grdb
6868
///
6969
/// - Parameter build: A result builder closure that prepares statements to insert every built row.
70-
public init(@InsertValuesBuilder<any Table> _ build: () -> [any Table]) {
70+
public init(@SeedsBuilder _ build: () -> [any Table]) {
7171
self.seeds = build()
7272
}
7373

@@ -103,3 +103,46 @@ public struct Seeds: Sequence {
103103
}
104104
}
105105
}
106+
107+
@resultBuilder
108+
public enum SeedsBuilder {
109+
public static func buildArray(_ components: [[any Table]]) -> [any Table] {
110+
components.flatMap(\.self)
111+
}
112+
113+
public static func buildBlock(_ components: [any Table]) -> [any Table] {
114+
components
115+
}
116+
117+
public static func buildEither(first component: [any Table]) -> [any Table] {
118+
component
119+
}
120+
121+
public static func buildEither(second component: [any Table]) -> [any Table] {
122+
component
123+
}
124+
125+
public static func buildExpression(_ expression: some Table) -> [any Table] {
126+
[expression]
127+
}
128+
129+
public static func buildExpression(_ expression: [any Table]) -> [any Table] {
130+
expression
131+
}
132+
133+
public static func buildLimitedAvailability(_ component: [any Table]) -> [any Table] {
134+
component
135+
}
136+
137+
public static func buildOptional(_ component: [any Table]?) -> [any Table] {
138+
component ?? []
139+
}
140+
141+
public static func buildPartialBlock(first: [any Table]) -> [any Table] {
142+
first
143+
}
144+
145+
public static func buildPartialBlock(accumulated: [any Table], next: [any Table]) -> [any Table] {
146+
accumulated + next
147+
}
148+
}

0 commit comments

Comments
 (0)