Skip to content

Commit

Permalink
🔨TableUpdate: skip query if no columns to update
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaelIsaev committed Nov 30, 2021
1 parent 6958f2d commit 5f32a0b
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions Sources/Bridges/Helpers/TableUpdate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ extension Table {
guard let items = allColumns(excluding: keyColumn) else {
return container.eventLoop.makeFailedFuture(BridgesError.valueIsNilInKeyColumnUpdateIsImpossible)
}
guard items.0.count > 0 else {
container.logger.debug("\(Self.tableName) update has been skipped cause nothing to update")
return container.eventLoop.makeSucceededVoidFuture()
}
return buildUpdateQuery(items: items.0, where: items.1 == items.2, returning: false)
.execute(on: db, on: container)
.transform(to: ())
Expand All @@ -146,6 +150,10 @@ extension Table {
guard let items = allColumns(excluding: keyColumn) else {
return container.eventLoop.makeFailedFuture(BridgesError.valueIsNilInKeyColumnUpdateIsImpossible)
}
guard items.0.count > 0 else {
container.logger.debug("\(Self.tableName) update has been skipped cause nothing to update")
return container.eventLoop.makeSucceededFuture(self)
}
return buildUpdateQuery(items: items.0, where: items.1 == items.2, returning: true)
.execute(on: db, on: container)
.all(decoding: Self.self)
Expand All @@ -162,7 +170,12 @@ extension Table {
on container: AnyBridgesObject,
where predicates: SwifQLable
) -> EventLoopFuture<Void> {
buildUpdateQuery(items: allColumns(), where: predicates, returning: false)
let items = allColumns()
guard items.count > 0 else {
container.logger.debug("\(Self.tableName) update has been skipped cause nothing to update")
return container.eventLoop.makeSucceededVoidFuture()
}
return buildUpdateQuery(items: items, where: predicates, returning: false)
.execute(on: db, on: container)
.transform(to: ())
}
Expand All @@ -172,7 +185,12 @@ extension Table {
on container: AnyBridgesObject,
where predicates: SwifQLable
) -> EventLoopFuture<Self> {
buildUpdateQuery(items: allColumns(), where: predicates, returning: true)
let items = allColumns()
guard items.count > 0 else {
container.logger.debug("\(Self.tableName) update has been skipped cause nothing to update")
return container.eventLoop.makeSucceededFuture(self)
}
return buildUpdateQuery(items: items, where: predicates, returning: true)
.execute(on: db, on: container)
.all(decoding: Self.self)
.flatMapThrowing { rows in
Expand Down Expand Up @@ -286,6 +304,10 @@ extension Table {
guard let items = allColumns(excluding: keyColumn) else {
return conn.eventLoop.makeFailedFuture(BridgesError.valueIsNilInKeyColumnUpdateIsImpossible)
}
guard items.0.count > 0 else {
conn.logger.debug("\(Self.tableName) update has been skipped cause nothing to update")
return conn.eventLoop.makeSucceededVoidFuture()
}
let query = buildUpdateQuery(items: items.0, where: items.1 == items.2, returning: false)
return conn.query(sql: query)
}
Expand All @@ -297,6 +319,10 @@ extension Table {
guard let items = allColumns(excluding: keyColumn) else {
return conn.eventLoop.makeFailedFuture(BridgesError.valueIsNilInKeyColumnUpdateIsImpossible)
}
guard items.0.count > 0 else {
conn.logger.debug("\(Self.tableName) update has been skipped cause nothing to update")
return conn.eventLoop.makeSucceededFuture(self)
}
let query = buildUpdateQuery(items: items.0, where: items.1 == items.2, returning: true)
return conn.query(sql: query, decoding: Self.self).flatMapThrowing { rows in
guard let row = rows.first else { throw BridgesError.failedToDecodeWithReturning }
Expand Down

0 comments on commit 5f32a0b

Please sign in to comment.