mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
SQLTableBuilder: column.defaults(to:)
This commit is contained in:
1 parent
985eb69c50
commit
c6fd36b75d
4 files changed
+10
-11
No files matched your search
@@ -523,14 +523,14 @@ public final class ColumnDefinition {
|
||||
/// Defines the default column value.
|
||||
///
|
||||
/// try db.create(table: "persons") { t in
|
||||
/// t.column("name", .Text).defaults("Anonymous")
|
||||
/// t.column("name", .Text).defaults(to: "Anonymous")
|
||||
/// }
|
||||
///
|
||||
/// See https://www.sqlite.org/lang_createtable.html#dfltval
|
||||
///
|
||||
/// - parameter value: A DatabaseValueConvertible value.
|
||||
/// - returns: Self so that you can further refine the column definition.
|
||||
public func defaults(value: DatabaseValueConvertible) -> ColumnDefinition {
|
||||
public func defaults(to value: DatabaseValueConvertible) -> ColumnDefinition {
|
||||
defaultExpression = value.sqlExpression
|
||||
return self
|
||||
}
|
||||
|
||||
@@ -134,7 +134,7 @@ dbQueue.inDatabase { db in
|
||||
try db.create(table: "pointOfInterests") { t in
|
||||
t.column("id", .Integer).primaryKey()
|
||||
t.column("title", .Text).notNull()
|
||||
t.column("favorite", .Boolean).notNull().defaults(false)
|
||||
t.column("favorite", .Boolean).notNull().defaults(to: false)
|
||||
t.column("longitude", .Double).notNull()
|
||||
t.column("latitude", .Double).notNull()
|
||||
}
|
||||
@@ -2164,7 +2164,7 @@ Once granted with a [database connection](#database-connections), you can setup
|
||||
try db.create(table: "pointOfInterests") { t in
|
||||
t.column("id", .Integer).primaryKey()
|
||||
t.column("title", .Text)
|
||||
t.column("favorite", .Boolean).notNull().defaults(false)
|
||||
t.column("favorite", .Boolean).notNull().defaults(to: false)
|
||||
t.column("longitude", .Double).notNull()
|
||||
t.column("latitude", .Double).notNull()
|
||||
}
|
||||
@@ -2206,7 +2206,7 @@ Define **not null** columns, and set **default** values:
|
||||
t.column("email", .Text).notNull()
|
||||
|
||||
// name TEXT NOT NULL DEFAULT 'Anonymous',
|
||||
t.column("name", .Text).notNull().defaults("Anonymous")
|
||||
t.column("name", .Text).notNull().defaults(to: "Anonymous")
|
||||
```
|
||||
|
||||
Use an individual column as **primary**, **unique**, or **foreign key**. When defining a foreign key, the referenced column is the primary key of the referenced table (unless you specify otherwise):
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
- [ ] column(...).defaults(to: false)
|
||||
- [ ] Fatal Error when MutablePersistable.update(_:columns:) is given columns that do not exist in the database.
|
||||
- [ ] What is the behavior inTransaction and inSavepoint behaviors in case of commit error? Code looks like we do not rollback, leaving the app in a weird state (out of Swift transaction block with a SQLite transaction that may still be opened).
|
||||
- [ ] GRDBCipher: remove limitations on iOS or OS X versions
|
||||
|
||||
@@ -148,10 +148,10 @@ class SQLTableBuilderTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.create(table: "test") { t in
|
||||
t.column("a", .Integer).defaults(1)
|
||||
t.column("b", .Integer).defaults(1.0)
|
||||
t.column("c", .Integer).defaults("'fooéı👨👨🏿🇫🇷🇨🇮'")
|
||||
t.column("d", .Integer).defaults("foo".dataUsingEncoding(NSUTF8StringEncoding)!)
|
||||
t.column("a", .Integer).defaults(to: 1)
|
||||
t.column("b", .Integer).defaults(to: 1.0)
|
||||
t.column("c", .Integer).defaults(to: "'fooéı👨👨🏿🇫🇷🇨🇮'")
|
||||
t.column("d", .Integer).defaults(to: "foo".dataUsingEncoding(NSUTF8StringEncoding)!)
|
||||
t.column("e", .Integer).defaults(sql: "NULL")
|
||||
}
|
||||
XCTAssertEqual(self.lastSQLQuery,
|
||||
@@ -363,7 +363,7 @@ class SQLTableBuilderTests: GRDBTestCase {
|
||||
self.sqlQueries.removeAll()
|
||||
try db.alter(table: "test") { t in
|
||||
t.add(column: "b", .Text)
|
||||
t.add(column: "c", .Integer).notNull().defaults(1)
|
||||
t.add(column: "c", .Integer).notNull().defaults(to: 1)
|
||||
}
|
||||
|
||||
XCTAssertEqual(self.sqlQueries[0], "ALTER TABLE \"test\" ADD COLUMN \"b\" TEXT;")
|
||||
|
||||
Reference in new issue
Block a user