mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
SQLTableBuilder: column.collate()
This commit is contained in:
1 parent
5b6efbbb5f
commit
10c32381d4
2 files changed
+49
No files matched your search
@@ -45,6 +45,7 @@ public class SQLColumnBuilder {
|
||||
var uniqueConflictResolution: SQLConflictResolution?
|
||||
var checkExpression: _SQLExpression?
|
||||
var defaultExpression: _SQLExpression?
|
||||
var collationName: String?
|
||||
|
||||
init(name: String, type: SQLColumnType) {
|
||||
self.name = name
|
||||
@@ -71,6 +72,14 @@ public class SQLColumnBuilder {
|
||||
defaultExpression = value.sqlExpression
|
||||
}
|
||||
|
||||
public func collate(collation: SQLCollation) {
|
||||
collationName = collation.rawValue
|
||||
}
|
||||
|
||||
public func collate(collation: DatabaseCollation) {
|
||||
collationName = collation.name
|
||||
}
|
||||
|
||||
var sql: String {
|
||||
var chunks: [String] = []
|
||||
chunks.append(name.quotedDatabaseIdentifier)
|
||||
@@ -122,6 +131,11 @@ public class SQLColumnBuilder {
|
||||
chunks.append("(" + defaultExpression.sql(&arguments) + ")")
|
||||
}
|
||||
|
||||
if let collationName = collationName {
|
||||
chunks.append("COLLATE")
|
||||
chunks.append(collationName)
|
||||
}
|
||||
|
||||
return chunks.joinWithSeparator(" ")
|
||||
}
|
||||
}
|
||||
@@ -131,6 +145,12 @@ public enum SQLOrdering : String {
|
||||
case Desc = "DESC"
|
||||
}
|
||||
|
||||
public enum SQLCollation : String {
|
||||
case Binary = "BINARY"
|
||||
case Nocase = "NOCASE"
|
||||
case Rtrim = "RTRIM"
|
||||
}
|
||||
|
||||
public enum SQLConflictResolution : String {
|
||||
case Rollback = "ROLLBACK"
|
||||
case Abort = "ABORT"
|
||||
|
||||
@@ -160,6 +160,35 @@ class SQLTableBuilderTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testColumnCollation() {
|
||||
assertNoError {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inTransaction { db in
|
||||
try db.create(table: "test") { t in
|
||||
t.column("name", .Text).collate(.Nocase)
|
||||
}
|
||||
XCTAssertEqual(self.lastSQLQuery,
|
||||
"CREATE TABLE \"test\" (" +
|
||||
"\"name\" TEXT COLLATE NOCASE" +
|
||||
")")
|
||||
return .Rollback
|
||||
}
|
||||
|
||||
let collation = DatabaseCollation("foo") { (lhs, rhs) in .OrderedSame }
|
||||
dbQueue.addCollation(collation)
|
||||
try dbQueue.inTransaction { db in
|
||||
try db.create(table: "test") { t in
|
||||
t.column("name", .Text).collate(collation)
|
||||
}
|
||||
XCTAssertEqual(self.lastSQLQuery,
|
||||
"CREATE TABLE \"test\" (" +
|
||||
"\"name\" TEXT COLLATE foo" +
|
||||
")")
|
||||
return .Rollback
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDropTable() {
|
||||
assertNoError {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
|
||||
Reference in new issue
Block a user