Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/apple/swift-log", from: "1.6.0"),
.package(url: "https://github.com/vapor/mysql-nio", from: "1.8.0"),
.package(url: "https://github.com/feather-framework/feather-database", exact: "1.0.0-beta.3"),
.package(url: "https://github.com/feather-framework/feather-database", exact: "1.0.0-beta.4"),
// [docc-plugin-placeholder]
],
targets: [
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

MySQL/MariaDB driver implementation for the abstract [Feather Database](https://github.com/feather-framework/feather-database) Swift API package.

[![Release: 1.0.0-beta.2](https://img.shields.io/badge/Release-1%2E0%2E0--beta%2E2-F05138)](https://github.com/feather-framework/feather-mysql-database/releases/tag/1.0.0-beta.2)
[
![Release: 1.0.0-beta.3](https://img.shields.io/badge/Release-1%2E0%2E0--beta%2E3-F05138)
](
https://github.com/feather-framework/feather-mysql-database/releases/tag/1.0.0-beta.3
)

## Features

Expand Down Expand Up @@ -32,7 +36,7 @@ MySQL/MariaDB driver implementation for the abstract [Feather Database](https://
Add the dependency to your `Package.swift`:

```swift
.package(url: "https://github.com/feather-framework/feather-mysql-database", exact: "1.0.0-beta.2"),
.package(url: "https://github.com/feather-framework/feather-mysql-database", exact: "1.0.0-beta.3"),
```

Then add `FeatherMySQLDatabase` to your target dependencies:
Expand Down
42 changes: 38 additions & 4 deletions Sources/FeatherMySQLDatabase/MySQLDatabaseConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,42 @@ import FeatherDatabase
import MySQLNIO
import NIOCore

extension DatabaseQuery {

fileprivate struct MySQLQuery {
var sql: String
var bindings: [MySQLData]
}

fileprivate func toMySQLQuery() -> MySQLQuery {
var mysqlSQL = sql
var mysqlBindings: [MySQLData] = []

for binding in bindings {
let idx = binding.index + 1
mysqlSQL =
mysqlSQL
.replacing("{{\(idx)}}", with: "?")

switch binding.binding {
case .int(let value):
mysqlBindings.append(.init(int: value))
case .double(let value):
mysqlBindings.append(.init(double: value))
case .string(let value):
mysqlBindings.append(.init(string: value))
}
}

return .init(
sql: mysqlSQL,
bindings: mysqlBindings
)
}
}

public struct MySQLDatabaseConnection: DatabaseConnection, Sendable {

public typealias Query = MySQLQuery
public typealias RowSequence = MySQLRowSequence

let connection: MySQLNIO.MySQLConnection
Expand All @@ -27,14 +60,15 @@ public struct MySQLDatabaseConnection: DatabaseConnection, Sendable {
/// - Returns: A query result containing the returned rows.
@discardableResult
public func run<T: Sendable>(
query: Query,
query: DatabaseQuery,
_ handler: (RowSequence) async throws -> T = { $0 }
) async throws(DatabaseError) -> T {
do {
let mysqlQuery = query.toMySQLQuery()
let rows =
try await connection.query(
query.sql,
query.bindings
mysqlQuery.sql,
mysqlQuery.bindings
)
.get()

Expand Down
181 changes: 0 additions & 181 deletions Sources/FeatherMySQLDatabase/MySQLDatabaseQuery.swift

This file was deleted.

45 changes: 21 additions & 24 deletions Tests/FeatherMySQLDatabaseTests/FeatherMySQLDatabaseTestSuite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -418,18 +418,15 @@ struct MySQLDatabaseTestSuite {
"""#
)

let insert = MySQLQuery(
unsafeSQL: #"""
INSERT INTO `\#(table)`
try await connection.run(
query: #"""
INSERT INTO `\#(unescaped: table)`
(`id`, `name`)
VALUES
(?, ?);
"""#,
bindings: [.init(int: 1), .init(string: "gizmo")]
(\#(1), \#("gizmo"));
"""#
)

try await connection.run(query: insert)

let result =
try await connection.run(
query: #"""
Expand Down Expand Up @@ -471,14 +468,15 @@ struct MySQLDatabaseTestSuite {
)

let body: String? = nil
let insert: MySQLQuery = #"""
INSERT INTO `\#(unescaped: table)`
(`id`, `body`)
VALUES
(1, \#(body));
"""#

try await connection.run(query: insert)
try await connection.run(
query: #"""
INSERT INTO `\#(unescaped: table)`
(`id`, `body`)
VALUES
(1, \#(body));
"""#
)

let result =
try await connection.run(
Expand Down Expand Up @@ -520,15 +518,14 @@ struct MySQLDatabaseTestSuite {
"""#
)

let label: MySQLData = .init(string: "alpha")
let insert: MySQLQuery = #"""
INSERT INTO `\#(unescaped: table)`
(`id`, `label`)
VALUES
(1, \#(label));
"""#

try await connection.run(query: insert)
try await connection.run(
query: #"""
INSERT INTO `\#(unescaped: table)`
(`id`, `label`)
VALUES
(1, \#("alpha"));
"""#
)

let result =
try await connection.run(
Expand Down