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 @@ -45,8 +45,8 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/apple/swift-log", from: "1.6.0"),
.package(url: "https://github.com/vapor/sqlite-nio", from: "1.12.0"),
.package(url: "https://github.com/feather-framework/feather-database", exact: "1.0.0-beta.3"),
.package(url: "https://github.com/swift-server/swift-service-lifecycle", from: "2.8.0"),
.package(url: "https://github.com/feather-framework/feather-database", exact: "1.0.0-beta.4"),
// [docc-plugin-placeholder]
],
targets: [
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
SQLite driver implementation for the abstract [Feather Database](https://github.com/feather-framework/feather-database) Swift API package.

[
![Release: 1.0.0-beta.4](https://img.shields.io/badge/Release-1%2E0%2E0--beta%2E4-F05138)
![Release: 1.0.0-beta.5](https://img.shields.io/badge/Release-1%2E0%2E0--beta%2E5-F05138)
](
https://github.com/feather-framework/feather-sqlite-database/releases/tag/1.0.0-beta.4
https://github.com/feather-framework/feather-sqlite-database/releases/tag/1.0.0-beta.5
)

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

```swift
.package(url: "https://github.com/feather-framework/feather-sqlite-database", exact: "1.0.0-beta.4"),
.package(url: "https://github.com/feather-framework/feather-sqlite-database", exact: "1.0.0-beta.5"),
```

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

extension DatabaseQuery {

fileprivate struct SQLiteQuery {
var sql: String
var bindings: [SQLiteData]
}

fileprivate func toSQLiteQuery() -> SQLiteQuery {
var sqliteSQL = sql
var sqliteBindings: [SQLiteData] = []

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

switch binding.binding {
case .int(let value):
sqliteBindings.append(.integer(value))
case .double(let value):
sqliteBindings.append(.float(value))
case .string(let value):
sqliteBindings.append(.text(value))
}
}

return .init(
sql: sqliteSQL,
bindings: sqliteBindings
)
}
}

public struct SQLiteDatabaseConnection: DatabaseConnection {

public typealias Query = SQLiteDatabaseQuery
public typealias RowSequence = SQLiteDatabaseRowSequence

var connection: SQLiteConnection
Expand All @@ -27,13 +60,14 @@ public struct SQLiteDatabaseConnection: DatabaseConnection {
/// - 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 sqliteQuery = query.toSQLiteQuery()
let result = try await connection.query(
query.sql,
query.bindings
sqliteQuery.sql,
sqliteQuery.bindings
)
return try await handler(
SQLiteDatabaseRowSequence(
Expand Down
199 changes: 0 additions & 199 deletions Sources/FeatherSQLiteDatabase/SQLiteDatabaseQuery.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -273,18 +273,15 @@ struct FeatherSQLiteDatabaseTestSuite {
"""#
)

let insert = SQLiteDatabaseQuery(
unsafeSQL: #"""
try await connection.run(
query: #"""
INSERT INTO "widgets"
("id", "name")
VALUES
(?, ?);
"""#,
bindings: [.integer(1), .text("gizmo")]
(\#(1), \#("gizmo"));
"""#
)

try await connection.run(query: insert)

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

let body: String? = nil
let insert: SQLiteDatabaseQuery = #"""
INSERT INTO "notes"
("id", "body")
VALUES
(1, \#(body));
"""#

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

let result =
try await connection.run(
Expand Down Expand Up @@ -357,15 +355,14 @@ struct FeatherSQLiteDatabaseTestSuite {
"""#
)

let label: SQLiteData = .text("alpha")
let insert: SQLiteDatabaseQuery = #"""
INSERT INTO "tags"
("id", "label")
VALUES
(1, \#(label));
"""#

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

let result =
try await connection.run(
Expand Down