diff --git a/.swift-version b/.swift-version index bb576dbd..9f55b2cc 100644 --- a/.swift-version +++ b/.swift-version @@ -1 +1 @@ -2.3 +3.0 diff --git a/.travis.yml b/.travis.yml index d855fb3c..125d7a0e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,39 +1,28 @@ language: objective-c rvm: 2.2 osx_image: xcode8 -matrix: - include: - - env: - - BUILD_SCHEME="SQLite iOS" - - IOS_SIMULATOR="iPhone SE" - - env: BUILD_SCHEME="SQLite Mac" - - env: - - VALIDATOR_SUBSPEC="none" - - IOS_SIMULATOR="iPhone SE" - - env: - - VALIDATOR_SUBSPEC="standard" - - IOS_SIMULATOR="iPhone SE" - - env: - - VALIDATOR_SUBSPEC="standalone" - - IOS_SIMULATOR="iPhone SE" - - os: osx - osx_image: xcode7.3 - env: BUILD_SCHEME="SQLite iOS" - - os: osx - osx_image: xcode7.3 - env: BUILD_SCHEME="SQLite Mac" - - os: osx - osx_image: xcode7.3 - env: VALIDATOR_SUBSPEC="none" - - os: osx - osx_image: xcode7.3 - env: VALIDATOR_SUBSPEC="standard" - - os: osx - osx_image: xcode7.3 - env: VALIDATOR_SUBSPEC="standalone" +env: + global: + - LC_CTYPE=en_US.UTF-8 + - LANG=en_US.UTF-8 + - IOS_FRAMEWORK_SCHEME="SQLite iOS" + - OSX_FRAMEWORK_SCHEME="SQLite Mac" + - TVOS_FRAMEWORK_SCHEME="SQLite tvOS" + - WATCHOS_FRAMEWORK_SCHEME="SQLite watchOS" + - IOS_SDK="iphonesimulator10.0" + - OSX_SDK="macosx10.12" + - TVOS_SDK="appletvsimulator10.0" + - WATCHOS_SDK="watchsimulator3.0" + matrix: + - DESTINATION="OS=10.0,name=iPhone 7 Plus" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" RUN_TESTS="YES" POD_LINT="YES" + # - DESTINATION="OS=3.0,name=Apple Watch - 42mm" SCHEME="$WATCHOS_FRAMEWORK_SCHEME" SDK="$WATCHOS_SDK" RUN_TESTS="NO" POD_LINT="NO" + - DESTINATION="OS=10.0,name=Apple TV 1080p" SCHEME="$TVOS_FRAMEWORK_SCHEME" SDK="$TVOS_SDK" RUN_TESTS="YES" POD_LINT="NO" + - DESTINATION="arch=x86_64" SCHEME="$OSX_FRAMEWORK_SCHEME" SDK="$OSX_SDK" RUN_TESTS="YES" POD_LINT="NO" before_install: - gem install xcpretty --no-document script: + - xcodebuild -version + - xcodebuild -showsdks - ./run-tests.sh after_failure: - find $HOME/Library/Developer/Xcode/DerivedData/ -name '*.log' -print0 | xargs -0 cat diff --git a/Makefile b/Makefile index 35edf493..bb49996e 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,6 @@ BUILD_TOOL = xcodebuild -BUILD_SCHEME = SQLite Mac -IOS_SIMULATOR = iPhone 6 -ifeq ($(BUILD_SCHEME),SQLite iOS) - BUILD_ARGUMENTS = -scheme "$(BUILD_SCHEME)" -destination "platform=iOS Simulator,name=$(IOS_SIMULATOR)" -else - BUILD_ARGUMENTS = -scheme "$(BUILD_SCHEME)" -endif + +BUILD_ARGUMENTS = -scheme "$(BUILD_SCHEME)" -sdk "$(BUILD_SDK)" -destination "$(BUILD_DESTINATION)" -configuration Debug ONLY_ACTIVE_ARCH=NO XCPRETTY := $(shell command -v xcpretty) SWIFTCOV := $(shell command -v swiftcov) @@ -14,13 +9,17 @@ GCOVR := $(shell command -v gcovr) default: test build: - $(BUILD_TOOL) $(BUILD_ARGUMENTS) +ifdef XCPRETTY + @set -o pipefail && $(BUILD_TOOL) $(BUILD_ARGUMENTS) build | $(XCPRETTY) -c +else + $(BUILD_TOOL) $(BUILD_ARGUMENTS) build +endif test: ifdef XCPRETTY - @set -o pipefail && $(BUILD_TOOL) $(BUILD_ARGUMENTS) test | $(XCPRETTY) -c + @set -o pipefail && $(BUILD_TOOL) $(BUILD_ARGUMENTS) ENABLE_TESTABILITY=YES test | $(XCPRETTY) -c else - $(BUILD_TOOL) $(BUILD_ARGUMENTS) test + $(BUILD_TOOL) $(BUILD_ARGUMENTS) ENABLE_TESTABILITY=YES test endif coverage: diff --git a/SQLite.xcodeproj/project.pbxproj b/SQLite.xcodeproj/project.pbxproj index ff98eec2..e847ff9c 100644 --- a/SQLite.xcodeproj/project.pbxproj +++ b/SQLite.xcodeproj/project.pbxproj @@ -970,7 +970,7 @@ SKIP_INSTALL = YES; "SWIFT_INCLUDE_PATHS[sdk=appletvos*]" = "$(SRCROOT)/CocoaPods/appletvos"; "SWIFT_INCLUDE_PATHS[sdk=appletvsimulator*]" = "$(SRCROOT)/CocoaPods/appletvsimulator"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; TVOS_DEPLOYMENT_TARGET = 9.1; }; name = Debug; @@ -992,7 +992,7 @@ SKIP_INSTALL = YES; "SWIFT_INCLUDE_PATHS[sdk=appletvos*]" = "$(SRCROOT)/CocoaPods/appletvos"; "SWIFT_INCLUDE_PATHS[sdk=appletvsimulator*]" = "$(SRCROOT)/CocoaPods/appletvsimulator"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; TVOS_DEPLOYMENT_TARGET = 9.1; }; name = Release; @@ -1005,6 +1005,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.stephencelis.SQLiteTests; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = appletvos; + SWIFT_VERSION = 3.0; TVOS_DEPLOYMENT_TARGET = 9.1; }; name = Debug; @@ -1017,6 +1018,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.stephencelis.SQLiteTests; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = appletvos; + SWIFT_VERSION = 3.0; TVOS_DEPLOYMENT_TARGET = 9.1; }; name = Release; @@ -1038,6 +1040,7 @@ PRODUCT_NAME = SQLite; SDKROOT = watchos; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = 4; WATCHOS_DEPLOYMENT_TARGET = 2.2; }; @@ -1060,6 +1063,7 @@ PRODUCT_NAME = SQLite; SDKROOT = watchos; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = 4; WATCHOS_DEPLOYMENT_TARGET = 2.2; }; @@ -1185,7 +1189,7 @@ "SWIFT_INCLUDE_PATHS[sdk=iphonesimulator*]" = "$(SRCROOT)/CocoaPods/iphonesimulator"; "SWIFT_INCLUDE_PATHS[sdk=iphonesimulator10.0]" = "$(SRCROOT)/CocoaPods/iphonesimulator-10.0"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -1210,7 +1214,7 @@ "SWIFT_INCLUDE_PATHS[sdk=iphoneos10.0]" = "$(SRCROOT)/CocoaPods/iphoneos-10.0"; "SWIFT_INCLUDE_PATHS[sdk=iphonesimulator*]" = "$(SRCROOT)/CocoaPods/iphonesimulator"; "SWIFT_INCLUDE_PATHS[sdk=iphonesimulator10.0]" = "$(SRCROOT)/CocoaPods/iphonesimulator-10.0"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -1221,7 +1225,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.stephencelis.SQLiteTests; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -1232,7 +1236,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.stephencelis.SQLiteTests; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -1259,7 +1263,7 @@ "SWIFT_INCLUDE_PATHS[sdk=macosx*]" = "$(SRCROOT)/CocoaPods/macosx"; "SWIFT_INCLUDE_PATHS[sdk=macosx10.11]" = "$(SRCROOT)/CocoaPods/macosx-10.11"; "SWIFT_INCLUDE_PATHS[sdk=macosx10.12]" = "$(SRCROOT)/CocoaPods/macosx-10.12"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -1286,7 +1290,7 @@ "SWIFT_INCLUDE_PATHS[sdk=macosx*]" = "$(SRCROOT)/CocoaPods/macosx"; "SWIFT_INCLUDE_PATHS[sdk=macosx10.11]" = "$(SRCROOT)/CocoaPods/macosx-10.11"; "SWIFT_INCLUDE_PATHS[sdk=macosx10.12]" = "$(SRCROOT)/CocoaPods/macosx-10.12"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -1301,7 +1305,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.stephencelis.SQLiteTests; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -1316,7 +1320,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.stephencelis.SQLiteTests; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/SQLite/Core/Blob.swift b/SQLite/Core/Blob.swift index 1b30ffa1..45d6604a 100644 --- a/SQLite/Core/Blob.swift +++ b/SQLite/Core/Blob.swift @@ -30,16 +30,16 @@ public struct Blob { self.bytes = bytes } - public init(bytes: UnsafePointer, length: Int) { - self.init(bytes: [UInt8](UnsafeBufferPointer( - start: UnsafePointer(bytes), count: length - ))) + public init(bytes: UnsafeRawPointer, length: Int) { + // TODO correct count + let i8bufptr = UnsafeBufferPointer(start: bytes.assumingMemoryBound(to: UInt8.self), count: length) + self.init(bytes: [UInt8](i8bufptr)) } public func toHex() -> String { return bytes.map { ($0 < 16 ? "0" : "") + String($0, radix: 16, uppercase: false) - }.joinWithSeparator("") + }.joined(separator: "") } } diff --git a/SQLite/Core/Connection.swift b/SQLite/Core/Connection.swift index 04fdd901..832910ba 100644 --- a/SQLite/Core/Connection.swift +++ b/SQLite/Core/Connection.swift @@ -39,24 +39,24 @@ public final class Connection { /// An in-memory database (equivalent to `.URI(":memory:")`). /// /// See: - case InMemory + case inMemory /// A temporary, file-backed database (equivalent to `.URI("")`). /// /// See: - case Temporary + case temporary /// A database located at the given URI filename (or path). /// /// See: /// /// - Parameter filename: A URI filename - case URI(String) + case uri(String) } - public var handle: COpaquePointer { return _handle } + public var handle: OpaquePointer { return _handle! } - private var _handle: COpaquePointer = nil + fileprivate var _handle: OpaquePointer? = nil /// Initializes a new SQLite connection. /// @@ -72,10 +72,10 @@ public final class Connection { /// Default: `false`. /// /// - Returns: A new database connection. - public init(_ location: Location = .InMemory, readonly: Bool = false) throws { + public init(_ location: Location = .inMemory, readonly: Bool = false) throws { let flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE - try check(sqlite3_open_v2(location.description, &_handle, flags | SQLITE_OPEN_FULLMUTEX, nil)) - dispatch_queue_set_specific(queue, Connection.queueKey, queueContext, nil) + _ = try check(sqlite3_open_v2(location.description, &_handle, flags | SQLITE_OPEN_FULLMUTEX, nil)) + queue.setSpecific(key: /*Migrator FIXME: Use a variable of type DispatchSpecificKey*/ Connection.queueKey, value: queueContext) } /// Initializes a new connection to a database. @@ -93,7 +93,7 @@ public final class Connection { /// /// - Returns: A new database connection. public convenience init(_ filename: String, readonly: Bool = false) throws { - try self.init(.URI(filename), readonly: readonly) + try self.init(.uri(filename), readonly: readonly) } deinit { @@ -131,8 +131,8 @@ public final class Connection { /// statements. /// /// - Throws: `Result.Error` if query execution fails. - public func execute(SQL: String) throws { - try sync { try self.check(sqlite3_exec(self.handle, SQL, nil, nil, nil)) } + public func execute(_ SQL: String) throws { + _ = try sync { try self.check(sqlite3_exec(self.handle, SQL, nil, nil, nil)) } } // MARK: - Prepare @@ -146,7 +146,7 @@ public final class Connection { /// - bindings: A list of parameters to bind to the statement. /// /// - Returns: A prepared statement. - @warn_unused_result public func prepare(statement: String, _ bindings: Binding?...) throws -> Statement { + public func prepare(_ statement: String, _ bindings: Binding?...) throws -> Statement { if !bindings.isEmpty { return try prepare(statement, bindings) } return try Statement(self, statement) } @@ -160,7 +160,7 @@ public final class Connection { /// - bindings: A list of parameters to bind to the statement. /// /// - Returns: A prepared statement. - @warn_unused_result public func prepare(statement: String, _ bindings: [Binding?]) throws -> Statement { + public func prepare(_ statement: String, _ bindings: [Binding?]) throws -> Statement { return try prepare(statement).bind(bindings) } @@ -173,7 +173,7 @@ public final class Connection { /// - bindings: A dictionary of named parameters to bind to the statement. /// /// - Returns: A prepared statement. - @warn_unused_result public func prepare(statement: String, _ bindings: [String: Binding?]) throws -> Statement { + public func prepare(_ statement: String, _ bindings: [String: Binding?]) throws -> Statement { return try prepare(statement).bind(bindings) } @@ -190,7 +190,7 @@ public final class Connection { /// - Throws: `Result.Error` if query execution fails. /// /// - Returns: The statement. - public func run(statement: String, _ bindings: Binding?...) throws -> Statement { + public func run(_ statement: String, _ bindings: Binding?...) throws -> Statement { return try run(statement, bindings) } @@ -205,7 +205,7 @@ public final class Connection { /// - Throws: `Result.Error` if query execution fails. /// /// - Returns: The statement. - public func run(statement: String, _ bindings: [Binding?]) throws -> Statement { + public func run(_ statement: String, _ bindings: [Binding?]) throws -> Statement { return try prepare(statement).run(bindings) } @@ -220,7 +220,7 @@ public final class Connection { /// - Throws: `Result.Error` if query execution fails. /// /// - Returns: The statement. - public func run(statement: String, _ bindings: [String: Binding?]) throws -> Statement { + public func run(_ statement: String, _ bindings: [String: Binding?]) throws -> Statement { return try prepare(statement).run(bindings) } @@ -236,7 +236,7 @@ public final class Connection { /// - bindings: A list of parameters to bind to the statement. /// /// - Returns: The first value of the first row returned. - @warn_unused_result public func scalar(statement: String, _ bindings: Binding?...) throws -> Binding? { + public func scalar(_ statement: String, _ bindings: Binding?...) throws -> Binding? { return try scalar(statement, bindings) } @@ -250,7 +250,7 @@ public final class Connection { /// - bindings: A list of parameters to bind to the statement. /// /// - Returns: The first value of the first row returned. - @warn_unused_result public func scalar(statement: String, _ bindings: [Binding?]) throws -> Binding? { + public func scalar(_ statement: String, _ bindings: [Binding?]) throws -> Binding? { return try prepare(statement).scalar(bindings) } @@ -264,7 +264,7 @@ public final class Connection { /// - bindings: A dictionary of named parameters to bind to the statement. /// /// - Returns: The first value of the first row returned. - @warn_unused_result public func scalar(statement: String, _ bindings: [String: Binding?]) throws -> Binding? { + public func scalar(_ statement: String, _ bindings: [String: Binding?]) throws -> Binding? { return try prepare(statement).scalar(bindings) } @@ -301,7 +301,7 @@ public final class Connection { /// must throw to roll the transaction back. /// /// - Throws: `Result.Error`, and rethrows. - public func transaction(mode: TransactionMode = .Deferred, block: () throws -> Void) throws { + public func transaction(_ mode: TransactionMode = .Deferred, block: @escaping () throws -> Void) throws { try transaction("BEGIN \(mode.rawValue) TRANSACTION", block, "COMMIT TRANSACTION", or: "ROLLBACK TRANSACTION") } @@ -321,23 +321,23 @@ public final class Connection { /// The block must throw to roll the savepoint back. /// /// - Throws: `SQLite.Result.Error`, and rethrows. - public func savepoint(name: String = NSUUID().UUIDString, block: () throws -> Void) throws { + public func savepoint(_ name: String = UUID().uuidString, block: @escaping () throws -> Void) throws { let name = name.quote("'") let savepoint = "SAVEPOINT \(name)" try transaction(savepoint, block, "RELEASE \(savepoint)", or: "ROLLBACK TO \(savepoint)") } - private func transaction(begin: String, _ block: () throws -> Void, _ commit: String, or rollback: String) throws { + fileprivate func transaction(_ begin: String, _ block: @escaping () throws -> Void, _ commit: String, or rollback: String) throws { return try sync { - try self.run(begin) + _ = try self.run(begin) do { try block() } catch { - try self.run(rollback) + _ = try self.run(rollback) throw error } - try self.run(commit) + _ = try self.run(commit) } } @@ -362,21 +362,21 @@ public final class Connection { /// busy error would otherwise be returned. It’s passed the number of /// times it’s been called for this lock. If it returns `true`, it will /// try again. If it returns `false`, no further attempts will be made. - public func busyHandler(callback: ((tries: Int) -> Bool)?) { + public func busyHandler(_ callback: ((_ tries: Int) -> Bool)?) { guard let callback = callback else { sqlite3_busy_handler(handle, nil, nil) busyHandler = nil return } - let box: BusyHandler = { callback(tries: Int($0)) ? 1 : 0 } + let box: BusyHandler = { callback(Int($0)) ? 1 : 0 } sqlite3_busy_handler(handle, { callback, tries in - unsafeBitCast(callback, BusyHandler.self)(tries) - }, unsafeBitCast(box, UnsafeMutablePointer.self)) + unsafeBitCast(callback, to: BusyHandler.self)(tries) + }, unsafeBitCast(box, to: UnsafeMutableRawPointer.self)) busyHandler = box } - private typealias BusyHandler = @convention(block) Int32 -> Int32 - private var busyHandler: BusyHandler? + fileprivate typealias BusyHandler = @convention(block) (Int32) -> Int32 + fileprivate var busyHandler: BusyHandler? /// Sets a handler to call when a statement is executed with the compiled /// SQL. @@ -385,21 +385,21 @@ public final class Connection { /// with the compiled SQL as its argument. /// /// db.trace { SQL in print(SQL) } - public func trace(callback: (String -> Void)?) { + public func trace(_ callback: ((String) -> Void)?) { guard let callback = callback else { sqlite3_trace(handle, nil, nil) trace = nil return } - let box: Trace = { callback(String.fromCString($0)!) } + let box: Trace = { callback(String(cString: $0)) } sqlite3_trace(handle, { callback, SQL in - unsafeBitCast(callback, Trace.self)(SQL) - }, unsafeBitCast(box, UnsafeMutablePointer.self)) + unsafeBitCast(callback, to: Trace.self)(SQL!) + }, unsafeBitCast(box, to: UnsafeMutableRawPointer.self)) trace = box } - private typealias Trace = @convention(block) UnsafePointer -> Void - private var trace: Trace? + fileprivate typealias Trace = @convention(block) (UnsafePointer) -> Void + fileprivate var trace: Trace? /// Registers a callback to be invoked whenever a row is inserted, updated, /// or deleted in a rowid table. @@ -407,7 +407,7 @@ public final class Connection { /// - Parameter callback: A callback invoked with the `Operation` (one of /// `.Insert`, `.Update`, or `.Delete`), database name, table name, and /// rowid. - public func updateHook(callback: ((operation: Operation, db: String, table: String, rowid: Int64) -> Void)?) { + public func updateHook(_ callback: ((_ operation: Operation, _ db: String, _ table: String, _ rowid: Int64) -> Void)?) { guard let callback = callback else { sqlite3_update_hook(handle, nil, nil) updateHook = nil @@ -416,26 +416,26 @@ public final class Connection { let box: UpdateHook = { callback( - operation: Operation(rawValue: $0), - db: String.fromCString($1)!, - table: String.fromCString($2)!, - rowid: $3 + Operation(rawValue: $0), + String(cString: $1), + String(cString: $2), + $3 ) } sqlite3_update_hook(handle, { callback, operation, db, table, rowid in - unsafeBitCast(callback, UpdateHook.self)(operation, db, table, rowid) - }, unsafeBitCast(box, UnsafeMutablePointer.self)) + unsafeBitCast(callback, to: UpdateHook.self)(operation, db!, table!, rowid) + }, unsafeBitCast(box, to: UnsafeMutableRawPointer.self)) updateHook = box } - private typealias UpdateHook = @convention(block) (Int32, UnsafePointer, UnsafePointer, Int64) -> Void - private var updateHook: UpdateHook? + fileprivate typealias UpdateHook = @convention(block) (Int32, UnsafePointer, UnsafePointer, Int64) -> Void + fileprivate var updateHook: UpdateHook? /// Registers a callback to be invoked whenever a transaction is committed. /// /// - Parameter callback: A callback invoked whenever a transaction is /// committed. If this callback throws, the transaction will be rolled /// back. - public func commitHook(callback: (() throws -> Void)?) { + public func commitHook(_ callback: (() throws -> Void)?) { guard let callback = callback else { sqlite3_commit_hook(handle, nil, nil) commitHook = nil @@ -451,18 +451,18 @@ public final class Connection { return 0 } sqlite3_commit_hook(handle, { callback in - unsafeBitCast(callback, CommitHook.self)() - }, unsafeBitCast(box, UnsafeMutablePointer.self)) + unsafeBitCast(callback, to: CommitHook.self)() + }, unsafeBitCast(box, to: UnsafeMutableRawPointer.self)) commitHook = box } - private typealias CommitHook = @convention(block) () -> Int32 - private var commitHook: CommitHook? + fileprivate typealias CommitHook = @convention(block) () -> Int32 + fileprivate var commitHook: CommitHook? /// Registers a callback to be invoked whenever a transaction rolls back. /// /// - Parameter callback: A callback invoked when a transaction is rolled /// back. - public func rollbackHook(callback: (() -> Void)?) { + public func rollbackHook(_ callback: (() -> Void)?) { guard let callback = callback else { sqlite3_rollback_hook(handle, nil, nil) rollbackHook = nil @@ -471,12 +471,12 @@ public final class Connection { let box: RollbackHook = { callback() } sqlite3_rollback_hook(handle, { callback in - unsafeBitCast(callback, RollbackHook.self)() - }, unsafeBitCast(box, UnsafeMutablePointer.self)) + unsafeBitCast(callback, to: RollbackHook.self)() + }, unsafeBitCast(box, to: UnsafeMutableRawPointer.self)) rollbackHook = box } - private typealias RollbackHook = @convention(block) () -> Void - private var rollbackHook: RollbackHook? + fileprivate typealias RollbackHook = @convention(block) () -> Void + fileprivate var rollbackHook: RollbackHook? /// Creates or redefines a custom SQL function. /// @@ -497,11 +497,11 @@ public final class Connection { /// - block: A block of code to run when the function is called. The block /// is called with an array of raw SQL values mapped to the function’s /// parameters and should return a raw SQL value (or nil). - public func createFunction(function: String, argumentCount: UInt? = nil, deterministic: Bool = false, _ block: (args: [Binding?]) -> Binding?) { + public func createFunction(_ function: String, argumentCount: UInt? = nil, deterministic: Bool = false, _ block: @escaping (_ args: [Binding?]) -> Binding?) { let argc = argumentCount.map { Int($0) } ?? -1 let box: Function = { context, argc, argv in let arguments: [Binding?] = (0...self), { context, argc, value in - unsafeBitCast(sqlite3_user_data(context), Function.self)(context, argc, value) + sqlite3_create_function_v2(handle, function, Int32(argc), flags, unsafeBitCast(box, to: UnsafeMutableRawPointer.self), { context, argc, value in + let function = unsafeBitCast(sqlite3_user_data(context), to: Function.self) + function(context, argc, value) }, nil, nil, nil) if functions[function] == nil { self.functions[function] = [:] } functions[function]?[argc] = box } - private typealias Function = @convention(block) (COpaquePointer, Int32, UnsafeMutablePointer) -> Void - private var functions = [String: [Int: Function]]() + fileprivate typealias Function = @convention(block) (OpaquePointer?, Int32, UnsafeMutablePointer?) -> Void + fileprivate var functions = [String: [Int: Function]]() /// The return type of a collation comparison function. - public typealias ComparisonResult = NSComparisonResult + public typealias ComparisonResult = Foundation.ComparisonResult /// Defines a new collating sequence. /// @@ -556,23 +557,26 @@ public final class Connection { /// /// - block: A collation function that takes two strings and returns the /// comparison result. - public func createCollation(collation: String, _ block: (lhs: String, rhs: String) -> ComparisonResult) throws { + public func createCollation(_ collation: String, _ block: @escaping (_ lhs: String, _ rhs: String) -> ComparisonResult) throws { + // TODO correct capacity let box: Collation = { lhs, rhs in - Int32(block(lhs: String.fromCString(UnsafePointer(lhs))!, rhs: String.fromCString(UnsafePointer(rhs))!).rawValue) + let lstr = String(cString: lhs.bindMemory(to: UInt8.self, capacity: 0)) + let rstr = String(cString: rhs.bindMemory(to: UInt8.self, capacity: 0)) + return Int32(Int(block(lstr, rstr).rawValue)) } - try check(sqlite3_create_collation_v2(handle, collation, SQLITE_UTF8, unsafeBitCast(box, UnsafeMutablePointer.self), { callback, _, lhs, _, rhs in - unsafeBitCast(callback, Collation.self)(lhs, rhs) + _ = try check(sqlite3_create_collation_v2(handle, collation, SQLITE_UTF8, unsafeBitCast(box, to: UnsafeMutableRawPointer.self), { callback, _, lhs, _, rhs in + unsafeBitCast(callback, to: Collation.self)(lhs!, rhs!) }, nil)) collations[collation] = box } - private typealias Collation = @convention(block) (UnsafePointer, UnsafePointer) -> Int32 - private var collations = [String: Collation]() + fileprivate typealias Collation = @convention(block) (UnsafeRawPointer, UnsafeRawPointer) -> Int32 + fileprivate var collations = [String: Collation]() // MARK: - Error Handling - func sync(block: () throws -> T) rethrows -> T { + func sync(_ block: @escaping () throws -> T) rethrows -> T { var success: T? - var failure: ErrorType? + var failure: Error? let box: () -> Void = { do { @@ -582,10 +586,10 @@ public final class Connection { } } - if dispatch_get_specific(Connection.queueKey) == queueContext { + if DispatchQueue.getSpecific(key: Connection.queueKey) == queueContext { box() } else { - dispatch_sync(queue, box) // FIXME: rdar://problem/21389236 + queue.sync(execute: box) // FIXME: rdar://problem/21389236 } if let failure = failure { @@ -595,7 +599,7 @@ public final class Connection { return success! } - func check(resultCode: Int32, statement: Statement? = nil) throws -> Int32 { + func check(_ resultCode: Int32, statement: Statement? = nil) throws -> Int32 { guard let error = Result(errorCode: resultCode, connection: self, statement: statement) else { return resultCode } @@ -603,18 +607,18 @@ public final class Connection { throw error } - private var queue = dispatch_queue_create("SQLite.Database", DISPATCH_QUEUE_SERIAL) + fileprivate var queue = DispatchQueue(label: "SQLite.Database", attributes: []) - private static let queueKey = unsafeBitCast(Connection.self, UnsafePointer.self) + fileprivate static let queueKey = DispatchSpecificKey() - private lazy var queueContext: UnsafeMutablePointer = unsafeBitCast(self, UnsafeMutablePointer.self) + fileprivate lazy var queueContext: Int = unsafeBitCast(self, to: Int.self) } extension Connection : CustomStringConvertible { public var description: String { - return String.fromCString(sqlite3_db_filename(handle, nil))! + return String(cString: sqlite3_db_filename(handle, nil)) } } @@ -623,11 +627,11 @@ extension Connection.Location : CustomStringConvertible { public var description: String { switch self { - case .InMemory: + case .inMemory: return ":memory:" - case .Temporary: + case .temporary: return "" - case .URI(let URI): + case .uri(let URI): return URI } } @@ -638,22 +642,22 @@ extension Connection.Location : CustomStringConvertible { public enum Operation { /// An INSERT operation. - case Insert + case insert /// An UPDATE operation. - case Update + case update /// A DELETE operation. - case Delete + case delete - private init(rawValue: Int32) { + fileprivate init(rawValue: Int32) { switch rawValue { case SQLITE_INSERT: - self = .Insert + self = .insert case SQLITE_UPDATE: - self = .Update + self = .update case SQLITE_DELETE: - self = .Delete + self = .delete default: fatalError("unhandled operation code: \(rawValue)") } @@ -661,17 +665,17 @@ public enum Operation { } -public enum Result : ErrorType { +public enum Result : Error { - private static let successCodes: Set = [SQLITE_OK, SQLITE_ROW, SQLITE_DONE] + fileprivate static let successCodes: Set = [SQLITE_OK, SQLITE_ROW, SQLITE_DONE] - case Error(message: String, code: Int32, statement: Statement?) + case error(message: String, code: Int32, statement: Statement?) init?(errorCode: Int32, connection: Connection, statement: Statement? = nil) { guard !Result.successCodes.contains(errorCode) else { return nil } - let message = String.fromCString(sqlite3_errmsg(connection.handle))! - self = Error(message: message, code: errorCode, statement: statement) + let message = String(cString: sqlite3_errmsg(connection.handle)) + self = .error(message: message, code: errorCode, statement: statement) } } @@ -680,7 +684,7 @@ extension Result : CustomStringConvertible { public var description: String { switch self { - case let .Error(message, _, statement): + case let .error(message, _, statement): guard let statement = statement else { return message } return "\(message) (\(statement))" diff --git a/SQLite/Core/Statement.swift b/SQLite/Core/Statement.swift index 5e172b68..01720433 100644 --- a/SQLite/Core/Statement.swift +++ b/SQLite/Core/Statement.swift @@ -31,13 +31,13 @@ import CSQLite /// A single SQL statement. public final class Statement { - private var handle: COpaquePointer = nil + fileprivate var handle: OpaquePointer? = nil - private let connection: Connection + fileprivate let connection: Connection init(_ connection: Connection, _ SQL: String) throws { self.connection = connection - try connection.check(sqlite3_prepare_v2(connection.handle, SQL, -1, &handle, nil)) + _ = try connection.check(sqlite3_prepare_v2(connection.handle, SQL, -1, &handle, nil)) } deinit { @@ -47,7 +47,7 @@ public final class Statement { public lazy var columnCount: Int = Int(sqlite3_column_count(self.handle)) public lazy var columnNames: [String] = (0.. Statement { + public func bind(_ values: Binding?...) -> Statement { return bind(values) } @@ -67,7 +67,7 @@ public final class Statement { /// - Parameter values: A list of parameters to bind to the statement. /// /// - Returns: The statement object (useful for chaining). - public func bind(values: [Binding?]) -> Statement { + public func bind(_ values: [Binding?]) -> Statement { if values.isEmpty { return self } reset() guard values.count == Int(sqlite3_bind_parameter_count(handle)) else { @@ -83,7 +83,7 @@ public final class Statement { /// statement. /// /// - Returns: The statement object (useful for chaining). - public func bind(values: [String: Binding?]) -> Statement { + public func bind(_ values: [String: Binding?]) -> Statement { reset() for (name, value) in values { let idx = sqlite3_bind_parameter_index(handle, name) @@ -95,7 +95,7 @@ public final class Statement { return self } - private func bind(value: Binding?, atIndex idx: Int) { + fileprivate func bind(_ value: Binding?, atIndex idx: Int) { if value == nil { sqlite3_bind_null(handle, Int32(idx)) } else if let value = value as? Blob { @@ -120,7 +120,7 @@ public final class Statement { /// - Throws: `Result.Error` if query execution fails. /// /// - Returns: The statement object (useful for chaining). - public func run(bindings: Binding?...) throws -> Statement { + public func run(_ bindings: Binding?...) throws -> Statement { guard bindings.isEmpty else { return try run(bindings) } @@ -135,7 +135,7 @@ public final class Statement { /// - Throws: `Result.Error` if query execution fails. /// /// - Returns: The statement object (useful for chaining). - public func run(bindings: [Binding?]) throws -> Statement { + public func run(_ bindings: [Binding?]) throws -> Statement { return try bind(bindings).run() } @@ -145,27 +145,27 @@ public final class Statement { /// - Throws: `Result.Error` if query execution fails. /// /// - Returns: The statement object (useful for chaining). - public func run(bindings: [String: Binding?]) throws -> Statement { + public func run(_ bindings: [String: Binding?]) throws -> Statement { return try bind(bindings).run() } /// - Parameter bindings: A list of parameters to bind to the statement. /// /// - Returns: The first value of the first row returned. - @warn_unused_result public func scalar(bindings: Binding?...) throws -> Binding? { + public func scalar(_ bindings: Binding?...) throws -> Binding? { guard bindings.isEmpty else { return try scalar(bindings) } reset(clearBindings: false) - try step() + _ = try step() return row[0] } /// - Parameter bindings: A list of parameters to bind to the statement. /// /// - Returns: The first value of the first row returned. - @warn_unused_result public func scalar(bindings: [Binding?]) throws -> Binding? { + public func scalar(_ bindings: [Binding?]) throws -> Binding? { return try bind(bindings).scalar() } @@ -174,7 +174,7 @@ public final class Statement { /// statement. /// /// - Returns: The first value of the first row returned. - @warn_unused_result public func scalar(bindings: [String: Binding?]) throws -> Binding? { + public func scalar(_ bindings: [String: Binding?]) throws -> Binding? { return try bind(bindings).scalar() } @@ -182,23 +182,23 @@ public final class Statement { return try connection.sync { try self.connection.check(sqlite3_step(self.handle)) == SQLITE_ROW } } - private func reset(clearBindings shouldClear: Bool = true) { + fileprivate func reset(clearBindings shouldClear: Bool = true) { sqlite3_reset(handle) if (shouldClear) { sqlite3_clear_bindings(handle) } } } -extension Statement : SequenceType { +extension Statement : Sequence { - public func generate() -> Statement { + public func makeIterator() -> Statement { reset(clearBindings: false) return self } } -extension Statement : GeneratorType { +extension Statement : IteratorProtocol { public func next() -> [Binding?]? { return try! step() ? Array(row) : nil @@ -209,19 +209,19 @@ extension Statement : GeneratorType { extension Statement : CustomStringConvertible { public var description: String { - return String.fromCString(sqlite3_sql(handle))! + return String(cString: sqlite3_sql(handle)) } } public struct Cursor { - private let handle: COpaquePointer + fileprivate let handle: OpaquePointer - private let columnCount: Int + fileprivate let columnCount: Int - private init(_ statement: Statement) { - handle = statement.handle + fileprivate init(_ statement: Statement) { + handle = statement.handle! columnCount = statement.columnCount } @@ -234,13 +234,13 @@ public struct Cursor { } public subscript(idx: Int) -> String { - return String.fromCString(UnsafePointer(sqlite3_column_text(handle, Int32(idx)))) ?? "" + return String(cString: UnsafePointer(sqlite3_column_text(handle, Int32(idx)))) } public subscript(idx: Int) -> Blob { let bytes = sqlite3_column_blob(handle, Int32(idx)) let length = Int(sqlite3_column_bytes(handle, Int32(idx))) - return Blob(bytes: bytes, length: length) + return Blob(bytes: bytes!, length: length) } // MARK: - @@ -256,7 +256,7 @@ public struct Cursor { } /// Cursors provide direct access to a statement’s current row. -extension Cursor : SequenceType { +extension Cursor : Sequence { public subscript(idx: Int) -> Binding? { switch sqlite3_column_type(handle, Int32(idx)) { @@ -275,11 +275,11 @@ extension Cursor : SequenceType { } } - public func generate() -> AnyGenerator { + public func makeIterator() -> AnyIterator { var idx = 0 - return AnyGenerator { + return AnyIterator { if idx >= self.columnCount { - return Optional.None + return Optional.none } else { idx += 1 return self[idx - 1] diff --git a/SQLite/Core/Value.swift b/SQLite/Core/Value.swift index 2cfb45ed..608f0ce6 100644 --- a/SQLite/Core/Value.swift +++ b/SQLite/Core/Value.swift @@ -39,7 +39,7 @@ public protocol Value : Expressible { // extensions cannot have inheritance clau static var declaredDatatype: String { get } - static func fromDatatypeValue(datatypeValue: Datatype) -> ValueType + static func fromDatatypeValue(_ datatypeValue: Datatype) -> ValueType var datatypeValue: Datatype { get } @@ -49,7 +49,7 @@ extension Double : Number, Value { public static let declaredDatatype = "REAL" - public static func fromDatatypeValue(datatypeValue: Double) -> Double { + public static func fromDatatypeValue(_ datatypeValue: Double) -> Double { return datatypeValue } @@ -63,7 +63,7 @@ extension Int64 : Number, Value { public static let declaredDatatype = "INTEGER" - public static func fromDatatypeValue(datatypeValue: Int64) -> Int64 { + public static func fromDatatypeValue(_ datatypeValue: Int64) -> Int64 { return datatypeValue } @@ -77,7 +77,7 @@ extension String : Binding, Value { public static let declaredDatatype = "TEXT" - public static func fromDatatypeValue(datatypeValue: String) -> String { + public static func fromDatatypeValue(_ datatypeValue: String) -> String { return datatypeValue } @@ -91,7 +91,7 @@ extension Blob : Binding, Value { public static let declaredDatatype = "BLOB" - public static func fromDatatypeValue(datatypeValue: Blob) -> Blob { + public static func fromDatatypeValue(_ datatypeValue: Blob) -> Blob { return datatypeValue } @@ -107,7 +107,7 @@ extension Bool : Binding, Value { public static var declaredDatatype = Int64.declaredDatatype - public static func fromDatatypeValue(datatypeValue: Int64) -> Bool { + public static func fromDatatypeValue(_ datatypeValue: Int64) -> Bool { return datatypeValue != 0 } @@ -121,7 +121,7 @@ extension Int : Number, Value { public static var declaredDatatype = Int64.declaredDatatype - public static func fromDatatypeValue(datatypeValue: Int64) -> Int { + public static func fromDatatypeValue(_ datatypeValue: Int64) -> Int { return Int(datatypeValue) } diff --git a/SQLite/Extensions/FTS4.swift b/SQLite/Extensions/FTS4.swift index 466c42c7..bd8c7b2d 100644 --- a/SQLite/Extensions/FTS4.swift +++ b/SQLite/Extensions/FTS4.swift @@ -24,15 +24,15 @@ extension Module { - @warn_unused_result public static func FTS4(column: Expressible, _ more: Expressible...) -> Module { + public static func FTS4(_ column: Expressible, _ more: Expressible...) -> Module { return FTS4([column] + more) } - @warn_unused_result public static func FTS4(columns: [Expressible] = [], tokenize tokenizer: Tokenizer? = nil) -> Module { + public static func FTS4(_ columns: [Expressible] = [], tokenize tokenizer: Tokenizer? = nil) -> Module { return FTS4(FTS4Config().columns(columns).tokenizer(tokenizer)) } - @warn_unused_result public static func FTS4(config: FTS4Config) -> Module { + public static func FTS4(_ config: FTS4Config) -> Module { return Module(name: "fts4", arguments: config.arguments()) } } @@ -51,15 +51,15 @@ extension VirtualTable { /// /// - Returns: An expression appended with a `MATCH` query against the given /// pattern. - @warn_unused_result public func match(pattern: String) -> Expression { + public func match(_ pattern: String) -> Expression { return "MATCH".infix(tableName(), pattern) } - @warn_unused_result public func match(pattern: Expression) -> Expression { + public func match(_ pattern: Expression) -> Expression { return "MATCH".infix(tableName(), pattern) } - @warn_unused_result public func match(pattern: Expression) -> Expression { + public func match(_ pattern: Expression) -> Expression { return "MATCH".infix(tableName(), pattern) } @@ -73,15 +73,15 @@ extension VirtualTable { /// - Parameter pattern: A pattern to match. /// /// - Returns: A query with the given `WHERE … MATCH` clause applied. - @warn_unused_result public func match(pattern: String) -> QueryType { + public func match(_ pattern: String) -> QueryType { return filter(match(pattern)) } - @warn_unused_result public func match(pattern: Expression) -> QueryType { + public func match(_ pattern: Expression) -> QueryType { return filter(match(pattern)) } - @warn_unused_result public func match(pattern: Expression) -> QueryType { + public func match(_ pattern: Expression) -> QueryType { return filter(match(pattern)) } @@ -93,7 +93,7 @@ public struct Tokenizer { public static let Porter = Tokenizer("porter") - @warn_unused_result public static func Unicode61(removeDiacritics removeDiacritics: Bool? = nil, tokenchars: Set = [], separators: Set = []) -> Tokenizer { + public static func Unicode61(removeDiacritics: Bool? = nil, tokenchars: Set = [], separators: Set = []) -> Tokenizer { var arguments = [String]() if let removeDiacritics = removeDiacritics { @@ -101,19 +101,19 @@ public struct Tokenizer { } if !tokenchars.isEmpty { - let joined = tokenchars.map { String($0) }.joinWithSeparator("") + let joined = tokenchars.map { String($0) }.joined(separator: "") arguments.append("tokenchars=\(joined)".quote()) } if !separators.isEmpty { - let joined = separators.map { String($0) }.joinWithSeparator("") + let joined = separators.map { String($0) }.joined(separator: "") arguments.append("separators=\(joined)".quote()) } return Tokenizer("unicode61", arguments) } - @warn_unused_result public static func Custom(name: String) -> Tokenizer { + public static func Custom(_ name: String) -> Tokenizer { return Tokenizer(Tokenizer.moduleName.quote(), [name.quote()]) } @@ -121,34 +121,34 @@ public struct Tokenizer { public let arguments: [String] - private init(_ name: String, _ arguments: [String] = []) { + fileprivate init(_ name: String, _ arguments: [String] = []) { self.name = name self.arguments = arguments } - private static let moduleName = "SQLite.swift" + fileprivate static let moduleName = "SQLite.swift" } extension Tokenizer : CustomStringConvertible { public var description: String { - return ([name] + arguments).joinWithSeparator(" ") + return ([name] + arguments).joined(separator: " ") } } extension Connection { - public func registerTokenizer(submoduleName: String, next: String -> (String, Range)?) throws { - try check(_SQLiteRegisterTokenizer(handle, Tokenizer.moduleName, submoduleName) { input, offset, length in - let string = String.fromCString(input)! + public func registerTokenizer(_ submoduleName: String, next: @escaping (String) -> (String, Range)?) throws { + _ = try check(_SQLiteRegisterTokenizer(handle, Tokenizer.moduleName, submoduleName) { input, offset, length in + let string = String(cString: input) guard let (token, range) = next(string) else { return nil } let view = string.utf8 - offset.memory += string.substringToIndex(range.startIndex).utf8.count - length.memory = Int32(range.startIndex.samePositionIn(view).distanceTo(range.endIndex.samePositionIn(view))) + offset.pointee += string.substring(to: range.lowerBound).utf8.count + length.pointee = Int32(view.distance(from: range.lowerBound.samePosition(in: view), to: range.upperBound.samePosition(in: view))) return token }) } @@ -157,7 +157,7 @@ extension Connection { /// Configuration options shared between the [FTS4](https://www.sqlite.org/fts3.html) and /// [FTS5](https://www.sqlite.org/fts5.html) extensions. -public class FTSConfig { +open class FTSConfig { public enum ColumnOption { /// [The notindexed= option](https://www.sqlite.org/fts3.html#section_6_5) case unindexed @@ -171,38 +171,38 @@ public class FTSConfig { var isContentless: Bool = false /// Adds a column definition - public func column(column: Expressible, _ options: [ColumnOption] = []) -> Self { + open func column(_ column: Expressible, _ options: [ColumnOption] = []) -> Self { self.columnDefinitions.append((column, options)) return self } - public func columns(columns: [Expressible]) -> Self { + open func columns(_ columns: [Expressible]) -> Self { for column in columns { - self.column(column) + _ = self.column(column) } return self } /// [Tokenizers](https://www.sqlite.org/fts3.html#tokenizer) - public func tokenizer(tokenizer: Tokenizer?) -> Self { + open func tokenizer(_ tokenizer: Tokenizer?) -> Self { self.tokenizer = tokenizer return self } /// [The prefix= option](https://www.sqlite.org/fts3.html#section_6_6) - public func prefix(prefix: [Int]) -> Self { + open func prefix(_ prefix: [Int]) -> Self { self.prefixes += prefix return self } /// [The content= option](https://www.sqlite.org/fts3.html#section_6_2) - public func externalContent(schema: SchemaType) -> Self { + open func externalContent(_ schema: SchemaType) -> Self { self.externalContentSchema = schema return self } /// [Contentless FTS4 Tables](https://www.sqlite.org/fts3.html#section_6_2_1) - public func contentless() -> Self { + open func contentless() -> Self { self.isContentless = true return self } @@ -217,15 +217,15 @@ public class FTSConfig { func options() -> Options { var options = Options() - options.append(formatColumnDefinitions()) + _ = options.append(formatColumnDefinitions()) if let tokenizer = tokenizer { - options.append("tokenize", value: Expression(literal: tokenizer.description)) + _ = options.append("tokenize", value: Expression(literal: tokenizer.description)) } - options.appendCommaSeparated("prefix", values:prefixes.sort().map { String($0) }) + _ = options.appendCommaSeparated("prefix", values:prefixes.sorted().map { String($0) }) if isContentless { - options.append("content", value: "") + _ = options.append("content", value: "") } else if let externalContentSchema = externalContentSchema { - options.append("content", value: externalContentSchema.tableName()) + _ = options.append("content", value: externalContentSchema.tableName()) } return options } @@ -233,28 +233,28 @@ public class FTSConfig { struct Options { var arguments = [Expressible]() - mutating func append(columns: [Expressible]) -> Options { - arguments.appendContentsOf(columns) + mutating func append(_ columns: [Expressible]) -> Options { + arguments.append(contentsOf: columns) return self } - mutating func appendCommaSeparated(key: String, values: [String]) -> Options { + mutating func appendCommaSeparated(_ key: String, values: [String]) -> Options { if values.isEmpty { return self } else { - return append(key, value: values.joinWithSeparator(",")) + return append(key, value: values.joined(separator: ",")) } } - mutating func append(key: String, value: CustomStringConvertible?) -> Options { + mutating func append(_ key: String, value: CustomStringConvertible?) -> Options { return append(key, value: value?.description) } - mutating func append(key: String, value: String?) -> Options { + mutating func append(_ key: String, value: String?) -> Options { return append(key, value: value.map { Expression($0) }) } - mutating func append(key: String, value: Expressible?) -> Options { + mutating func append(_ key: String, value: Expressible?) -> Options { if let value = value { arguments.append("=".join([Expression(literal: key), value])) } @@ -264,10 +264,10 @@ public class FTSConfig { } /// Configuration for the [FTS4](https://www.sqlite.org/fts3.html) extension. -public class FTS4Config : FTSConfig { +open class FTS4Config : FTSConfig { /// [The matchinfo= option](https://www.sqlite.org/fts3.html#section_6_4) public enum MatchInfo : CustomStringConvertible { - case FTS3 + case fts3 public var description: String { return "fts3" } @@ -276,14 +276,14 @@ public class FTS4Config : FTSConfig { /// [FTS4 options](https://www.sqlite.org/fts3.html#fts4_options) public enum Order : CustomStringConvertible { /// Data structures are optimized for returning results in ascending order by docid (default) - case Asc + case asc /// FTS4 stores its data in such a way as to optimize returning results in descending order by docid. - case Desc + case desc public var description: String { switch self { - case Asc: return "asc" - case Desc: return "desc" + case .asc: return "asc" + case .desc: return "desc" } } } @@ -298,31 +298,31 @@ public class FTS4Config : FTSConfig { } /// [The compress= and uncompress= options](https://www.sqlite.org/fts3.html#section_6_1) - public func compress(functionName: String) -> Self { + open func compress(_ functionName: String) -> Self { self.compressFunction = functionName return self } /// [The compress= and uncompress= options](https://www.sqlite.org/fts3.html#section_6_1) - public func uncompress(functionName: String) -> Self { + open func uncompress(_ functionName: String) -> Self { self.uncompressFunction = functionName return self } /// [The languageid= option](https://www.sqlite.org/fts3.html#section_6_3) - public func languageId(columnName: String) -> Self { + open func languageId(_ columnName: String) -> Self { self.languageId = columnName return self } /// [The matchinfo= option](https://www.sqlite.org/fts3.html#section_6_4) - public func matchInfo(matchInfo: MatchInfo) -> Self { + open func matchInfo(_ matchInfo: MatchInfo) -> Self { self.matchInfo = matchInfo return self } /// [FTS4 options](https://www.sqlite.org/fts3.html#fts4_options) - public func order(order: Order) -> Self { + open func order(_ order: Order) -> Self { self.order = order return self } @@ -332,11 +332,11 @@ public class FTS4Config : FTSConfig { for (column, _) in (columnDefinitions.filter { $0.options.contains(.unindexed) }) { options.append("notindexed", value: column) } - options.append("languageid", value: languageId) - options.append("compress", value: compressFunction) - options.append("uncompress", value: uncompressFunction) - options.append("matchinfo", value: matchInfo) - options.append("order", value: order) + _ = options.append("languageid", value: languageId) + _ = options.append("compress", value: compressFunction) + _ = options.append("uncompress", value: uncompressFunction) + _ = options.append("matchinfo", value: matchInfo) + _ = options.append("order", value: order) return options } } diff --git a/SQLite/Extensions/FTS5.swift b/SQLite/Extensions/FTS5.swift index 97056819..9204d5b2 100644 --- a/SQLite/Extensions/FTS5.swift +++ b/SQLite/Extensions/FTS5.swift @@ -23,7 +23,7 @@ // extension Module { - @warn_unused_result public static func FTS5(config: FTS5Config) -> Module { + public static func FTS5(_ config: FTS5Config) -> Module { return Module(name: "fts5", arguments: config.arguments()) } } @@ -32,20 +32,20 @@ extension Module { /// /// **Note:** this is currently only applicable when using SQLite.swift together with a FTS5-enabled version /// of SQLite. -public class FTS5Config : FTSConfig { +open class FTS5Config : FTSConfig { public enum Detail : CustomStringConvertible { /// store rowid, column number, term offset - case Full + case full /// store rowid, column number - case Column + case column /// store rowid - case None + case none public var description: String { switch self { - case Full: return "full" - case Column: return "column" - case None: return "none" + case .full: return "full" + case .column: return "column" + case .none: return "none" } } } @@ -58,30 +58,30 @@ public class FTS5Config : FTSConfig { } /// [External Content Tables](https://www.sqlite.org/fts5.html#section_4_4_2) - public func contentRowId(column: Expressible) -> Self { + open func contentRowId(_ column: Expressible) -> Self { self.contentRowId = column return self } /// [The Columnsize Option](https://www.sqlite.org/fts5.html#section_4_5) - public func columnSize(size: Int) -> Self { + open func columnSize(_ size: Int) -> Self { self.columnSize = size return self } /// [The Detail Option](https://www.sqlite.org/fts5.html#section_4_6) - public func detail(detail: Detail) -> Self { + open func detail(_ detail: Detail) -> Self { self.detail = detail return self } override func options() -> Options { var options = super.options() - options.append("content_rowid", value: contentRowId) + _ = options.append("content_rowid", value: contentRowId) if let columnSize = columnSize { - options.append("columnsize", value: Expression(value: columnSize)) + _ = options.append("columnsize", value: Expression(value: columnSize)) } - options.append("detail", value: detail) + _ = options.append("detail", value: detail) return options } diff --git a/SQLite/Extensions/RTree.swift b/SQLite/Extensions/RTree.swift index a5571ea6..4fc1a235 100644 --- a/SQLite/Extensions/RTree.swift +++ b/SQLite/Extensions/RTree.swift @@ -24,11 +24,11 @@ extension Module { - @warn_unused_result public static func RTree(primaryKey: Expression, _ pairs: (Expression, Expression)...) -> Module { + public static func RTree(_ primaryKey: Expression, _ pairs: (Expression, Expression)...) -> Module where T.Datatype == Int64, U.Datatype == Double { var arguments: [Expressible] = [primaryKey] for pair in pairs { - arguments.appendContentsOf([pair.0, pair.1] as [Expressible]) + arguments.append(contentsOf: [pair.0, pair.1] as [Expressible]) } return Module(name: "rtree", arguments: arguments) diff --git a/SQLite/Foundation.swift b/SQLite/Foundation.swift index 52b2ea02..5c266a05 100644 --- a/SQLite/Foundation.swift +++ b/SQLite/Foundation.swift @@ -24,34 +24,36 @@ import Foundation -extension NSData : Value { +extension Data : Value { - public class var declaredDatatype: String { + public static var declaredDatatype: String { return Blob.declaredDatatype } - public class func fromDatatypeValue(dataValue: Blob) -> NSData { - return NSData(bytes: dataValue.bytes, length: dataValue.bytes.count) + public static func fromDatatypeValue(_ dataValue: Blob) -> Data { + return Data(bytes: UnsafePointer(dataValue.bytes), count: dataValue.bytes.count) } public var datatypeValue: Blob { - return Blob(bytes: bytes, length: length) + return withUnsafeBytes { ptr -> Blob in + return Blob(bytes: ptr, length: count) + } } } -extension NSDate : Value { +extension Date : Value { - public class var declaredDatatype: String { + public static var declaredDatatype: String { return String.declaredDatatype } - public class func fromDatatypeValue(stringValue: String) -> NSDate { - return dateFormatter.dateFromString(stringValue)! + public static func fromDatatypeValue(_ stringValue: String) -> Date { + return dateFormatter.date(from: stringValue)! } public var datatypeValue: String { - return dateFormatter.stringFromDate(self) + return dateFormatter.string(from: self) } } @@ -59,11 +61,11 @@ extension NSDate : Value { /// A global date formatter used to serialize and deserialize `NSDate` objects. /// If multiple date formats are used in an application’s database(s), use a /// custom `Value` type per additional format. -public var dateFormatter: NSDateFormatter = { - let formatter = NSDateFormatter() +public var dateFormatter: DateFormatter = { + let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS" - formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") - formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0) + formatter.locale = Locale(identifier: "en_US_POSIX") + formatter.timeZone = TimeZone(secondsFromGMT: 0) return formatter }() @@ -71,17 +73,17 @@ public var dateFormatter: NSDateFormatter = { extension QueryType { - public subscript(column: Expression) -> Expression { + public subscript(column: Expression) -> Expression { return namespace(column) } - public subscript(column: Expression) -> Expression { + public subscript(column: Expression) -> Expression { return namespace(column) } - public subscript(column: Expression) -> Expression { + public subscript(column: Expression) -> Expression { return namespace(column) } - public subscript(column: Expression) -> Expression { + public subscript(column: Expression) -> Expression { return namespace(column) } @@ -89,17 +91,17 @@ extension QueryType { extension Row { - public subscript(column: Expression) -> NSData { + public subscript(column: Expression) -> Data { return get(column) } - public subscript(column: Expression) -> NSData? { + public subscript(column: Expression) -> Data? { return get(column) } - public subscript(column: Expression) -> NSDate { + public subscript(column: Expression) -> Date { return get(column) } - public subscript(column: Expression) -> NSDate? { + public subscript(column: Expression) -> Date? { return get(column) } diff --git a/SQLite/Helpers.swift b/SQLite/Helpers.swift index cc6da27c..e9a17ce6 100644 --- a/SQLite/Helpers.swift +++ b/SQLite/Helpers.swift @@ -47,28 +47,28 @@ extension Optional : _OptionalType { } // let SQLITE_STATIC = unsafeBitCast(0, sqlite3_destructor_type.self) -let SQLITE_TRANSIENT = unsafeBitCast(-1, sqlite3_destructor_type.self) +let SQLITE_TRANSIENT = unsafeBitCast(-1, to: sqlite3_destructor_type.self) extension String { - @warn_unused_result func quote(mark: Character = "\"") -> String { + func quote(_ mark: Character = "\"") -> String { let escaped = characters.reduce("") { string, character in string + (character == mark ? "\(mark)\(mark)" : "\(character)") } return "\(mark)\(escaped)\(mark)" } - @warn_unused_result func join(expressions: [Expressible]) -> Expressible { + func join(_ expressions: [Expressible]) -> Expressible { var (template, bindings) = ([String](), [Binding?]()) for expressible in expressions { let expression = expressible.expression template.append(expression.template) - bindings.appendContentsOf(expression.bindings) + bindings.append(contentsOf: expression.bindings) } - return Expression(template.joinWithSeparator(self), bindings) + return Expression(template.joined(separator: self), bindings) } - @warn_unused_result func infix(lhs: Expressible, _ rhs: Expressible, wrap: Bool = true) -> Expression { + func infix(_ lhs: Expressible, _ rhs: Expressible, wrap: Bool = true) -> Expression { let expression = Expression(" \(self) ".join([lhs, rhs]).expression) guard wrap else { return expression @@ -76,37 +76,37 @@ extension String { return "".wrap(expression) } - @warn_unused_result func prefix(expressions: Expressible) -> Expressible { + func prefix(_ expressions: Expressible) -> Expressible { return "\(self) ".wrap(expressions) as Expression } - @warn_unused_result func prefix(expressions: [Expressible]) -> Expressible { + func prefix(_ expressions: [Expressible]) -> Expressible { return "\(self) ".wrap(expressions) as Expression } - @warn_unused_result func wrap(expression: Expressible) -> Expression { + func wrap(_ expression: Expressible) -> Expression { return Expression("\(self)(\(expression.expression.template))", expression.expression.bindings) } - @warn_unused_result func wrap(expressions: [Expressible]) -> Expression { + func wrap(_ expressions: [Expressible]) -> Expression { return wrap(", ".join(expressions)) } } -@warn_unused_result func infix(lhs: Expressible, _ rhs: Expressible, wrap: Bool = true, function: String = #function) -> Expression { +func infix(_ lhs: Expressible, _ rhs: Expressible, wrap: Bool = true, function: String = #function) -> Expression { return function.infix(lhs, rhs, wrap: wrap) } -@warn_unused_result func wrap(expression: Expressible, function: String = #function) -> Expression { +func wrap(_ expression: Expressible, function: String = #function) -> Expression { return function.wrap(expression) } -@warn_unused_result func wrap(expressions: [Expressible], function: String = #function) -> Expression { +func wrap(_ expressions: [Expressible], function: String = #function) -> Expression { return function.wrap(", ".join(expressions)) } -@warn_unused_result func transcode(literal: Binding?) -> String { +func transcode(_ literal: Binding?) -> String { guard let literal = literal else { return "NULL" } switch literal { @@ -119,10 +119,10 @@ extension String { } } -@warn_unused_result func value(v: Binding) -> A { +func value(_ v: Binding) -> A { return A.fromDatatypeValue(v as! A.Datatype) as! A } -@warn_unused_result func value(v: Binding?) -> A { +func value(_ v: Binding?) -> A { return value(v!) } diff --git a/SQLite/Typed/AggregateFunctions.swift b/SQLite/Typed/AggregateFunctions.swift index 5775e0fe..249bbe60 100644 --- a/SQLite/Typed/AggregateFunctions.swift +++ b/SQLite/Typed/AggregateFunctions.swift @@ -232,7 +232,7 @@ extension ExpressionType where UnderlyingType : _OptionalType, UnderlyingType.Wr extension ExpressionType where UnderlyingType == Int { - @warn_unused_result static func count(star: Star) -> Expression { + static func count(_ star: Star) -> Expression { return wrap(star(nil, nil)) } @@ -246,6 +246,6 @@ extension ExpressionType where UnderlyingType == Int { /// /// - Returns: An expression returning `count(*)` (when called with the `*` /// function literal). -@warn_unused_result public func count(star: Star) -> Expression { +public func count(_ star: Star) -> Expression { return Expression.count(star) } diff --git a/SQLite/Typed/Collation.swift b/SQLite/Typed/Collation.swift index 5a632055..e2ff9d10 100644 --- a/SQLite/Typed/Collation.swift +++ b/SQLite/Typed/Collation.swift @@ -28,18 +28,18 @@ public enum Collation { /// Compares string by raw data. - case Binary + case binary /// Like binary, but folds uppercase ASCII letters into their lowercase /// equivalents. - case Nocase + case nocase /// Like binary, but strips trailing space. - case Rtrim + case rtrim /// A custom collating sequence identified by the given string, registered /// using `Database.create(collation:…)` - case Custom(String) + case custom(String) } @@ -55,13 +55,13 @@ extension Collation : CustomStringConvertible { public var description : String { switch self { - case Binary: + case .binary: return "BINARY" - case Nocase: + case .nocase: return "NOCASE" - case Rtrim: + case .rtrim: return "RTRIM" - case Custom(let collation): + case .custom(let collation): return collation.quote() } } diff --git a/SQLite/Typed/CoreFunctions.swift b/SQLite/Typed/CoreFunctions.swift index b597f3ba..9d17a326 100644 --- a/SQLite/Typed/CoreFunctions.swift +++ b/SQLite/Typed/CoreFunctions.swift @@ -66,7 +66,7 @@ extension ExpressionType where UnderlyingType == Double { /// // round("salary", 2) /// /// - Returns: A copy of the expression wrapped with the `round` function. - @warn_unused_result public func round(precision: Int? = nil) -> Expression { + public func round(_ precision: Int? = nil) -> Expression { guard let precision = precision else { return wrap([self]) } @@ -86,7 +86,7 @@ extension ExpressionType where UnderlyingType == Double? { /// // round("salary", 2) /// /// - Returns: A copy of the expression wrapped with the `round` function. - @warn_unused_result public func round(precision: Int? = nil) -> Expression { + public func round(_ precision: Int? = nil) -> Expression { guard let precision = precision else { return wrap(self) } @@ -103,13 +103,13 @@ extension ExpressionType where UnderlyingType : Value, UnderlyingType.Datatype = /// // random() /// /// - Returns: An expression calling the `random` function. - @warn_unused_result public static func random() -> Expression { + public static func random() -> Expression { return "random".wrap([]) } } -extension ExpressionType where UnderlyingType == NSData { +extension ExpressionType where UnderlyingType == Data { /// Builds an expression representing the `randomblob` function. /// @@ -119,7 +119,7 @@ extension ExpressionType where UnderlyingType == NSData { /// - Parameter length: Length in bytes. /// /// - Returns: An expression calling the `randomblob` function. - @warn_unused_result public static func random(length: Int) -> Expression { + public static func random(_ length: Int) -> Expression { return "randomblob".wrap([]) } @@ -131,7 +131,7 @@ extension ExpressionType where UnderlyingType == NSData { /// - Parameter length: Length in bytes. /// /// - Returns: An expression calling the `zeroblob` function. - @warn_unused_result public static func allZeros(length: Int) -> Expression { + public static func allZeros(_ length: Int) -> Expression { return "zeroblob".wrap([]) } @@ -148,7 +148,7 @@ extension ExpressionType where UnderlyingType == NSData { } -extension ExpressionType where UnderlyingType == NSData? { +extension ExpressionType where UnderlyingType == Data? { /// Builds a copy of the expression wrapped with the `length` function. /// @@ -216,7 +216,7 @@ extension ExpressionType where UnderlyingType == String { /// /// - Returns: A copy of the expression appended with a `LIKE` query against /// the given pattern. - @warn_unused_result public func like(pattern: String, escape character: Character? = nil) -> Expression { + public func like(_ pattern: String, escape character: Character? = nil) -> Expression { guard let character = character else { return "LIKE".infix(self, pattern) } @@ -234,7 +234,7 @@ extension ExpressionType where UnderlyingType == String { /// /// - Returns: A copy of the expression appended with a `GLOB` query against /// the given pattern. - @warn_unused_result public func glob(pattern: String) -> Expression { + public func glob(_ pattern: String) -> Expression { return "GLOB".infix(self, pattern) } @@ -249,7 +249,7 @@ extension ExpressionType where UnderlyingType == String { /// /// - Returns: A copy of the expression appended with a `MATCH` query /// against the given pattern. - @warn_unused_result public func match(pattern: String) -> Expression { + public func match(_ pattern: String) -> Expression { return "MATCH".infix(self, pattern) } @@ -260,7 +260,7 @@ extension ExpressionType where UnderlyingType == String { /// /// - Returns: A copy of the expression appended with a `REGEXP` query /// against the given pattern. - @warn_unused_result public func regexp(pattern: String) -> Expression { + public func regexp(_ pattern: String) -> Expression { return "REGEXP".infix(self, pattern) } @@ -275,7 +275,7 @@ extension ExpressionType where UnderlyingType == String { /// /// - Returns: A copy of the expression appended with a `COLLATE` clause /// with the given sequence. - @warn_unused_result public func collate(collation: Collation) -> Expression { + public func collate(_ collation: Collation) -> Expression { return "COLLATE".infix(self, collation) } @@ -290,7 +290,7 @@ extension ExpressionType where UnderlyingType == String { /// - Parameter characters: A set of characters to trim. /// /// - Returns: A copy of the expression wrapped with the `ltrim` function. - @warn_unused_result public func ltrim(characters: Set? = nil) -> Expression { + public func ltrim(_ characters: Set? = nil) -> Expression { guard let characters = characters else { return wrap(self) } @@ -308,7 +308,7 @@ extension ExpressionType where UnderlyingType == String { /// - Parameter characters: A set of characters to trim. /// /// - Returns: A copy of the expression wrapped with the `rtrim` function. - @warn_unused_result public func rtrim(characters: Set? = nil) -> Expression { + public func rtrim(_ characters: Set? = nil) -> Expression { guard let characters = characters else { return wrap(self) } @@ -326,7 +326,7 @@ extension ExpressionType where UnderlyingType == String { /// - Parameter characters: A set of characters to trim. /// /// - Returns: A copy of the expression wrapped with the `trim` function. - @warn_unused_result public func trim(characters: Set? = nil) -> Expression { + public func trim(_ characters: Set? = nil) -> Expression { guard let characters = characters else { return wrap([self]) } @@ -346,11 +346,11 @@ extension ExpressionType where UnderlyingType == String { /// - replacement: The replacement string. /// /// - Returns: A copy of the expression wrapped with the `replace` function. - @warn_unused_result public func replace(pattern: String, with replacement: String) -> Expression { + public func replace(_ pattern: String, with replacement: String) -> Expression { return "replace".wrap([self, pattern, replacement]) } - @warn_unused_result public func substring(location: Int, length: Int? = nil) -> Expression { + public func substring(_ location: Int, length: Int? = nil) -> Expression { guard let length = length else { return "substr".wrap([self, location]) } @@ -358,7 +358,7 @@ extension ExpressionType where UnderlyingType == String { } public subscript(range: Range) -> Expression { - return substring(range.startIndex, length: range.endIndex - range.startIndex) + return substring(range.lowerBound, length: range.upperBound - range.lowerBound) } } @@ -416,7 +416,7 @@ extension ExpressionType where UnderlyingType == String? { /// /// - Returns: A copy of the expression appended with a `LIKE` query against /// the given pattern. - @warn_unused_result public func like(pattern: String, escape character: Character? = nil) -> Expression { + public func like(_ pattern: String, escape character: Character? = nil) -> Expression { guard let character = character else { return "LIKE".infix(self, pattern) } @@ -434,7 +434,7 @@ extension ExpressionType where UnderlyingType == String? { /// /// - Returns: A copy of the expression appended with a `GLOB` query against /// the given pattern. - @warn_unused_result public func glob(pattern: String) -> Expression { + public func glob(_ pattern: String) -> Expression { return "GLOB".infix(self, pattern) } @@ -449,7 +449,7 @@ extension ExpressionType where UnderlyingType == String? { /// /// - Returns: A copy of the expression appended with a `MATCH` query /// against the given pattern. - @warn_unused_result public func match(pattern: String) -> Expression { + public func match(_ pattern: String) -> Expression { return "MATCH".infix(self, pattern) } @@ -460,7 +460,7 @@ extension ExpressionType where UnderlyingType == String? { /// /// - Returns: A copy of the expression appended with a `REGEXP` query /// against the given pattern. - @warn_unused_result public func regexp(pattern: String) -> Expression { + public func regexp(_ pattern: String) -> Expression { return "REGEXP".infix(self, pattern) } @@ -475,7 +475,7 @@ extension ExpressionType where UnderlyingType == String? { /// /// - Returns: A copy of the expression appended with a `COLLATE` clause /// with the given sequence. - @warn_unused_result public func collate(collation: Collation) -> Expression { + public func collate(_ collation: Collation) -> Expression { return "COLLATE".infix(self, collation) } @@ -490,7 +490,7 @@ extension ExpressionType where UnderlyingType == String? { /// - Parameter characters: A set of characters to trim. /// /// - Returns: A copy of the expression wrapped with the `ltrim` function. - @warn_unused_result public func ltrim(characters: Set? = nil) -> Expression { + public func ltrim(_ characters: Set? = nil) -> Expression { guard let characters = characters else { return wrap(self) } @@ -508,7 +508,7 @@ extension ExpressionType where UnderlyingType == String? { /// - Parameter characters: A set of characters to trim. /// /// - Returns: A copy of the expression wrapped with the `rtrim` function. - @warn_unused_result public func rtrim(characters: Set? = nil) -> Expression { + public func rtrim(_ characters: Set? = nil) -> Expression { guard let characters = characters else { return wrap(self) } @@ -526,7 +526,7 @@ extension ExpressionType where UnderlyingType == String? { /// - Parameter characters: A set of characters to trim. /// /// - Returns: A copy of the expression wrapped with the `trim` function. - @warn_unused_result public func trim(characters: Set? = nil) -> Expression { + public func trim(_ characters: Set? = nil) -> Expression { guard let characters = characters else { return wrap(self) } @@ -546,7 +546,7 @@ extension ExpressionType where UnderlyingType == String? { /// - replacement: The replacement string. /// /// - Returns: A copy of the expression wrapped with the `replace` function. - @warn_unused_result public func replace(pattern: String, with replacement: String) -> Expression { + public func replace(_ pattern: String, with replacement: String) -> Expression { return "replace".wrap([self, pattern, replacement]) } @@ -565,7 +565,7 @@ extension ExpressionType where UnderlyingType == String? { /// - length: An optional substring length. /// /// - Returns: A copy of the expression wrapped with the `substr` function. - @warn_unused_result public func substring(location: Int, length: Int? = nil) -> Expression { + public func substring(_ location: Int, length: Int? = nil) -> Expression { guard let length = length else { return "substr".wrap([self, location]) } @@ -582,12 +582,12 @@ extension ExpressionType where UnderlyingType == String? { /// /// - Returns: A copy of the expression wrapped with the `substr` function. public subscript(range: Range) -> Expression { - return substring(range.startIndex, length: range.endIndex - range.startIndex) + return substring(range.lowerBound, length: range.upperBound - range.lowerBound) } } -extension CollectionType where Generator.Element : Value, Index.Distance == Int { +extension Collection where Iterator.Element : Value, IndexDistance == Int { /// Builds a copy of the expression prepended with an `IN` check against the /// collection. @@ -600,8 +600,8 @@ extension CollectionType where Generator.Element : Value, Index.Distance == Int /// /// - Returns: A copy of the expression prepended with an `IN` check against /// the collection. - @warn_unused_result public func contains(expression: Expression) -> Expression { - let templates = [String](count: count, repeatedValue: "?").joinWithSeparator(", ") + public func contains(_ expression: Expression) -> Expression { + let templates = [String](repeating: "?", count: count).joined(separator: ", ") return "IN".infix(expression, Expression("(\(templates))", map { $0.datatypeValue })) } @@ -616,8 +616,8 @@ extension CollectionType where Generator.Element : Value, Index.Distance == Int /// /// - Returns: A copy of the expression prepended with an `IN` check against /// the collection. - @warn_unused_result public func contains(expression: Expression) -> Expression { - let templates = [String](count: count, repeatedValue: "?").joinWithSeparator(", ") + public func contains(_ expression: Expression) -> Expression { + let templates = [String](repeating: "?", count: count).joined(separator: ", ") return "IN".infix(expression, Expression("(\(templates))", map { $0.datatypeValue })) } diff --git a/SQLite/Typed/CustomFunctions.swift b/SQLite/Typed/CustomFunctions.swift index 068d0340..64b54edb 100644 --- a/SQLite/Typed/CustomFunctions.swift +++ b/SQLite/Typed/CustomFunctions.swift @@ -39,83 +39,83 @@ public extension Connection { /// The assigned types must be explicit. /// /// - Returns: A closure returning an SQL expression to call the function. - public func createFunction(function: String, deterministic: Bool = false, _ block: () -> Z) throws -> (() -> Expression) { + public func createFunction(_ function: String, deterministic: Bool = false, _ block: @escaping () -> Z) throws -> (() -> Expression) { let fn = try createFunction(function, 0, deterministic) { _ in block() } return { fn([]) } } - public func createFunction(function: String, deterministic: Bool = false, _ block: () -> Z?) throws -> (() -> Expression) { + public func createFunction(_ function: String, deterministic: Bool = false, _ block: @escaping () -> Z?) throws -> (() -> Expression) { let fn = try createFunction(function, 0, deterministic) { _ in block() } return { fn([]) } } // MARK: - - public func createFunction(function: String, deterministic: Bool = false, _ block: A -> Z) throws -> (Expression -> Expression) { + public func createFunction(_ function: String, deterministic: Bool = false, _ block: @escaping (A) -> Z) throws -> ((Expression) -> Expression) { let fn = try createFunction(function, 1, deterministic) { args in block(value(args[0])) } return { arg in fn([arg]) } } - public func createFunction(function function: String, deterministic: Bool = false, _ block: A? -> Z) throws -> (Expression -> Expression) { + public func createFunction(function: String, deterministic: Bool = false, _ block: @escaping (A?) -> Z) throws -> ((Expression) -> Expression) { let fn = try createFunction(function, 1, deterministic) { args in block(args[0].map(value)) } return { arg in fn([arg]) } } - public func createFunction(function function: String, deterministic: Bool = false, _ block: A -> Z?) throws -> (Expression -> Expression) { + public func createFunction(function: String, deterministic: Bool = false, _ block: @escaping (A) -> Z?) throws -> ((Expression) -> Expression) { let fn = try createFunction(function, 1, deterministic) { args in block(value(args[0])) } return { arg in fn([arg]) } } - public func createFunction(function function: String, deterministic: Bool = false, _ block: A? -> Z?) throws -> (Expression -> Expression) { + public func createFunction(function: String, deterministic: Bool = false, _ block: @escaping (A?) -> Z?) throws -> ((Expression) -> Expression) { let fn = try createFunction(function, 1, deterministic) { args in block(args[0].map(value)) } return { arg in fn([arg]) } } // MARK: - - public func createFunction(function: String, deterministic: Bool = false, _ block: (A, B) -> Z) throws -> (Expression, Expression) -> Expression { + public func createFunction(_ function: String, deterministic: Bool = false, _ block: @escaping (A, B) -> Z) throws -> (Expression, Expression) -> Expression { let fn = try createFunction(function, 1, deterministic) { args in block(value(args[0]), value(args[1])) } return { a, b in fn([a, b]) } } - public func createFunction(function: String, deterministic: Bool = false, _ block: (A?, B) -> Z) throws -> (Expression, Expression) -> Expression { + public func createFunction(_ function: String, deterministic: Bool = false, _ block: @escaping (A?, B) -> Z) throws -> (Expression, Expression) -> Expression { let fn = try createFunction(function, 1, deterministic) { args in block(args[0].map(value), value(args[1])) } return { a, b in fn([a, b]) } } - public func createFunction(function: String, deterministic: Bool = false, _ block: (A, B?) -> Z) throws -> (Expression, Expression) -> Expression { + public func createFunction(_ function: String, deterministic: Bool = false, _ block: @escaping (A, B?) -> Z) throws -> (Expression, Expression) -> Expression { let fn = try createFunction(function, 1, deterministic) { args in block(value(args[0]), args[1].map(value)) } return { a, b in fn([a, b]) } } - public func createFunction(function: String, deterministic: Bool = false, _ block: (A, B) -> Z?) throws -> (Expression, Expression) -> Expression { + public func createFunction(_ function: String, deterministic: Bool = false, _ block: @escaping (A, B) -> Z?) throws -> (Expression, Expression) -> Expression { let fn = try createFunction(function, 1, deterministic) { args in block(value(args[0]), value(args[1])) } return { a, b in fn([a, b]) } } - public func createFunction(function: String, deterministic: Bool = false, _ block: (A?, B?) -> Z) throws -> (Expression, Expression) -> Expression { + public func createFunction(_ function: String, deterministic: Bool = false, _ block: @escaping (A?, B?) -> Z) throws -> (Expression, Expression) -> Expression { let fn = try createFunction(function, 1, deterministic) { args in block(args[0].map(value), args[1].map(value)) } return { a, b in fn([a, b]) } } - public func createFunction(function: String, deterministic: Bool = false, _ block: (A?, B) -> Z?) throws -> (Expression, Expression) -> Expression { + public func createFunction(_ function: String, deterministic: Bool = false, _ block: @escaping (A?, B) -> Z?) throws -> (Expression, Expression) -> Expression { let fn = try createFunction(function, 1, deterministic) { args in block(args[0].map(value), value(args[1])) } return { a, b in fn([a, b]) } } - public func createFunction(function: String, deterministic: Bool = false, _ block: (A, B?) -> Z?) throws -> (Expression, Expression) -> Expression { + public func createFunction(_ function: String, deterministic: Bool = false, _ block: @escaping (A, B?) -> Z?) throws -> (Expression, Expression) -> Expression { let fn = try createFunction(function, 1, deterministic) { args in block(value(args[0]), args[1].map(value)) } return { a, b in fn([a, b]) } } - public func createFunction(function: String, deterministic: Bool = false, _ block: (A?, B?) -> Z?) throws -> (Expression, Expression) -> Expression { + public func createFunction(_ function: String, deterministic: Bool = false, _ block: @escaping (A?, B?) -> Z?) throws -> (Expression, Expression) -> Expression { let fn = try createFunction(function, 1, deterministic) { args in block(args[0].map(value), args[1].map(value)) } return { a, b in fn([a, b]) } } // MARK: - - private func createFunction(function: String, _ argumentCount: UInt, _ deterministic: Bool, _ block: [Binding?] -> Z) throws -> ([Expressible] -> Expression) { + fileprivate func createFunction(_ function: String, _ argumentCount: UInt, _ deterministic: Bool, _ block: @escaping ([Binding?]) -> Z) throws -> (([Expressible]) -> Expression) { createFunction(function, argumentCount: argumentCount, deterministic: deterministic) { arguments in block(arguments).datatypeValue } @@ -124,7 +124,7 @@ public extension Connection { } } - private func createFunction(function: String, _ argumentCount: UInt, _ deterministic: Bool, _ block: [Binding?] -> Z?) throws -> ([Expressible] -> Expression) { + fileprivate func createFunction(_ function: String, _ argumentCount: UInt, _ deterministic: Bool, _ block: @escaping ([Binding?]) -> Z?) throws -> (([Expressible]) -> Expression) { createFunction(function, argumentCount: argumentCount, deterministic: deterministic) { arguments in block(arguments)?.datatypeValue } diff --git a/SQLite/Typed/Expression.swift b/SQLite/Typed/Expression.swift index 2e71b970..3198901c 100644 --- a/SQLite/Typed/Expression.swift +++ b/SQLite/Typed/Expression.swift @@ -138,10 +138,10 @@ extension Value { public let rowid = Expression("ROWID") -public func cast(expression: Expression) -> Expression { +public func cast(_ expression: Expression) -> Expression { return Expression("CAST (\(expression.template) AS \(U.declaredDatatype))", expression.bindings) } -public func cast(expression: Expression) -> Expression { +public func cast(_ expression: Expression) -> Expression { return Expression("CAST (\(expression.template) AS \(U.declaredDatatype))", expression.bindings) } diff --git a/SQLite/Typed/Operators.swift b/SQLite/Typed/Operators.swift index 816560a5..616015dc 100644 --- a/SQLite/Typed/Operators.swift +++ b/SQLite/Typed/Operators.swift @@ -52,433 +52,433 @@ public func +(lhs: String, rhs: Expression) -> Expression { // MARK: - -public func +(lhs: Expression, rhs: Expression) -> Expression { +public func +(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func +(lhs: Expression, rhs: Expression) -> Expression { +public func +(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func +(lhs: Expression, rhs: Expression) -> Expression { +public func +(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func +(lhs: Expression, rhs: Expression) -> Expression { +public func +(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func +(lhs: Expression, rhs: V) -> Expression { +public func +(lhs: Expression, rhs: V) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func +(lhs: Expression, rhs: V) -> Expression { +public func +(lhs: Expression, rhs: V) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func +(lhs: V, rhs: Expression) -> Expression { +public func +(lhs: V, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func +(lhs: V, rhs: Expression) -> Expression { +public func +(lhs: V, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func -(lhs: Expression, rhs: Expression) -> Expression { +public func -(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func -(lhs: Expression, rhs: Expression) -> Expression { +public func -(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func -(lhs: Expression, rhs: Expression) -> Expression { +public func -(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func -(lhs: Expression, rhs: Expression) -> Expression { +public func -(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func -(lhs: Expression, rhs: V) -> Expression { +public func -(lhs: Expression, rhs: V) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func -(lhs: Expression, rhs: V) -> Expression { +public func -(lhs: Expression, rhs: V) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func -(lhs: V, rhs: Expression) -> Expression { +public func -(lhs: V, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func -(lhs: V, rhs: Expression) -> Expression { +public func -(lhs: V, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func *(lhs: Expression, rhs: Expression) -> Expression { +public func *(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func *(lhs: Expression, rhs: Expression) -> Expression { +public func *(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func *(lhs: Expression, rhs: Expression) -> Expression { +public func *(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func *(lhs: Expression, rhs: Expression) -> Expression { +public func *(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func *(lhs: Expression, rhs: V) -> Expression { +public func *(lhs: Expression, rhs: V) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func *(lhs: Expression, rhs: V) -> Expression { +public func *(lhs: Expression, rhs: V) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func *(lhs: V, rhs: Expression) -> Expression { +public func *(lhs: V, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func *(lhs: V, rhs: Expression) -> Expression { +public func *(lhs: V, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func /(lhs: Expression, rhs: Expression) -> Expression { +public func /(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func /(lhs: Expression, rhs: Expression) -> Expression { +public func /(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func /(lhs: Expression, rhs: Expression) -> Expression { +public func /(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func /(lhs: Expression, rhs: Expression) -> Expression { +public func /(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func /(lhs: Expression, rhs: V) -> Expression { +public func /(lhs: Expression, rhs: V) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func /(lhs: Expression, rhs: V) -> Expression { +public func /(lhs: Expression, rhs: V) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func /(lhs: V, rhs: Expression) -> Expression { +public func /(lhs: V, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public func /(lhs: V, rhs: Expression) -> Expression { +public func /(lhs: V, rhs: Expression) -> Expression where V.Datatype : Number { return infix(lhs, rhs) } -public prefix func -(rhs: Expression) -> Expression { +public prefix func -(rhs: Expression) -> Expression where V.Datatype : Number { return wrap(rhs) } -public prefix func -(rhs: Expression) -> Expression { +public prefix func -(rhs: Expression) -> Expression where V.Datatype : Number { return wrap(rhs) } // MARK: - -public func %(lhs: Expression, rhs: Expression) -> Expression { +public func %(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func %(lhs: Expression, rhs: Expression) -> Expression { +public func %(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func %(lhs: Expression, rhs: Expression) -> Expression { +public func %(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func %(lhs: Expression, rhs: Expression) -> Expression { +public func %(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func %(lhs: Expression, rhs: V) -> Expression { +public func %(lhs: Expression, rhs: V) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func %(lhs: Expression, rhs: V) -> Expression { +public func %(lhs: Expression, rhs: V) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func %(lhs: V, rhs: Expression) -> Expression { +public func %(lhs: V, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func %(lhs: V, rhs: Expression) -> Expression { +public func %(lhs: V, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func <<(lhs: Expression, rhs: Expression) -> Expression { +public func <<(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func <<(lhs: Expression, rhs: Expression) -> Expression { +public func <<(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func <<(lhs: Expression, rhs: Expression) -> Expression { +public func <<(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func <<(lhs: Expression, rhs: Expression) -> Expression { +public func <<(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func <<(lhs: Expression, rhs: V) -> Expression { +public func <<(lhs: Expression, rhs: V) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func <<(lhs: Expression, rhs: V) -> Expression { +public func <<(lhs: Expression, rhs: V) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func <<(lhs: V, rhs: Expression) -> Expression { +public func <<(lhs: V, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func <<(lhs: V, rhs: Expression) -> Expression { +public func <<(lhs: V, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func >>(lhs: Expression, rhs: Expression) -> Expression { +public func >>(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func >>(lhs: Expression, rhs: Expression) -> Expression { +public func >>(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func >>(lhs: Expression, rhs: Expression) -> Expression { +public func >>(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func >>(lhs: Expression, rhs: Expression) -> Expression { +public func >>(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func >>(lhs: Expression, rhs: V) -> Expression { +public func >>(lhs: Expression, rhs: V) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func >>(lhs: Expression, rhs: V) -> Expression { +public func >>(lhs: Expression, rhs: V) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func >>(lhs: V, rhs: Expression) -> Expression { +public func >>(lhs: V, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func >>(lhs: V, rhs: Expression) -> Expression { +public func >>(lhs: V, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func &(lhs: Expression, rhs: Expression) -> Expression { +public func &(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func &(lhs: Expression, rhs: Expression) -> Expression { +public func &(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func &(lhs: Expression, rhs: Expression) -> Expression { +public func &(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func &(lhs: Expression, rhs: Expression) -> Expression { +public func &(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func &(lhs: Expression, rhs: V) -> Expression { +public func &(lhs: Expression, rhs: V) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func &(lhs: Expression, rhs: V) -> Expression { +public func &(lhs: Expression, rhs: V) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func &(lhs: V, rhs: Expression) -> Expression { +public func &(lhs: V, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func &(lhs: V, rhs: Expression) -> Expression { +public func &(lhs: V, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func |(lhs: Expression, rhs: Expression) -> Expression { +public func |(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func |(lhs: Expression, rhs: Expression) -> Expression { +public func |(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func |(lhs: Expression, rhs: Expression) -> Expression { +public func |(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func |(lhs: Expression, rhs: Expression) -> Expression { +public func |(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func |(lhs: Expression, rhs: V) -> Expression { +public func |(lhs: Expression, rhs: V) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func |(lhs: Expression, rhs: V) -> Expression { +public func |(lhs: Expression, rhs: V) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func |(lhs: V, rhs: Expression) -> Expression { +public func |(lhs: V, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func |(lhs: V, rhs: Expression) -> Expression { +public func |(lhs: V, rhs: Expression) -> Expression where V.Datatype == Int64 { return infix(lhs, rhs) } -public func ^(lhs: Expression, rhs: Expression) -> Expression { +public func ^(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return (~(lhs & rhs)) & (lhs | rhs) } -public func ^(lhs: Expression, rhs: Expression) -> Expression { +public func ^(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return (~(lhs & rhs)) & (lhs | rhs) } -public func ^(lhs: Expression, rhs: Expression) -> Expression { +public func ^(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return (~(lhs & rhs)) & (lhs | rhs) } -public func ^(lhs: Expression, rhs: Expression) -> Expression { +public func ^(lhs: Expression, rhs: Expression) -> Expression where V.Datatype == Int64 { return (~(lhs & rhs)) & (lhs | rhs) } -public func ^(lhs: Expression, rhs: V) -> Expression { +public func ^(lhs: Expression, rhs: V) -> Expression where V.Datatype == Int64 { return (~(lhs & rhs)) & (lhs | rhs) } -public func ^(lhs: Expression, rhs: V) -> Expression { +public func ^(lhs: Expression, rhs: V) -> Expression where V.Datatype == Int64 { return (~(lhs & rhs)) & (lhs | rhs) } -public func ^(lhs: V, rhs: Expression) -> Expression { +public func ^(lhs: V, rhs: Expression) -> Expression where V.Datatype == Int64 { return (~(lhs & rhs)) & (lhs | rhs) } -public func ^(lhs: V, rhs: Expression) -> Expression { +public func ^(lhs: V, rhs: Expression) -> Expression where V.Datatype == Int64 { return (~(lhs & rhs)) & (lhs | rhs) } -public prefix func ~(rhs: Expression) -> Expression { +public prefix func ~(rhs: Expression) -> Expression where V.Datatype == Int64 { return wrap(rhs) } -public prefix func ~(rhs: Expression) -> Expression { +public prefix func ~(rhs: Expression) -> Expression where V.Datatype == Int64 { return wrap(rhs) } // MARK: - -public func ==(lhs: Expression, rhs: Expression) -> Expression { +public func ==(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Equatable { return "=".infix(lhs, rhs) } -public func ==(lhs: Expression, rhs: Expression) -> Expression { +public func ==(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Equatable { return "=".infix(lhs, rhs) } -public func ==(lhs: Expression, rhs: Expression) -> Expression { +public func ==(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Equatable { return "=".infix(lhs, rhs) } -public func ==(lhs: Expression, rhs: Expression) -> Expression { +public func ==(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Equatable { return "=".infix(lhs, rhs) } -public func ==(lhs: Expression, rhs: V) -> Expression { +public func ==(lhs: Expression, rhs: V) -> Expression where V.Datatype : Equatable { return "=".infix(lhs, rhs) } -public func ==(lhs: Expression, rhs: V?) -> Expression { +public func ==(lhs: Expression, rhs: V?) -> Expression where V.Datatype : Equatable { guard let rhs = rhs else { return "IS".infix(lhs, Expression(value: nil)) } return "=".infix(lhs, rhs) } -public func ==(lhs: V, rhs: Expression) -> Expression { +public func ==(lhs: V, rhs: Expression) -> Expression where V.Datatype : Equatable { return "=".infix(lhs, rhs) } -public func ==(lhs: V?, rhs: Expression) -> Expression { +public func ==(lhs: V?, rhs: Expression) -> Expression where V.Datatype : Equatable { guard let lhs = lhs else { return "IS".infix(Expression(value: nil), rhs) } return "=".infix(lhs, rhs) } -public func !=(lhs: Expression, rhs: Expression) -> Expression { +public func !=(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Equatable { return infix(lhs, rhs) } -public func !=(lhs: Expression, rhs: Expression) -> Expression { +public func !=(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Equatable { return infix(lhs, rhs) } -public func !=(lhs: Expression, rhs: Expression) -> Expression { +public func !=(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Equatable { return infix(lhs, rhs) } -public func !=(lhs: Expression, rhs: Expression) -> Expression { +public func !=(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Equatable { return infix(lhs, rhs) } -public func !=(lhs: Expression, rhs: V) -> Expression { +public func !=(lhs: Expression, rhs: V) -> Expression where V.Datatype : Equatable { return infix(lhs, rhs) } -public func !=(lhs: Expression, rhs: V?) -> Expression { +public func !=(lhs: Expression, rhs: V?) -> Expression where V.Datatype : Equatable { guard let rhs = rhs else { return "IS NOT".infix(lhs, Expression(value: nil)) } return infix(lhs, rhs) } -public func !=(lhs: V, rhs: Expression) -> Expression { +public func !=(lhs: V, rhs: Expression) -> Expression where V.Datatype : Equatable { return infix(lhs, rhs) } -public func !=(lhs: V?, rhs: Expression) -> Expression { +public func !=(lhs: V?, rhs: Expression) -> Expression where V.Datatype : Equatable { guard let lhs = lhs else { return "IS NOT".infix(Expression(value: nil), rhs) } return infix(lhs, rhs) } -public func >(lhs: Expression, rhs: Expression) -> Expression { +public func >(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func >(lhs: Expression, rhs: Expression) -> Expression { +public func >(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func >(lhs: Expression, rhs: Expression) -> Expression { +public func >(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func >(lhs: Expression, rhs: Expression) -> Expression { +public func >(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func >(lhs: Expression, rhs: V) -> Expression { +public func >(lhs: Expression, rhs: V) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func >(lhs: Expression, rhs: V) -> Expression { +public func >(lhs: Expression, rhs: V) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func >(lhs: V, rhs: Expression) -> Expression { +public func >(lhs: V, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func >(lhs: V, rhs: Expression) -> Expression { +public func >(lhs: V, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func >=(lhs: Expression, rhs: Expression) -> Expression { +public func >=(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func >=(lhs: Expression, rhs: Expression) -> Expression { +public func >=(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func >=(lhs: Expression, rhs: Expression) -> Expression { +public func >=(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func >=(lhs: Expression, rhs: Expression) -> Expression { +public func >=(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func >=(lhs: Expression, rhs: V) -> Expression { +public func >=(lhs: Expression, rhs: V) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func >=(lhs: Expression, rhs: V) -> Expression { +public func >=(lhs: Expression, rhs: V) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func >=(lhs: V, rhs: Expression) -> Expression { +public func >=(lhs: V, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func >=(lhs: V, rhs: Expression) -> Expression { +public func >=(lhs: V, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func <(lhs: Expression, rhs: Expression) -> Expression { +public func <(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func <(lhs: Expression, rhs: Expression) -> Expression { +public func <(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func <(lhs: Expression, rhs: Expression) -> Expression { +public func <(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func <(lhs: Expression, rhs: Expression) -> Expression { +public func <(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func <(lhs: Expression, rhs: V) -> Expression { +public func <(lhs: Expression, rhs: V) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func <(lhs: Expression, rhs: V) -> Expression { +public func <(lhs: Expression, rhs: V) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func <(lhs: V, rhs: Expression) -> Expression { +public func <(lhs: V, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func <(lhs: V, rhs: Expression) -> Expression { +public func <(lhs: V, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func <=(lhs: Expression, rhs: Expression) -> Expression { +public func <=(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func <=(lhs: Expression, rhs: Expression) -> Expression { +public func <=(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func <=(lhs: Expression, rhs: Expression) -> Expression { +public func <=(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func <=(lhs: Expression, rhs: Expression) -> Expression { +public func <=(lhs: Expression, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func <=(lhs: Expression, rhs: V) -> Expression { +public func <=(lhs: Expression, rhs: V) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func <=(lhs: Expression, rhs: V) -> Expression { +public func <=(lhs: Expression, rhs: V) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func <=(lhs: V, rhs: Expression) -> Expression { +public func <=(lhs: V, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func <=(lhs: V, rhs: Expression) -> Expression { +public func <=(lhs: V, rhs: Expression) -> Expression where V.Datatype : Comparable { return infix(lhs, rhs) } -public func ~=, V.Datatype == I.Bound>(lhs: I, rhs: Expression) -> Expression { - return Expression("\(rhs.template) BETWEEN ? AND ?", rhs.bindings + [lhs.start, lhs.end]) +public func ~=(lhs: CountableClosedRange, rhs: Expression) -> Expression where V.Datatype : Binding & Comparable { + return Expression("\(rhs.template) BETWEEN ? AND ?", rhs.bindings + [lhs.lowerBound as? Binding, lhs.upperBound as? Binding]) } -public func ~=, V.Datatype == I.Bound>(lhs: I, rhs: Expression) -> Expression { - return Expression("\(rhs.template) BETWEEN ? AND ?", rhs.bindings + [lhs.start, lhs.end]) +public func ~=(lhs: CountableClosedRange, rhs: Expression) -> Expression where V.Datatype : Binding & Comparable { + return Expression("\(rhs.template) BETWEEN ? AND ?", rhs.bindings + [lhs.lowerBound as? Binding, lhs.upperBound as? Binding]) } // MARK: - diff --git a/SQLite/Typed/Query.swift b/SQLite/Typed/Query.swift index 36b3b2e1..d0e4572d 100644 --- a/SQLite/Typed/Query.swift +++ b/SQLite/Typed/Query.swift @@ -50,7 +50,7 @@ extension SchemaType { /// - Parameter all: A list of expressions to select. /// /// - Returns: A query with the given `SELECT` clause applied. - public func select(column1: Expressible, _ more: Expressible...) -> Self { + public func select(_ column1: Expressible, _ more: Expressible...) -> Self { return select(false, [column1] + more) } @@ -81,7 +81,7 @@ extension SchemaType { /// - Parameter all: A list of expressions to select. /// /// - Returns: A query with the given `SELECT` clause applied. - public func select(all: [Expressible]) -> Self { + public func select(_ all: [Expressible]) -> Self { return select(false, all) } @@ -110,7 +110,7 @@ extension SchemaType { /// - Parameter star: A star literal. /// /// - Returns: A query with the given `SELECT *` clause applied. - public func select(star: Star) -> Self { + public func select(_ star: Star) -> Self { return select([star(nil, nil)]) } @@ -139,10 +139,10 @@ extension SchemaType { /// - Parameter all: A list of expressions to select. /// /// - Returns: A query with the given `SELECT` clause applied. - public func select(column: Expression) -> ScalarQuery { + public func select(_ column: Expression) -> ScalarQuery { return select(false, [column]) } - public func select(column: Expression) -> ScalarQuery { + public func select(_ column: Expression) -> ScalarQuery { return select(false, [column]) } @@ -173,7 +173,7 @@ extension SchemaType { extension QueryType { - private func select(distinct: Bool, _ columns: [Expressible]) -> Q { + fileprivate func select(_ distinct: Bool, _ columns: [Expressible]) -> Q { var query = Q.init(clauses.from.name, database: clauses.from.database) query.clauses = clauses query.clauses.select = (distinct, columns) @@ -199,7 +199,7 @@ extension QueryType { /// - condition: A boolean expression describing the join condition. /// /// - Returns: A query with the given `JOIN` clause applied. - public func join(table: QueryType, on condition: Expression) -> Self { + public func join(_ table: QueryType, on condition: Expression) -> Self { return join(table, on: Expression(condition)) } @@ -220,7 +220,7 @@ extension QueryType { /// - condition: A boolean expression describing the join condition. /// /// - Returns: A query with the given `JOIN` clause applied. - public func join(table: QueryType, on condition: Expression) -> Self { + public func join(_ table: QueryType, on condition: Expression) -> Self { return join(.Inner, table, on: condition) } @@ -243,7 +243,7 @@ extension QueryType { /// - condition: A boolean expression describing the join condition. /// /// - Returns: A query with the given `JOIN` clause applied. - public func join(type: JoinType, _ table: QueryType, on condition: Expression) -> Self { + public func join(_ type: JoinType, _ table: QueryType, on condition: Expression) -> Self { return join(type, table, on: Expression(condition)) } @@ -266,7 +266,7 @@ extension QueryType { /// - condition: A boolean expression describing the join condition. /// /// - Returns: A query with the given `JOIN` clause applied. - public func join(type: JoinType, _ table: QueryType, on condition: Expression) -> Self { + public func join(_ type: JoinType, _ table: QueryType, on condition: Expression) -> Self { var query = self query.clauses.join.append((type: type, query: table, condition: table.clauses.filters.map { condition && $0 } ?? condition as Expressible)) return query @@ -285,7 +285,7 @@ extension QueryType { /// - Parameter condition: A boolean expression to filter on. /// /// - Returns: A query with the given `WHERE` clause applied. - public func filter(predicate: Expression) -> Self { + public func filter(_ predicate: Expression) -> Self { return filter(Expression(predicate)) } @@ -300,7 +300,7 @@ extension QueryType { /// - Parameter condition: A boolean expression to filter on. /// /// - Returns: A query with the given `WHERE` clause applied. - public func filter(predicate: Expression) -> Self { + public func filter(_ predicate: Expression) -> Self { var query = self query.clauses.filters = query.clauses.filters.map { $0 && predicate } ?? predicate return query @@ -313,7 +313,7 @@ extension QueryType { /// - Parameter by: A list of columns to group by. /// /// - Returns: A query with the given `GROUP BY` clause applied. - public func group(by: Expressible...) -> Self { + public func group(_ by: Expressible...) -> Self { return group(by) } @@ -322,7 +322,7 @@ extension QueryType { /// - Parameter by: A list of columns to group by. /// /// - Returns: A query with the given `GROUP BY` clause applied. - public func group(by: [Expressible]) -> Self { + public func group(_ by: [Expressible]) -> Self { return group(by, nil) } @@ -335,7 +335,7 @@ extension QueryType { /// - having: A condition determining which groups are returned. /// /// - Returns: A query with the given `GROUP BY`–`HAVING` clause applied. - public func group(by: Expressible, having: Expression) -> Self { + public func group(_ by: Expressible, having: Expression) -> Self { return group([by], having: having) } @@ -348,7 +348,7 @@ extension QueryType { /// - having: A condition determining which groups are returned. /// /// - Returns: A query with the given `GROUP BY`–`HAVING` clause applied. - public func group(by: Expressible, having: Expression) -> Self { + public func group(_ by: Expressible, having: Expression) -> Self { return group([by], having: having) } @@ -361,7 +361,7 @@ extension QueryType { /// - having: A condition determining which groups are returned. /// /// - Returns: A query with the given `GROUP BY`–`HAVING` clause applied. - public func group(by: [Expressible], having: Expression) -> Self { + public func group(_ by: [Expressible], having: Expression) -> Self { return group(by, Expression(having)) } @@ -374,11 +374,11 @@ extension QueryType { /// - having: A condition determining which groups are returned. /// /// - Returns: A query with the given `GROUP BY`–`HAVING` clause applied. - public func group(by: [Expressible], having: Expression) -> Self { + public func group(_ by: [Expressible], having: Expression) -> Self { return group(by, having) } - private func group(by: [Expressible], _ having: Expression?) -> Self { + fileprivate func group(_ by: [Expressible], _ having: Expression?) -> Self { var query = self query.clauses.group = (by, having) return query @@ -398,7 +398,7 @@ extension QueryType { /// - Parameter by: An ordered list of columns and directions to sort by. /// /// - Returns: A query with the given `ORDER BY` clause applied. - public func order(by: Expressible...) -> Self { + public func order(_ by: Expressible...) -> Self { return order(by) } @@ -414,7 +414,7 @@ extension QueryType { /// - Parameter by: An ordered list of columns and directions to sort by. /// /// - Returns: A query with the given `ORDER BY` clause applied. - public func order(by: [Expressible]) -> Self { + public func order(_ by: [Expressible]) -> Self { var query = self query.clauses.order = by return query @@ -433,7 +433,7 @@ extension QueryType { /// return unlimited rows). /// /// - Returns: A query with the given LIMIT clause applied. - public func limit(length: Int?) -> Self { + public func limit(_ length: Int?) -> Self { return limit(length, nil) } @@ -451,12 +451,12 @@ extension QueryType { /// - offset: The number of rows to skip. /// /// - Returns: A query with the given LIMIT and OFFSET clauses applied. - public func limit(length: Int, offset: Int) -> Self { + public func limit(_ length: Int, offset: Int) -> Self { return limit(length, offset) } // prevents limit(nil, offset: 5) - private func limit(length: Int?, _ offset: Int?) -> Self { + fileprivate func limit(_ length: Int?, _ offset: Int?) -> Self { var query = self query.clauses.limit = length.map { ($0, offset) } return query @@ -468,7 +468,7 @@ extension QueryType { // MARK: - - private var selectClause: Expressible { + fileprivate var selectClause: Expressible { return " ".join([ Expression(literal: clauses.select.distinct ? "SELECT DISTINCT" : "SELECT"), ", ".join(clauses.select.columns), @@ -477,7 +477,7 @@ extension QueryType { ]) } - private var joinClause: Expressible? { + fileprivate var joinClause: Expressible? { guard !clauses.join.isEmpty else { return nil } @@ -492,7 +492,7 @@ extension QueryType { }) } - private var whereClause: Expressible? { + fileprivate var whereClause: Expressible? { guard let filters = clauses.filters else { return nil } @@ -503,7 +503,7 @@ extension QueryType { ]) } - private var groupByClause: Expressible? { + fileprivate var groupByClause: Expressible? { guard let group = clauses.group else { return nil } @@ -526,7 +526,7 @@ extension QueryType { ]) } - private var orderClause: Expressible? { + fileprivate var orderClause: Expressible? { guard !clauses.order.isEmpty else { return nil } @@ -537,7 +537,7 @@ extension QueryType { ]) } - private var limitOffsetClause: Expressible? { + fileprivate var limitOffsetClause: Expressible? { guard let limit = clauses.limit else { return nil } @@ -556,7 +556,7 @@ extension QueryType { // MARK: - - public func alias(aliasName: String) -> Self { + public func alias(_ aliasName: String) -> Self { var query = self query.clauses.from = (clauses.from.name, aliasName, clauses.from.database) return query @@ -566,11 +566,11 @@ extension QueryType { // // MARK: INSERT - public func insert(value: Setter, _ more: Setter...) -> Insert { + public func insert(_ value: Setter, _ more: Setter...) -> Insert { return insert([value] + more) } - public func insert(values: [Setter]) -> Insert { + public func insert(_ values: [Setter]) -> Insert { return insert(nil, values) } @@ -582,7 +582,7 @@ extension QueryType { return insert(onConflict, values) } - private func insert(or: OnConflict?, _ values: [Setter]) -> Insert { + fileprivate func insert(_ or: OnConflict?, _ values: [Setter]) -> Insert { let insert = values.reduce((columns: [Expressible](), values: [Expressible]())) { insert, setter in (insert.columns + [setter.column], insert.values + [setter.value]) } @@ -616,7 +616,7 @@ extension QueryType { /// - Parameter query: A query to `SELECT` results from. /// /// - Returns: The number of updated rows and statement. - public func insert(query: QueryType) -> Update { + public func insert(_ query: QueryType) -> Update { return Update(" ".join([ Expression(literal: "INSERT INTO"), tableName(), @@ -626,11 +626,11 @@ extension QueryType { // MARK: UPDATE - public func update(values: Setter...) -> Update { + public func update(_ values: Setter...) -> Update { return update(values) } - public func update(values: [Setter]) -> Update { + public func update(_ values: [Setter]) -> Update { let clauses: [Expressible?] = [ Expression(literal: "UPDATE"), tableName(), @@ -671,7 +671,7 @@ extension QueryType { /// /// - Returns: A column expression namespaced with the query’s table name or /// alias. - public func namespace(column: Expression) -> Expression { + public func namespace(_ column: Expression) -> Expression { return Expression(".".join([tableName(), column]).expression) } @@ -733,7 +733,7 @@ extension QueryType { // TODO: alias support func tableName(alias aliased: Bool = false) -> Expressible { - guard let alias = clauses.from.alias where aliased else { + guard let alias = clauses.from.alias , aliased else { return database(namespace: clauses.from.alias ?? clauses.from.name) } @@ -744,7 +744,7 @@ extension QueryType { ]) } - func tableName(qualified qualified: Bool) -> Expressible { + func tableName(qualified: Bool) -> Expressible { if qualified { return tableName() } @@ -880,7 +880,7 @@ public struct Delete : ExpressionType { extension Connection { - public func prepare(query: QueryType) throws -> AnySequence { + public func prepare(_ query: QueryType) throws -> AnySequence { let expression = query.expression let statement = try prepare(expression.template, expression.bindings) @@ -889,11 +889,11 @@ extension Connection { column: for each in query.clauses.select.columns ?? [Expression(literal: "*")] { var names = each.expression.template.characters.split { $0 == "." }.map(String.init) let column = names.removeLast() - let namespace = names.joinWithSeparator(".") + let namespace = names.joined(separator: ".") - func expandGlob(namespace: Bool) -> (QueryType throws -> Void) { + func expandGlob(_ namespace: Bool) -> ((QueryType) throws -> Void) { return { (query: QueryType) throws -> (Void) in - var q = query.dynamicType.init(query.clauses.from.name, database: query.clauses.from.database) + var q = type(of: query).init(query.clauses.from.name, database: query.clauses.from.database) q.clauses.select = query.clauses.select let e = q.expression var names = try self.prepare(e.template, e.bindings).columnNames.map { $0.quote() } @@ -928,34 +928,34 @@ extension Connection { }() return AnySequence { - AnyGenerator { statement.next().map { Row(columnNames, $0) } } + AnyIterator { statement.next().map { Row(columnNames, $0) } } } } - public func scalar(query: ScalarQuery) throws -> V { + public func scalar(_ query: ScalarQuery) throws -> V { let expression = query.expression return value(try scalar(expression.template, expression.bindings)) } - public func scalar(query: ScalarQuery) throws -> V.ValueType? { + public func scalar(_ query: ScalarQuery) throws -> V.ValueType? { let expression = query.expression guard let value = try scalar(expression.template, expression.bindings) as? V.Datatype else { return nil } return V.fromDatatypeValue(value) } - public func scalar(query: Select) throws -> V { + public func scalar(_ query: Select) throws -> V { let expression = query.expression return value(try scalar(expression.template, expression.bindings)) } - public func scalar(query: Select) throws -> V.ValueType? { + public func scalar(_ query: Select) throws -> V.ValueType? { let expression = query.expression guard let value = try scalar(expression.template, expression.bindings) as? V.Datatype else { return nil } return V.fromDatatypeValue(value) } - public func pluck(query: QueryType) throws -> Row? { - return try prepare(query.limit(1, query.clauses.limit?.offset)).generate().next() + public func pluck(_ query: QueryType) throws -> Row? { + return try prepare(query.limit(1, query.clauses.limit?.offset)).makeIterator().next() } /// Runs an `Insert` query. @@ -968,10 +968,10 @@ extension Connection { /// - Parameter query: An insert query. /// /// - Returns: The insert’s rowid. - public func run(query: Insert) throws -> Int64 { + public func run(_ query: Insert) throws -> Int64 { let expression = query.expression return try sync { - try self.run(expression.template, expression.bindings) + _ = try self.run(expression.template, expression.bindings) return self.lastInsertRowid! } } @@ -984,10 +984,10 @@ extension Connection { /// - Parameter query: An update query. /// /// - Returns: The number of updated rows. - public func run(query: Update) throws -> Int { + public func run(_ query: Update) throws -> Int { let expression = query.expression return try sync { - try self.run(expression.template, expression.bindings) + _ = try self.run(expression.template, expression.bindings) return self.changes } } @@ -999,10 +999,10 @@ extension Connection { /// - Parameter query: A delete query. /// /// - Returns: The number of deleted rows. - public func run(query: Delete) throws -> Int { + public func run(_ query: Delete) throws -> Int { let expression = query.expression return try sync { - try self.run(expression.template, expression.bindings) + _ = try self.run(expression.template, expression.bindings) return self.changes } } @@ -1011,11 +1011,11 @@ extension Connection { public struct Row { - private let columnNames: [String: Int] + fileprivate let columnNames: [String: Int] - private let values: [Binding?] + fileprivate let values: [Binding?] - private init(_ columnNames: [String: Int], _ values: [Binding?]) { + fileprivate init(_ columnNames: [String: Int], _ values: [Binding?]) { self.columnNames = columnNames self.values = values } @@ -1025,11 +1025,11 @@ public struct Row { /// - Parameter column: An expression representing a column selected in a Query. /// /// - Returns: The value for the given column. - public func get(column: Expression) -> V { + public func get(_ column: Expression) -> V { return get(Expression(column))! } - public func get(column: Expression) -> V? { - func valueAtIndex(idx: Int) -> V? { + public func get(_ column: Expression) -> V? { + func valueAtIndex(_ idx: Int) -> V? { guard let value = values[idx] as? V.Datatype else { return nil } return (V.fromDatatypeValue(value) as? V)! } @@ -1039,7 +1039,7 @@ public struct Row { switch similar.count { case 0: - fatalError("no such column '\(column.template)' in columns: \(columnNames.keys.sort())") + fatalError("no such column '\(column.template)' in columns: \(columnNames.keys.sorted())") case 1: return valueAtIndex(columnNames[similar[0]]!) default: @@ -1143,7 +1143,7 @@ public struct QueryClauses { var limit: (length: Int, offset: Int?)? - private init(_ name: String, alias: String?, database: String?) { + fileprivate init(_ name: String, alias: String?, database: String?) { self.from = (name, alias, database) } diff --git a/SQLite/Typed/Schema.swift b/SQLite/Typed/Schema.swift index 16e11c16..4c6862c0 100644 --- a/SQLite/Typed/Schema.swift +++ b/SQLite/Typed/Schema.swift @@ -26,7 +26,7 @@ extension SchemaType { // MARK: - DROP TABLE / VIEW / VIRTUAL TABLE - public func drop(ifExists ifExists: Bool = false) -> String { + public func drop(ifExists: Bool = false) -> String { return drop("TABLE", tableName(), ifExists) } @@ -36,7 +36,7 @@ extension Table { // MARK: - CREATE TABLE - public func create(temporary temporary: Bool = false, ifNotExists: Bool = false, @noescape block: TableBuilder -> Void) -> String { + public func create(temporary: Bool = false, ifNotExists: Bool = false, block: (TableBuilder) -> Void) -> String { let builder = TableBuilder() block(builder) @@ -49,7 +49,7 @@ extension Table { return " ".join(clauses.flatMap { $0 }).asSQL() } - public func create(query: QueryType, temporary: Bool = false, ifNotExists: Bool = false) -> String { + public func create(_ query: QueryType, temporary: Bool = false, ifNotExists: Bool = false) -> String { let clauses: [Expressible?] = [ create(Table.identifier, tableName(), temporary ? .Temporary : nil, ifNotExists), Expression(literal: "AS"), @@ -61,55 +61,55 @@ extension Table { // MARK: - ALTER TABLE … ADD COLUMN - public func addColumn(name: Expression, check: Expression? = nil, defaultValue: V) -> String { + public func addColumn(_ name: Expression, check: Expression? = nil, defaultValue: V) -> String { return addColumn(definition(name, V.declaredDatatype, nil, false, false, check, defaultValue, nil, nil)) } - public func addColumn(name: Expression, check: Expression, defaultValue: V) -> String { + public func addColumn(_ name: Expression, check: Expression, defaultValue: V) -> String { return addColumn(definition(name, V.declaredDatatype, nil, false, false, check, defaultValue, nil, nil)) } - public func addColumn(name: Expression, check: Expression? = nil, defaultValue: V? = nil) -> String { + public func addColumn(_ name: Expression, check: Expression? = nil, defaultValue: V? = nil) -> String { return addColumn(definition(name, V.declaredDatatype, nil, true, false, check, defaultValue, nil, nil)) } - public func addColumn(name: Expression, check: Expression, defaultValue: V? = nil) -> String { + public func addColumn(_ name: Expression, check: Expression, defaultValue: V? = nil) -> String { return addColumn(definition(name, V.declaredDatatype, nil, true, false, check, defaultValue, nil, nil)) } - public func addColumn(name: Expression, unique: Bool = false, check: Expression? = nil, references table: QueryType, _ other: Expression) -> String { + public func addColumn(_ name: Expression, unique: Bool = false, check: Expression? = nil, references table: QueryType, _ other: Expression) -> String where V.Datatype == Int64 { return addColumn(definition(name, V.declaredDatatype, nil, false, unique, check, nil, (table, other), nil)) } - public func addColumn(name: Expression, unique: Bool = false, check: Expression, references table: QueryType, _ other: Expression) -> String { + public func addColumn(_ name: Expression, unique: Bool = false, check: Expression, references table: QueryType, _ other: Expression) -> String where V.Datatype == Int64 { return addColumn(definition(name, V.declaredDatatype, nil, false, unique, check, nil, (table, other), nil)) } - public func addColumn(name: Expression, unique: Bool = false, check: Expression? = nil, references table: QueryType, _ other: Expression) -> String { + public func addColumn(_ name: Expression, unique: Bool = false, check: Expression? = nil, references table: QueryType, _ other: Expression) -> String where V.Datatype == Int64 { return addColumn(definition(name, V.declaredDatatype, nil, true, unique, check, nil, (table, other), nil)) } - public func addColumn(name: Expression, unique: Bool = false, check: Expression, references table: QueryType, _ other: Expression) -> String { + public func addColumn(_ name: Expression, unique: Bool = false, check: Expression, references table: QueryType, _ other: Expression) -> String where V.Datatype == Int64 { return addColumn(definition(name, V.declaredDatatype, nil, true, unique, check, nil, (table, other), nil)) } - public func addColumn(name: Expression, check: Expression? = nil, defaultValue: V, collate: Collation) -> String { + public func addColumn(_ name: Expression, check: Expression? = nil, defaultValue: V, collate: Collation) -> String where V.Datatype == String { return addColumn(definition(name, V.declaredDatatype, nil, false, false, check, defaultValue, nil, collate)) } - public func addColumn(name: Expression, check: Expression, defaultValue: V, collate: Collation) -> String { + public func addColumn(_ name: Expression, check: Expression, defaultValue: V, collate: Collation) -> String where V.Datatype == String { return addColumn(definition(name, V.declaredDatatype, nil, false, false, check, defaultValue, nil, collate)) } - public func addColumn(name: Expression, check: Expression? = nil, defaultValue: V? = nil, collate: Collation) -> String { + public func addColumn(_ name: Expression, check: Expression? = nil, defaultValue: V? = nil, collate: Collation) -> String where V.Datatype == String { return addColumn(definition(name, V.declaredDatatype, nil, true, false, check, defaultValue, nil, collate)) } - public func addColumn(name: Expression, check: Expression, defaultValue: V? = nil, collate: Collation) -> String { + public func addColumn(_ name: Expression, check: Expression, defaultValue: V? = nil, collate: Collation) -> String where V.Datatype == String { return addColumn(definition(name, V.declaredDatatype, nil, true, false, check, defaultValue, nil, collate)) } - private func addColumn(expression: Expressible) -> String { + fileprivate func addColumn(_ expression: Expressible) -> String { return " ".join([ Expression(literal: "ALTER TABLE"), tableName(), @@ -120,17 +120,17 @@ extension Table { // MARK: - ALTER TABLE … RENAME TO - public func rename(to: Table) -> String { + public func rename(_ to: Table) -> String { return rename(to: to) } // MARK: - CREATE INDEX - public func createIndex(columns: Expressible...) -> String { + public func createIndex(_ columns: Expressible...) -> String { return createIndex(columns) } - public func createIndex(columns: [Expressible], unique: Bool = false, ifNotExists: Bool = false) -> String { + public func createIndex(_ columns: [Expressible], unique: Bool = false, ifNotExists: Bool = false) -> String { let clauses: [Expressible?] = [ create("INDEX", indexName(columns), unique ? .Unique : nil, ifNotExists), Expression(literal: "ON"), @@ -143,16 +143,16 @@ extension Table { // MARK: - DROP INDEX - public func dropIndex(columns: Expressible...) -> String { + public func dropIndex(_ columns: Expressible...) -> String { return dropIndex(columns) } - public func dropIndex(columns: [Expressible], ifExists: Bool = false) -> String { + public func dropIndex(_ columns: [Expressible], ifExists: Bool = false) -> String { return drop("INDEX", indexName(columns), ifExists) } - private func indexName(columns: [Expressible]) -> Expressible { - let string = (["index", clauses.from.name, "on"] + columns.map { $0.expression.template }).joinWithSeparator(" ").lowercaseString + fileprivate func indexName(_ columns: [Expressible]) -> Expressible { + let string = (["index", clauses.from.name, "on"] + columns.map { $0.expression.template }).joined(separator: " ").lowercased() let index = string.characters.reduce("") { underscored, character in guard character != "\"" else { @@ -173,7 +173,7 @@ extension View { // MARK: - CREATE VIEW - public func create(query: QueryType, temporary: Bool = false, ifNotExists: Bool = false) -> String { + public func create(_ query: QueryType, temporary: Bool = false, ifNotExists: Bool = false) -> String { let clauses: [Expressible?] = [ create(View.identifier, tableName(), temporary ? .Temporary : nil, ifNotExists), Expression(literal: "AS"), @@ -185,7 +185,7 @@ extension View { // MARK: - DROP VIEW - public func drop(ifExists ifExists: Bool = false) -> String { + public func drop(ifExists: Bool = false) -> String { return drop("VIEW", tableName(), ifExists) } @@ -195,7 +195,7 @@ extension VirtualTable { // MARK: - CREATE VIRTUAL TABLE - public func create(using: Module, ifNotExists: Bool = false) -> String { + public func create(_ using: Module, ifNotExists: Bool = false) -> String { let clauses: [Expressible?] = [ create(VirtualTable.identifier, tableName(), nil, ifNotExists), Expression(literal: "USING"), @@ -207,7 +207,7 @@ extension VirtualTable { // MARK: - ALTER TABLE … RENAME TO - public func rename(to: VirtualTable) -> String { + public func rename(_ to: VirtualTable) -> String { return rename(to: to) } @@ -215,155 +215,155 @@ extension VirtualTable { public final class TableBuilder { - private var definitions = [Expressible]() + fileprivate var definitions = [Expressible]() - public func column(name: Expression, unique: Bool = false, check: Expression? = nil, defaultValue: Expression? = nil) { + public func column(_ name: Expression, unique: Bool = false, check: Expression? = nil, defaultValue: Expression? = nil) { column(name, V.declaredDatatype, nil, false, unique, check, defaultValue, nil, nil) } - public func column(name: Expression, unique: Bool = false, check: Expression? = nil, defaultValue: V) { + public func column(_ name: Expression, unique: Bool = false, check: Expression? = nil, defaultValue: V) { column(name, V.declaredDatatype, nil, false, unique, check, defaultValue, nil, nil) } - public func column(name: Expression, unique: Bool = false, check: Expression, defaultValue: Expression? = nil) { + public func column(_ name: Expression, unique: Bool = false, check: Expression, defaultValue: Expression? = nil) { column(name, V.declaredDatatype, nil, false, unique, check, defaultValue, nil, nil) } - public func column(name: Expression, unique: Bool = false, check: Expression, defaultValue: V) { + public func column(_ name: Expression, unique: Bool = false, check: Expression, defaultValue: V) { column(name, V.declaredDatatype, nil, false, unique, check, defaultValue, nil, nil) } - public func column(name: Expression, unique: Bool = false, check: Expression? = nil, defaultValue: Expression? = nil) { + public func column(_ name: Expression, unique: Bool = false, check: Expression? = nil, defaultValue: Expression? = nil) { column(name, V.declaredDatatype, nil, true, unique, check, defaultValue, nil, nil) } - public func column(name: Expression, unique: Bool = false, check: Expression? = nil, defaultValue: Expression) { + public func column(_ name: Expression, unique: Bool = false, check: Expression? = nil, defaultValue: Expression) { column(name, V.declaredDatatype, nil, true, unique, check, defaultValue, nil, nil) } - public func column(name: Expression, unique: Bool = false, check: Expression? = nil, defaultValue: V) { + public func column(_ name: Expression, unique: Bool = false, check: Expression? = nil, defaultValue: V) { column(name, V.declaredDatatype, nil, true, unique, check, defaultValue, nil, nil) } - public func column(name: Expression, unique: Bool = false, check: Expression, defaultValue: Expression? = nil) { + public func column(_ name: Expression, unique: Bool = false, check: Expression, defaultValue: Expression? = nil) { column(name, V.declaredDatatype, nil, true, unique, check, defaultValue, nil, nil) } - public func column(name: Expression, unique: Bool = false, check: Expression, defaultValue: Expression) { + public func column(_ name: Expression, unique: Bool = false, check: Expression, defaultValue: Expression) { column(name, V.declaredDatatype, nil, true, unique, check, defaultValue, nil, nil) } - public func column(name: Expression, unique: Bool = false, check: Expression, defaultValue: V) { + public func column(_ name: Expression, unique: Bool = false, check: Expression, defaultValue: V) { column(name, V.declaredDatatype, nil, true, unique, check, defaultValue, nil, nil) } - public func column(name: Expression, primaryKey: Bool, check: Expression? = nil, defaultValue: Expression? = nil) { - column(name, V.declaredDatatype, primaryKey ? .Default : nil, false, false, check, defaultValue, nil, nil) + public func column(_ name: Expression, primaryKey: Bool, check: Expression? = nil, defaultValue: Expression? = nil) { + column(name, V.declaredDatatype, primaryKey ? .default : nil, false, false, check, defaultValue, nil, nil) } - public func column(name: Expression, primaryKey: Bool, check: Expression, defaultValue: Expression? = nil) { - column(name, V.declaredDatatype, primaryKey ? .Default : nil, false, false, check, defaultValue, nil, nil) + public func column(_ name: Expression, primaryKey: Bool, check: Expression, defaultValue: Expression? = nil) { + column(name, V.declaredDatatype, primaryKey ? .default : nil, false, false, check, defaultValue, nil, nil) } - public func column(name: Expression, primaryKey: PrimaryKey, check: Expression? = nil) { + public func column(_ name: Expression, primaryKey: PrimaryKey, check: Expression? = nil) where V.Datatype == Int64 { column(name, V.declaredDatatype, primaryKey, false, false, check, nil, nil, nil) } - public func column(name: Expression, primaryKey: PrimaryKey, check: Expression) { + public func column(_ name: Expression, primaryKey: PrimaryKey, check: Expression) where V.Datatype == Int64 { column(name, V.declaredDatatype, primaryKey, false, false, check, nil, nil, nil) } - public func column(name: Expression, unique: Bool = false, check: Expression? = nil, references table: QueryType, _ other: Expression) { + public func column(_ name: Expression, unique: Bool = false, check: Expression? = nil, references table: QueryType, _ other: Expression) where V.Datatype == Int64 { column(name, V.declaredDatatype, nil, false, unique, check, nil, (table, other), nil) } - public func column(name: Expression, unique: Bool = false, check: Expression, references table: QueryType, _ other: Expression) { + public func column(_ name: Expression, unique: Bool = false, check: Expression, references table: QueryType, _ other: Expression) where V.Datatype == Int64 { column(name, V.declaredDatatype, nil, false, unique, check, nil, (table, other), nil) } - public func column(name: Expression, unique: Bool = false, check: Expression? = nil, references table: QueryType, _ other: Expression) { + public func column(_ name: Expression, unique: Bool = false, check: Expression? = nil, references table: QueryType, _ other: Expression) where V.Datatype == Int64 { column(name, V.declaredDatatype, nil, true, unique, check, nil, (table, other), nil) } - public func column(name: Expression, unique: Bool = false, check: Expression, references table: QueryType, _ other: Expression) { + public func column(_ name: Expression, unique: Bool = false, check: Expression, references table: QueryType, _ other: Expression) where V.Datatype == Int64 { column(name, V.declaredDatatype, nil, true, unique, check, nil, (table, other), nil) } - public func column(name: Expression, unique: Bool = false, check: Expression? = nil, defaultValue: Expression? = nil, collate: Collation) { + public func column(_ name: Expression, unique: Bool = false, check: Expression? = nil, defaultValue: Expression? = nil, collate: Collation) where V.Datatype == String { column(name, V.declaredDatatype, nil, false, unique, check, defaultValue, nil, collate) } - public func column(name: Expression, unique: Bool = false, check: Expression? = nil, defaultValue: V, collate: Collation) { + public func column(_ name: Expression, unique: Bool = false, check: Expression? = nil, defaultValue: V, collate: Collation) where V.Datatype == String { column(name, V.declaredDatatype, nil, false, unique, check, defaultValue, nil, collate) } - public func column(name: Expression, unique: Bool = false, check: Expression, defaultValue: Expression? = nil, collate: Collation) { + public func column(_ name: Expression, unique: Bool = false, check: Expression, defaultValue: Expression? = nil, collate: Collation) where V.Datatype == String { column(name, V.declaredDatatype, nil, false, unique, check, defaultValue, nil, collate) } - public func column(name: Expression, unique: Bool = false, check: Expression, defaultValue: V, collate: Collation) { + public func column(_ name: Expression, unique: Bool = false, check: Expression, defaultValue: V, collate: Collation) where V.Datatype == String { column(name, V.declaredDatatype, nil, false, unique, check, defaultValue, nil, collate) } - public func column(name: Expression, unique: Bool = false, check: Expression? = nil, defaultValue: Expression? = nil, collate: Collation) { + public func column(_ name: Expression, unique: Bool = false, check: Expression? = nil, defaultValue: Expression? = nil, collate: Collation) where V.Datatype == String { column(name, V.declaredDatatype, nil, false, unique, check, defaultValue, nil, collate) } - public func column(name: Expression, unique: Bool = false, check: Expression? = nil, defaultValue: Expression, collate: Collation) { + public func column(_ name: Expression, unique: Bool = false, check: Expression? = nil, defaultValue: Expression, collate: Collation) where V.Datatype == String { column(name, V.declaredDatatype, nil, false, unique, check, defaultValue, nil, collate) } - public func column(name: Expression, unique: Bool = false, check: Expression? = nil, defaultValue: V, collate: Collation) { + public func column(_ name: Expression, unique: Bool = false, check: Expression? = nil, defaultValue: V, collate: Collation) where V.Datatype == String { column(name, V.declaredDatatype, nil, false, unique, check, defaultValue, nil, collate) } - public func column(name: Expression, unique: Bool = false, check: Expression, defaultValue: Expression? = nil, collate: Collation) { + public func column(_ name: Expression, unique: Bool = false, check: Expression, defaultValue: Expression? = nil, collate: Collation) where V.Datatype == String { column(name, V.declaredDatatype, nil, false, unique, check, defaultValue, nil, collate) } - public func column(name: Expression, unique: Bool = false, check: Expression, defaultValue: Expression, collate: Collation) { + public func column(_ name: Expression, unique: Bool = false, check: Expression, defaultValue: Expression, collate: Collation) where V.Datatype == String { column(name, V.declaredDatatype, nil, false, unique, check, defaultValue, nil, collate) } - public func column(name: Expression, unique: Bool = false, check: Expression, defaultValue: V, collate: Collation) { + public func column(_ name: Expression, unique: Bool = false, check: Expression, defaultValue: V, collate: Collation) where V.Datatype == String { column(name, V.declaredDatatype, nil, false, unique, check, defaultValue, nil, collate) } - private func column(name: Expressible, _ datatype: String, _ primaryKey: PrimaryKey?, _ null: Bool, _ unique: Bool, _ check: Expressible?, _ defaultValue: Expressible?, _ references: (QueryType, Expressible)?, _ collate: Collation?) { + fileprivate func column(_ name: Expressible, _ datatype: String, _ primaryKey: PrimaryKey?, _ null: Bool, _ unique: Bool, _ check: Expressible?, _ defaultValue: Expressible?, _ references: (QueryType, Expressible)?, _ collate: Collation?) { definitions.append(definition(name, datatype, primaryKey, null, unique, check, defaultValue, references, collate)) } // MARK: - - public func primaryKey(column: Expression) { + public func primaryKey(_ column: Expression) { primaryKey([column]) } - public func primaryKey(compositeA: Expression, _ b: Expression) { + public func primaryKey(_ compositeA: Expression, _ b: Expression) { primaryKey([compositeA, b]) } - public func primaryKey(compositeA: Expression, _ b: Expression, _ c: Expression) { + public func primaryKey(_ compositeA: Expression, _ b: Expression, _ c: Expression) { primaryKey([compositeA, b, c]) } - private func primaryKey(composite: [Expressible]) { + fileprivate func primaryKey(_ composite: [Expressible]) { definitions.append("PRIMARY KEY".prefix(composite)) } - public func unique(columns: Expressible...) { + public func unique(_ columns: Expressible...) { unique(columns) } - public func unique(columns: [Expressible]) { + public func unique(_ columns: [Expressible]) { definitions.append("UNIQUE".prefix(columns)) } - public func check(condition: Expression) { + public func check(_ condition: Expression) { check(Expression(condition)) } - public func check(condition: Expression) { + public func check(_ condition: Expression) { definitions.append("CHECK".prefix(condition)) } @@ -381,29 +381,29 @@ public final class TableBuilder { } - public func foreignKey(column: Expression, references table: QueryType, _ other: Expression, update: Dependency? = nil, delete: Dependency? = nil) { + public func foreignKey(_ column: Expression, references table: QueryType, _ other: Expression, update: Dependency? = nil, delete: Dependency? = nil) { foreignKey(column, (table, other), update, delete) } - public func foreignKey(column: Expression, references table: QueryType, _ other: Expression, update: Dependency? = nil, delete: Dependency? = nil) { + public func foreignKey(_ column: Expression, references table: QueryType, _ other: Expression, update: Dependency? = nil, delete: Dependency? = nil) { foreignKey(column, (table, other), update, delete) } - public func foreignKey(composite: (Expression, Expression), references table: QueryType, _ other: (Expression, Expression), update: Dependency? = nil, delete: Dependency? = nil) { + public func foreignKey(_ composite: (Expression, Expression), references table: QueryType, _ other: (Expression, Expression), update: Dependency? = nil, delete: Dependency? = nil) { let composite = ", ".join([composite.0, composite.1]) let references = (table, ", ".join([other.0, other.1])) foreignKey(composite, references, update, delete) } - public func foreignKey(composite: (Expression, Expression, Expression), references table: QueryType, _ other: (Expression, Expression, Expression), update: Dependency? = nil, delete: Dependency? = nil) { + public func foreignKey(_ composite: (Expression, Expression, Expression), references table: QueryType, _ other: (Expression, Expression, Expression), update: Dependency? = nil, delete: Dependency? = nil) { let composite = ", ".join([composite.0, composite.1, composite.2]) let references = (table, ", ".join([other.0, other.1, other.2])) foreignKey(composite, references, update, delete) } - private func foreignKey(column: Expressible, _ references: (QueryType, Expressible), _ update: Dependency?, _ delete: Dependency?) { + fileprivate func foreignKey(_ column: Expressible, _ references: (QueryType, Expressible), _ update: Dependency?, _ delete: Dependency?) { let clauses: [Expressible?] = [ "FOREIGN KEY".prefix(column), reference(references), @@ -418,17 +418,17 @@ public final class TableBuilder { public enum PrimaryKey { - case Default + case `default` - case Autoincrement + case autoincrement } public struct Module { - private let name: String + fileprivate let name: String - private let arguments: [Expressible] + fileprivate let arguments: [Expressible] public init(_ name: String, _ arguments: [Expressible]) { self.init(name: name.quote(), arguments: arguments) @@ -453,7 +453,7 @@ extension Module : Expressible { private extension QueryType { - func create(identifier: String, _ name: Expressible, _ modifier: Modifier?, _ ifNotExists: Bool) -> Expressible { + func create(_ identifier: String, _ name: Expressible, _ modifier: Modifier?, _ ifNotExists: Bool) -> Expressible { let clauses: [Expressible?] = [ Expression(literal: "CREATE"), modifier.map { Expression(literal: $0.rawValue) }, @@ -465,7 +465,7 @@ private extension QueryType { return " ".join(clauses.flatMap { $0 }) } - func rename(to to: Self) -> String { + func rename(to: Self) -> String { return " ".join([ Expression(literal: "ALTER TABLE"), tableName(), @@ -474,7 +474,7 @@ private extension QueryType { ]).asSQL() } - func drop(identifier: String, _ name: Expressible, _ ifExists: Bool) -> String { + func drop(_ identifier: String, _ name: Expressible, _ ifExists: Bool) -> String { let clauses: [Expressible?] = [ Expression(literal: "DROP \(identifier)"), ifExists ? Expression(literal: "IF EXISTS") : nil, @@ -486,11 +486,11 @@ private extension QueryType { } -private func definition(column: Expressible, _ datatype: String, _ primaryKey: PrimaryKey?, _ null: Bool, _ unique: Bool, _ check: Expressible?, _ defaultValue: Expressible?, _ references: (QueryType, Expressible)?, _ collate: Collation?) -> Expressible { +private func definition(_ column: Expressible, _ datatype: String, _ primaryKey: PrimaryKey?, _ null: Bool, _ unique: Bool, _ check: Expressible?, _ defaultValue: Expressible?, _ references: (QueryType, Expressible)?, _ collate: Collation?) -> Expressible { let clauses: [Expressible?] = [ column, Expression(literal: datatype), - primaryKey.map { Expression(literal: $0 == .Autoincrement ? "PRIMARY KEY AUTOINCREMENT" : "PRIMARY KEY") }, + primaryKey.map { Expression(literal: $0 == .autoincrement ? "PRIMARY KEY AUTOINCREMENT" : "PRIMARY KEY") }, null ? nil : Expression(literal: "NOT NULL"), unique ? Expression(literal: "UNIQUE") : nil, check.map { " ".join([Expression(literal: "CHECK"), $0]) }, @@ -502,7 +502,7 @@ private func definition(column: Expressible, _ datatype: String, _ primaryKey: P return " ".join(clauses.flatMap { $0 }) } -private func reference(primary: (QueryType, Expressible)) -> Expressible { +private func reference(_ primary: (QueryType, Expressible)) -> Expressible { return " ".join([ Expression(literal: "REFERENCES"), primary.0.tableName(qualified: false), diff --git a/SQLite/Typed/Setter.swift b/SQLite/Typed/Setter.swift index d8bf775a..86f16fca 100644 --- a/SQLite/Typed/Setter.swift +++ b/SQLite/Typed/Setter.swift @@ -22,38 +22,40 @@ // THE SOFTWARE. // -infix operator <- { - associativity left - precedence 135 - assignment +precedencegroup ColumnAssignment { + associativity: left + assignment: true + lowerThan: AssignmentPrecedence } +infix operator <- : ColumnAssignment + public struct Setter { let column: Expressible let value: Expressible - private init(column: Expression, value: Expression) { + fileprivate init(column: Expression, value: Expression) { self.column = column self.value = value } - private init(column: Expression, value: V) { + fileprivate init(column: Expression, value: V) { self.column = column self.value = value } - private init(column: Expression, value: Expression) { + fileprivate init(column: Expression, value: Expression) { self.column = column self.value = value } - private init(column: Expression, value: Expression) { + fileprivate init(column: Expression, value: Expression) { self.column = column self.value = value } - private init(column: Expression, value: V?) { + fileprivate init(column: Expression, value: V?) { self.column = column self.value = Expression(value: value) } @@ -100,176 +102,176 @@ public func +=(column: Expression, value: String) -> Setter { return column <- column + value } -public func +=(column: Expression, value: Expression) -> Setter { +public func +=(column: Expression, value: Expression) -> Setter where V.Datatype : Number { return column <- column + value } -public func +=(column: Expression, value: V) -> Setter { +public func +=(column: Expression, value: V) -> Setter where V.Datatype : Number { return column <- column + value } -public func +=(column: Expression, value: Expression) -> Setter { +public func +=(column: Expression, value: Expression) -> Setter where V.Datatype : Number { return column <- column + value } -public func +=(column: Expression, value: Expression) -> Setter { +public func +=(column: Expression, value: Expression) -> Setter where V.Datatype : Number { return column <- column + value } -public func +=(column: Expression, value: V) -> Setter { +public func +=(column: Expression, value: V) -> Setter where V.Datatype : Number { return column <- column + value } -public func -=(column: Expression, value: Expression) -> Setter { +public func -=(column: Expression, value: Expression) -> Setter where V.Datatype : Number { return column <- column - value } -public func -=(column: Expression, value: V) -> Setter { +public func -=(column: Expression, value: V) -> Setter where V.Datatype : Number { return column <- column - value } -public func -=(column: Expression, value: Expression) -> Setter { +public func -=(column: Expression, value: Expression) -> Setter where V.Datatype : Number { return column <- column - value } -public func -=(column: Expression, value: Expression) -> Setter { +public func -=(column: Expression, value: Expression) -> Setter where V.Datatype : Number { return column <- column - value } -public func -=(column: Expression, value: V) -> Setter { +public func -=(column: Expression, value: V) -> Setter where V.Datatype : Number { return column <- column - value } -public func *=(column: Expression, value: Expression) -> Setter { +public func *=(column: Expression, value: Expression) -> Setter where V.Datatype : Number { return column <- column * value } -public func *=(column: Expression, value: V) -> Setter { +public func *=(column: Expression, value: V) -> Setter where V.Datatype : Number { return column <- column * value } -public func *=(column: Expression, value: Expression) -> Setter { +public func *=(column: Expression, value: Expression) -> Setter where V.Datatype : Number { return column <- column * value } -public func *=(column: Expression, value: Expression) -> Setter { +public func *=(column: Expression, value: Expression) -> Setter where V.Datatype : Number { return column <- column * value } -public func *=(column: Expression, value: V) -> Setter { +public func *=(column: Expression, value: V) -> Setter where V.Datatype : Number { return column <- column * value } -public func /=(column: Expression, value: Expression) -> Setter { +public func /=(column: Expression, value: Expression) -> Setter where V.Datatype : Number { return column <- column / value } -public func /=(column: Expression, value: V) -> Setter { +public func /=(column: Expression, value: V) -> Setter where V.Datatype : Number { return column <- column / value } -public func /=(column: Expression, value: Expression) -> Setter { +public func /=(column: Expression, value: Expression) -> Setter where V.Datatype : Number { return column <- column / value } -public func /=(column: Expression, value: Expression) -> Setter { +public func /=(column: Expression, value: Expression) -> Setter where V.Datatype : Number { return column <- column / value } -public func /=(column: Expression, value: V) -> Setter { +public func /=(column: Expression, value: V) -> Setter where V.Datatype : Number { return column <- column / value } -public func %=(column: Expression, value: Expression) -> Setter { +public func %=(column: Expression, value: Expression) -> Setter where V.Datatype == Int64 { return column <- column % value } -public func %=(column: Expression, value: V) -> Setter { +public func %=(column: Expression, value: V) -> Setter where V.Datatype == Int64 { return column <- column % value } -public func %=(column: Expression, value: Expression) -> Setter { +public func %=(column: Expression, value: Expression) -> Setter where V.Datatype == Int64 { return column <- column % value } -public func %=(column: Expression, value: Expression) -> Setter { +public func %=(column: Expression, value: Expression) -> Setter where V.Datatype == Int64 { return column <- column % value } -public func %=(column: Expression, value: V) -> Setter { +public func %=(column: Expression, value: V) -> Setter where V.Datatype == Int64 { return column <- column % value } -public func <<=(column: Expression, value: Expression) -> Setter { +public func <<=(column: Expression, value: Expression) -> Setter where V.Datatype == Int64 { return column <- column << value } -public func <<=(column: Expression, value: V) -> Setter { +public func <<=(column: Expression, value: V) -> Setter where V.Datatype == Int64 { return column <- column << value } -public func <<=(column: Expression, value: Expression) -> Setter { +public func <<=(column: Expression, value: Expression) -> Setter where V.Datatype == Int64 { return column <- column << value } -public func <<=(column: Expression, value: Expression) -> Setter { +public func <<=(column: Expression, value: Expression) -> Setter where V.Datatype == Int64 { return column <- column << value } -public func <<=(column: Expression, value: V) -> Setter { +public func <<=(column: Expression, value: V) -> Setter where V.Datatype == Int64 { return column <- column << value } -public func >>=(column: Expression, value: Expression) -> Setter { +public func >>=(column: Expression, value: Expression) -> Setter where V.Datatype == Int64 { return column <- column >> value } -public func >>=(column: Expression, value: V) -> Setter { +public func >>=(column: Expression, value: V) -> Setter where V.Datatype == Int64 { return column <- column >> value } -public func >>=(column: Expression, value: Expression) -> Setter { +public func >>=(column: Expression, value: Expression) -> Setter where V.Datatype == Int64 { return column <- column >> value } -public func >>=(column: Expression, value: Expression) -> Setter { +public func >>=(column: Expression, value: Expression) -> Setter where V.Datatype == Int64 { return column <- column >> value } -public func >>=(column: Expression, value: V) -> Setter { +public func >>=(column: Expression, value: V) -> Setter where V.Datatype == Int64 { return column <- column >> value } -public func &=(column: Expression, value: Expression) -> Setter { +public func &=(column: Expression, value: Expression) -> Setter where V.Datatype == Int64 { return column <- column & value } -public func &=(column: Expression, value: V) -> Setter { +public func &=(column: Expression, value: V) -> Setter where V.Datatype == Int64 { return column <- column & value } -public func &=(column: Expression, value: Expression) -> Setter { +public func &=(column: Expression, value: Expression) -> Setter where V.Datatype == Int64 { return column <- column & value } -public func &=(column: Expression, value: Expression) -> Setter { +public func &=(column: Expression, value: Expression) -> Setter where V.Datatype == Int64 { return column <- column & value } -public func &=(column: Expression, value: V) -> Setter { +public func &=(column: Expression, value: V) -> Setter where V.Datatype == Int64 { return column <- column & value } -public func |=(column: Expression, value: Expression) -> Setter { +public func |=(column: Expression, value: Expression) -> Setter where V.Datatype == Int64 { return column <- column | value } -public func |=(column: Expression, value: V) -> Setter { +public func |=(column: Expression, value: V) -> Setter where V.Datatype == Int64 { return column <- column | value } -public func |=(column: Expression, value: Expression) -> Setter { +public func |=(column: Expression, value: Expression) -> Setter where V.Datatype == Int64 { return column <- column | value } -public func |=(column: Expression, value: Expression) -> Setter { +public func |=(column: Expression, value: Expression) -> Setter where V.Datatype == Int64 { return column <- column | value } -public func |=(column: Expression, value: V) -> Setter { +public func |=(column: Expression, value: V) -> Setter where V.Datatype == Int64 { return column <- column | value } -public func ^=(column: Expression, value: Expression) -> Setter { +public func ^=(column: Expression, value: Expression) -> Setter where V.Datatype == Int64 { return column <- column ^ value } -public func ^=(column: Expression, value: V) -> Setter { +public func ^=(column: Expression, value: V) -> Setter where V.Datatype == Int64 { return column <- column ^ value } -public func ^=(column: Expression, value: Expression) -> Setter { +public func ^=(column: Expression, value: Expression) -> Setter where V.Datatype == Int64 { return column <- column ^ value } -public func ^=(column: Expression, value: Expression) -> Setter { +public func ^=(column: Expression, value: Expression) -> Setter where V.Datatype == Int64 { return column <- column ^ value } -public func ^=(column: Expression, value: V) -> Setter { +public func ^=(column: Expression, value: V) -> Setter where V.Datatype == Int64 { return column <- column ^ value } -public postfix func ++(column: Expression) -> Setter { +public postfix func ++(column: Expression) -> Setter where V.Datatype == Int64 { return Expression(column) += 1 } -public postfix func ++(column: Expression) -> Setter { +public postfix func ++(column: Expression) -> Setter where V.Datatype == Int64 { return Expression(column) += 1 } -public postfix func --(column: Expression) -> Setter { +public postfix func --(column: Expression) -> Setter where V.Datatype == Int64 { return Expression(column) -= 1 } -public postfix func --(column: Expression) -> Setter { +public postfix func --(column: Expression) -> Setter where V.Datatype == Int64 { return Expression(column) -= 1 } diff --git a/SQLiteTests/ConnectionTests.swift b/SQLiteTests/ConnectionTests.swift index 3d7dd3eb..0e8ebea6 100644 --- a/SQLiteTests/ConnectionTests.swift +++ b/SQLiteTests/ConnectionTests.swift @@ -10,7 +10,7 @@ class ConnectionTests : SQLiteTestCase { } func test_init_withInMemory_returnsInMemoryConnection() { - let db = try! Connection(.InMemory) + let db = try! Connection(.inMemory) XCTAssertEqual("", db.description) } @@ -20,12 +20,12 @@ class ConnectionTests : SQLiteTestCase { } func test_init_withTemporary_returnsTemporaryConnection() { - let db = try! Connection(.Temporary) + let db = try! Connection(.temporary) XCTAssertEqual("", db.description) } func test_init_withURI_returnsURIConnection() { - let db = try! Connection(.URI("\(NSTemporaryDirectory())/SQLite.swift Tests.sqlite3")) + let db = try! Connection(.uri("\(NSTemporaryDirectory())/SQLite.swift Tests.sqlite3")) XCTAssertEqual("\(NSTemporaryDirectory())/SQLite.swift Tests.sqlite3", db.description) } @@ -48,7 +48,7 @@ class ConnectionTests : SQLiteTestCase { } func test_lastInsertRowid_returnsLastIdAfterInserts() { - try! InsertUser("alice") + _ = try! InsertUser("alice") XCTAssertEqual(1, db.lastInsertRowid!) } @@ -57,17 +57,17 @@ class ConnectionTests : SQLiteTestCase { } func test_changes_returnsNumberOfChanges() { - try! InsertUser("alice") + _ = try! InsertUser("alice") XCTAssertEqual(1, db.changes) - try! InsertUser("betsy") + _ = try! InsertUser("betsy") XCTAssertEqual(1, db.changes) } func test_totalChanges_returnsTotalNumberOfChanges() { XCTAssertEqual(0, db.totalChanges) - try! InsertUser("alice") + _ = try! InsertUser("alice") XCTAssertEqual(1, db.totalChanges) - try! InsertUser("betsy") + _ = try! InsertUser("betsy") XCTAssertEqual(2, db.totalChanges) } @@ -79,10 +79,10 @@ class ConnectionTests : SQLiteTestCase { } func test_run_preparesRunsAndReturnsStatements() { - try! db.run("SELECT * FROM users WHERE admin = 0") - try! db.run("SELECT * FROM users WHERE admin = ?", 0) - try! db.run("SELECT * FROM users WHERE admin = ?", [0]) - try! db.run("SELECT * FROM users WHERE admin = $admin", ["$admin": 0]) + _ = try! db.run("SELECT * FROM users WHERE admin = 0") + _ = try! db.run("SELECT * FROM users WHERE admin = ?", 0) + _ = try! db.run("SELECT * FROM users WHERE admin = ?", [0]) + _ = try! db.run("SELECT * FROM users WHERE admin = $admin", ["$admin": 0]) AssertSQL("SELECT * FROM users WHERE admin = 0", 4) } @@ -116,7 +116,7 @@ class ConnectionTests : SQLiteTestCase { let stmt = try! db.prepare("INSERT INTO users (email) VALUES (?)", "alice@example.com") try! db.transaction { - try stmt.run() + _ = try stmt.run() } AssertSQL("BEGIN DEFERRED TRANSACTION") @@ -130,8 +130,8 @@ class ConnectionTests : SQLiteTestCase { do { try db.transaction { - try stmt.run() - try stmt.run() + _ = try stmt.run() + _ = try stmt.run() } } catch { } @@ -147,7 +147,7 @@ class ConnectionTests : SQLiteTestCase { try! db.savepoint("1") { try db.savepoint("2") { - try db.run("INSERT INTO users (email) VALUES (?)", "alice@example.com") + _ = try db.run("INSERT INTO users (email) VALUES (?)", "alice@example.com") } } @@ -167,14 +167,14 @@ class ConnectionTests : SQLiteTestCase { do { try db.savepoint("1") { try db.savepoint("2") { - try stmt.run() - try stmt.run() - try stmt.run() + _ = try stmt.run() + _ = try stmt.run() + _ = try stmt.run() } try db.savepoint("2") { - try stmt.run() - try stmt.run() - try stmt.run() + _ = try stmt.run() + _ = try stmt.run() + _ = try stmt.run() } } } catch { @@ -192,41 +192,41 @@ class ConnectionTests : SQLiteTestCase { func test_updateHook_setsUpdateHook_withInsert() { async { done in db.updateHook { operation, db, table, rowid in - XCTAssertEqual(Operation.Insert, operation) + XCTAssertEqual(Operation.insert, operation) XCTAssertEqual("main", db) XCTAssertEqual("users", table) XCTAssertEqual(1, rowid) done() } - try! InsertUser("alice") + _ = try! InsertUser("alice") } } func test_updateHook_setsUpdateHook_withUpdate() { - try! InsertUser("alice") + _ = try! InsertUser("alice") async { done in db.updateHook { operation, db, table, rowid in - XCTAssertEqual(Operation.Update, operation) + XCTAssertEqual(Operation.update, operation) XCTAssertEqual("main", db) XCTAssertEqual("users", table) XCTAssertEqual(1, rowid) done() } - try! db.run("UPDATE users SET email = 'alice@example.com'") + _ = try! db.run("UPDATE users SET email = 'alice@example.com'") } } func test_updateHook_setsUpdateHook_withDelete() { - try! InsertUser("alice") + _ = try! InsertUser("alice") async { done in db.updateHook { operation, db, table, rowid in - XCTAssertEqual(Operation.Delete, operation) + XCTAssertEqual(Operation.delete, operation) XCTAssertEqual("main", db) XCTAssertEqual("users", table) XCTAssertEqual(1, rowid) done() } - try! db.run("DELETE FROM users WHERE id = 1") + _ = try! db.run("DELETE FROM users WHERE id = 1") } } @@ -236,7 +236,7 @@ class ConnectionTests : SQLiteTestCase { done() } try! db.transaction { - try self.InsertUser("alice") + _ = try self.InsertUser("alice") } XCTAssertEqual(1, try! db.scalar("SELECT count(*) FROM users") as? Int64) } @@ -247,8 +247,8 @@ class ConnectionTests : SQLiteTestCase { db.rollbackHook(done) do { try db.transaction { - try self.InsertUser("alice") - try self.InsertUser("alice") // throw + _ = try self.InsertUser("alice") + _ = try self.InsertUser("alice") // throw } } catch { } @@ -264,7 +264,7 @@ class ConnectionTests : SQLiteTestCase { db.rollbackHook(done) do { try db.transaction { - try self.InsertUser("alice") + _ = try self.InsertUser("alice") } } catch { } @@ -288,14 +288,14 @@ class ConnectionTests : SQLiteTestCase { func test_createCollation_createsCollation() { try! db.createCollation("NODIACRITIC") { lhs, rhs in - return lhs.compare(rhs, options: .DiacriticInsensitiveSearch) + return lhs.compare(rhs, options: .diacriticInsensitive) } XCTAssertEqual(1, try! db.scalar("SELECT ? = ? COLLATE NODIACRITIC", "cafe", "café") as? Int64) } func test_createCollation_createsQuotableCollation() { try! db.createCollation("NO DIACRITIC") { lhs, rhs in - return lhs.compare(rhs, options: .DiacriticInsensitiveSearch) + return lhs.compare(rhs, options: .diacriticInsensitive) } XCTAssertEqual(1, try! db.scalar("SELECT ? = ? COLLATE \"NO DIACRITIC\"", "cafe", "café") as? Int64) } @@ -308,9 +308,9 @@ class ConnectionTests : SQLiteTestCase { } let stmt = try! db.prepare("SELECT *, sleep(?) FROM users", 0.1) - try! stmt.run() + _ = try! stmt.run() - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(10 * NSEC_PER_MSEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), db.interrupt) + _ = DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.background).asyncAfter(deadline: DispatchTime.now() + Double(Int64(10 * NSEC_PER_MSEC)) / Double(NSEC_PER_SEC), execute: db.interrupt) AssertThrows(try stmt.run()) } diff --git a/SQLiteTests/CoreFunctionsTests.swift b/SQLiteTests/CoreFunctionsTests.swift index 8f7460d5..db37ff7f 100644 --- a/SQLiteTests/CoreFunctionsTests.swift +++ b/SQLiteTests/CoreFunctionsTests.swift @@ -55,15 +55,15 @@ class CoreFunctionsTests : XCTestCase { } func test_collate_buildsExpressionWithCollateOperator() { - AssertSQL("(\"string\" COLLATE BINARY)", string.collate(.Binary)) - AssertSQL("(\"string\" COLLATE NOCASE)", string.collate(.Nocase)) - AssertSQL("(\"string\" COLLATE RTRIM)", string.collate(.Rtrim)) - AssertSQL("(\"string\" COLLATE \"CUSTOM\")", string.collate(.Custom("CUSTOM"))) - - AssertSQL("(\"stringOptional\" COLLATE BINARY)", stringOptional.collate(.Binary)) - AssertSQL("(\"stringOptional\" COLLATE NOCASE)", stringOptional.collate(.Nocase)) - AssertSQL("(\"stringOptional\" COLLATE RTRIM)", stringOptional.collate(.Rtrim)) - AssertSQL("(\"stringOptional\" COLLATE \"CUSTOM\")", stringOptional.collate(.Custom("CUSTOM"))) + AssertSQL("(\"string\" COLLATE BINARY)", string.collate(.binary)) + AssertSQL("(\"string\" COLLATE NOCASE)", string.collate(.nocase)) + AssertSQL("(\"string\" COLLATE RTRIM)", string.collate(.rtrim)) + AssertSQL("(\"string\" COLLATE \"CUSTOM\")", string.collate(.custom("CUSTOM"))) + + AssertSQL("(\"stringOptional\" COLLATE BINARY)", stringOptional.collate(.binary)) + AssertSQL("(\"stringOptional\" COLLATE NOCASE)", stringOptional.collate(.nocase)) + AssertSQL("(\"stringOptional\" COLLATE RTRIM)", stringOptional.collate(.rtrim)) + AssertSQL("(\"stringOptional\" COLLATE \"CUSTOM\")", stringOptional.collate(.custom("CUSTOM"))) } func test_ltrim_wrapsStringWithLtrimFunction() { diff --git a/SQLiteTests/FTS4Tests.swift b/SQLiteTests/FTS4Tests.swift index 8b4e4f96..92f197a2 100644 --- a/SQLiteTests/FTS4Tests.swift +++ b/SQLiteTests/FTS4Tests.swift @@ -121,19 +121,19 @@ class FTS4ConfigTests : XCTestCase { func test_config_matchinfo() { XCTAssertEqual( "CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(matchinfo=\"fts3\")", - sql(config.matchInfo(.FTS3))) + sql(config.matchInfo(.fts3))) } func test_config_order_asc() { XCTAssertEqual( "CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(order=\"asc\")", - sql(config.order(.Asc))) + sql(config.order(.asc))) } func test_config_order_desc() { XCTAssertEqual( "CREATE VIRTUAL TABLE \"virtual_table\" USING fts4(order=\"desc\")", - sql(config.order(.Desc))) + sql(config.order(.desc))) } func test_config_compress() { @@ -163,14 +163,14 @@ class FTS4ConfigTests : XCTestCase { .column(string, [.unindexed]) .column(date, [.unindexed]) .externalContent(table) - .matchInfo(.FTS3) + .matchInfo(.fts3) .languageId("lid") - .order(.Desc) + .order(.desc) .prefix([2, 4])) ) } - func sql(config: FTS4Config) -> String { + func sql(_ config: FTS4Config) -> String { return virtualTable.create(.FTS4(config)) } } @@ -184,24 +184,24 @@ class FTS4IntegrationTests : SQLiteTestCase { let locale = CFLocaleCopyCurrent() let tokenizerName = "tokenizer" - let tokenizer = CFStringTokenizerCreate(nil, "", CFRangeMake(0, 0), UInt(kCFStringTokenizerUnitWord), locale) + let tokenizer = CFStringTokenizerCreate(nil, "" as CFString!, CFRangeMake(0, 0), UInt(kCFStringTokenizerUnitWord), locale) try! db.registerTokenizer(tokenizerName) { string in - CFStringTokenizerSetString(tokenizer, string, CFRangeMake(0, CFStringGetLength(string))) - if CFStringTokenizerAdvanceToNextToken(tokenizer) == .None { + CFStringTokenizerSetString(tokenizer, string as CFString, CFRangeMake(0, CFStringGetLength(string as CFString))) + if CFStringTokenizerAdvanceToNextToken(tokenizer).isEmpty { return nil } let range = CFStringTokenizerGetCurrentTokenRange(tokenizer) - let input = CFStringCreateWithSubstring(kCFAllocatorDefault, string, range) - let token = CFStringCreateMutableCopy(nil, range.length, input) + let input = CFStringCreateWithSubstring(kCFAllocatorDefault, string as CFString, range)! + let token = CFStringCreateMutableCopy(nil, range.length, input)! CFStringLowercase(token, locale) CFStringTransform(token, nil, kCFStringTransformStripDiacritics, false) - return (token as String, string.rangeOfString(input as String)!) + return (token as String, string.range(of: input as String)!) } - try! db.run(emails.create(.FTS4([subject, body], tokenize: .Custom(tokenizerName)))) + _ = try! db.run(emails.create(.FTS4([subject, body], tokenize: .Custom(tokenizerName)))) AssertSQL("CREATE VIRTUAL TABLE \"emails\" USING fts4(\"subject\", \"body\", tokenize=\"SQLite.swift\" \"tokenizer\")") - try! db.run(emails.insert(subject <- "Aún más cáfe!")) + _ = try! db.run(emails.insert(subject <- "Aún más cáfe!")) XCTAssertEqual(1, try! db.scalar(emails.filter(emails.match("aun")).count)) } #endif diff --git a/SQLiteTests/FTS5Tests.swift b/SQLiteTests/FTS5Tests.swift index aa3965bd..63d8dc40 100644 --- a/SQLiteTests/FTS5Tests.swift +++ b/SQLiteTests/FTS5Tests.swift @@ -90,19 +90,19 @@ class FTS5Tests: XCTestCase { func test_detail_full() { XCTAssertEqual( "CREATE VIRTUAL TABLE \"virtual_table\" USING fts5(detail=\"full\")", - sql(config.detail(.Full))) + sql(config.detail(.full))) } func test_detail_column() { XCTAssertEqual( "CREATE VIRTUAL TABLE \"virtual_table\" USING fts5(detail=\"column\")", - sql(config.detail(.Column))) + sql(config.detail(.column))) } func test_detail_none() { XCTAssertEqual( "CREATE VIRTUAL TABLE \"virtual_table\" USING fts5(detail=\"none\")", - sql(config.detail(.None))) + sql(config.detail(.none))) } func test_fts5_config_all() { @@ -118,7 +118,7 @@ class FTS5Tests: XCTestCase { ) } - func sql(config: FTS5Config) -> String { + func sql(_ config: FTS5Config) -> String { return virtualTable.create(.FTS5(config)) } } diff --git a/SQLiteTests/OperatorsTests.swift b/SQLiteTests/OperatorsTests.swift index 86aa34ed..819cf292 100644 --- a/SQLiteTests/OperatorsTests.swift +++ b/SQLiteTests/OperatorsTests.swift @@ -283,4 +283,4 @@ class OperatorsTests : XCTestCase { AssertSQL("((1 = 1) AND ((1 = 1) OR (1 = 1)))", n == n && (n == n || n == n)) } -} \ No newline at end of file +} diff --git a/SQLiteTests/QueryTests.swift b/SQLiteTests/QueryTests.swift index f584bfcf..df2ff073 100644 --- a/SQLiteTests/QueryTests.swift +++ b/SQLiteTests/QueryTests.swift @@ -298,10 +298,10 @@ class QueryIntegrationTests : SQLiteTestCase { let managers = users.alias("managers") let alice = try! db.run(users.insert(email <- "alice@example.com")) - try! db.run(users.insert(email <- "betsy@example.com", managerId <- alice)) + _ = try! db.run(users.insert(email <- "betsy@example.com", managerId <- alice)) for user in try! db.prepare(users.join(managers, on: managers[id] == users[managerId])) { - user[users[managerId]] + _ = user[users[managerId]] } } diff --git a/SQLiteTests/SchemaTests.swift b/SQLiteTests/SchemaTests.swift index 416b3646..01bedda0 100644 --- a/SQLiteTests/SchemaTests.swift +++ b/SQLiteTests/SchemaTests.swift @@ -283,15 +283,15 @@ class SchemaTests : XCTestCase { func test_column_withIntegerExpression_compilesPrimaryKeyAutoincrementColumnDefinitionExpression() { XCTAssertEqual( "CREATE TABLE \"table\" (\"int64\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)", - table.create { t in t.column(int64, primaryKey: .Autoincrement) } + table.create { t in t.column(int64, primaryKey: .autoincrement) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"int64\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL CHECK (\"int64\" > 0))", - table.create { t in t.column(int64, primaryKey: .Autoincrement, check: int64 > 0) } + table.create { t in t.column(int64, primaryKey: .autoincrement, check: int64 > 0) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"int64\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL CHECK (\"int64Optional\" > 0))", - table.create { t in t.column(int64, primaryKey: .Autoincrement, check: int64Optional > 0) } + table.create { t in t.column(int64, primaryKey: .autoincrement, check: int64Optional > 0) } ) } @@ -354,172 +354,172 @@ class SchemaTests : XCTestCase { func test_column_withStringExpression_compilesCollatedColumnDefinitionExpression() { XCTAssertEqual( "CREATE TABLE \"table\" (\"string\" TEXT NOT NULL COLLATE RTRIM)", - table.create { t in t.column(string, collate: .Rtrim) } + table.create { t in t.column(string, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"string\" TEXT NOT NULL UNIQUE COLLATE RTRIM)", - table.create { t in t.column(string, unique: true, collate: .Rtrim) } + table.create { t in t.column(string, unique: true, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"string\" TEXT NOT NULL CHECK (\"string\" != '') COLLATE RTRIM)", - table.create { t in t.column(string, check: string != "", collate: .Rtrim) } + table.create { t in t.column(string, check: string != "", collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"string\" TEXT NOT NULL CHECK (\"stringOptional\" != '') COLLATE RTRIM)", - table.create { t in t.column(string, check: stringOptional != "", collate: .Rtrim) } + table.create { t in t.column(string, check: stringOptional != "", collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"string\" TEXT NOT NULL DEFAULT (\"string\") COLLATE RTRIM)", - table.create { t in t.column(string, defaultValue: string, collate: .Rtrim) } + table.create { t in t.column(string, defaultValue: string, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"string\" TEXT NOT NULL DEFAULT ('string') COLLATE RTRIM)", - table.create { t in t.column(string, defaultValue: "string", collate: .Rtrim) } + table.create { t in t.column(string, defaultValue: "string", collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"string\" TEXT NOT NULL UNIQUE CHECK (\"string\" != '') COLLATE RTRIM)", - table.create { t in t.column(string, unique: true, check: string != "", collate: .Rtrim) } + table.create { t in t.column(string, unique: true, check: string != "", collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"string\" TEXT NOT NULL UNIQUE CHECK (\"stringOptional\" != '') COLLATE RTRIM)", - table.create { t in t.column(string, unique: true, check: stringOptional != "", collate: .Rtrim) } + table.create { t in t.column(string, unique: true, check: stringOptional != "", collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"string\" TEXT NOT NULL UNIQUE DEFAULT (\"string\") COLLATE RTRIM)", - table.create { t in t.column(string, unique: true, defaultValue: string, collate: .Rtrim) } + table.create { t in t.column(string, unique: true, defaultValue: string, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"string\" TEXT NOT NULL UNIQUE DEFAULT ('string') COLLATE RTRIM)", - table.create { t in t.column(string, unique: true, defaultValue: "string", collate: .Rtrim) } + table.create { t in t.column(string, unique: true, defaultValue: "string", collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"string\" TEXT NOT NULL UNIQUE CHECK (\"string\" != '') DEFAULT (\"string\") COLLATE RTRIM)", - table.create { t in t.column(string, unique: true, check: string != "", defaultValue: string, collate: .Rtrim) } + table.create { t in t.column(string, unique: true, check: string != "", defaultValue: string, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"string\" TEXT NOT NULL UNIQUE CHECK (\"stringOptional\" != '') DEFAULT (\"string\") COLLATE RTRIM)", - table.create { t in t.column(string, unique: true, check: stringOptional != "", defaultValue: string, collate: .Rtrim) } + table.create { t in t.column(string, unique: true, check: stringOptional != "", defaultValue: string, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"string\" TEXT NOT NULL UNIQUE CHECK (\"string\" != '') DEFAULT ('string') COLLATE RTRIM)", - table.create { t in t.column(string, unique: true, check: string != "", defaultValue: "string", collate: .Rtrim) } + table.create { t in t.column(string, unique: true, check: string != "", defaultValue: "string", collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"string\" TEXT NOT NULL UNIQUE CHECK (\"stringOptional\" != '') DEFAULT ('string') COLLATE RTRIM)", - table.create { t in t.column(string, unique: true, check: stringOptional != "", defaultValue: "string", collate: .Rtrim) } + table.create { t in t.column(string, unique: true, check: stringOptional != "", defaultValue: "string", collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"string\" TEXT NOT NULL CHECK (\"string\" != '') DEFAULT (\"string\") COLLATE RTRIM)", - table.create { t in t.column(string, check: string != "", defaultValue: string, collate: .Rtrim) } + table.create { t in t.column(string, check: string != "", defaultValue: string, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"string\" TEXT NOT NULL CHECK (\"stringOptional\" != '') DEFAULT (\"string\") COLLATE RTRIM)", - table.create { t in t.column(string, check: stringOptional != "", defaultValue: string, collate: .Rtrim) } + table.create { t in t.column(string, check: stringOptional != "", defaultValue: string, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"string\" TEXT NOT NULL CHECK (\"string\" != '') DEFAULT ('string') COLLATE RTRIM)", - table.create { t in t.column(string, check: string != "", defaultValue: "string", collate: .Rtrim) } + table.create { t in t.column(string, check: string != "", defaultValue: "string", collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"string\" TEXT NOT NULL CHECK (\"stringOptional\" != '') DEFAULT ('string') COLLATE RTRIM)", - table.create { t in t.column(string, check: stringOptional != "", defaultValue: "string", collate: .Rtrim) } + table.create { t in t.column(string, check: stringOptional != "", defaultValue: "string", collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL COLLATE RTRIM)", - table.create { t in t.column(stringOptional, collate: .Rtrim) } + table.create { t in t.column(stringOptional, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL UNIQUE COLLATE RTRIM)", - table.create { t in t.column(stringOptional, unique: true, collate: .Rtrim) } + table.create { t in t.column(stringOptional, unique: true, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL CHECK (\"string\" != '') COLLATE RTRIM)", - table.create { t in t.column(stringOptional, check: string != "", collate: .Rtrim) } + table.create { t in t.column(stringOptional, check: string != "", collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL CHECK (\"stringOptional\" != '') COLLATE RTRIM)", - table.create { t in t.column(stringOptional, check: stringOptional != "", collate: .Rtrim) } + table.create { t in t.column(stringOptional, check: stringOptional != "", collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL DEFAULT (\"string\") COLLATE RTRIM)", - table.create { t in t.column(stringOptional, defaultValue: string, collate: .Rtrim) } + table.create { t in t.column(stringOptional, defaultValue: string, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL DEFAULT (\"stringOptional\") COLLATE RTRIM)", - table.create { t in t.column(stringOptional, defaultValue: stringOptional, collate: .Rtrim) } + table.create { t in t.column(stringOptional, defaultValue: stringOptional, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL DEFAULT ('string') COLLATE RTRIM)", - table.create { t in t.column(stringOptional, defaultValue: "string", collate: .Rtrim) } + table.create { t in t.column(stringOptional, defaultValue: "string", collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL UNIQUE CHECK (\"string\" != '') COLLATE RTRIM)", - table.create { t in t.column(stringOptional, unique: true, check: string != "", collate: .Rtrim) } + table.create { t in t.column(stringOptional, unique: true, check: string != "", collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL UNIQUE CHECK (\"stringOptional\" != '') COLLATE RTRIM)", - table.create { t in t.column(stringOptional, unique: true, check: stringOptional != "", collate: .Rtrim) } + table.create { t in t.column(stringOptional, unique: true, check: stringOptional != "", collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL UNIQUE DEFAULT (\"string\") COLLATE RTRIM)", - table.create { t in t.column(stringOptional, unique: true, defaultValue: string, collate: .Rtrim) } + table.create { t in t.column(stringOptional, unique: true, defaultValue: string, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL UNIQUE DEFAULT (\"stringOptional\") COLLATE RTRIM)", - table.create { t in t.column(stringOptional, unique: true, defaultValue: stringOptional, collate: .Rtrim) } + table.create { t in t.column(stringOptional, unique: true, defaultValue: stringOptional, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL UNIQUE DEFAULT ('string') COLLATE RTRIM)", - table.create { t in t.column(stringOptional, unique: true, defaultValue: "string", collate: .Rtrim) } + table.create { t in t.column(stringOptional, unique: true, defaultValue: "string", collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL UNIQUE CHECK (\"string\" != '') DEFAULT (\"string\") COLLATE RTRIM)", - table.create { t in t.column(stringOptional, unique: true, check: string != "", defaultValue: string, collate: .Rtrim) } + table.create { t in t.column(stringOptional, unique: true, check: string != "", defaultValue: string, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL UNIQUE CHECK (\"string\" != '') DEFAULT (\"stringOptional\") COLLATE RTRIM)", - table.create { t in t.column(stringOptional, unique: true, check: string != "", defaultValue: stringOptional, collate: .Rtrim) } + table.create { t in t.column(stringOptional, unique: true, check: string != "", defaultValue: stringOptional, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL UNIQUE CHECK (\"stringOptional\" != '') DEFAULT (\"string\") COLLATE RTRIM)", - table.create { t in t.column(stringOptional, unique: true, check: stringOptional != "", defaultValue: string, collate: .Rtrim) } + table.create { t in t.column(stringOptional, unique: true, check: stringOptional != "", defaultValue: string, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL UNIQUE CHECK (\"stringOptional\" != '') DEFAULT (\"stringOptional\") COLLATE RTRIM)", - table.create { t in t.column(stringOptional, unique: true, check: stringOptional != "", defaultValue: stringOptional, collate: .Rtrim) } + table.create { t in t.column(stringOptional, unique: true, check: stringOptional != "", defaultValue: stringOptional, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL UNIQUE CHECK (\"string\" != '') DEFAULT ('string') COLLATE RTRIM)", - table.create { t in t.column(stringOptional, unique: true, check: string != "", defaultValue: "string", collate: .Rtrim) } + table.create { t in t.column(stringOptional, unique: true, check: string != "", defaultValue: "string", collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL UNIQUE CHECK (\"stringOptional\" != '') DEFAULT ('string') COLLATE RTRIM)", - table.create { t in t.column(stringOptional, unique: true, check: stringOptional != "", defaultValue: "string", collate: .Rtrim) } + table.create { t in t.column(stringOptional, unique: true, check: stringOptional != "", defaultValue: "string", collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL CHECK (\"string\" != '') DEFAULT (\"string\") COLLATE RTRIM)", - table.create { t in t.column(stringOptional, check: string != "", defaultValue: string, collate: .Rtrim) } + table.create { t in t.column(stringOptional, check: string != "", defaultValue: string, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL CHECK (\"stringOptional\" != '') DEFAULT (\"string\") COLLATE RTRIM)", - table.create { t in t.column(stringOptional, check: stringOptional != "", defaultValue: string, collate: .Rtrim) } + table.create { t in t.column(stringOptional, check: stringOptional != "", defaultValue: string, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL CHECK (\"string\" != '') DEFAULT (\"stringOptional\") COLLATE RTRIM)", - table.create { t in t.column(stringOptional, check: string != "", defaultValue: stringOptional, collate: .Rtrim) } + table.create { t in t.column(stringOptional, check: string != "", defaultValue: stringOptional, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL CHECK (\"stringOptional\" != '') DEFAULT (\"stringOptional\") COLLATE RTRIM)", - table.create { t in t.column(stringOptional, check: stringOptional != "", defaultValue: stringOptional, collate: .Rtrim) } + table.create { t in t.column(stringOptional, check: stringOptional != "", defaultValue: stringOptional, collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL CHECK (\"string\" != '') DEFAULT ('string') COLLATE RTRIM)", - table.create { t in t.column(stringOptional, check: string != "", defaultValue: "string", collate: .Rtrim) } + table.create { t in t.column(stringOptional, check: string != "", defaultValue: "string", collate: .rtrim) } ) XCTAssertEqual( "CREATE TABLE \"table\" (\"stringOptional\" TEXT NOT NULL CHECK (\"stringOptional\" != '') DEFAULT ('string') COLLATE RTRIM)", - table.create { t in t.column(stringOptional, check: stringOptional != "", defaultValue: "string", collate: .Rtrim) } + table.create { t in t.column(stringOptional, check: stringOptional != "", defaultValue: "string", collate: .rtrim) } ) } @@ -676,36 +676,36 @@ class SchemaTests : XCTestCase { func test_addColumn_withStringExpression_compilesCollatedAlterTableExpression() { XCTAssertEqual( "ALTER TABLE \"table\" ADD COLUMN \"string\" TEXT NOT NULL DEFAULT ('string') COLLATE RTRIM", - table.addColumn(string, defaultValue: "string", collate: .Rtrim) + table.addColumn(string, defaultValue: "string", collate: .rtrim) ) XCTAssertEqual( "ALTER TABLE \"table\" ADD COLUMN \"string\" TEXT NOT NULL CHECK (\"string\" != '') DEFAULT ('string') COLLATE RTRIM", - table.addColumn(string, check: string != "", defaultValue: "string", collate: .Rtrim) + table.addColumn(string, check: string != "", defaultValue: "string", collate: .rtrim) ) XCTAssertEqual( "ALTER TABLE \"table\" ADD COLUMN \"string\" TEXT NOT NULL CHECK (\"stringOptional\" != '') DEFAULT ('string') COLLATE RTRIM", - table.addColumn(string, check: stringOptional != "", defaultValue: "string", collate: .Rtrim) + table.addColumn(string, check: stringOptional != "", defaultValue: "string", collate: .rtrim) ) XCTAssertEqual( "ALTER TABLE \"table\" ADD COLUMN \"stringOptional\" TEXT COLLATE RTRIM", - table.addColumn(stringOptional, collate: .Rtrim) + table.addColumn(stringOptional, collate: .rtrim) ) XCTAssertEqual( "ALTER TABLE \"table\" ADD COLUMN \"stringOptional\" TEXT CHECK (\"string\" != '') COLLATE RTRIM", - table.addColumn(stringOptional, check: string != "", collate: .Rtrim) + table.addColumn(stringOptional, check: string != "", collate: .rtrim) ) XCTAssertEqual( "ALTER TABLE \"table\" ADD COLUMN \"stringOptional\" TEXT CHECK (\"stringOptional\" != '') COLLATE RTRIM", - table.addColumn(stringOptional, check: stringOptional != "", collate: .Rtrim) + table.addColumn(stringOptional, check: stringOptional != "", collate: .rtrim) ) XCTAssertEqual( "ALTER TABLE \"table\" ADD COLUMN \"stringOptional\" TEXT CHECK (\"string\" != '') DEFAULT ('string') COLLATE RTRIM", - table.addColumn(stringOptional, check: string != "", defaultValue: "string", collate: .Rtrim) + table.addColumn(stringOptional, check: string != "", defaultValue: "string", collate: .rtrim) ) XCTAssertEqual( "ALTER TABLE \"table\" ADD COLUMN \"stringOptional\" TEXT CHECK (\"stringOptional\" != '') DEFAULT ('string') COLLATE RTRIM", - table.addColumn(stringOptional, check: stringOptional != "", defaultValue: "string", collate: .Rtrim) + table.addColumn(stringOptional, check: stringOptional != "", defaultValue: "string", collate: .rtrim) ) } diff --git a/SQLiteTests/TestHelpers.swift b/SQLiteTests/TestHelpers.swift index 464b9c27..7b7715b1 100644 --- a/SQLiteTests/TestHelpers.swift +++ b/SQLiteTests/TestHelpers.swift @@ -32,22 +32,22 @@ class SQLiteTestCase : XCTestCase { ) } - func InsertUsers(names: String...) throws { + func InsertUsers(_ names: String...) throws { try InsertUsers(names) } - func InsertUsers(names: [String]) throws { - for name in names { try InsertUser(name) } + func InsertUsers(_ names: [String]) throws { + for name in names { _ = try InsertUser(name) } } - func InsertUser(name: String, age: Int? = nil, admin: Bool = false) throws -> Statement { + func InsertUser(_ name: String, age: Int? = nil, admin: Bool = false) throws -> Statement { return try db.run( "INSERT INTO \"users\" (email, age, admin) values (?, ?, ?)", "\(name)@example.com", age?.datatypeValue, admin.datatypeValue ) } - func AssertSQL(SQL: String, _ executions: Int = 1, _ message: String? = nil, file: StaticString = #file, line: UInt = #line) { + func AssertSQL(_ SQL: String, _ executions: Int = 1, _ message: String? = nil, file: StaticString = #file, line: UInt = #line) { XCTAssertEqual( executions, trace[SQL] ?? 0, message ?? SQL, @@ -55,8 +55,8 @@ class SQLiteTestCase : XCTestCase { ) } - func AssertSQL(SQL: String, _ statement: Statement, _ message: String? = nil, file: StaticString = #file, line: UInt = #line) { - try! statement.run() + func AssertSQL(_ SQL: String, _ statement: Statement, _ message: String? = nil, file: StaticString = #file, line: UInt = #line) { + _ = try! statement.run() AssertSQL(SQL, 1, message, file: file, line: line) if let count = trace[SQL] { trace[SQL] = count - 1 } } @@ -67,10 +67,10 @@ class SQLiteTestCase : XCTestCase { // if let count = trace[SQL] { trace[SQL] = count - 1 } // } - func async(expect description: String = "async", timeout: Double = 5, @noescape block: (() -> Void) -> Void) { - let expectation = expectationWithDescription(description) + func async(expect description: String = "async", timeout: Double = 5, block: (@escaping () -> Void) -> Void) { + let expectation = self.expectation(description: description) block(expectation.fulfill) - waitForExpectationsWithTimeout(timeout, handler: nil) + waitForExpectations(timeout: timeout, handler: nil) } } @@ -81,8 +81,8 @@ let boolOptional = Expression("boolOptional") let data = Expression("blob") let dataOptional = Expression("blobOptional") -let date = Expression("date") -let dateOptional = Expression("dateOptional") +let date = Expression("date") +let dateOptional = Expression("dateOptional") let double = Expression("double") let doubleOptional = Expression("doubleOptional") @@ -96,13 +96,13 @@ let int64Optional = Expression("int64Optional") let string = Expression("string") let stringOptional = Expression("stringOptional") -func AssertSQL(@autoclosure expression1: () -> String, @autoclosure _ expression2: () -> Expressible, file: StaticString = #file, line: UInt = #line) { +func AssertSQL(_ expression1: @autoclosure () -> String, _ expression2: @autoclosure () -> Expressible, file: StaticString = #file, line: UInt = #line) { XCTAssertEqual(expression1(), expression2().asSQL(), file: file, line: line) } -func AssertThrows(@autoclosure expression: () throws -> T, file: StaticString = #file, line: UInt = #line) { +func AssertThrows(_ expression: @autoclosure () throws -> T, file: StaticString = #file, line: UInt = #line) { do { - try expression() + _ = try expression() XCTFail("expression expected to throw", file: file, line: line) } catch { XCTAssert(true, file: file, line: line) diff --git a/run-tests.sh b/run-tests.sh index bc8cd773..03570c2c 100755 --- a/run-tests.sh +++ b/run-tests.sh @@ -1,11 +1,13 @@ #!/bin/bash set -ev -if [ -n "$BUILD_SCHEME" ]; then - if [ -n "$IOS_SIMULATOR" ]; then - make test BUILD_SCHEME="$BUILD_SCHEME" IOS_SIMULATOR="$IOS_SIMULATOR" - else - make test BUILD_SCHEME="$BUILD_SCHEME" - fi -elif [ -n "$VALIDATOR_SUBSPEC" ]; then - cd CocoaPodsTests && make test +if [ $RUN_TESTS == "YES" ]; then + # Build Framework in Debug and Run Tests if specified + make test BUILD_SCHEME="$SCHEME" BUILD_SDK="$SDK" BUILD_DESTINATION="$DESTINATION" BUILD_CONFIGURATION=Debug + # Build Framework in Release and Run Tests if specified + make test BUILD_SCHEME="$SCHEME" BUILD_SDK="$SDK" BUILD_DESTINATION="$DESTINATION" BUILD_CONFIGURATION=Release +else + # Build Framework in Debug + make build BUILD_SCHEME="$SCHEME" BUILD_SDK="$SDK" BUILD_DESTINATION="$DESTINATION" BUILD_CONFIGURATION=Debug + # Build Framework in Release + make build BUILD_SCHEME="$SCHEME" BUILD_SDK="$SDK" BUILD_DESTINATION="$DESTINATION" BUILD_CONFIGURATION=Release fi