Skip to content
Merged
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
20 changes: 20 additions & 0 deletions Sources/SQLiteData/FetchAll.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ public struct FetchAll<Element: Sendable>: Sendable {
}

extension FetchAll {
@available(*, deprecated, message: "Remove unused parameters: 'database', 'scheduler'.")
public init(
wrappedValue: [Element] = [],
database: (any DatabaseReader)? = nil,
scheduler: some ValueObservationScheduler & Hashable
)
where Element: StructuredQueriesCore._Selection, Element.QueryOutput == Element {
sharedReader = SharedReader(value: wrappedValue)
}

/// Initializes this property with a query that fetches every row from a table.
///
/// - Parameters:
Expand Down Expand Up @@ -393,6 +403,16 @@ extension FetchAll: Equatable where Element: Equatable {
sharedReader.update()
}

@available(*, deprecated, message: "Remove unused parameters: 'database', 'animation'.")
public init(
wrappedValue: [Element] = [],
database: (any DatabaseReader)? = nil,
animation: Animation
)
where Element: StructuredQueriesCore._Selection, Element.QueryOutput == Element {
sharedReader = SharedReader(value: wrappedValue)
}

/// Initializes this property with a query that fetches every row from a table.
///
/// - Parameters:
Expand Down
77 changes: 65 additions & 12 deletions Sources/SQLiteData/FetchOne.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ public struct FetchOne<Value: Sendable>: Sendable {
sharedReader = SharedReader(value: wrappedValue)
}

/// Initializes this property with a wrapped value.
///
/// - Parameter wrappedValue: A default value to associate with this property.
public init(wrappedValue: sending Value)
where
Value: _Selection,
Value.QueryOutput == Value
{
sharedReader = SharedReader(value: wrappedValue)
}

/// Initializes this property with a query that fetches the first row from a table.
///
/// - Parameters:
Expand Down Expand Up @@ -121,18 +132,6 @@ public struct FetchOne<Value: Sendable>: Sendable {
)
}

/// Initializes this property with a wrapped value.
///
/// - Parameter wrappedValue: A default value to associate with this property.
public init(wrappedValue: sending Value)
where
Value: _OptionalProtocol,
Value: _Selection,
Value.QueryOutput == Value
{
sharedReader = SharedReader(value: wrappedValue)
}

/// Initializes this property with a query associated with the wrapped value.
///
/// - Parameters:
Expand Down Expand Up @@ -430,6 +429,33 @@ public struct FetchOne<Value: Sendable>: Sendable {
}

extension FetchOne {
@available(*, deprecated, message: "Remove unused parameters: 'database', 'scheduler'.")
public init(
wrappedValue: sending Value,
database: (any DatabaseReader)? = nil,
scheduler: some ValueObservationScheduler & Hashable
)
where
Value: _Selection,
Value.QueryOutput == Value
{
sharedReader = SharedReader(value: wrappedValue)
}

@available(*, deprecated, message: "Remove unused parameters: 'database', 'scheduler'.")
public init(
wrappedValue: sending Value = Value._none,
database: (any DatabaseReader)? = nil,
scheduler: some ValueObservationScheduler & Hashable
)
where
Value: _OptionalProtocol,
Value: _Selection,
Value.QueryOutput == Value
{
sharedReader = SharedReader(value: wrappedValue)
}

/// Initializes this property with a query that fetches the first row from a table.
///
/// - Parameters:
Expand Down Expand Up @@ -880,6 +906,33 @@ extension FetchOne: Equatable where Value: Equatable {
sharedReader.update()
}

@available(*, deprecated, message: "Remove unused parameters: 'database', 'animation'.")
public init(
wrappedValue: sending Value,
database: (any DatabaseReader)? = nil,
animation: Animation
)
where
Value: _Selection,
Value.QueryOutput == Value
{
sharedReader = SharedReader(value: wrappedValue)
}

@available(*, deprecated, message: "Remove unused parameters: 'database', 'animation'.")
public init(
wrappedValue: sending Value = Value._none,
database: (any DatabaseReader)? = nil,
animation: Animation
)
where
Value: _OptionalProtocol,
Value: _Selection,
Value.QueryOutput == Value
{
sharedReader = SharedReader(value: wrappedValue)
}

/// Initializes this property with a query that fetches the first row from a table.
///
/// - Parameters:
Expand Down
20 changes: 20 additions & 0 deletions Tests/SQLiteDataTests/FetchAllTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import Foundation
import SQLiteData
import Testing

#if canImport(SwiftUI)
import SwiftUI
#endif

@Suite(.dependency(\.defaultDatabase, try .database()))
struct FetchAllTests {
@Dependency(\.defaultDatabase) var database
Expand Down Expand Up @@ -63,6 +67,16 @@ struct FetchAllTests {
)
}
}

@available(*, deprecated)
@Test func fetchAllSelection_Deprecated() async throws {
#if canImport(SwiftUI)
do {
@FetchAll(animation: .default) var row: [Row]
#expect($row.loadError == nil)
}
#endif
}
}

@Table
Expand All @@ -73,6 +87,12 @@ private struct Record: Equatable {
@Column(as: Date?.UnixTimeRepresentation.self)
var optionalDate: Date?
}

@Selection
private struct Row {
let id: Int
}

extension DatabaseWriter where Self == DatabaseQueue {
fileprivate static func database() throws -> DatabaseQueue {
let database = try DatabaseQueue()
Expand Down
35 changes: 35 additions & 0 deletions Tests/SQLiteDataTests/FetchOneTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import Foundation
import SQLiteData
import Testing

#if canImport(SwiftUI)
import SwiftUI
#endif

@Suite(.dependency(\.defaultDatabase, try .database())) struct FetchOneTests {
@Dependency(\.defaultDatabase) var database

Expand Down Expand Up @@ -176,6 +180,31 @@ import Testing
_record = FetchOne(wrappedValue: Record(id: 0), Record.all)
#expect(record.id == 1)
}

@Test func fetchOneSelection() async throws {
do {
@FetchOne var row: Row?
#expect($row.loadError == nil)
}
do {
@FetchOne var row = Row(id: 1)
#expect($row.loadError == nil)
}
}

@available(*, deprecated)
@Test func fetchOneSelection_Deprecated() async throws {
#if canImport(SwiftUI)
do {
@FetchOne(animation: .default) var row: Row?
#expect($row.loadError == nil)
}
do {
@FetchOne(animation: .default) var row = Row(id: 1)
#expect($row.loadError == nil)
}
#endif
}
}

@Table
Expand All @@ -187,6 +216,12 @@ private struct Record: Equatable {
@Column(as: Date?.UnixTimeRepresentation.self)
var optionalDate: Date?
}

@Selection
private struct Row {
let id: Int
}

extension DatabaseWriter where Self == DatabaseQueue {
fileprivate static func database() throws -> DatabaseQueue {
let database = try DatabaseQueue()
Expand Down