Skip to content

Commit

Permalink
🔨Table+AllColumns: pass logger to warn about skipped columns
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaelIsaev committed Nov 30, 2021
1 parent 5f32a0b commit 7ae80c0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 35 deletions.
13 changes: 8 additions & 5 deletions Sources/Bridges/Helpers/Table+AllColumns.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

import Foundation
import SwifQL
import Logging

extension Table {
typealias Columns = [(name: String, value: SwifQLable, isChanged: Bool)]

func allColumns() -> Columns {
func allColumns(logger: Logger) -> Columns {
columns.compactMap {
guard let value = $0.property.inputValue?.swifQLable else {
logger.debug("⚠️ Skipped column \($0.name) for \(Self.tableName) table cause it doesn't conform to SwifQLable")
return nil
}
return ($0.name.label, value, $0.property.isChanged)
Expand All @@ -22,9 +24,10 @@ extension Table {

func allColumns<Column: ColumnRepresentable>(
excluding keyColumn: KeyPath<Self, Column>,
excluding: [KeyPathLastPath] = []
excluding: [KeyPathLastPath] = [],
logger: Logger
) -> (columns: Columns, columnKey: Path.Column, columnValue: SwifQLable)? {
let items = allColumns()
let items = allColumns(logger: logger)
let keyColumnName = Self.key(for: keyColumn)
guard let keyColumnValue = items.first(where: { $0.0 == keyColumnName })?.1 else {
return nil
Expand All @@ -37,8 +40,8 @@ extension Table {
)
}

func allColumns(excluding: [KeyPathLastPath] = []) -> Columns {
let items = allColumns()
func allColumns(excluding: [KeyPathLastPath], logger: Logger) -> Columns {
let items = allColumns(logger: logger)
let excludingColumns: [String] = excluding.map { $0.lastPath }
return items.filter { !excludingColumns.contains($0.0) && $0.2 }
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Bridges/Helpers/TableDelete.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension Table {
on db: DatabaseIdentifier,
on container: AnyBridgesObject
) -> EventLoopFuture<Void> {
guard let items = allColumns(excluding: keyColumn) else {
guard let items = allColumns(excluding: keyColumn, logger: container.logger) else {
return container.eventLoop.makeFailedFuture(BridgesError.valueIsNilInKeyColumnUpdateIsImpossible)
}
return buildDeleteQuery(items: items.0, where: items.1 == items.2, returning: false)
Expand All @@ -37,7 +37,7 @@ extension Table {
on db: DatabaseIdentifier,
on container: AnyBridgesObject
) -> EventLoopFuture<Self> {
guard let items = allColumns(excluding: keyColumn) else {
guard let items = allColumns(excluding: keyColumn, logger: container.logger) else {
return container.eventLoop.makeFailedFuture(BridgesError.valueIsNilInKeyColumnUpdateIsImpossible)
}
return buildDeleteQuery(items: items.0, where: items.1 == items.2, returning: true)
Expand All @@ -55,7 +55,7 @@ extension Table {
on keyColumn: KeyPath<Self, Column>,
on conn: BridgeConnection
) -> EventLoopFuture<Void> {
guard let items = allColumns(excluding: keyColumn) else {
guard let items = allColumns(excluding: keyColumn, logger: conn.logger) else {
return conn.eventLoop.makeFailedFuture(BridgesError.valueIsNilInKeyColumnUpdateIsImpossible)
}
let query = buildDeleteQuery(items: items.0, where: items.1 == items.2, returning: false)
Expand Down
8 changes: 4 additions & 4 deletions Sources/Bridges/Helpers/TableInsert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ extension Table {
on db: DatabaseIdentifier,
on container: AnyBridgesObject
) -> EventLoopFuture<Void> {
buildInsertQuery(schema: schema, items: allColumns(), returning: false)
buildInsertQuery(schema: schema, items: allColumns(logger: container.logger), returning: false)
.execute(on: db, on: container)
.transform(to: ())
}
Expand All @@ -74,7 +74,7 @@ extension Table {
on db: DatabaseIdentifier,
on container: AnyBridgesObject
) -> EventLoopFuture<Self> {
buildInsertQuery(schema: schema, items: allColumns(), returning: true)
buildInsertQuery(schema: schema, items: allColumns(logger: container.logger), returning: true)
.execute(on: db, on: container)
.all(decoding: Self.self)
.flatMapThrowing { rows in
Expand Down Expand Up @@ -106,12 +106,12 @@ extension Table {
///

private func _insertNonReturning(schema: String?, on conn: BridgeConnection) -> EventLoopFuture<Void> {
let query = buildInsertQuery(schema: schema, items: allColumns(), returning: false)
let query = buildInsertQuery(schema: schema, items: allColumns(logger: conn.logger), returning: false)
return conn.query(sql: query)
}

private func _insert(schema: String?, on conn: BridgeConnection) -> EventLoopFuture<Self> {
let query = buildInsertQuery(schema: schema, items: allColumns(), returning: true)
let query = buildInsertQuery(schema: schema, items: allColumns(logger: conn.logger), returning: true)
return conn.query(sql: query, decoding: Self.self).flatMapThrowing { rows in
guard let row = rows.first else { throw BridgesError.failedToDecodeWithReturning }
return row
Expand Down
14 changes: 7 additions & 7 deletions Sources/Bridges/Helpers/TableUpdate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ extension Table {
on db: DatabaseIdentifier,
on container: AnyBridgesObject
) -> EventLoopFuture<Void> {
guard let items = allColumns(excluding: keyColumn) else {
guard let items = allColumns(excluding: keyColumn, logger: container.logger) else {
return container.eventLoop.makeFailedFuture(BridgesError.valueIsNilInKeyColumnUpdateIsImpossible)
}
guard items.0.count > 0 else {
Expand All @@ -147,7 +147,7 @@ extension Table {
on db: DatabaseIdentifier,
on container: AnyBridgesObject
) -> EventLoopFuture<Self> {
guard let items = allColumns(excluding: keyColumn) else {
guard let items = allColumns(excluding: keyColumn, logger: container.logger) else {
return container.eventLoop.makeFailedFuture(BridgesError.valueIsNilInKeyColumnUpdateIsImpossible)
}
guard items.0.count > 0 else {
Expand All @@ -170,7 +170,7 @@ extension Table {
on container: AnyBridgesObject,
where predicates: SwifQLable
) -> EventLoopFuture<Void> {
let items = allColumns()
let items = allColumns(logger: container.logger)
guard items.count > 0 else {
container.logger.debug("\(Self.tableName) update has been skipped cause nothing to update")
return container.eventLoop.makeSucceededVoidFuture()
Expand All @@ -185,7 +185,7 @@ extension Table {
on container: AnyBridgesObject,
where predicates: SwifQLable
) -> EventLoopFuture<Self> {
let items = allColumns()
let items = allColumns(logger: container.logger)
guard items.count > 0 else {
container.logger.debug("\(Self.tableName) update has been skipped cause nothing to update")
return container.eventLoop.makeSucceededFuture(self)
Expand Down Expand Up @@ -301,7 +301,7 @@ extension Table {
on keyColumn: KeyPath<Self, Column>,
on conn: BridgeConnection
) -> EventLoopFuture<Void> {
guard let items = allColumns(excluding: keyColumn) else {
guard let items = allColumns(excluding: keyColumn, logger: conn.logger) else {
return conn.eventLoop.makeFailedFuture(BridgesError.valueIsNilInKeyColumnUpdateIsImpossible)
}
guard items.0.count > 0 else {
Expand All @@ -316,7 +316,7 @@ extension Table {
on keyColumn: KeyPath<Self, Column>,
on conn: BridgeConnection
) -> EventLoopFuture<Self> {
guard let items = allColumns(excluding: keyColumn) else {
guard let items = allColumns(excluding: keyColumn, logger: conn.logger) else {
return conn.eventLoop.makeFailedFuture(BridgesError.valueIsNilInKeyColumnUpdateIsImpossible)
}
guard items.0.count > 0 else {
Expand All @@ -333,7 +333,7 @@ extension Table {
///

public func update(on conn: BridgeConnection, where predicates: SwifQLable) -> EventLoopFuture<Void> {
conn.query(sql: buildUpdateQuery(items: allColumns(), where: predicates, returning: false))
conn.query(sql: buildUpdateQuery(items: allColumns(logger: conn.logger), where: predicates, returning: false))
}
}

Expand Down
32 changes: 16 additions & 16 deletions Sources/Bridges/Helpers/TableUpsert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ extension Table {
on db: DatabaseIdentifier,
on container: AnyBridgesObject
) -> EventLoopFuture<Void> {
guard let updateItems = allColumns(excluding: conflictColumn, excluding: excluding) else {
guard let updateItems = allColumns(excluding: conflictColumn, excluding: excluding, logger: container.logger) else {
return container.eventLoop.makeFailedFuture(BridgesError.valueIsNilInKeyColumnUpdateIsImpossible)
}
return buildUpsertQuery(
schema: schema,
insertionItems: allColumns(),
insertionItems: allColumns(logger: container.logger),
updateItems: updateItems.0,
conflictColumn: updateItems.1,
returning: false
Expand All @@ -195,12 +195,12 @@ extension Table {
on db: DatabaseIdentifier,
on container: AnyBridgesObject
) -> EventLoopFuture<Self> {
guard let updateItems = allColumns(excluding: conflictColumn, excluding: excluding) else {
guard let updateItems = allColumns(excluding: conflictColumn, excluding: excluding, logger: container.logger) else {
return container.eventLoop.makeFailedFuture(BridgesError.valueIsNilInKeyColumnUpdateIsImpossible)
}
return buildUpsertQuery(
schema: schema,
insertionItems: allColumns(),
insertionItems: allColumns(logger: container.logger),
updateItems: updateItems.0,
conflictColumn: updateItems.1,
returning: true
Expand Down Expand Up @@ -332,8 +332,8 @@ extension Table {
) -> EventLoopFuture<Void> {
buildUpsertQuery(
schema: schema,
insertionItems: allColumns(),
updateItems: allColumns(excluding: excluding),
insertionItems: allColumns(logger: container.logger),
updateItems: allColumns(excluding: excluding, logger: container.logger),
conflictConstraint: conflictConstraint,
returning: false
)
Expand All @@ -350,8 +350,8 @@ extension Table {
) -> EventLoopFuture<Self> {
buildUpsertQuery(
schema: schema,
insertionItems: allColumns(),
updateItems: allColumns(excluding: excluding),
insertionItems: allColumns(logger: container.logger),
updateItems: allColumns(excluding: excluding, logger: container.logger),
conflictConstraint: conflictConstraint,
returning: true
)
Expand Down Expand Up @@ -471,12 +471,12 @@ extension Table {
schema: String?,
on conn: BridgeConnection
) -> EventLoopFuture<Void> {
guard let updateItems = allColumns(excluding: conflictColumn, excluding: excluding) else {
guard let updateItems = allColumns(excluding: conflictColumn, excluding: excluding, logger: conn.logger) else {
return conn.eventLoop.makeFailedFuture(BridgesError.valueIsNilInKeyColumnUpdateIsImpossible)
}
let query = buildUpsertQuery(
schema: schema,
insertionItems: allColumns(),
insertionItems: allColumns(logger: conn.logger),
updateItems: updateItems.0,
conflictColumn: updateItems.1,
returning: false
Expand All @@ -490,12 +490,12 @@ extension Table {
schema: String?,
on conn: BridgeConnection
) -> EventLoopFuture<Self> {
guard let updateItems = allColumns(excluding: conflictColumn, excluding: excluding) else {
guard let updateItems = allColumns(excluding: conflictColumn, excluding: excluding, logger: conn.logger) else {
return conn.eventLoop.makeFailedFuture(BridgesError.valueIsNilInKeyColumnUpdateIsImpossible)
}
let query = buildUpsertQuery(
schema: schema,
insertionItems: allColumns(),
insertionItems: allColumns(logger: conn.logger),
updateItems: updateItems.0,
conflictColumn: updateItems.1,
returning: true
Expand Down Expand Up @@ -616,8 +616,8 @@ extension Table {
) -> EventLoopFuture<Void> {
let query = buildUpsertQuery(
schema: schema,
insertionItems: allColumns(),
updateItems: allColumns(excluding: excluding),
insertionItems: allColumns(logger: conn.logger),
updateItems: allColumns(excluding: excluding, logger: conn.logger),
conflictConstraint: conflictConstraint,
returning: false
)
Expand All @@ -632,8 +632,8 @@ extension Table {
) -> EventLoopFuture<Self> {
let query = buildUpsertQuery(
schema: schema,
insertionItems: allColumns(),
updateItems: allColumns(excluding: excluding),
insertionItems: allColumns(logger: conn.logger),
updateItems: allColumns(excluding: excluding, logger: conn.logger),
conflictConstraint: conflictConstraint,
returning: true
)
Expand Down

0 comments on commit 7ae80c0

Please sign in to comment.