Generate columns from query interface expressions

This allows to avoid SQL injection
This commit is contained in:
Gwendal Roué committed 2020-10-31 14:16:04 +01:00
1 parent 5114cc4d6d
commit b8dd2ce2f4
3 files changed
+50 -8

No files matched your search

@@ -822,7 +822,7 @@ public final class ColumnDefinition {
}
#if GRDBCUSTOMSQLITE
/// Adds a generated column constraint on the column.
/// Defines the column as a generated column.
///
/// try db.create(table: "player") { t in
/// t.column("id", .integer).primaryKey()
@@ -831,8 +831,8 @@ public final class ColumnDefinition {
/// 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.
/// 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.
@@ -840,7 +840,10 @@ public final class ColumnDefinition {
/// defaults to `.virtual`.
/// - returns: Self so that you can further refine the column definition.
@discardableResult
public func generatedAs(sql: String, _ qualification: GeneratedColumnQualification = .virtual) -> Self
public func generatedAs(
sql: String,
_ qualification: GeneratedColumnQualification = .virtual)
-> Self
{
let expression = SQLLiteral(sql: sql).sqlExpression
generatedColumnConstraint = GeneratedColumnConstraint(
@@ -848,6 +851,35 @@ public final class ColumnDefinition {
qualification: qualification)
return self
}
/// Defines the column as a generated 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(Column("score") + Column("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:
/// - expression: The generated expression.
/// - qualification: The generated column's qualification, which
/// defaults to `.virtual`.
/// - returns: Self so that you can further refine the column definition.
@discardableResult
public func generatedAs(
_ expression: SQLExpressible,
_ qualification: GeneratedColumnQualification = .virtual)
-> Self
{
generatedColumnConstraint = GeneratedColumnConstraint(
expression: expression.sqlExpression,
qualification: qualification)
return self
}
#endif
/// Defines a foreign key.