From aad7218df668e784b815d9be772722e823672f01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 19 Nov 2015 08:06:41 +0100 Subject: [PATCH] Functions --- .../Public/Core/Function/FunctionTests.swift | 66 ++++++++++++++++--- GRDB/Core/Database.swift | 47 ++++++++++++- 2 files changed, 100 insertions(+), 13 deletions(-) diff --git a/GRDB Tests/Public/Core/Function/FunctionTests.swift b/GRDB Tests/Public/Core/Function/FunctionTests.swift index d1551772d..d525999cc 100644 --- a/GRDB Tests/Public/Core/Function/FunctionTests.swift +++ b/GRDB Tests/Public/Core/Function/FunctionTests.swift @@ -17,16 +17,6 @@ struct CustomFunctionResult : DatabaseValueConvertible { } class FunctionTests: GRDBTestCase { -// // Crash: SQLite error 1 with statement `SELECT f(1)`: wrong number of arguments to function f() -// func testAddFunctionArity0WithBadNumberOfArguments() { -// assertNoError { -// dbQueue.inDatabase { db in -// db.addFunction("f") { nil } -// Row.fetchOne(db, "SELECT f(1)") -// } -// } -// } - func testAddFunctionReturningNull() { assertNoError { dbQueue.inDatabase { db in @@ -145,4 +135,60 @@ class FunctionTests: GRDBTestCase { } } } + + func testFunctionThrowingDatabaseErrorWithMessage() { + assertNoError { + try dbQueue.inDatabase { db in + db.addFunction("f", argumentCount: 1) { databaseValues in + throw DatabaseError(message: "custom error message") + } + do { + try db.execute("CREATE TABLE items (value INT)") + try db.execute("INSERT INTO items VALUES (f(1))") + XCTFail("Expected DatabaseError") + } catch let error as DatabaseError { + XCTAssertEqual(error.code, SQLITE_ERROR) + XCTAssertEqual(error.message, "custom error message") + } + } + } + } + + func testFunctionThrowingDatabaseErrorWithCode() { + assertNoError { + try dbQueue.inDatabase { db in + db.addFunction("f", argumentCount: 1) { databaseValues in + throw DatabaseError(code: 123, message: "custom error message") + } + do { + try db.execute("CREATE TABLE items (value INT)") + try db.execute("INSERT INTO items VALUES (f(1))") + XCTFail("Expected DatabaseError") + } catch let error as DatabaseError { + XCTAssertEqual(error.code, 123) + XCTAssertEqual(error.message, "custom error message") + } + } + } + } + + func testFunctionThrowingCustomError() { + assertNoError { + try dbQueue.inDatabase { db in + db.addFunction("f", argumentCount: 1) { databaseValues in + throw NSError(domain: "CustomErrorDomain", code: 123, userInfo: [NSLocalizedDescriptionKey: "custom error message"]) + } + do { + try db.execute("CREATE TABLE items (value INT)") + try db.execute("INSERT INTO items VALUES (f(1))") + XCTFail("Expected DatabaseError") + } catch let error as DatabaseError { + XCTAssertEqual(error.code, 1) + XCTAssertTrue(error.message!.containsString("CustomErrorDomain")) + XCTAssertTrue(error.message!.containsString("123")) + XCTAssertTrue(error.message!.containsString("custom error message")) + } + } + } + } } diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 941a49e6e..672847312 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -329,7 +329,10 @@ public final class Database { // ========================================================================= // MARK: - Functions - /// TODO + /// Remove a function. + /// + /// - parameter identifier: A function identifier returned by addFunction() + /// or addVariadicFunction(). public func removeFunction(identifier: DatabaseFunctionIdentifier) { functions.removeValueForKey(identifier) let code = sqlite3_create_function_v2( @@ -343,7 +346,24 @@ public final class Database { } } - /// TODO + /// Add or redefine a function with a variable number of arguments. + /// + /// db.addVariadicFunction("f") { databaseValues in + /// return databaseValues.count + /// } + /// Int.fetchOne(db, "SELECT f()")! // 0 + /// Int.fetchOne(db, "SELECT f(1)")! // 1 + /// Int.fetchOne(db, "SELECT f(1,1)"! // 2 + /// + /// - parameter name: The function name. + /// - parameter pure: Whether the function is "pure", which means that its + /// results only depends on its inputs. When a function is pure, SQLite + /// has the opportunity to perform additional optimizations. Default value + /// is false. + /// - parameter function: A function that takes an array of DatabaseValue + /// arguments, and returns an optional DatabaseValueConvertible such as + /// Int, String, NSDate, etc. + /// - returns: A function identifier that can be used with removeFunction(). public func addVariadicFunction(name: String, pure: Bool = false, function: [DatabaseValue] throws -> DatabaseValueConvertible?) -> DatabaseFunctionIdentifier { let identifier = DatabaseFunctionIdentifier(name: name, argumentCount: -1) let dbFunction = DatabaseFunction(pure: pure) { (context, argc, argv) in @@ -354,7 +374,28 @@ public final class Database { return identifier } - /// TODO + /// Add or redefine a function with a fixed number of arguments. + /// + /// db.addFunction("succ", argumentCount: 1) { databaseValues in + /// let dbv = databaseValues.first! + /// guard let int = dbv.value() as Int? else { + /// return nil + /// } + /// return int + 1 + /// } + /// Int.fetchOne(db, "SELECT succ(1)")! // 2 + /// + /// - parameter name: The function name. + /// - parameter argumentCount: The number of arguments of the function. + /// - parameter pure: Whether the function is "pure", which means that its + /// results only depends on its inputs. When a function is pure, SQLite + /// has the opportunity to perform additional optimizations. Default value + /// is false. + /// - parameter function: A function that takes an array of DatabaseValue + /// arguments, and returns an optional DatabaseValueConvertible such as + /// Int, String, NSDate, etc. The array is guaranteed to have exactly + /// argumentCount elements. + /// - returns: A function identifier that can be used with removeFunction(). public func addFunction(name: String, argumentCount: Int, pure: Bool = false, function: [DatabaseValue] throws -> DatabaseValueConvertible?) -> DatabaseFunctionIdentifier { guard argumentCount >= 0 else { fatalError("Invalid negative argument count. Use addVariadicFunction() for arguments with variable arguments count.")