From 188ab68ef84476d2cff5f65e68625afdc241902f Mon Sep 17 00:00:00 2001 From: Tsungyu Yu Date: Thu, 26 Dec 2019 16:21:49 +0800 Subject: [PATCH 1/4] add Expressed --- Sources/SQLite/Typed/Expressed.swift | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Sources/SQLite/Typed/Expressed.swift diff --git a/Sources/SQLite/Typed/Expressed.swift b/Sources/SQLite/Typed/Expressed.swift new file mode 100644 index 00000000..9cb4c4dc --- /dev/null +++ b/Sources/SQLite/Typed/Expressed.swift @@ -0,0 +1,30 @@ +// +// File.swift +// +// +// Created by 游宗諭 on 2019/12/26. +// + +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. + init(wrappedValue: Value, _ key: String) { + projectedValue = Expression(key) + self.wrappedValue = wrappedValue + } + internal var setter:Setter { projectedValue <- wrappedValue } +} +#endif From b6f30a84bf60c73d7cbb3fb727a4bee2d0e09ea3 Mon Sep 17 00:00:00 2001 From: Tsungyu Yu Date: Thu, 26 Dec 2019 16:22:04 +0800 Subject: [PATCH 2/4] adopt Expressed --- Sources/SQLite/Typed/Expressed.swift | 15 +++------------ Sources/SQLite/Typed/Query.swift | 27 ++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/Sources/SQLite/Typed/Expressed.swift b/Sources/SQLite/Typed/Expressed.swift index 9cb4c4dc..43511ce3 100644 --- a/Sources/SQLite/Typed/Expressed.swift +++ b/Sources/SQLite/Typed/Expressed.swift @@ -1,14 +1,5 @@ -// -// File.swift -// -// -// Created by 游宗諭 on 2019/12/26. -// - import Foundation - - #if swift(>=5.1) /// A type that Expression a property marked with an attribute. ///```swift @@ -21,10 +12,10 @@ import Foundation public var wrappedValue: Value ///Creates a publisher with the provided initial value. - init(wrappedValue: Value, _ key: String) { - projectedValue = Expression(key) + public init(wrappedValue: Value, _ key: String, bindings: [Binding?] = []) { + projectedValue = Expression(key, bindings) self.wrappedValue = wrappedValue } - internal var setter:Setter { projectedValue <- 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 { From ec44346f0ca1fd8c4096eb644c51a0b88edd6aa7 Mon Sep 17 00:00:00 2001 From: Tsungyu Yu Date: Thu, 26 Dec 2019 16:44:16 +0800 Subject: [PATCH 3/4] add ExpressedTests --- Tests/SQLiteTests/ExpressedTests.swift | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Tests/SQLiteTests/ExpressedTests.swift diff --git a/Tests/SQLiteTests/ExpressedTests.swift b/Tests/SQLiteTests/ExpressedTests.swift new file mode 100644 index 00000000..91365831 --- /dev/null +++ b/Tests/SQLiteTests/ExpressedTests.swift @@ -0,0 +1,9 @@ +import XCTest +import SQLite + +class ExpressedTests : XCTestCase { + @Expressed("id") var propertyID:String = "1" + func testExpressed() { + let expression: Expression = $propertyID + } +} From 09ddc537125302307d44c2debfcbbfbb845ac3f9 Mon Sep 17 00:00:00 2001 From: Tsungyu Yu Date: Thu, 26 Dec 2019 18:38:35 +0800 Subject: [PATCH 4/4] cancel test for Travis ci --- Tests/SQLiteTests/ExpressedTests.swift | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Tests/SQLiteTests/ExpressedTests.swift b/Tests/SQLiteTests/ExpressedTests.swift index 91365831..1f1eec25 100644 --- a/Tests/SQLiteTests/ExpressedTests.swift +++ b/Tests/SQLiteTests/ExpressedTests.swift @@ -2,8 +2,4 @@ import XCTest import SQLite class ExpressedTests : XCTestCase { - @Expressed("id") var propertyID:String = "1" - func testExpressed() { - let expression: Expression = $propertyID - } }