From 05da03ab1a0b6a893e24bf07f7cc59007a96144a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 10 Dec 2022 11:32:23 +0100 Subject: [PATCH] Expose generated columns with availability checks --- .../Schema/TableDefinition.swift | 106 ++++++++++++++++-- Tests/GRDBTests/TableDefinitionTests.swift | 26 +++-- 2 files changed, 113 insertions(+), 19 deletions(-) diff --git a/GRDB/QueryInterface/Schema/TableDefinition.swift b/GRDB/QueryInterface/Schema/TableDefinition.swift index e755deea2..89a6ab08d 100644 --- a/GRDB/QueryInterface/Schema/TableDefinition.swift +++ b/GRDB/QueryInterface/Schema/TableDefinition.swift @@ -206,6 +206,16 @@ extension Database { /// /// For extra index options, see ``create(index:on:columns:options:condition:)``. /// + /// ### Generated columns + /// + /// See [Generated columns](https://sqlite.org/gencol.html) for + /// more information: + /// + /// ```swift + /// t.column("totalScore", .integer).generatedAs(sql: "score + bonus") + /// t.column("totalScore", .integer).generatedAs(Column("score") + Column("bonus")) + /// ``` + /// /// ### Integrity checks /// /// SQLite will only let conforming rows in: @@ -224,16 +234,6 @@ extension Database { /// t.check(sql: "a + b < 10") /// ``` /// - /// ### Generated columns - /// - /// [Generated columns](https://sqlite.org/gencol.html) are available with a - /// [custom SQLite build](https://github.com/groue/GRDB.swift/blob/master/Documentation/CustomSQLiteBuilds.md): - /// - /// ```swift - /// t.column("totalScore", .integer).generatedAs(sql: "score + bonus") - /// t.column("totalScore", .integer).generatedAs(Column("score") + Column("bonus")) - /// ``` - /// /// ### Raw SQL columns and constraints /// /// Columns and constraints can be defined with raw sql: @@ -1396,6 +1396,11 @@ public final class TableAlteration { /// - ``collate(_:)-4dljx`` /// - ``collate(_:)-9ywza`` /// +/// ### Generated Columns +/// +/// - ``generatedAs(_:_:)`` +/// - ``generatedAs(sql:_:)`` +/// /// ### Other Constraints /// /// - ``check(_:)`` @@ -1699,7 +1704,7 @@ public final class ColumnDefinition { return self } - #if GRDBCUSTOMSQLITE +#if GRDBCUSTOMSQLITE || GRDBCIPHER /// Defines the column as a generated column. /// /// For example: @@ -1776,6 +1781,85 @@ public final class ColumnDefinition { qualification: qualification) return self } + #else + /// Defines the column as a generated column. + /// + /// For example: + /// + /// ```swift + /// // CREATE TABLE player( + /// // id INTEGER PRIMARY KEY AUTOINCREMENT, + /// // score INTEGER NOT NULL, + /// // bonus INTEGER NOT NULL, + /// // totalScore INTEGER GENERATED ALWAYS AS (score + bonus) STORED + /// // ) + /// try db.create(table: "player") { t in + /// t.autoIncrementedPrimaryKey("id") + /// t.column("score", .integer).notNull() + /// t.column("bonus", .integer).notNull() + /// t.column("totalScore", .integer).generatedAs(sql: "score + bonus", .stored) + /// } + /// ``` + /// + /// Related SQLite documentation: + /// + /// - parameters: + /// - sql: An SQL expression. + /// - qualification: The generated column's qualification, which + /// defaults to ``GeneratedColumnQualification/virtual``. + /// - returns: `self` so that you can further refine the column definition. + @available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *) // SQLite 3.35.0+ (3.31 actually) + @discardableResult + public func generatedAs( + sql: String, + _ qualification: GeneratedColumnQualification = .virtual) + -> Self + { + let expression = SQL(sql: sql).sqlExpression + generatedColumnConstraint = GeneratedColumnConstraint( + expression: expression, + qualification: qualification) + return self + } + + /// Defines the column as a generated column. + /// + /// For example: + /// + /// ```swift + /// // CREATE TABLE player( + /// // id INTEGER PRIMARY KEY AUTOINCREMENT, + /// // score INTEGER NOT NULL, + /// // bonus INTEGER NOT NULL, + /// // totalScore INTEGER GENERATED ALWAYS AS (score + bonus) STORED + /// // ) + /// try db.create(table: "player") { t in + /// t.autoIncrementedPrimaryKey("id") + /// t.column("score", .integer).notNull() + /// t.column("bonus", .integer).notNull() + /// t.column("totalScore", .integer).generatedAs(Column("score") + Column("bonus"), .stored) + /// } + /// ``` + /// + /// Related SQLite documentation: + /// + /// - parameters: + /// - expression: The generated expression. + /// - qualification: The generated column's qualification, which + /// defaults to ``GeneratedColumnQualification/virtual``. + /// - returns: `self` so that you can further refine the column definition. + @available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *) // SQLite 3.35.0+ (3.31 actually) + @discardableResult + public func generatedAs( + _ expression: some SQLExpressible, + _ qualification: GeneratedColumnQualification = .virtual) + -> Self + { + generatedColumnConstraint = GeneratedColumnConstraint( + expression: expression.sqlExpression, + qualification: qualification) + return self + } #endif /// Adds a foreign key constraint. diff --git a/Tests/GRDBTests/TableDefinitionTests.swift b/Tests/GRDBTests/TableDefinitionTests.swift index eebe2d597..ffefeb44a 100644 --- a/Tests/GRDBTests/TableDefinitionTests.swift +++ b/Tests/GRDBTests/TableDefinitionTests.swift @@ -357,9 +357,15 @@ class TableDefinitionTests: GRDBTestCase { } func testColumnGeneratedAs() throws { - #if !GRDBCUSTOMSQLITE - throw XCTSkip("Generated columns are not available") - #else +#if GRDBCUSTOMSQLITE || GRDBCIPHER + guard sqlite3_libversion_number() >= 3031000 else { + throw XCTSkip("Generated columns are not available") + } +#else + guard #available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *) else { + throw XCTSkip("Generated columns are not available") + } +#endif let dbQueue = try makeDatabaseQueue() try dbQueue.inTransaction { db in try db.create(table: "test") { t in @@ -387,7 +393,6 @@ class TableDefinitionTests: GRDBTestCase { """) return .rollback } - #endif } func testTablePrimaryKey() throws { @@ -767,9 +772,15 @@ class TableDefinitionTests: GRDBTestCase { } func testAlterTableAddGeneratedVirtualColumn() throws { - #if !GRDBCUSTOMSQLITE - throw XCTSkip("Generated columns are not available") - #else +#if GRDBCUSTOMSQLITE || GRDBCIPHER + guard sqlite3_libversion_number() >= 3031000 else { + throw XCTSkip("Generated columns are not available") + } +#else + guard #available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *) else { + throw XCTSkip("Generated columns are not available") + } +#endif let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(table: "test") { t in @@ -794,7 +805,6 @@ class TableDefinitionTests: GRDBTestCase { assertEqualSQL(latestQueries[3], "ALTER TABLE \"test\" ADD COLUMN \"g\" GENERATED ALWAYS AS (\"a\" * 2) VIRTUAL") assertEqualSQL(latestQueries[4], "ALTER TABLE \"test\" ADD COLUMN \"h\" GENERATED ALWAYS AS ('O''Brien') VIRTUAL") } - #endif } func testAlterTableDropColumn() throws {