Skip to content

Commit eadc7be

Browse files
authored
Merge pull request #31 from vapor/alpha
alpha
2 parents 7a7b76e + 625f6d4 commit eadc7be

16 files changed

+198
-462
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ Database
77
database.db
88
Package.resolved
99
DerivedData
10+
.swiftpm
1011

Package.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.0
1+
// swift-tools-version:5.1
22
import PackageDescription
33

44
let package = Package(
@@ -7,18 +7,14 @@ let package = Package(
77
.library(name: "FluentSQLiteDriver", targets: ["FluentSQLiteDriver"]),
88
],
99
dependencies: [
10-
.package(url: "https://github.com/vapor/fluent-kit.git", .branch("master")),
11-
.package(url: "https://github.com/vapor/nio-sqlite.git", .branch("master")),
12-
.package(url: "https://github.com/vapor/sql.git", .branch("master")),
13-
.package(url: "https://github.com/vapor/nio-kit.git", .branch("master")),
10+
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.0.0-alpha"),
11+
.package(url: "https://github.com/vapor/sqlite-kit.git", from: "4.0.0-alpha"),
1412
],
1513
targets: [
1614
.target(name: "FluentSQLiteDriver", dependencies: [
1715
"FluentKit",
1816
"FluentSQL",
19-
"NIOKit",
20-
"NIOSQLite",
21-
"SQLKit"
17+
"SQLiteKit",
2218
]),
2319
.testTarget(name: "FluentSQLiteDriverTests", dependencies: ["FluentBenchmark", "FluentSQLiteDriver"]),
2420
]

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
<p align="center">
2-
<img src="https://user-images.githubusercontent.com/1342803/36623069-9e1f049e-18cf-11e8-99ee-5bf317486191.png" height="64" alt="Fluent SQLite">
2+
<img src="https://user-images.githubusercontent.com/1342803/59050726-08a4dd80-8859-11e9-88c0-7b3915690c46.png" alt="FluentSQLiteDriver">
33
<br>
44
<br>
5-
<a href="http://docs.vapor.codes/3.0/fluent/getting-started/">
6-
<img src="http://img.shields.io/badge/read_the-docs-2196f3.svg" alt="Documentation">
5+
<a href="https://api.vapor.codes/fluent-sqlite-driver/master/FluentSQLiteDriver/index.html">
6+
<img src="http://img.shields.io/badge/api-docs-2196f3.svg" alt="Documentation">
77
</a>
88
<a href="https://discord.gg/vapor">
99
<img src="https://img.shields.io/discord/431917998102675485.svg" alt="Team Chat">
1010
</a>
1111
<a href="LICENSE">
1212
<img src="http://img.shields.io/badge/license-MIT-brightgreen.svg" alt="MIT License">
1313
</a>
14-
<a href="https://circleci.com/gh/vapor/fluent-sqlite">
15-
<img src="https://circleci.com/gh/vapor/fluent-sqlite.svg?style=shield" alt="Continuous Integration">
14+
<a href="https://circleci.com/gh/vapor/fluent-sqlite-driver">
15+
<img src="https://circleci.com/gh/vapor/fluent-sqlite-driver.svg?style=shield" alt="Continuous Integration">
1616
</a>
1717
<a href="https://swift.org">
18-
<img src="http://img.shields.io/badge/swift-4.1-brightgreen.svg" alt="Swift 4.1">
18+
<img src="http://img.shields.io/badge/swift-5.1-brightgreen.svg" alt="Swift 5.1">
1919
</a>
2020
</p>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
extension ConnectionPool: Database where Source.Connection: Database {
2+
public func execute(_ query: DatabaseQuery, _ onOutput: @escaping (DatabaseOutput) throws -> ()) -> EventLoopFuture<Void> {
3+
return self.withConnection { $0.execute(query, onOutput) }
4+
}
5+
6+
public func execute(_ schema: DatabaseSchema) -> EventLoopFuture<Void> {
7+
return self.withConnection { $0.execute(schema) }
8+
}
9+
10+
public func withConnection<T>(_ closure: @escaping (Database) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
11+
return self.withConnection { (conn: Source.Connection) in
12+
return closure(conn)
13+
}
14+
}
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
extension DatabaseID {
2+
public static var sqlite: DatabaseID {
3+
return .init(string: "sqlite")
4+
}
5+
}
6+
7+
extension Databases {
8+
public mutating func sqlite(
9+
configuration: SQLiteConfiguration = .init(storage: .memory),
10+
threadPool: NIOThreadPool,
11+
poolConfiguration: ConnectionPoolConfig = .init(),
12+
as id: DatabaseID = .sqlite,
13+
isDefault: Bool = true
14+
) {
15+
let db = SQLiteConnectionSource(
16+
configuration: configuration,
17+
threadPool: threadPool,
18+
on: self.eventLoop
19+
)
20+
let pool = ConnectionPool(config: poolConfiguration, source: db)
21+
self.add(pool, as: id, isDefault: isDefault)
22+
}
23+
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
@_exported import FluentKit
2-
3-
@_exported import class NIOKit.ConnectionPool
4-
5-
@_exported import NIOSQLite
2+
@_exported import SQLiteKit

Sources/FluentSQLiteDriver/SQLite+Fluent.swift

Lines changed: 0 additions & 210 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import FluentSQL
2+
3+
extension SQLiteConnection: Database {
4+
public func withConnection<T>(_ closure: @escaping (Database) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
5+
return closure(self)
6+
}
7+
8+
public func execute(_ query: DatabaseQuery, _ onOutput: @escaping (DatabaseOutput) throws -> ()) -> EventLoopFuture<Void> {
9+
let sql = SQLQueryConverter(delegate: SQLiteConverterDelegate()).convert(query)
10+
return self.execute(sql: sql) { row in
11+
try onOutput(row as! DatabaseOutput)
12+
}.flatMapThrowing {
13+
switch query.action {
14+
case .create:
15+
let row = LastInsertRow(lastAutoincrementID: self.lastAutoincrementID)
16+
try onOutput(row)
17+
default: break
18+
}
19+
}
20+
}
21+
22+
public func execute(_ schema: DatabaseSchema) -> EventLoopFuture<Void> {
23+
let sql = SQLSchemaConverter(delegate: SQLiteConverterDelegate()).convert(schema)
24+
return self.execute(sql: sql) { row in
25+
fatalError("unexpected output")
26+
}
27+
}
28+
}
29+
30+
private struct LastInsertRow: DatabaseOutput {
31+
var description: String {
32+
return ["id": lastAutoincrementID].description
33+
}
34+
35+
let lastAutoincrementID: Int64?
36+
37+
func contains(field: String) -> Bool {
38+
return field == "fluentID"
39+
}
40+
41+
func decode<T>(field: String, as type: T.Type) throws -> T where T : Decodable {
42+
switch field {
43+
case "fluentID":
44+
if T.self is Int.Type {
45+
return Int(self.lastAutoincrementID!) as! T
46+
} else {
47+
fatalError()
48+
}
49+
default: throw FluentError.missingField(name: field)
50+
}
51+
}
52+
}
53+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import FluentSQL
2+
3+
struct SQLiteConverterDelegate: SQLConverterDelegate {
4+
func customDataType(_ dataType: DatabaseSchema.DataType) -> SQLExpression? {
5+
switch dataType {
6+
case .string: return SQLRaw("TEXT")
7+
case .datetime: return SQLRaw("REAL")
8+
case .int64: return SQLRaw("INTEGER")
9+
default: return nil
10+
}
11+
}
12+
13+
func nestedFieldExpression(_ column: String, _ path: [String]) -> SQLExpression {
14+
return SQLRaw("JSON_EXTRACT(\(column), '$.\(path[0])')")
15+
}
16+
}

0 commit comments

Comments
 (0)