Skip to content

Commit 115ad41

Browse files
Enable the encodable api to pass the onConflict parameter to the QueryType.insert function
1 parent 0a9893e commit 115ad41

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Sources/SQLite/Typed/Coding.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,30 @@ extension QueryType {
4444
try encodable.encode(to: encoder)
4545
return self.insert(encoder.setters + otherSetters)
4646
}
47+
48+
/// Creates an `INSERT` statement by encoding the given object
49+
/// This method converts any custom nested types to JSON data and does not handle any sort
50+
/// of object relationships. If you want to support relationships between objects you will
51+
/// have to provide your own Encodable implementations that encode the correct ids.
52+
/// The onConflict will be passed to the actual insert function to define what should happen
53+
/// when an error occurs during the insert operation.
54+
///
55+
/// - Parameters:
56+
///
57+
/// - onConlict: Define what happens when an insert operation fails
58+
///
59+
/// - encodable: An encodable object to insert
60+
///
61+
/// - userInfo: User info to be passed to encoder
62+
///
63+
/// - otherSetters: Any other setters to include in the insert
64+
///
65+
/// - Returns: An `INSERT` statement fort the encodable object
66+
public func insert(or onConflict: OnConflict, encodable: Encodable, userInfo: [CodingUserInfoKey:Any] = [:], otherSetters: [Setter] = []) throws -> Insert {
67+
let encoder = SQLiteEncoder(userInfo: userInfo)
68+
try encodable.encode(to: encoder)
69+
return self.insert(or: onConflict, encoder.setters + otherSetters)
70+
}
4771

4872
/// Creates an `UPDATE` statement by encoding the given object
4973
/// This method converts any custom nested types to JSON data and does not handle any sort

0 commit comments

Comments
 (0)