Skip to content

Commit

Permalink
Actually support query logging (#326)
Browse files Browse the repository at this point in the history
* Query logging now works as it's supposed to. Also update dependency minimums.

* Use logging metadata for logging queries
  • Loading branch information
gwynne authored May 29, 2024
1 parent dd21809 commit ac03d7c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
8 changes: 3 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ let package = Package(
.library(name: "MySQLKit", targets: ["MySQLKit"]),
],
dependencies: [
.package(url: "https://github.com/vapor/mysql-nio.git", from: "1.7.1"),
.package(url: "https://github.com/vapor/sql-kit.git", from: "3.28.0"),
.package(url: "https://github.com/vapor/mysql-nio.git", from: "1.7.2"),
.package(url: "https://github.com/vapor/sql-kit.git", from: "3.29.3"),
.package(url: "https://github.com/vapor/async-kit.git", from: "1.19.0"),
.package(url: "https://github.com/apple/swift-crypto.git", "2.0.0" ..< "4.0.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.64.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.65.0"),
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.26.0"),
],
targets: [
Expand Down Expand Up @@ -47,6 +47,4 @@ let package = Package(
var swiftSettings: [SwiftSetting] { [
.enableUpcomingFeature("ConciseMagicFile"),
.enableUpcomingFeature("ForwardTrailingClosures"),
.enableUpcomingFeature("DisableOutwardActorInference"),
.enableExperimentalFeature("StrictConcurrency=complete"),
] }
6 changes: 3 additions & 3 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ let package = Package(
.library(name: "MySQLKit", targets: ["MySQLKit"]),
],
dependencies: [
.package(url: "https://github.com/vapor/mysql-nio.git", from: "1.7.1"),
.package(url: "https://github.com/vapor/sql-kit.git", from: "3.29.2"),
.package(url: "https://github.com/vapor/mysql-nio.git", from: "1.7.2"),
.package(url: "https://github.com/vapor/sql-kit.git", from: "3.29.3"),
.package(url: "https://github.com/vapor/async-kit.git", from: "1.19.0"),
.package(url: "https://github.com/apple/swift-crypto.git", "2.0.0" ..< "4.0.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.64.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.65.0"),
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.26.0"),
],
targets: [
Expand Down
16 changes: 14 additions & 2 deletions Sources/MySQLKit/MySQLDatabase+SQL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ extension MySQLDatabase {
/// - Returns: An instance of `SQLDatabase` which accesses the same database as `self`.
public func sql(
encoder: MySQLDataEncoder = .init(),
decoder: MySQLDataDecoder = .init()
decoder: MySQLDataDecoder = .init(),
queryLogLevel: Logger.Level? = .debug
) -> any SQLDatabase {
MySQLSQLDatabase(database: .init(value: self), encoder: encoder, decoder: decoder)
MySQLSQLDatabase(database: .init(value: self), encoder: encoder, decoder: decoder, queryLogLevel: queryLogLevel)
}
}

Expand Down Expand Up @@ -40,10 +41,17 @@ private struct MySQLSQLDatabase<D: MySQLDatabase>: SQLDatabase {
// See `SQLDatabase.dialect`.
var dialect: any SQLDialect { MySQLDialect() }

// See `SQLDatabase.queryLogLevel`.
let queryLogLevel: Logger.Level?

// See `SQLDatabase.execute(sql:_:)`.
func execute(sql query: any SQLExpression, _ onRow: @escaping @Sendable (any SQLRow) -> ()) -> EventLoopFuture<Void> {
let (sql, binds) = self.serialize(query)

if let queryLogLevel = self.queryLogLevel {
self.logger.log(level: queryLogLevel, "Executing query", metadata: ["sql": .string(sql), "binds": .array(binds.map { .string("\($0)") })])
}

do {
return try self.database.value.query(
sql,
Expand All @@ -59,6 +67,10 @@ private struct MySQLSQLDatabase<D: MySQLDatabase>: SQLDatabase {
func execute(sql query: any SQLExpression, _ onRow: @escaping @Sendable (any SQLRow) -> ()) async throws {
let (sql, binds) = self.serialize(query)

if let queryLogLevel = self.queryLogLevel {
self.logger.log(level: queryLogLevel, "Executing query", metadata: ["sql": .string(sql), "binds": .array(binds.map { .string("\($0)") })])
}

return try await self.database.value.query(
sql,
binds.map { try self.encoder.encode($0) },
Expand Down

0 comments on commit ac03d7c

Please sign in to comment.