-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1bde239
commit 435a3ac
Showing
3 changed files
with
89 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
public struct SQLQueryEncoder { | ||
public init() { } | ||
|
||
public func encode<E>(_ encodable: E) throws -> [String: SQLExpression] | ||
where E: Encodable | ||
{ | ||
let encoder = _Encoder() | ||
try encodable.encode(to: encoder) | ||
return encoder.row | ||
} | ||
} | ||
|
||
private final class _Encoder: Encoder { | ||
var codingPath: [CodingKey] { | ||
return [] | ||
} | ||
|
||
var userInfo: [CodingUserInfoKey : Any] { | ||
return [:] | ||
} | ||
|
||
var row: [String: SQLExpression] | ||
|
||
init() { | ||
self.row = [:] | ||
} | ||
|
||
func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key : CodingKey { | ||
return KeyedEncodingContainer(_KeyedEncoder(self)) | ||
} | ||
|
||
struct _KeyedEncoder<Key>: KeyedEncodingContainerProtocol | ||
where Key: CodingKey | ||
{ | ||
var codingPath: [CodingKey] { | ||
return [] | ||
} | ||
let encoder: _Encoder | ||
init(_ encoder: _Encoder) { | ||
self.encoder = encoder | ||
} | ||
|
||
mutating func encodeNil(forKey key: Key) throws { | ||
self.encoder.row[key.stringValue] = SQLLiteral.null | ||
} | ||
|
||
mutating func encode<T>(_ value: T, forKey key: Key) throws where T : Encodable { | ||
self.encoder.row[key.stringValue] = SQLBind(value) | ||
} | ||
|
||
mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> where NestedKey : CodingKey { | ||
fatalError() | ||
} | ||
|
||
mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer { | ||
fatalError() | ||
} | ||
|
||
mutating func superEncoder() -> Encoder { | ||
return self.encoder | ||
} | ||
|
||
mutating func superEncoder(forKey key: Key) -> Encoder { | ||
return self.encoder | ||
} | ||
} | ||
|
||
func unkeyedContainer() -> UnkeyedEncodingContainer { | ||
fatalError() | ||
} | ||
|
||
func singleValueContainer() -> SingleValueEncodingContainer { | ||
fatalError() | ||
} | ||
} |