mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Initial commit for generated column support
This commit is contained in:
1 parent
ca18f509e7
commit
071bbc7905
3 files changed
+74
-18
No files matched your search
@@ -619,6 +619,19 @@ public final class ColumnDefinition {
|
||||
var deferred: Bool
|
||||
}
|
||||
|
||||
/// The GeneratedColumnQualification enum defines whether a generated column is virtual or stored
|
||||
///
|
||||
/// See https://sqlite.org/gencol.html#virtual_versus_stored_columns
|
||||
public enum GeneratedColumnQualification {
|
||||
case virtual
|
||||
case stored
|
||||
}
|
||||
|
||||
private struct GeneratedColumnConstraint {
|
||||
var expression: SQLExpression
|
||||
var qualification: GeneratedColumnQualification
|
||||
}
|
||||
|
||||
fileprivate let name: String
|
||||
private let type: Database.ColumnType?
|
||||
fileprivate var primaryKey: (conflictResolution: Database.ConflictResolution?, autoincrement: Bool)?
|
||||
@@ -628,6 +641,7 @@ public final class ColumnDefinition {
|
||||
private var foreignKeyConstraints: [ForeignKeyConstraint] = []
|
||||
private var defaultExpression: SQLExpression?
|
||||
private var collationName: String?
|
||||
private var generatedColumnConstraint: GeneratedColumnConstraint?
|
||||
|
||||
init(name: String, type: Database.ColumnType?) {
|
||||
self.name = name
|
||||
@@ -806,6 +820,32 @@ public final class ColumnDefinition {
|
||||
return self
|
||||
}
|
||||
|
||||
/// Adds a generated column constraint on the column.
|
||||
///
|
||||
/// try db.create(table: "player") { t in
|
||||
/// t.column("id", .integer).primaryKey()
|
||||
/// t.column("score", .integer).notNull()
|
||||
/// t.column("bonus", .integer).notNull()
|
||||
/// t.column("totalScore", .integer).generatedAs(sql: "score + bonus", .stored)
|
||||
/// }
|
||||
///
|
||||
/// See https://sqlite.org/gencol.html. Note particularly the limitations of generated
|
||||
/// columns, e.g. they may not have a default value.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - sql: An SQL expression.
|
||||
/// - qualification: The generated column's qualification.
|
||||
/// - returns: Self so that you can further refine the column definition.
|
||||
@discardableResult
|
||||
public func generatedAs(sql: String, _ qualification: GeneratedColumnQualification) -> Self
|
||||
{
|
||||
let expression = SQLLiteral(sql: sql).sqlExpression
|
||||
generatedColumnConstraint = GeneratedColumnConstraint(
|
||||
expression: expression,
|
||||
qualification: qualification)
|
||||
return self
|
||||
}
|
||||
|
||||
/// Defines a foreign key.
|
||||
///
|
||||
/// try db.create(table: "book") { t in
|
||||
@@ -931,6 +971,18 @@ public final class ColumnDefinition {
|
||||
}
|
||||
}
|
||||
|
||||
if let constraint = generatedColumnConstraint {
|
||||
try chunks.append("GENERATED ALWAYS AS (\(constraint.expression.quotedSQL(db)))")
|
||||
let qualificationLiteral: String
|
||||
switch constraint.qualification {
|
||||
case .stored:
|
||||
qualificationLiteral = "STORED"
|
||||
case .virtual:
|
||||
qualificationLiteral = "VIRTUAL"
|
||||
}
|
||||
chunks.append(qualificationLiteral)
|
||||
}
|
||||
|
||||
return chunks.joined(separator: " ")
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user