Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion Sources/PostgresKit/PostgresDatabase+SQL.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import PostgresNIO
import Dispatch
import Logging
import SQLKit

Expand Down Expand Up @@ -47,6 +48,9 @@ extension PostgresSQLDatabase: SQLDatabase, PostgresDatabase {
if let queryLogLevel = self.queryLogLevel {
self.logger.log(level: queryLogLevel, "Executing query", metadata: ["sql": .string(sql), "binds": .array(binds.map { .string("\($0)") })])
}

let startTime = DispatchTime.now()

return self.eventLoop.makeCompletedFuture {
var bindings = PostgresBindings(capacity: binds.count)
for bind in binds {
Expand All @@ -59,7 +63,16 @@ extension PostgresSQLDatabase: SQLDatabase, PostgresDatabase {
logger: $0.logger,
{ onRow($0.sql(decodingContext: self.decodingContext)) }
)
} }.map { _ in }
} }.map { _ in
if let queryLogLevel = self.queryLogLevel {
let executionTime = Int(DispatchTime.now().uptimeNanoseconds / UInt64(1_000_000)
- startTime.uptimeNanoseconds / UInt64(1_000_000))
self.logger.log(level: queryLogLevel, "Query executed in \(executionTime) ms",
metadata: ["sql": .string(sql),
"binds": .array(binds.map { .string("\($0)") }),
"execution_time_ms" : .stringConvertible(executionTime)])
}
}
}

func execute(
Expand All @@ -72,6 +85,8 @@ extension PostgresSQLDatabase: SQLDatabase, PostgresDatabase {
self.logger.log(level: queryLogLevel, "Executing query", metadata: ["sql": .string(sql), "binds": .array(binds.map { .string("\($0)") })])
}

let startTime = DispatchTime.now()

var bindings = PostgresBindings(capacity: binds.count)
for bind in binds {
try PostgresDataTranslation.encode(value: bind, in: self.encodingContext, to: &bindings)
Expand All @@ -84,6 +99,15 @@ extension PostgresSQLDatabase: SQLDatabase, PostgresDatabase {
{ onRow($0.sql(decodingContext: self.decodingContext)) }
)
}.get()

if let queryLogLevel = self.queryLogLevel {
let executionTime = Int(DispatchTime.now().uptimeNanoseconds / UInt64(1_000_000)
- startTime.uptimeNanoseconds / UInt64(1_000_000))
self.logger.log(level: queryLogLevel, "Query executed in \(executionTime) ms",
metadata: ["sql": .string(sql),
"binds": .array(binds.map { .string("\($0)") }),
"execution_time_ms" : .stringConvertible(executionTime)])
}
}


Expand Down