Skip to content

Commit

Permalink
Merge pull request #34 from vapor/dbkit-gm
Browse files Browse the repository at this point in the history
dbkit 1.0.0 gm
  • Loading branch information
tanner0101 committed Apr 25, 2018
2 parents 70fa600 + 7d8d962 commit a4a40e8
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 46 deletions.
12 changes: 10 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@ let package = Package(
],
dependencies: [
// 🌎 Utility package containing tools for byte manipulation, Codable, OS APIs, and debugging.
.package(url: "https://github.com/vapor/core.git", from: "3.0.0-rc.2"),
.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", from: "1.0.0"),

// *️⃣ Build SQL queries in Swift. Extensible, protocol-based design that supports DQL, DML, and DDL.
.package(url: "https://github.com/vapor/sql.git", from: "1.0.0"),
],
targets: [
.target(name: "CSQLite"),
.target(name: "SQLite", dependencies: ["Async", "Bits", "Core", "CSQLite", "Debugging"]),
.target(name: "SQLite", dependencies: [
"Async", "Bits", "Core", "CSQLite", "DatabaseKit", "Debugging", "SQL"
]),
.testTarget(name: "SQLiteTests", dependencies: ["SQLite"]),
]
)
2 changes: 1 addition & 1 deletion Sources/SQLite/Data/SQLiteData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ extension FixedWidthInteger {
throw SQLiteError(problem: .warning, reason: "Int too small for \(Self.self): \(int)", source: .capture())
}
return numericCast(int)
default: throw SQLiteError(problem: .warning, reason: "Could not convert to String: \(data)", source: .capture())
default: throw SQLiteError(problem: .warning, reason: "Could not convert to \(Self.self): \(data)", source: .capture())
}
}

Expand Down
3 changes: 3 additions & 0 deletions Sources/SQLite/Database/Exports.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@_exported import Core
@_exported import DatabaseKit
@_exported import SQL
36 changes: 20 additions & 16 deletions Sources/SQLite/Database/SQLiteConnection.swift
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
import Async
import CSQLite
import Dispatch

/// SQlite connection. Use this to create statements that can be executed.
public final class SQLiteConnection: BasicWorker {
public typealias Raw = OpaquePointer
public var raw: Raw
public final class SQLiteConnection: BasicWorker, DatabaseConnection {
/// Raw SQLite connection type.
internal typealias Raw = OpaquePointer

/// Reference to the database that created this connection.
public let database: SQLiteDatabase
/// See `DatabaseConnection`
public var isClosed: Bool

/// This connection's eventloop.
/// See `BasicWorker`.
public let eventLoop: EventLoop

/// See `Extendable`.
public var extend: Extend

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

/// Raw pointer to this SQLite connection.
internal var raw: Raw

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

return String(cString: raw)
}

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

/// Returns an identifier for the last inserted row.
Expand All @@ -41,6 +44,7 @@ public final class SQLiteConnection: BasicWorker {

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

Expand Down
33 changes: 16 additions & 17 deletions Sources/SQLite/Database/SQLiteDatabase.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import Async
import CSQLite
import Dispatch
import Foundation

/// SQlite database. Used to make connections.
public final class SQLiteDatabase {
public final class SQLiteDatabase: Database, LogSupporting {
/// The path to the SQLite file.
public let storage: SQLiteStorage

/// If set, query logs will be sent to the supplied logger.
public var logger: SQLiteLogger?

/// Create a new SQLite database.
public init(storage: SQLiteStorage) throws {
self.storage = storage
Expand All @@ -23,15 +17,8 @@ public final class SQLiteDatabase {
}
}

/// Opens a connection to the SQLite database at a given path.
/// If the database does not already exist, it will be created.
///
/// The supplied DispatchQueue will be used to dispatch output stream calls.
/// Make sure to supply the event loop to this parameter so you get called back
/// on the appropriate thread.
public func makeConnection(
on worker: Worker
) -> Future<SQLiteConnection> {
/// See `Database`.
public func newConnection(on worker: Worker) -> Future<SQLiteConnection> {
let promise = worker.eventLoop.newPromise(SQLiteConnection.self)
do {
// make connection
Expand All @@ -45,11 +32,23 @@ public final class SQLiteDatabase {
throw SQLiteError(problem: .error, reason: "Unexpected nil database.", source: .capture())
}

let conn = SQLiteConnection(raw: r, database: self, on: worker)
let conn = SQLiteConnection(raw: r, on: worker)
promise.succeed(result: conn)
} catch {
promise.fail(error: error)
}
return promise.futureResult
}

/// See `LogSupporting`.
public static func enableLogging(_ logger: DatabaseLogger, on conn: SQLiteConnection) {
conn.logger = logger
}
}

extension DatabaseIdentifier {
/// Default `DatabaseIdentifier` for SQLite databases.
public static var sqlite: DatabaseIdentifier<SQLiteDatabase> {
return "sqlite"
}
}
7 changes: 0 additions & 7 deletions Sources/SQLite/Database/SQLiteLogger.swift

This file was deleted.

3 changes: 1 addition & 2 deletions Sources/SQLite/Query/SQLiteQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,10 @@ public final class SQLiteQuery {
/// Executes the query, blocking until complete.
private func blockingExecute() throws -> SQLiteResults? {
var columns: [SQLiteColumn] = []

var raw: Raw?

// log before anything happens, in case there's an error
connection.database.logger?.log(query: self)
connection.logger?.record(query: string, values: binds.map { $0.description })

let ret = sqlite3_prepare_v2(connection.raw, string, -1, &raw, nil)
guard ret == SQLITE_OK else {
Expand Down
2 changes: 1 addition & 1 deletion Tests/SQLiteTests/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ extension SQLiteConnection {
static func makeTest() throws -> SQLiteConnection {
let group = MultiThreadedEventLoopGroup(numThreads: 1)
let sqlite = try SQLiteDatabase(storage: .memory)
return try sqlite.makeConnection(on: group).wait()
return try sqlite.newConnection(on: group).wait()
}
}
7 changes: 7 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ jobs:
- run:
name: Run unit tests
command: swift test

linux-release:
docker:
- image: codevapor/swift:4.1
steps:
- checkout
- run:
name: Compile code with optimizations
command: swift build -c release
Expand Down Expand Up @@ -49,6 +55,7 @@ workflows:
jobs:
- linux
- linux-fluent
- linux-release
# - macos

nightly:
Expand Down

0 comments on commit a4a40e8

Please sign in to comment.