Skip to content

Commit

Permalink
update package.swift + circle deps
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 committed Jun 15, 2018
1 parent 05877de commit 1ae3650
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let package = Package(
.package(url: "https://github.com/vapor/core.git", from: "3.0.0"),

// 🗄 Core services for creating database integrations.
.package(url: "https://github.com/vapor/database-kit.git", .branch("fluent-gm")),
.package(url: "https://github.com/vapor/database-kit.git", from: "1.0.0"),
],
targets: [
.testTarget(name: "SQLiteTests", dependencies: ["SQLite"]),
Expand Down
21 changes: 6 additions & 15 deletions Sources/SQLite/Database/SQLiteConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,34 @@ public final class SQLiteConnection: BasicWorker, DatabaseConnection {

/// Optional logger, if set queries should be logged to it.
public var logger: DatabaseLogger?

private let blockingIO: BlockingIOThreadPool

internal let c: OpaquePointer
internal let database: SQLiteDatabase

/// Returns the last error message, if one exists.
internal var errorMessage: String? {
guard let raw = sqlite3_errmsg(c) else {
guard let raw = sqlite3_errmsg(database.handle) else {
return nil
}
return String(cString: raw)
}

/// Create a new SQLite conncetion.
internal init(c: OpaquePointer, blockingIO: BlockingIOThreadPool, on worker: Worker) {
self.c = c
internal init(database: SQLiteDatabase, on worker: Worker) {
self.database = database
self.eventLoop = worker.eventLoop
self.extend = [:]
self.isClosed = false
self.blockingIO = blockingIO
}

/// Returns an identifier for the last inserted row.
public var lastAutoincrementID: Int? {
let id = sqlite3_last_insert_rowid(c)
let id = sqlite3_last_insert_rowid(database.handle)
return Int(id)
}

/// Closes the database connection.
public func close() {
isClosed = true
sqlite3_close(c)
}

public func query(_ query: SQLiteQuery) -> Future<[[SQLiteColumn: SQLiteData]]> {
Expand Down Expand Up @@ -82,7 +78,7 @@ public final class SQLiteConnection: BasicWorker, DatabaseConnection {
let promise = eventLoop.newPromise(Void.self)
// log before anything happens, in case there's an error
logger?.record(query: string, values: parameters.map { $0.description })
blockingIO.submit { state in
database.blockingIO.submit { state in
do {
let statement = try SQLiteStatement(query: string, on: self)
try statement.bind(parameters)
Expand All @@ -104,9 +100,4 @@ public final class SQLiteConnection: BasicWorker, DatabaseConnection {
}
return promise.futureResult
}

/// Closes the database when deinitialized.
deinit {
close()
}
}
7 changes: 4 additions & 3 deletions Sources/SQLite/Database/SQLiteDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public final class SQLiteDatabase: Database, LogSupporting {
/// The path to the SQLite file.
public let storage: SQLiteStorage

private let blockingIO: BlockingIOThreadPool
internal let blockingIO: BlockingIOThreadPool

private let handle: OpaquePointer
internal let handle: OpaquePointer

/// Create a new SQLite database.
public init(storage: SQLiteStorage = .memory, threadPool: BlockingIOThreadPool? = nil) throws {
Expand All @@ -29,7 +29,7 @@ public final class SQLiteDatabase: Database, LogSupporting {

/// See `Database`.
public func newConnection(on worker: Worker) -> Future<SQLiteConnection> {
let conn = SQLiteConnection(c: handle, blockingIO: blockingIO, on: worker)
let conn = SQLiteConnection(database: self, on: worker)
return worker.future(conn)
}

Expand All @@ -39,6 +39,7 @@ public final class SQLiteDatabase: Database, LogSupporting {
}

deinit {
sqlite3_close(handle)
self.blockingIO.shutdownGracefully { error in
if let error = error {
print("[SQLite] [ERROR] Could not shutdown BlockingIOThreadPool: \(error)")
Expand Down
2 changes: 1 addition & 1 deletion Sources/SQLite/Database/SQLiteStatement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal struct SQLiteStatement {

internal init(query: String, on connection: SQLiteConnection) throws {
var handle: OpaquePointer?
let ret = sqlite3_prepare_v2(connection.c, query, -1, &handle, nil)
let ret = sqlite3_prepare_v2(connection.database.handle, query, -1, &handle, nil)
guard ret == SQLITE_OK, let c = handle else {
throw SQLiteError(statusCode: ret, connection: connection, source: .capture())
}
Expand Down
6 changes: 4 additions & 2 deletions Tests/SQLiteTests/SQLiteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ class SQLiteTests: XCTestCase {
group.enter()
DispatchQueue.global().async {
let conn = try! db.newConnection(on: a).wait()
for _ in 0..<100 {
for i in 0..<100 {
print("a \(i)")
let res = try! conn.query("SELECT (1 + 1) as a;").wait()
print(res)
}
Expand All @@ -215,7 +216,8 @@ class SQLiteTests: XCTestCase {
group.enter()
DispatchQueue.global().async {
let conn = try! db.newConnection(on: b).wait()
for _ in 0..<100 {
for i in 0..<100 {
print("b \(i)")
let res = try! conn.query("SELECT (1 + 1) as b;").wait()
print(res)
}
Expand Down
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
steps:
- run:
name: Clone Fluent SQLite
command: git clone -b gm https://github.com/vapor/fluent-sqlite.git
command: git clone -b master https://github.com/vapor/fluent-sqlite.git
working_directory: ~/
- run:
name: Switch Fluent SQLite to this SQLite revision
Expand Down

0 comments on commit 1ae3650

Please sign in to comment.