Skip to content

Commit 59e719e

Browse files
authored
Merge pull request #47 from vapor/tn-beta-5
FluentKit Beta 5
2 parents f90bd95 + 37d270b commit 59e719e

File tree

6 files changed

+134
-212
lines changed

6 files changed

+134
-212
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ let package = Package(
1010
.library(name: "FluentSQLiteDriver", targets: ["FluentSQLiteDriver"]),
1111
],
1212
dependencies: [
13-
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.0.0-beta.2"),
13+
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.0.0-beta.5"),
1414
.package(url: "https://github.com/vapor/sqlite-kit.git", from: "4.0.0-beta.2"),
1515
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
1616
],

Sources/FluentSQLiteDriver/FluentSQLiteDatabase.swift

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ struct _FluentSQLiteDatabase {
66
}
77

88
extension _FluentSQLiteDatabase: Database {
9-
func execute(query: DatabaseQuery, onRow: @escaping (DatabaseRow) -> ()) -> EventLoopFuture<Void> {
9+
func execute(
10+
query: DatabaseQuery,
11+
onOutput: @escaping (DatabaseOutput) -> ()
12+
) -> EventLoopFuture<Void> {
1013
let sql = SQLQueryConverter(delegate: SQLiteConverterDelegate()).convert(query)
1114
let (string, binds) = self.serialize(sql)
1215
let data: [SQLiteData]
@@ -19,12 +22,18 @@ extension _FluentSQLiteDatabase: Database {
1922
}
2023
return self.database.withConnection { connection in
2124
connection.logging(to: self.logger)
22-
.query(string, data, onRow)
25+
.query(string, data) { row in
26+
onOutput(row)
27+
}
2328
.flatMap {
2429
switch query.action {
2530
case .create:
2631
return connection.lastAutoincrementID().map {
27-
onRow(LastInsertRow(idKey: query.idKey, lastAutoincrementID: $0))
32+
let row = LastInsertRow(
33+
lastAutoincrementID: $0,
34+
customIDKey: query.customIDKey
35+
)
36+
onOutput(row)
2837
}
2938
default:
3039
return self.eventLoop.makeSucceededFuture(())
@@ -51,6 +60,14 @@ extension _FluentSQLiteDatabase: Database {
5160
}
5261

5362
func execute(schema: DatabaseSchema) -> EventLoopFuture<Void> {
63+
switch schema.action {
64+
case .update:
65+
if schema.createFields.isEmpty {
66+
return self.eventLoop.makeSucceededFuture(())
67+
}
68+
default:
69+
break
70+
}
5471
let sql = SQLSchemaConverter(delegate: SQLiteConverterDelegate()).convert(schema)
5572
let (string, binds) = self.serialize(sql)
5673
let data: [SQLiteData]
@@ -65,6 +82,10 @@ extension _FluentSQLiteDatabase: Database {
6582
fatalError("Unexpected output: \($0)")
6683
}
6784
}
85+
86+
func execute(enum: DatabaseEnum) -> EventLoopFuture<Void> {
87+
return self.eventLoop.makeSucceededFuture(())
88+
}
6889

6990
func withConnection<T>(_ closure: @escaping (Database) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
7091
self.database.withConnection {
@@ -101,20 +122,24 @@ extension _FluentSQLiteDatabase: SQLiteDatabase {
101122
}
102123
}
103124

104-
private struct LastInsertRow: DatabaseRow {
125+
private struct LastInsertRow: DatabaseOutput {
105126
var description: String {
106127
["id": self.lastAutoincrementID].description
107128
}
108129

109-
let idKey: String
110130
let lastAutoincrementID: Int
131+
let customIDKey: FieldKey?
132+
133+
func schema(_ schema: String) -> DatabaseOutput {
134+
return self
135+
}
111136

112-
func contains(field: String) -> Bool {
113-
field == self.idKey
137+
func contains(_ field: FieldKey) -> Bool {
138+
field == .id || field == self.customIDKey
114139
}
115140

116-
func decode<T>(field: String, as type: T.Type, for database: Database) throws -> T where T : Decodable {
117-
guard field == self.idKey else {
141+
func decode<T>(_ field: FieldKey, as type: T.Type) throws -> T where T : Decodable {
142+
guard field == .id || field == self.customIDKey else {
118143
fatalError("Cannot decode field from last insert row: \(field).")
119144
}
120145
if let autoincrementInitializable = T.self as? AutoincrementIDInitializable.Type {

Sources/FluentSQLiteDriver/FluentSQLiteDriver.swift

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
1-
extension DatabaseDriverFactory {
1+
extension DatabaseConfigurationFactory {
22
public static func sqlite(
33
_ configuration: SQLiteConfiguration = .init(storage: .memory),
44
maxConnectionsPerEventLoop: Int = 1
55
) -> Self {
6-
return .init { databases in
7-
let db = SQLiteConnectionSource(
6+
return .init {
7+
FluentSQLiteConfiguration(
88
configuration: configuration,
9-
threadPool: databases.threadPool
10-
)
11-
let pool = EventLoopGroupConnectionPool(
12-
source: db,
139
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
14-
on: databases.eventLoopGroup
10+
middleware: []
1511
)
16-
return _FluentSQLiteDriver(pool: pool)
1712
}
1813
}
1914
}
2015

16+
struct FluentSQLiteConfiguration: DatabaseConfiguration {
17+
let configuration: SQLiteConfiguration
18+
let maxConnectionsPerEventLoop: Int
19+
var middleware: [AnyModelMiddleware]
20+
21+
func makeDriver(for databases: Databases) -> DatabaseDriver {
22+
let db = SQLiteConnectionSource(
23+
configuration: configuration,
24+
threadPool: databases.threadPool
25+
)
26+
let pool = EventLoopGroupConnectionPool(
27+
source: db,
28+
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
29+
on: databases.eventLoopGroup
30+
)
31+
return _FluentSQLiteDriver(pool: pool)
32+
}
33+
}
34+
2135
extension SQLiteConfiguration {
2236
public static func file(_ path: String) -> Self {
2337
.init(storage: .file(path: path))

Sources/FluentSQLiteDriver/SQLiteConverterDelegate.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ import FluentSQL
33
struct SQLiteConverterDelegate: SQLConverterDelegate {
44
func customDataType(_ dataType: DatabaseSchema.DataType) -> SQLExpression? {
55
switch dataType {
6-
case .string: return SQLRaw("TEXT")
7-
case .datetime: return SQLRaw("REAL")
8-
case .int64: return SQLRaw("INTEGER")
6+
case .string:
7+
return SQLRaw("TEXT")
8+
case .datetime:
9+
return SQLRaw("REAL")
10+
case .int64:
11+
return SQLRaw("INTEGER")
12+
case .enum:
13+
return SQLRaw("TEXT")
914
default: return nil
1015
}
1116
}
1217

1318
func nestedFieldExpression(_ column: String, _ path: [String]) -> SQLExpression {
14-
return SQLRaw("JSON_EXTRACT(\(column), '$.\(path[0])')")
19+
let path = path.joined(separator: ".")
20+
return SQLRaw("JSON_EXTRACT(\(column), '$.\(path)')")
1521
}
1622
}
Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
1-
extension SQLiteRow: DatabaseRow {
2-
public func contains(field: String) -> Bool {
3-
return self.column(field) != nil
1+
extension SQLiteRow: DatabaseOutput {
2+
public func schema(_ schema: String) -> DatabaseOutput {
3+
SchemaOutput(row: self, schema: schema)
44
}
55

6-
public func decode<T>(
7-
field: String,
8-
as type: T.Type,
9-
for database: Database
10-
) throws -> T where T : Decodable {
11-
return try self.decode(column: field, as: T.self)
6+
public func contains(_ field: FieldKey) -> Bool {
7+
self.column(field.description) != nil
8+
}
9+
10+
public func decode<T>(_ field: FieldKey, as type: T.Type) throws -> T
11+
where T: Decodable
12+
{
13+
try self.decode(column: field.description, as: T.self)
14+
}
15+
}
16+
17+
private struct SchemaOutput: DatabaseOutput {
18+
let row: SQLiteRow
19+
let schema: String
20+
21+
var description: String {
22+
self.row.description
23+
}
24+
25+
func schema(_ schema: String) -> DatabaseOutput {
26+
SchemaOutput(row: self.row, schema: schema)
27+
}
28+
29+
func contains(_ field: FieldKey) -> Bool {
30+
self.row.contains(column: self.schema + "_" + field.description)
31+
}
32+
33+
func decode<T>(_ field: FieldKey, as type: T.Type) throws -> T
34+
where T : Decodable
35+
{
36+
try self.row.decode(column: self.schema + "_" + field.description, as: T.self)
1237
}
1338
}

0 commit comments

Comments
 (0)