Skip to content

Commit a705c40

Browse files
committed
Merge pull request stephencelis#78 from xenadu/master
Support non-integer primary key in shorthand function
2 parents 10ce76f + 2b3780c commit a705c40

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

SQLite Tests/SchemaTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ private let age = Expression<Int?>("age")
77
private let salary = Expression<Double>("salary")
88
private let admin = Expression<Bool>("admin")
99
private let manager_id = Expression<Int64?>("manager_id")
10+
private let uniqueIdentifier = Expression<UniqueIdentifier>("uniqueIdentifier")
1011

1112
class SchemaTests: XCTestCase {
1213

@@ -45,6 +46,18 @@ class SchemaTests: XCTestCase {
4546
)
4647
}
4748

49+
func test_createTable_column_nonIntegerPrimaryKey() {
50+
ExpectExecution(db, "CREATE TABLE \"users\" (\"email\" TEXT PRIMARY KEY NOT NULL)",
51+
db.create(table: users) { $0.column(email, primaryKey: true) }
52+
)
53+
}
54+
55+
func test_createTable_column_customTypePrimaryKey() {
56+
ExpectExecution(db, "CREATE TABLE \"users\" (\"uniqueIdentifier\" TEXT PRIMARY KEY NOT NULL)",
57+
db.create(table: users) { $0.column(uniqueIdentifier, primaryKey: true) }
58+
)
59+
}
60+
4861
func test_createTable_column_withPrimaryKey_buildsPrimaryKeyClause() {
4962
ExpectExecution(db, "CREATE TABLE \"users\" (\"id\" INTEGER PRIMARY KEY NOT NULL)",
5063
db.create(table: users) { t in

SQLite Tests/TestHelper.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,16 @@ func ExpectExecution(db: Database, SQL: String, statement: @autoclosure () -> St
5555
func ExpectExecution(db: Database, SQL: String, query: Query) {
5656
ExpectExecutions(db, [SQL: 1]) { _ in for _ in query {} }
5757
}
58+
59+
60+
public class UniqueIdentifier : NSUUID, Value {
61+
public class var declaredDatatype:String { get { return "TEXT" } }
62+
63+
public class func fromDatatypeValue(datatypeValue: String) -> UniqueIdentifier {
64+
return UniqueIdentifier(UUIDString: datatypeValue)!
65+
}
66+
67+
public var datatypeValue: String {
68+
return self.UUIDString
69+
}
70+
}

SQLite/Schema.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,8 @@ public final class SchemaBuilder {
193193
) {
194194
column(Expression<V>(name), nil, true, unique, check, value.map { Expression(value: $0) })
195195
}
196-
197-
// MARK: - INTEGER Columns
198-
199-
// MARK: PRIMARY KEY
200-
201-
public func column<V: Value where V.Datatype == Int64>(
196+
197+
public func column<V: Value>(
202198
name: Expression<V>,
203199
primaryKey: Bool,
204200
unique: Bool = false,
@@ -207,6 +203,10 @@ public final class SchemaBuilder {
207203
column(name, primaryKey ? .Default : nil, false, unique, check, nil, nil)
208204
}
209205
206+
// MARK: - INTEGER Columns
207+
208+
// MARK: PRIMARY KEY
209+
210210
public enum PrimaryKey {
211211
212212
case Default

0 commit comments

Comments
 (0)