diff --git a/Sources/SQLite/Typed/Expressed.swift b/Sources/SQLite/Typed/Expressed.swift new file mode 100644 index 00000000..43511ce3 --- /dev/null +++ b/Sources/SQLite/Typed/Expressed.swift @@ -0,0 +1,21 @@ +import Foundation + +#if swift(>=5.1) +/// A type that Expression a property marked with an attribute. +///```swift +///@Expressed("id") var id = UUID().string +///``` +/// +@propertyWrapper public struct Expressed where Value: SQLite.Value { + ///The property for which this instance exposes a Expression. + public let projectedValue:Expression + + public var wrappedValue: Value + ///Creates a publisher with the provided initial value. + public init(wrappedValue: Value, _ key: String, bindings: [Binding?] = []) { + projectedValue = Expression(key, bindings) + self.wrappedValue = wrappedValue + } + public var setter:Setter { projectedValue <- wrappedValue } +} +#endif diff --git a/Sources/SQLite/Typed/Query.swift b/Sources/SQLite/Typed/Query.swift index f6ef6df8..55fdda17 100644 --- a/Sources/SQLite/Typed/Query.swift +++ b/Sources/SQLite/Typed/Query.swift @@ -672,7 +672,32 @@ extension QueryType { query.expression ]).expression) } - + #if swift(>=5.1) + func insert(_ value: Expressed, _ more: Expressed...) -> Insert where V: Value { + + insert([value.setter] + more.map{$0.setter}) + } + // + // MARK: INSERT + public func insert(_ values: [Expressed]) -> Insert { + return insert(nil, values) + } + + public func insert(or onConflict: OnConflict, _ values: Expressed...) -> Insert { + return insert(or: onConflict, values) + } + + public func insert(or onConflict: OnConflict, _ values: [Expressed]) -> Insert { + return insert(onConflict, values) + } + + fileprivate func insert(_ or: OnConflict?, _ expresseds: [Expressed]) -> Insert { + let values:[Setter] = expresseds.map{ expressed in + expressed.setter + } + return insert(or, values) + } + #endif // MARK: UPDATE public func update(_ values: Setter...) -> Update { diff --git a/Tests/SQLiteTests/ExpressedTests.swift b/Tests/SQLiteTests/ExpressedTests.swift new file mode 100644 index 00000000..1f1eec25 --- /dev/null +++ b/Tests/SQLiteTests/ExpressedTests.swift @@ -0,0 +1,5 @@ +import XCTest +import SQLite + +class ExpressedTests : XCTestCase { +}