This commit is contained in:
Gwendal Roué committed 2021-06-17 19:30:03 +02:00
1 parent d8cb3066a1
commit 1a2de126ce
4 files changed
+86 -11

No files matched your search

@@ -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)
}