Skip to content

Commit

Permalink
upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
dileping committed Mar 26, 2017
1 parent 7520adc commit 8d0dc79
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
16 changes: 13 additions & 3 deletions Sources/Demo/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,19 @@ let comments = Comments()
print("Inserted shit:", ressult)
}*/

comments.filter { c in
c.comment == "WTF"
}.delete.execute(in: swirl).onComplete { res in
let q = comments.map { c in
(c.personId, c.comment)
}

let ins = q.filter { pid, comment in
comment == "WTF"
} ?+= (5, "OK")

let upd = q.filter { pid, comment in
comment == "WTF1"
} ?+= (5, "WTF-MOD")

swirl.execute([ins, upd]).onComplete { res in
print(res)
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/Swirl/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ extension QueryLike {
}

extension QueryLike where Ret.Value : EntityLike, DS : Table {
func insert(item: Ret.Value.Bind) -> Renderlet {
func insertlet(item: Ret.Value.Bind) -> Renderlet {
let q = self.query
let rep = Ret.Value.unbind(bound: item).rep()
return { dialect in
dialect.render(insert: rep, into: q.dataset, ret: q.ret)
}
}

func insert(items: [Ret.Value.Bind]) -> Renderlet {
func insertlet(items: [Ret.Value.Bind]) -> Renderlet {
let q = self.query
let reps = items.map(Ret.Value.unbind).map{$0.rep()}
return { dialect in
dialect.render(insert: reps, into: q.dataset, ret: q.ret)
}
}

func update(with values: Ret.Value.Bind) -> Renderlet {
func updatelet(with values: Ret.Value.Bind) -> Renderlet {
let q = self.query
let rep = Ret.Value.unbind(bound: values).rep()
return { dialect in
Expand Down
32 changes: 29 additions & 3 deletions Sources/Swirl/SwirlOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public extension QueryLike where Ret.Value : EntityLike {

infix operator ++= : AssignmentPrecedence
infix operator ?= : AssignmentPrecedence
infix operator ?+= : AssignmentPrecedence

private enum UpsertException : Error {
case notUpdated
}

public extension QueryLike where Ret.Value : EntityLike, DS : Table {
private static func count<F: FutureProtocol>(from: F, swirl: Swirl) -> Future<Int> where F.Value == ResultSet? {
Expand Down Expand Up @@ -133,15 +138,15 @@ public extension QueryLike where Ret.Value : EntityLike, DS : Table {
}

public func insert(item: Ret.Value.Bind) -> SwirlOperation<Int> {
return insert(renderlet: self.insert(item: item))
return insert(renderlet: insertlet(item: item))
}

public func insert(items: [Ret.Value.Bind]) -> SwirlOperation<Int> {
return insert(renderlet: self.insert(items: items))
return insert(renderlet: insertlet(items: items))
}

public func update(with values: Ret.Value.Bind) -> SwirlOperation<Int> {
let renderlet: Renderlet = self.update(with: values)
let renderlet = updatelet(with: values)
return SwirlOperation { swirl in
Self.count(from: swirl.execute(renderlet: renderlet), swirl: swirl)
}
Expand All @@ -154,6 +159,23 @@ public extension QueryLike where Ret.Value : EntityLike, DS : Table {
}
}

public func upsert(item: Ret.Value.Bind) -> SwirlOperation<Int> {
let updatelet = self.updatelet(with: item)

return SwirlOperation { swirl in
swirl.sequencial.flatMap { swirl in
Self.count(from: swirl.execute(renderlet: updatelet), swirl: swirl).map { count in
if count > 0 {
return count
}
throw UpsertException.notUpdated
}.recoverWith { (e:UpsertException) in
self.insert(item: item).execute(in: swirl)
}
}
}
}

public static func +=(q:Self, item: Ret.Value.Bind) -> SwirlOperation<Int> {
return q.insert(item: item)
}
Expand All @@ -166,4 +188,8 @@ public extension QueryLike where Ret.Value : EntityLike, DS : Table {
public static func ?=(q:Self, item: Ret.Value.Bind) -> SwirlOperation<Int> {
return q.update(with: item)
}

public static func ?+=(q:Self, item: Ret.Value.Bind) -> SwirlOperation<Int> {
return q.upsert(item: item)
}
}

0 comments on commit 8d0dc79

Please sign in to comment.