mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
Fix #988
This commit is contained in:
1 parent
d8cb3066a1
commit
1a2de126ce
4 files changed
+86
-11
No files matched your search
@@ -45,10 +45,18 @@ public struct FTS3: VirtualTableModule {
|
||||
/// The virtual table module name
|
||||
public let moduleName = "fts3"
|
||||
|
||||
// TODO: remove when `makeTableDefinition()` is no longer a requirement
|
||||
/// Reserved; part of the VirtualTableModule protocol.
|
||||
///
|
||||
/// See Database.create(virtualTable:using:)
|
||||
public func makeTableDefinition() -> FTS3TableDefinition {
|
||||
preconditionFailure()
|
||||
}
|
||||
|
||||
/// Reserved; part of the VirtualTableModule protocol.
|
||||
///
|
||||
/// See Database.create(virtualTable:using:)
|
||||
public func makeTableDefinition(configuration: VirtualTableConfiguration) -> FTS3TableDefinition {
|
||||
FTS3TableDefinition()
|
||||
}
|
||||
|
||||
|
||||
+24
-5
@@ -25,11 +25,19 @@ public struct FTS4: VirtualTableModule {
|
||||
/// The virtual table module name
|
||||
public let moduleName = "fts4"
|
||||
|
||||
// TODO: remove when `makeTableDefinition()` is no longer a requirement
|
||||
/// Reserved; part of the VirtualTableModule protocol.
|
||||
///
|
||||
/// See Database.create(virtualTable:using:)
|
||||
public func makeTableDefinition() -> FTS4TableDefinition {
|
||||
FTS4TableDefinition()
|
||||
preconditionFailure()
|
||||
}
|
||||
|
||||
/// Reserved; part of the VirtualTableModule protocol.
|
||||
///
|
||||
/// See Database.create(virtualTable:using:)
|
||||
public func makeTableDefinition(configuration: VirtualTableConfiguration) -> FTS4TableDefinition {
|
||||
FTS4TableDefinition(configuration: configuration)
|
||||
}
|
||||
|
||||
/// Reserved; part of the VirtualTableModule protocol.
|
||||
@@ -113,20 +121,26 @@ public struct FTS4: VirtualTableModule {
|
||||
|
||||
let oldRowID = "old.\(rowIDColumn.quotedDatabaseIdentifier)"
|
||||
|
||||
let ifNotExists = definition.configuration.ifNotExists
|
||||
? "IF NOT EXISTS "
|
||||
: ""
|
||||
|
||||
// swiftlint:disable line_length
|
||||
try db.execute(sql: """
|
||||
CREATE TRIGGER \("__\(tableName)_bu".quotedDatabaseIdentifier) BEFORE UPDATE ON \(content) BEGIN
|
||||
CREATE TRIGGER \(ifNotExists)\("__\(tableName)_bu".quotedDatabaseIdentifier) BEFORE UPDATE ON \(content) BEGIN
|
||||
DELETE FROM \(ftsTable) WHERE docid=\(oldRowID);
|
||||
END;
|
||||
CREATE TRIGGER \("__\(tableName)_bd".quotedDatabaseIdentifier) BEFORE DELETE ON \(content) BEGIN
|
||||
CREATE TRIGGER \(ifNotExists)\("__\(tableName)_bd".quotedDatabaseIdentifier) BEFORE DELETE ON \(content) BEGIN
|
||||
DELETE FROM \(ftsTable) WHERE docid=\(oldRowID);
|
||||
END;
|
||||
CREATE TRIGGER \("__\(tableName)_au".quotedDatabaseIdentifier) AFTER UPDATE ON \(content) BEGIN
|
||||
CREATE TRIGGER \(ifNotExists)\("__\(tableName)_au".quotedDatabaseIdentifier) AFTER UPDATE ON \(content) BEGIN
|
||||
INSERT INTO \(ftsTable)(\(ftsColumns)) VALUES(\(newContentColumns));
|
||||
END;
|
||||
CREATE TRIGGER \("__\(tableName)_ai".quotedDatabaseIdentifier) AFTER INSERT ON \(content) BEGIN
|
||||
CREATE TRIGGER \(ifNotExists)\("__\(tableName)_ai".quotedDatabaseIdentifier) AFTER INSERT ON \(content) BEGIN
|
||||
INSERT INTO \(ftsTable)(\(ftsColumns)) VALUES(\(newContentColumns));
|
||||
END;
|
||||
""")
|
||||
// swiftlint:enable line_length
|
||||
|
||||
// https://www.sqlite.org/fts3.html#*fts4rebuidcmd
|
||||
|
||||
@@ -151,6 +165,7 @@ public final class FTS4TableDefinition {
|
||||
case synchronized(contentTable: String)
|
||||
}
|
||||
|
||||
fileprivate let configuration: VirtualTableConfiguration
|
||||
fileprivate var columns: [FTS4ColumnDefinition] = []
|
||||
fileprivate var contentMode: ContentMode = .raw(content: nil)
|
||||
|
||||
@@ -213,6 +228,10 @@ public final class FTS4TableDefinition {
|
||||
/// See https://www.sqlite.org/fts3.html#the_prefix_option
|
||||
public var prefixes: Set<Int>?
|
||||
|
||||
init(configuration: VirtualTableConfiguration) {
|
||||
self.configuration = configuration
|
||||
}
|
||||
|
||||
/// Appends a table column.
|
||||
///
|
||||
/// try db.create(virtualTable: "document", using: FTS4()) { t in
|
||||
|
||||
+26
-5
@@ -52,9 +52,19 @@ public struct FTS5: VirtualTableModule {
|
||||
/// The virtual table module name
|
||||
public let moduleName = "fts5"
|
||||
|
||||
/// Don't use this method.
|
||||
// TODO: remove when `makeTableDefinition()` is no longer a requirement
|
||||
/// Reserved; part of the VirtualTableModule protocol.
|
||||
///
|
||||
/// See Database.create(virtualTable:using:)
|
||||
public func makeTableDefinition() -> FTS5TableDefinition {
|
||||
FTS5TableDefinition()
|
||||
preconditionFailure()
|
||||
}
|
||||
|
||||
/// Reserved; part of the VirtualTableModule protocol.
|
||||
///
|
||||
/// See Database.create(virtualTable:using:)
|
||||
public func makeTableDefinition(configuration: VirtualTableConfiguration) -> FTS5TableDefinition {
|
||||
FTS5TableDefinition(configuration: configuration)
|
||||
}
|
||||
|
||||
/// Don't use this method.
|
||||
@@ -150,18 +160,24 @@ public struct FTS5: VirtualTableModule {
|
||||
.map { "old.\($0.quotedDatabaseIdentifier)" }
|
||||
.joined(separator: ", ")
|
||||
|
||||
let ifNotExists = definition.configuration.ifNotExists
|
||||
? "IF NOT EXISTS "
|
||||
: ""
|
||||
|
||||
// swiftlint:disable line_length
|
||||
try db.execute(sql: """
|
||||
CREATE TRIGGER \("__\(tableName)_ai".quotedDatabaseIdentifier) AFTER INSERT ON \(content) BEGIN
|
||||
CREATE TRIGGER \(ifNotExists)\("__\(tableName)_ai".quotedDatabaseIdentifier) AFTER INSERT ON \(content) BEGIN
|
||||
INSERT INTO \(ftsTable)(\(ftsColumns)) VALUES (\(newContentColumns));
|
||||
END;
|
||||
CREATE TRIGGER \("__\(tableName)_ad".quotedDatabaseIdentifier) AFTER DELETE ON \(content) BEGIN
|
||||
CREATE TRIGGER \(ifNotExists)\("__\(tableName)_ad".quotedDatabaseIdentifier) AFTER DELETE ON \(content) BEGIN
|
||||
INSERT INTO \(ftsTable)(\(ftsTable), \(ftsColumns)) VALUES('delete', \(oldContentColumns));
|
||||
END;
|
||||
CREATE TRIGGER \("__\(tableName)_au".quotedDatabaseIdentifier) AFTER UPDATE ON \(content) BEGIN
|
||||
CREATE TRIGGER \(ifNotExists)\("__\(tableName)_au".quotedDatabaseIdentifier) AFTER UPDATE ON \(content) BEGIN
|
||||
INSERT INTO \(ftsTable)(\(ftsTable), \(ftsColumns)) VALUES('delete', \(oldContentColumns));
|
||||
INSERT INTO \(ftsTable)(\(ftsColumns)) VALUES (\(newContentColumns));
|
||||
END;
|
||||
""")
|
||||
// swiftlint:enable line_length
|
||||
|
||||
// https://sqlite.org/fts5.html#the_rebuild_command
|
||||
|
||||
@@ -250,6 +266,7 @@ public final class FTS5TableDefinition {
|
||||
case synchronized(contentTable: String)
|
||||
}
|
||||
|
||||
fileprivate let configuration: VirtualTableConfiguration
|
||||
fileprivate var columns: [FTS5ColumnDefinition] = []
|
||||
fileprivate var contentMode: ContentMode = .raw(content: nil, contentRowID: nil)
|
||||
|
||||
@@ -335,6 +352,10 @@ public final class FTS5TableDefinition {
|
||||
/// https://www.sqlite.org/fts5.html#the_detail_option
|
||||
public var detail: String?
|
||||
|
||||
init(configuration: VirtualTableConfiguration) {
|
||||
self.configuration = configuration
|
||||
}
|
||||
|
||||
/// Appends a table column.
|
||||
///
|
||||
/// try db.create(virtualTable: "document", using: FTS5()) { t in
|
||||
|
||||
@@ -21,14 +21,26 @@ public protocol VirtualTableModule {
|
||||
/// The name of the module.
|
||||
var moduleName: String { get }
|
||||
|
||||
// TODO: remove this requirement
|
||||
/// Returns a table definition that is passed as the closure argument in the
|
||||
/// `Database.create(virtualTable:using:)` method:
|
||||
///
|
||||
/// try db.create(virtualTable: "item", using: module) { t in
|
||||
/// // t is the result of makeTableDefinition()
|
||||
/// }
|
||||
///
|
||||
/// - warning: This method is DEPRECATED. Define
|
||||
/// `makeTableDefinition(configuration:)` instead.
|
||||
func makeTableDefinition() -> TableDefinition
|
||||
|
||||
/// Returns a table definition that is passed as the closure argument in the
|
||||
/// `Database.create(virtualTable:using:)` method:
|
||||
///
|
||||
/// try db.create(virtualTable: "item", using: module) { t in
|
||||
/// // t is the result of makeTableDefinition(configuration:)
|
||||
/// }
|
||||
func makeTableDefinition(configuration: VirtualTableConfiguration) -> TableDefinition
|
||||
|
||||
/// Returns the module arguments for the `CREATE VIRTUAL TABLE` query.
|
||||
func moduleArguments(for definition: TableDefinition, in db: Database) throws -> [String]
|
||||
|
||||
@@ -37,6 +49,20 @@ public protocol VirtualTableModule {
|
||||
func database(_ db: Database, didCreate tableName: String, using definition: TableDefinition) throws
|
||||
}
|
||||
|
||||
extension VirtualTableModule {
|
||||
// Support for VirtualTableModule types defined by users
|
||||
// TODO: remove when `makeTableDefinition()` is no longer a requirement
|
||||
public func makeTableDefinition(configuration: VirtualTableConfiguration) -> TableDefinition {
|
||||
makeTableDefinition()
|
||||
}
|
||||
}
|
||||
|
||||
public struct VirtualTableConfiguration {
|
||||
/// If true, existing objects must not be replaced, or generate any error
|
||||
/// (even if they do not match the objects that would be created otherwise.)
|
||||
var ifNotExists: Bool
|
||||
}
|
||||
|
||||
extension Database {
|
||||
|
||||
// MARK: - Database Schema
|
||||
@@ -104,7 +130,8 @@ extension Database {
|
||||
throws
|
||||
{
|
||||
// Define virtual table
|
||||
let definition = module.makeTableDefinition()
|
||||
let configuration = VirtualTableConfiguration(ifNotExists: ifNotExists)
|
||||
let definition = module.makeTableDefinition(configuration: configuration)
|
||||
if let body = body {
|
||||
body(definition)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user