From d74c5264d381ecb37cbba4ebf3bb76551b008d72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 6 Sep 2020 18:28:22 +0200 Subject: [PATCH] Update documentation for functions, collations, and tokenizers --- Documentation/FTS5Tokenizers.md | 6 +++- GRDB/Core/DatabaseFunction.swift | 4 +-- README.md | 54 ++++++++++++++++++++++---------- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/Documentation/FTS5Tokenizers.md b/Documentation/FTS5Tokenizers.md index 7b380a4dc..14e827be1 100644 --- a/Documentation/FTS5Tokenizers.md +++ b/Documentation/FTS5Tokenizers.md @@ -66,7 +66,11 @@ Once you have a custom tokenizer type that adopts [FTS5CustomTokenizer](#fts5cus ```swift class MyTokenizer : FTS5CustomTokenizer { ... } -dbQueue.add(tokenizer: MyTokenizer.self) // or dbPool.add +var config = Configuration() +config.prepareDatabase { db in + db.add(tokenizer: MyTokenizer.self) +} +let dbQueue = try DatabaseQueue(path: dbPath, configuration: config) ``` **Create [full-text tables](../../../#create-fts5-virtual-tables) that use the custom tokenizer:** diff --git a/GRDB/Core/DatabaseFunction.swift b/GRDB/Core/DatabaseFunction.swift index 15f1f53ae..e46e6ddd0 100644 --- a/GRDB/Core/DatabaseFunction.swift +++ b/GRDB/Core/DatabaseFunction.swift @@ -75,8 +75,8 @@ public final class DatabaseFunction: Hashable { /// /// let dbQueue = DatabaseQueue() /// let fn = DatabaseFunction("mysum", argumentCount: 1, aggregate: MySum.self) - /// dbQueue.add(function: fn) /// try dbQueue.write { db in + /// db.add(function: fn) /// try db.execute(sql: "CREATE TABLE test(i)") /// try db.execute(sql: "INSERT INTO test(i) VALUES (1)") /// try db.execute(sql: "INSERT INTO test(i) VALUES (2)") @@ -364,8 +364,8 @@ extension DatabaseFunction { /// /// let dbQueue = DatabaseQueue() /// let fn = DatabaseFunction("mysum", argumentCount: 1, aggregate: MySum.self) -/// dbQueue.add(function: fn) /// try dbQueue.write { db in +/// db.add(function: fn) /// try db.execute(sql: "CREATE TABLE test(i)") /// try db.execute(sql: "INSERT INTO test(i) VALUES (1)") /// try db.execute(sql: "INSERT INTO test(i) VALUES (2)") diff --git a/README.md b/README.md index 6ddce7912..26ab331c3 100644 --- a/README.md +++ b/README.md @@ -1781,6 +1781,10 @@ SELECT maxLength(name) FROM player; -- custom aggregate ### Custom SQL Functions +A *function* argument takes an array of [DatabaseValue](#databasevalue), and returns any valid [value](#values) (Bool, Int, String, Date, Swift enums, etc.) The number of database values is guaranteed to be *argumentCount*. + +SQLite has the opportunity to perform additional optimizations when functions are "pure", which means that their result only depends on their arguments. So make sure to set the *pure* argument to true when possible. + ```swift let reverse = DatabaseFunction("reverse", argumentCount: 1, pure: true) { (values: [DatabaseValue]) in // Extract string value, if any... @@ -1790,7 +1794,16 @@ let reverse = DatabaseFunction("reverse", argumentCount: 1, pure: true) { (value // ... and return reversed string: return String(string.reversed()) } -dbQueue.add(function: reverse) // Or dbPool.add(function: ...) +``` + +You make a function available to a database connection through its configuration: + +```swift +var config = Configuration() +config.prepareDatabase { db in + db.add(function: reverse) +} +let dbQueue = try DatabaseQueue(path: dbPath, configuration: config) try dbQueue.read { db in // "oof" @@ -1798,10 +1811,6 @@ try dbQueue.read { db in } ``` -The *function* argument takes an array of [DatabaseValue](#databasevalue), and returns any valid [value](#values) (Bool, Int, String, Date, Swift enums, etc.) The number of database values is guaranteed to be *argumentCount*. - -SQLite has the opportunity to perform additional optimizations when functions are "pure", which means that their result only depends on their arguments. So make sure to set the *pure* argument to true when possible. - **Functions can take a variable number of arguments:** @@ -1812,12 +1821,10 @@ let averageOf = DatabaseFunction("averageOf", pure: true) { (values: [DatabaseVa let doubles = values.compactMap { Double.fromDatabaseValue($0) } return doubles.reduce(0, +) / Double(doubles.count) } -dbQueue.add(function: averageOf) +db.add(function: averageOf) -try dbQueue.read { db in - // 2.0 - try Double.fetchOne(db, sql: "SELECT averageOf(1, 2, 3)")! -} +// 2.0 +try Double.fetchOne(db, sql: "SELECT averageOf(1, 2, 3)")! ``` @@ -1833,12 +1840,10 @@ let sqrt = DatabaseFunction("sqrt", argumentCount: 1, pure: true) { (values: [Da } return sqrt(double) } -dbQueue.add(function: sqrt) +db.add(function: sqrt) // SQLite error 1 with statement `SELECT sqrt(-1)`: invalid negative number -try dbQueue.read { db in - try Double.fetchOne(db, sql: "SELECT sqrt(-1)")! -} +try Double.fetchOne(db, sql: "SELECT sqrt(-1)")! ``` @@ -1898,8 +1903,16 @@ let maxLength = DatabaseFunction( argumentCount: 1, pure: true, aggregate: MaxLength.self) +``` -dbQueue.add(function: maxLength) // Or dbPool.add(function: ...) +Like [custom SQL Functions](#custom-sql-functions), you make an aggregate function available to a database connection through its configuration: + +```swift +var config = Configuration() +config.prepareDatabase { db in + db.add(function: maxLength) +} +let dbQueue = try DatabaseQueue(path: dbPath, configuration: config) try dbQueue.read { db in // Some Int @@ -6851,8 +6864,15 @@ let players = try Player.order(nameColumn.collating(collation)).fetchAll(db) let collation = DatabaseCollation("customCollation") { (lhs, rhs) -> NSComparisonResult in // return the comparison of lhs and rhs strings. } -dbQueue.add(collation: collation) // Or dbPool.add(collation: ...) -``` + +// Make the collation available to a database connection +var config = Configuration() +config.prepareDatabase { db in + db.add(collation: collation) +} +let dbQueue = try DatabaseQueue(path: dbPath, configuration: config) +-``` + ## Memory Management