mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
Assume NSData WIP
This commit is contained in:
1 parent
ee87aefb4c
commit
990c34e1d9
12 files changed
+205
-428
No files matched your search
@@ -16,14 +16,14 @@ class DatabaseValueTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
func testDatabaseValueEquatable() {
|
||||
let fooBlob = Blob(data: "foo".dataUsingEncoding(NSUTF8StringEncoding))!
|
||||
let barBlob = Blob(data: "bar".dataUsingEncoding(NSUTF8StringEncoding))!
|
||||
let fooData = "foo".dataUsingEncoding(NSUTF8StringEncoding)!
|
||||
let barData = "bar".dataUsingEncoding(NSUTF8StringEncoding)!
|
||||
|
||||
XCTAssertEqual(DatabaseValue.Null, DatabaseValue.Null)
|
||||
XCTAssertNotEqual(DatabaseValue.Null, DatabaseValue(int64: 1))
|
||||
XCTAssertNotEqual(DatabaseValue.Null, DatabaseValue(double: 1.0))
|
||||
XCTAssertNotEqual(DatabaseValue.Null, DatabaseValue(string: "foo"))
|
||||
XCTAssertNotEqual(DatabaseValue.Null, DatabaseValue(blob: fooBlob))
|
||||
XCTAssertNotEqual(DatabaseValue.Null, DatabaseValue(data: fooData))
|
||||
|
||||
XCTAssertNotEqual(DatabaseValue(int64: 1), DatabaseValue.Null)
|
||||
XCTAssertEqual(DatabaseValue(int64: 1), DatabaseValue(int64: 1))
|
||||
@@ -38,7 +38,7 @@ class DatabaseValueTests: GRDBTestCase {
|
||||
XCTAssertNotEqual(DatabaseValue(int64: 1), DatabaseValue(string: "foo"))
|
||||
XCTAssertNotEqual(DatabaseValue(int64: 1), DatabaseValue(string: "1"))
|
||||
XCTAssertNotEqual(DatabaseValue(int64: 1), DatabaseValue(string: "1.0"))
|
||||
XCTAssertNotEqual(DatabaseValue(int64: 1), DatabaseValue(blob: fooBlob))
|
||||
XCTAssertNotEqual(DatabaseValue(int64: 1), DatabaseValue(data: fooData))
|
||||
|
||||
XCTAssertNotEqual(DatabaseValue(double: 1.0), DatabaseValue.Null)
|
||||
XCTAssertEqual(DatabaseValue(double: 1.0), DatabaseValue(int64: 1))
|
||||
@@ -53,20 +53,20 @@ class DatabaseValueTests: GRDBTestCase {
|
||||
XCTAssertNotEqual(DatabaseValue(double: 1.0), DatabaseValue(string: "foo"))
|
||||
XCTAssertNotEqual(DatabaseValue(double: 1.0), DatabaseValue(string: "1"))
|
||||
XCTAssertNotEqual(DatabaseValue(double: 1.0), DatabaseValue(string: "1.0"))
|
||||
XCTAssertNotEqual(DatabaseValue(double: 1.0), DatabaseValue(blob: fooBlob))
|
||||
XCTAssertNotEqual(DatabaseValue(double: 1.0), DatabaseValue(data: fooData))
|
||||
|
||||
XCTAssertNotEqual(DatabaseValue(string: "foo"), DatabaseValue.Null)
|
||||
XCTAssertNotEqual(DatabaseValue(string: "foo"), DatabaseValue(int64: 1))
|
||||
XCTAssertNotEqual(DatabaseValue(string: "foo"), DatabaseValue(double: 1.0))
|
||||
XCTAssertEqual(DatabaseValue(string: "foo"), DatabaseValue(string: "foo"))
|
||||
XCTAssertNotEqual(DatabaseValue(string: "foo"), DatabaseValue(string: "bar"))
|
||||
XCTAssertNotEqual(DatabaseValue(string: "foo"), DatabaseValue(blob: fooBlob))
|
||||
XCTAssertNotEqual(DatabaseValue(string: "foo"), DatabaseValue(data: fooData))
|
||||
|
||||
XCTAssertNotEqual(DatabaseValue(blob: fooBlob), DatabaseValue.Null)
|
||||
XCTAssertNotEqual(DatabaseValue(blob: fooBlob), DatabaseValue(int64: 1))
|
||||
XCTAssertNotEqual(DatabaseValue(blob: fooBlob), DatabaseValue(double: 1.0))
|
||||
XCTAssertNotEqual(DatabaseValue(blob: fooBlob), DatabaseValue(string: "foo"))
|
||||
XCTAssertEqual(DatabaseValue(blob: fooBlob), DatabaseValue(blob: fooBlob))
|
||||
XCTAssertNotEqual(DatabaseValue(blob: fooBlob), DatabaseValue(blob: barBlob))
|
||||
XCTAssertNotEqual(DatabaseValue(data: fooData), DatabaseValue.Null)
|
||||
XCTAssertNotEqual(DatabaseValue(data: fooData), DatabaseValue(int64: 1))
|
||||
XCTAssertNotEqual(DatabaseValue(data: fooData), DatabaseValue(double: 1.0))
|
||||
XCTAssertNotEqual(DatabaseValue(data: fooData), DatabaseValue(string: "foo"))
|
||||
XCTAssertEqual(DatabaseValue(data: fooData), DatabaseValue(data: fooData))
|
||||
XCTAssertNotEqual(DatabaseValue(data: fooData), DatabaseValue(data: barData))
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
import XCTest
|
||||
import GRDB
|
||||
|
||||
class BlobTests: GRDBTestCase {
|
||||
|
||||
func testBlobCanNotStoreEmptyData() {
|
||||
// SQLite can't store zero-length blob.
|
||||
let blob = Blob(data: NSData())
|
||||
XCTAssertTrue(blob == nil)
|
||||
}
|
||||
|
||||
func testBlobCanNotCopyZeroLengthBuffer() {
|
||||
// SQLite can't store zero-length blob.
|
||||
let blob = Blob(bytes: nil, length: 0)
|
||||
XCTAssertTrue(blob == nil)
|
||||
}
|
||||
|
||||
func testBlobCanNotReferebceZeroLengthBuffer() {
|
||||
// SQLite can't store zero-length blob.
|
||||
let blob = Blob(bytesNoCopy: nil, length: 0, freeWhenDone: false)
|
||||
XCTAssertTrue(blob == nil)
|
||||
}
|
||||
|
||||
func testMemoryBehavior() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute("CREATE TABLE datas (data BLOB)")
|
||||
|
||||
let data = "foo".dataUsingEncoding(NSUTF8StringEncoding)
|
||||
try db.execute("INSERT INTO datas (data) VALUES (?)", arguments: [data])
|
||||
|
||||
// This blob should not copy SQLite data. This can not be tested here: test it at runtime with a breakpoint.
|
||||
for row in Row.fetch(db, "SELECT * FROM datas") {
|
||||
let blob: Blob = row.value(atIndex: 0)
|
||||
XCTAssertEqual(blob.data, data)
|
||||
}
|
||||
|
||||
// This blob should have a copy of SQLite data. This can not be tested here: test it at runtime with a breakpoint.
|
||||
let row = Row.fetchOne(db, "SELECT * FROM datas")!
|
||||
let blob: Blob = row.value(atIndex: 0)
|
||||
XCTAssertEqual(blob.data, data)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+32
-32
@@ -20,7 +20,7 @@ extension DatabaseValue {
|
||||
return .Real
|
||||
case is String:
|
||||
return .Text
|
||||
case is GRDB.Blob:
|
||||
case is NSData:
|
||||
return .Blob
|
||||
default:
|
||||
fatalError("Unexpected type")
|
||||
@@ -77,7 +77,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertTrue((dbv.value() as Double?) == nil)
|
||||
XCTAssertEqual((dbv.value() as String?)!, "0")
|
||||
XCTAssertEqual((dbv.value() as String), "0")
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -97,7 +97,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertTrue((dbv.value() as Double?) == nil)
|
||||
XCTAssertEqual((dbv.value() as String?)!, "0")
|
||||
XCTAssertEqual((dbv.value() as String), "0")
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -117,7 +117,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertTrue((dbv.value() as Double?) == nil)
|
||||
XCTAssertEqual((dbv.value() as String?)!, "0")
|
||||
XCTAssertEqual((dbv.value() as String), "0")
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -137,7 +137,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertTrue((dbv.value() as Double?) == nil)
|
||||
XCTAssertEqual((dbv.value() as String?)!, "0.0")
|
||||
XCTAssertEqual((dbv.value() as String), "0.0")
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -157,7 +157,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertTrue((dbv.value() as Double?) == nil)
|
||||
XCTAssertEqual((dbv.value() as String?)!, "3.0e+5")
|
||||
XCTAssertEqual((dbv.value() as String), "3.0e+5")
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -177,7 +177,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertTrue((dbv.value() as Double?) == nil)
|
||||
XCTAssertEqual((dbv.value() as String?)!, "foo")
|
||||
XCTAssertEqual((dbv.value() as String), "foo")
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -196,7 +196,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertTrue((dbv.value() as Int64?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Double?) == nil)
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertEqual((dbv.value() as Blob?), Blob(data: "foo".dataUsingEncoding(NSUTF8StringEncoding))!)
|
||||
XCTAssertEqual((dbv.value() as NSData?), "foo".dataUsingEncoding(NSUTF8StringEncoding))
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -271,7 +271,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((dbv.value() as Double?)!, 0.0)
|
||||
XCTAssertEqual((dbv.value() as Double), 0.0)
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -295,7 +295,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((dbv.value() as Double?)!, 0.0)
|
||||
XCTAssertEqual((dbv.value() as Double), 0.0)
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -319,7 +319,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((dbv.value() as Double?)!, 0.0)
|
||||
XCTAssertEqual((dbv.value() as Double), 0.0)
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -343,7 +343,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((dbv.value() as Double?)!, Double(300000))
|
||||
XCTAssertEqual((dbv.value() as Double), Double(300000))
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -361,7 +361,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((dbv.value() as Double?)!, 1e20)
|
||||
XCTAssertEqual((dbv.value() as Double), 1e20)
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -385,7 +385,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((dbv.value() as Double?)!, Double(300000))
|
||||
XCTAssertEqual((dbv.value() as Double), Double(300000))
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -403,7 +403,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((dbv.value() as Double?)!, 1e20)
|
||||
XCTAssertEqual((dbv.value() as Double), 1e20)
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -423,7 +423,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertTrue((dbv.value() as Double?) == nil)
|
||||
XCTAssertEqual((dbv.value() as String?)!, "foo")
|
||||
XCTAssertEqual((dbv.value() as String), "foo")
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -442,7 +442,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertTrue((dbv.value() as Int64?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Double?) == nil)
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertEqual((dbv.value() as Blob?), Blob(data: "foo".dataUsingEncoding(NSUTF8StringEncoding))!)
|
||||
XCTAssertEqual((dbv.value() as NSData?), "foo".dataUsingEncoding(NSUTF8StringEncoding))
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -477,7 +477,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((dbv.value() as Double?)!, 0.0)
|
||||
XCTAssertEqual((dbv.value() as Double), 0.0)
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -501,7 +501,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((dbv.value() as Double?)!, 0.0)
|
||||
XCTAssertEqual((dbv.value() as Double), 0.0)
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -525,7 +525,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((dbv.value() as Double?)!, 0.0)
|
||||
XCTAssertEqual((dbv.value() as Double), 0.0)
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -549,7 +549,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((dbv.value() as Double?)!, 0.0)
|
||||
XCTAssertEqual((dbv.value() as Double), 0.0)
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -569,7 +569,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertTrue((dbv.value() as Double?) == nil)
|
||||
XCTAssertEqual((dbv.value() as String?)!, "3.0e+5")
|
||||
XCTAssertEqual((dbv.value() as String), "3.0e+5")
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -588,7 +588,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertTrue((dbv.value() as Int64?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Double?) == nil)
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertEqual((dbv.value() as Blob?), Blob(data: "foo".dataUsingEncoding(NSUTF8StringEncoding))!)
|
||||
XCTAssertEqual((dbv.value() as NSData?), "foo".dataUsingEncoding(NSUTF8StringEncoding))
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -637,7 +637,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((dbv.value() as Double?)!, 0.0)
|
||||
XCTAssertEqual((dbv.value() as Double), 0.0)
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -661,7 +661,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((dbv.value() as Double?)!, 0.0)
|
||||
XCTAssertEqual((dbv.value() as Double), 0.0)
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -685,7 +685,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((dbv.value() as Double?)!, 0.0)
|
||||
XCTAssertEqual((dbv.value() as Double), 0.0)
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -709,7 +709,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((dbv.value() as Double?)!, Double(300000))
|
||||
XCTAssertEqual((dbv.value() as Double), Double(300000))
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -727,7 +727,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((dbv.value() as Double?)!, 1e20)
|
||||
XCTAssertEqual((dbv.value() as Double), 1e20)
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -751,7 +751,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((dbv.value() as Double?)!, Double(300000))
|
||||
XCTAssertEqual((dbv.value() as Double), Double(300000))
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -769,7 +769,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((dbv.value() as Double?)!, 1e20)
|
||||
XCTAssertEqual((dbv.value() as Double), 1e20)
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -789,7 +789,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertTrue((dbv.value() as Double?) == nil)
|
||||
XCTAssertEqual((dbv.value() as String?)!, "foo")
|
||||
XCTAssertEqual((dbv.value() as String), "foo")
|
||||
XCTAssertTrue((dbv.value() as Blob?) == nil)
|
||||
XCTAssertTrue((dbv.value() as NSData?) == nil)
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
@@ -808,7 +808,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
XCTAssertTrue((dbv.value() as Int64?) == nil)
|
||||
XCTAssertTrue((dbv.value() as Double?) == nil)
|
||||
XCTAssertTrue((dbv.value() as String?) == nil)
|
||||
XCTAssertEqual((dbv.value() as Blob?), Blob(data: "foo".dataUsingEncoding(NSUTF8StringEncoding))!)
|
||||
XCTAssertEqual((dbv.value() as NSData?), "foo".dataUsingEncoding(NSUTF8StringEncoding))
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import XCTest
|
||||
import GRDB
|
||||
|
||||
class NSDataTests: GRDBTestCase {
|
||||
|
||||
func testDatabaseValueCanNotStoreEmptyData() {
|
||||
// SQLite can't store zero-length blob.
|
||||
let databaseValue = DatabaseValue(data: NSData())
|
||||
XCTAssertEqual(databaseValue, DatabaseValue.Null)
|
||||
}
|
||||
|
||||
func testMemoryBehavior() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute("CREATE TABLE datas (data BLOB)")
|
||||
|
||||
let data = "foo".dataUsingEncoding(NSUTF8StringEncoding)
|
||||
try db.execute("INSERT INTO datas (data) VALUES (?)", arguments: [data])
|
||||
|
||||
// This blob should not copy SQLite data. This can not be tested here: test it at runtime with a breakpoint.
|
||||
for row in Row.fetch(db, "SELECT * FROM datas") {
|
||||
// This data should be copied:
|
||||
let copiedData: NSData = row.value(atIndex: 0)
|
||||
XCTAssertEqual(copiedData, data)
|
||||
|
||||
// This data should not be copied, and extraced raw from the sqliteStatement
|
||||
let nonCopiedData = row.dataNoCopy(atIndex: 0)
|
||||
XCTAssertEqual(nonCopiedData, data)
|
||||
}
|
||||
|
||||
let row = Row.fetchOne(db, "SELECT * FROM datas")!
|
||||
|
||||
// This data should not be copied:
|
||||
let nonCopiedData1: NSData = row.value(atIndex: 0)
|
||||
XCTAssertEqual(nonCopiedData1, data)
|
||||
|
||||
// This data should not be copied:
|
||||
let nonCopiedData2 = row.dataNoCopy(atIndex: 0)
|
||||
XCTAssertEqual(nonCopiedData2, data)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+31
-31
@@ -49,7 +49,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Double?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertEqual((row.value(atIndex: 0) as String?)!, "0")
|
||||
XCTAssertEqual((row.value(atIndex: 0) as String), "0")
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -68,7 +68,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Double?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertEqual((row.value(atIndex: 0) as String?)!, "0")
|
||||
XCTAssertEqual((row.value(atIndex: 0) as String), "0")
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -87,7 +87,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Double?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertEqual((row.value(atIndex: 0) as String?)!, "0")
|
||||
XCTAssertEqual((row.value(atIndex: 0) as String), "0")
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -106,7 +106,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Double?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertEqual((row.value(atIndex: 0) as String?)!, "0.0")
|
||||
XCTAssertEqual((row.value(atIndex: 0) as String), "0.0")
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -125,7 +125,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Double?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertEqual((row.value(atIndex: 0) as String?)!, "3.0e+5")
|
||||
XCTAssertEqual((row.value(atIndex: 0) as String), "3.0e+5")
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -144,7 +144,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Double?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertEqual((row.value(atIndex: 0) as String?)!, "foo")
|
||||
XCTAssertEqual((row.value(atIndex: 0) as String), "foo")
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -162,7 +162,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Int64?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Double?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Blob?), Blob(data: "foo".dataUsingEncoding(NSUTF8StringEncoding))!)
|
||||
XCTAssertEqual((row.value(atIndex: 0) as NSData?), "foo".dataUsingEncoding(NSUTF8StringEncoding))
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -236,7 +236,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double?)!, 0.0)
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double), 0.0)
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -259,7 +259,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double?)!, 0.0)
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double), 0.0)
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -282,7 +282,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double?)!, 0.0)
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double), 0.0)
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -305,7 +305,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double?)!, Double(300000))
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double), Double(300000))
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -322,7 +322,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double?)!, 1e20)
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double), 1e20)
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -345,7 +345,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double?)!, Double(300000))
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double), Double(300000))
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -362,7 +362,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double?)!, 1e20)
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double), 1e20)
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -381,7 +381,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Double?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertEqual((row.value(atIndex: 0) as String?)!, "foo")
|
||||
XCTAssertEqual((row.value(atIndex: 0) as String), "foo")
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -399,7 +399,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Int64?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Double?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Blob?), Blob(data: "foo".dataUsingEncoding(NSUTF8StringEncoding))!)
|
||||
XCTAssertEqual((row.value(atIndex: 0) as NSData?), "foo".dataUsingEncoding(NSUTF8StringEncoding))
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -433,7 +433,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double?)!, 0.0)
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double), 0.0)
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -456,7 +456,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double?)!, 0.0)
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double), 0.0)
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -479,7 +479,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double?)!, 0.0)
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double), 0.0)
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -502,7 +502,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double?)!, 0.0)
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double), 0.0)
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -521,7 +521,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Double?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertEqual((row.value(atIndex: 0) as String?)!, "3.0e+5")
|
||||
XCTAssertEqual((row.value(atIndex: 0) as String), "3.0e+5")
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -539,7 +539,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Int64?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Double?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Blob?), Blob(data: "foo".dataUsingEncoding(NSUTF8StringEncoding))!)
|
||||
XCTAssertEqual((row.value(atIndex: 0) as NSData?), "foo".dataUsingEncoding(NSUTF8StringEncoding))
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -587,7 +587,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double?)!, 0.0)
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double), 0.0)
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -610,7 +610,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double?)!, 0.0)
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double), 0.0)
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -633,7 +633,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double?)!, 0.0)
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double), 0.0)
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -656,7 +656,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double?)!, Double(300000))
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double), Double(300000))
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -673,7 +673,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double?)!, 1e20)
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double), 1e20)
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -696,7 +696,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double?)!, Double(300000))
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double), Double(300000))
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -713,7 +713,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double?)!, 1e20)
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Double), 1e20)
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -732,7 +732,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Double?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertEqual((row.value(atIndex: 0) as String?)!, "foo")
|
||||
XCTAssertEqual((row.value(atIndex: 0) as String), "foo")
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Blob?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertTrue((row.value(atIndex: 0) as NSData?) == nil)
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
@@ -750,7 +750,7 @@ class SQLiteStatementConversionTests : GRDBTestCase {
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Int64?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as Double?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertFalse((row.value(atIndex: 0) as String?) == nil) // incompatible with DatabaseValue conversion
|
||||
XCTAssertEqual((row.value(atIndex: 0) as Blob?), Blob(data: "foo".dataUsingEncoding(NSUTF8StringEncoding))!)
|
||||
XCTAssertEqual((row.value(atIndex: 0) as NSData?), "foo".dataUsingEncoding(NSUTF8StringEncoding))
|
||||
}
|
||||
return .Rollback
|
||||
}
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
import XCTest
|
||||
import GRDB
|
||||
|
||||
class NSDataTests: GRDBTestCase {
|
||||
|
||||
func testMemoryBehavior() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute("CREATE TABLE datas (data BLOB)")
|
||||
|
||||
let data = "foo".dataUsingEncoding(NSUTF8StringEncoding)
|
||||
try db.execute("INSERT INTO datas (data) VALUES (?)", arguments: [data])
|
||||
|
||||
for row in Row.fetch(db, "SELECT * FROM datas") {
|
||||
// This data should hold a copy of SQLite data. This can not be tested here: test it at runtime with a breakpoint.
|
||||
//
|
||||
// At commit 1022309f8079ed5072a92e340a1eab913d4f7d23, the data is actually copied *twice*.
|
||||
// This is not very efficient (TODO).
|
||||
//
|
||||
// The reason for it is:
|
||||
// 1. NSData does not adopt SQLiteStatementConvertible because the protocol can't be adopted by non-final classes.
|
||||
// (Maybe we can do something about it)
|
||||
// 2. Thus the value extraction of plain DatabaseValueConvertible is invoked.
|
||||
// 3. This involves creating a DatabaseValue, which makes a first copy of the data into a stand-alone Blob.
|
||||
// 4. The returned NSData returns a (second) copy of this Blob's data.
|
||||
let copiedData1: NSData = row.value(atIndex: 0)
|
||||
XCTAssertEqual(copiedData1, data)
|
||||
|
||||
// This data should hold a copy of the blob data.
|
||||
let blob: Blob = row.value(atIndex: 0)
|
||||
let copiedData2 = blob.data
|
||||
XCTAssertEqual(copiedData2, data)
|
||||
XCTAssertNotEqual(copiedData2.bytes, blob.bytes)
|
||||
|
||||
// This data does not hold any copy:
|
||||
let nonCopiedData = row.dataNoCopy(atIndex: 0)
|
||||
XCTAssertEqual(nonCopiedData, data)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -124,8 +124,6 @@
|
||||
56A238A31B9C753B0082EB20 /* DatabaseStorable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238A01B9C753B0082EB20 /* DatabaseStorable.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
56A238A41B9C753B0082EB20 /* Record.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238A11B9C753B0082EB20 /* Record.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
56A238A51B9C753B0082EB20 /* Record.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238A11B9C753B0082EB20 /* Record.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
56A238A71B9C760C0082EB20 /* Blob.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238A61B9C760C0082EB20 /* Blob.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
56A238A81B9C760C0082EB20 /* Blob.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238A61B9C760C0082EB20 /* Blob.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
56A238AA1B9C7E540082EB20 /* StatementArguments+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238A91B9C7E540082EB20 /* StatementArguments+Foundation.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
56A238AB1B9C7E540082EB20 /* StatementArguments+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238A91B9C7E540082EB20 /* StatementArguments+Foundation.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
56A238AD1B9C90090082EB20 /* NSNull.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238AC1B9C90090082EB20 /* NSNull.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
@@ -154,10 +152,8 @@
|
||||
56BB862C1BA988F2001F9168 /* FMResultSet.m in Sources */ = {isa = PBXBuildFile; fileRef = 56BB86221BA988F2001F9168 /* FMResultSet.m */; settings = {ASSET_TAGS = (); }; };
|
||||
56BB862F1BA98AA9001F9168 /* libsqlite3.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 56BB862E1BA98AA9001F9168 /* libsqlite3.tbd */; };
|
||||
56BB86311BA98AB0001F9168 /* libsqlite3.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 56BB86301BA98AB0001F9168 /* libsqlite3.tbd */; };
|
||||
56CA21B71BB3F89F009A04C5 /* NSDataTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CA21B61BB3F89F009A04C5 /* NSDataTests.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
56CA21B81BB3F89F009A04C5 /* NSDataTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CA21B61BB3F89F009A04C5 /* NSDataTests.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
56CA21BA1BB3FCF7009A04C5 /* BlobTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CA21B91BB3FCF7009A04C5 /* BlobTests.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
56CA21BB1BB3FCF7009A04C5 /* BlobTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CA21B91BB3FCF7009A04C5 /* BlobTests.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
56CA21BA1BB3FCF7009A04C5 /* NSDataTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CA21B91BB3FCF7009A04C5 /* NSDataTests.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
56CA21BB1BB3FCF7009A04C5 /* NSDataTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CA21B91BB3FCF7009A04C5 /* NSDataTests.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
56CA221D1BB41533009A04C5 /* FMDBPerformanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CA221A1BB41533009A04C5 /* FMDBPerformanceTests.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
56CA22201BB4153F009A04C5 /* StephenCelisSQLitePerformanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CA221B1BB41533009A04C5 /* StephenCelisSQLitePerformanceTests.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
56CA22221BB41565009A04C5 /* PerformanceRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CA22211BB41565009A04C5 /* PerformanceRecord.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
@@ -345,7 +341,6 @@
|
||||
56A238981B9C752B0082EB20 /* RowConvertible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowConvertible.swift; sourceTree = "<group>"; };
|
||||
56A238A01B9C753B0082EB20 /* DatabaseStorable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseStorable.swift; sourceTree = "<group>"; };
|
||||
56A238A11B9C753B0082EB20 /* Record.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Record.swift; sourceTree = "<group>"; };
|
||||
56A238A61B9C760C0082EB20 /* Blob.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Blob.swift; sourceTree = "<group>"; };
|
||||
56A238A91B9C7E540082EB20 /* StatementArguments+Foundation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "StatementArguments+Foundation.swift"; sourceTree = "<group>"; };
|
||||
56A238AC1B9C90090082EB20 /* NSNull.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSNull.swift; sourceTree = "<group>"; };
|
||||
56A238AF1B9C90BB0082EB20 /* NSString.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSString.swift; sourceTree = "<group>"; };
|
||||
@@ -371,8 +366,7 @@
|
||||
56BB862D1BA98933001F9168 /* GRDBTests-Bridging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GRDBTests-Bridging.h"; sourceTree = "<group>"; };
|
||||
56BB862E1BA98AA9001F9168 /* libsqlite3.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libsqlite3.tbd; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.0.sdk/usr/lib/libsqlite3.tbd; sourceTree = DEVELOPER_DIR; };
|
||||
56BB86301BA98AB0001F9168 /* libsqlite3.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libsqlite3.tbd; path = usr/lib/libsqlite3.tbd; sourceTree = SDKROOT; };
|
||||
56CA21B61BB3F89F009A04C5 /* NSDataTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDataTests.swift; sourceTree = "<group>"; };
|
||||
56CA21B91BB3FCF7009A04C5 /* BlobTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlobTests.swift; sourceTree = "<group>"; };
|
||||
56CA21B91BB3FCF7009A04C5 /* NSDataTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDataTests.swift; sourceTree = "<group>"; };
|
||||
56CA21FE1BB414FE009A04C5 /* SQLite.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = SQLite.xcodeproj; path = SQLite.swift/SQLite.xcodeproj; sourceTree = "<group>"; };
|
||||
56CA221A1BB41533009A04C5 /* FMDBPerformanceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FMDBPerformanceTests.swift; sourceTree = "<group>"; };
|
||||
56CA221B1BB41533009A04C5 /* StephenCelisSQLitePerformanceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StephenCelisSQLitePerformanceTests.swift; sourceTree = "<group>"; };
|
||||
@@ -556,11 +550,11 @@
|
||||
56A238191B9C74A90082EB20 /* DatabaseValueConvertible */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
56CA21B91BB3FCF7009A04C5 /* BlobTests.swift */,
|
||||
56A238B51B9CA2590082EB20 /* DatabaseTimestampTests.swift */,
|
||||
56A2381B1B9C74A90082EB20 /* DatabaseValueConversionTests.swift */,
|
||||
56E8CE0C1BB4FA5600828BEC /* DatabaseValueConvertibleFetchTests.swift */,
|
||||
56A2381A1B9C74A90082EB20 /* DatabaseValueConvertibleSubclassTests.swift */,
|
||||
56CA21B91BB3FCF7009A04C5 /* NSDataTests.swift */,
|
||||
56A2381C1B9C74A90082EB20 /* RawRepresentableTests.swift */,
|
||||
);
|
||||
path = DatabaseValueConvertible;
|
||||
@@ -628,7 +622,6 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
56A238951B9C752B0082EB20 /* Protocols */,
|
||||
56A238A61B9C760C0082EB20 /* Blob.swift */,
|
||||
56A238701B9C75030082EB20 /* Configuration.swift */,
|
||||
56A238711B9C75030082EB20 /* Database.swift */,
|
||||
56A238721B9C75030082EB20 /* DatabaseChanges.swift */,
|
||||
@@ -756,7 +749,6 @@
|
||||
56F0B98C1B6001C600A2F135 /* Foundation */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
56CA21B61BB3F89F009A04C5 /* NSDataTests.swift */,
|
||||
56F0B98D1B6001C600A2F135 /* NSDateComponentsTests.swift */,
|
||||
56F0B98E1B6001C600A2F135 /* NSDateTests.swift */,
|
||||
565D5D701BBC694D00DC9BD4 /* Row+FoundationTests.swift */,
|
||||
@@ -1114,7 +1106,6 @@
|
||||
56A2380D1B9C6E440082EB20 /* NSData.swift in Sources */,
|
||||
56A2387C1B9C75030082EB20 /* Configuration.swift in Sources */,
|
||||
56A238B11B9C90BB0082EB20 /* NSString.swift in Sources */,
|
||||
56A238A81B9C760C0082EB20 /* Blob.swift in Sources */,
|
||||
56A238B41B9C919D0082EB20 /* NSNumber.swift in Sources */,
|
||||
56A2388C1B9C75030082EB20 /* Statement.swift in Sources */,
|
||||
56A238A31B9C753B0082EB20 /* DatabaseStorable.swift in Sources */,
|
||||
@@ -1157,7 +1148,6 @@
|
||||
56A2386E1B9C74A90082EB20 /* RecordUpdateFromRowTests.swift in Sources */,
|
||||
56A238541B9C74A90082EB20 /* PrimaryKeyMultipleTests.swift in Sources */,
|
||||
56FDECE31BB32DFD009AD709 /* MetalRowTests.swift in Sources */,
|
||||
56CA21B81BB3F89F009A04C5 /* NSDataTests.swift in Sources */,
|
||||
56BB862A1BA988F2001F9168 /* FMDatabaseQueue.m in Sources */,
|
||||
56A238681B9C74A90082EB20 /* RecordInitializersTests.swift in Sources */,
|
||||
565B0FFC1BBC80720098DE03 /* RowConvertible+FoundationTests.swift in Sources */,
|
||||
@@ -1182,7 +1172,7 @@
|
||||
56EE57401BB31DBA007A6A95 /* GRDBPerformanceTests.swift in Sources */,
|
||||
56A2383C1B9C74A90082EB20 /* DatabaseErrorTests.swift in Sources */,
|
||||
56A238521B9C74A90082EB20 /* MinimalPrimaryKeySingleTests.swift in Sources */,
|
||||
56CA21BB1BB3FCF7009A04C5 /* BlobTests.swift in Sources */,
|
||||
56CA21BB1BB3FCF7009A04C5 /* NSDataTests.swift in Sources */,
|
||||
56A238B71B9CA2590082EB20 /* DatabaseTimestampTests.swift in Sources */,
|
||||
565D5D751BBC70AE00DC9BD4 /* StatementArguments+FoundationTests.swift in Sources */,
|
||||
56F0B9921B6001C600A2F135 /* NSDateTests.swift in Sources */,
|
||||
@@ -1217,7 +1207,6 @@
|
||||
56BB862B1BA988F2001F9168 /* FMResultSet.m in Sources */,
|
||||
56A2386D1B9C74A90082EB20 /* RecordUpdateFromRowTests.swift in Sources */,
|
||||
56FDECE21BB32DFD009AD709 /* MetalRowTests.swift in Sources */,
|
||||
56CA21B71BB3F89F009A04C5 /* NSDataTests.swift in Sources */,
|
||||
56A238531B9C74A90082EB20 /* PrimaryKeyMultipleTests.swift in Sources */,
|
||||
56BB86291BA988F2001F9168 /* FMDatabaseQueue.m in Sources */,
|
||||
56A238671B9C74A90082EB20 /* RecordInitializersTests.swift in Sources */,
|
||||
@@ -1244,7 +1233,7 @@
|
||||
56E8CE101BB4FE5B00828BEC /* SQLiteStatementConvertibleFetchTests.swift in Sources */,
|
||||
56A238511B9C74A90082EB20 /* MinimalPrimaryKeySingleTests.swift in Sources */,
|
||||
56DAC7101BB504400010FE01 /* StephenCelisSQLitePerformanceTests.swift in Sources */,
|
||||
56CA21BA1BB3FCF7009A04C5 /* BlobTests.swift in Sources */,
|
||||
56CA21BA1BB3FCF7009A04C5 /* NSDataTests.swift in Sources */,
|
||||
56A238B61B9CA2590082EB20 /* DatabaseTimestampTests.swift in Sources */,
|
||||
56DAC70F1BB504400010FE01 /* GRDBPerformanceTests.swift in Sources */,
|
||||
565D5D741BBC70AE00DC9BD4 /* StatementArguments+FoundationTests.swift in Sources */,
|
||||
@@ -1276,7 +1265,6 @@
|
||||
56A238811B9C75030082EB20 /* DatabaseError.swift in Sources */,
|
||||
56A238991B9C752B0082EB20 /* DatabaseTableMapping.swift in Sources */,
|
||||
56A238851B9C75030082EB20 /* DatabaseValue.swift in Sources */,
|
||||
56A238A71B9C760C0082EB20 /* Blob.swift in Sources */,
|
||||
56A238A41B9C753B0082EB20 /* Record.swift in Sources */,
|
||||
56A2388F1B9C75030082EB20 /* UpdateStatement.swift in Sources */,
|
||||
56A238091B9C6DD20082EB20 /* StandardLibraryTypes.swift in Sources */,
|
||||
|
||||
@@ -1,136 +0,0 @@
|
||||
/// A Database Blob
|
||||
public final class Blob : Equatable {
|
||||
|
||||
/// A pointer to the blob's contents.
|
||||
public let bytes: UnsafePointer<Void>
|
||||
|
||||
/// The number of bytes in the blob.
|
||||
public let length: Int
|
||||
|
||||
let freeWhenDone: Bool
|
||||
|
||||
/**
|
||||
Returns a Blob containing *length* bytes copied from the buffer *bytes*.
|
||||
|
||||
Returns nil if length is zero (SQLite can't store empty blobs).
|
||||
|
||||
- parameter bytes: A buffer containing blob data.
|
||||
- parameter length: The number of bytes to copy from *bytes*. This value
|
||||
must not exceed the length of bytes. If zero, the result is nil.
|
||||
*/
|
||||
public init?(bytes: UnsafePointer<Void>, length: Int) {
|
||||
guard length > 0 else {
|
||||
// SQLite can't store empty blobs
|
||||
self.bytes = nil
|
||||
self.length = 0
|
||||
self.freeWhenDone = false
|
||||
return nil
|
||||
}
|
||||
|
||||
let copy = UnsafeMutablePointer<RawByte>.alloc(length)
|
||||
copy.initializeFrom(unsafeBitCast(bytes, UnsafeMutablePointer<RawByte>.self), count: length)
|
||||
self.bytes = unsafeBitCast(copy, UnsafePointer<Void>.self)
|
||||
self.length = length
|
||||
self.freeWhenDone = true
|
||||
}
|
||||
|
||||
/**
|
||||
Returns a Blob containing *length* bytes from the buffer *bytes*.
|
||||
|
||||
Returns nil if length is zero (SQLite can't store empty blobs).
|
||||
|
||||
- parameter bytes: A buffer containing blob data.
|
||||
- parameter length: The number of bytes. If zero, the result is nil.
|
||||
- parameter freeWhenDone: If true, the returned object takes ownership of
|
||||
the bytes pointer and frees it on deallocation.
|
||||
*/
|
||||
public init?(bytesNoCopy bytes: UnsafePointer<Void>, length: Int, freeWhenDone: Bool) {
|
||||
guard length > 0 else {
|
||||
// SQLite can't store empty blobs
|
||||
self.bytes = nil
|
||||
self.length = 0
|
||||
self.freeWhenDone = false
|
||||
return nil
|
||||
}
|
||||
|
||||
self.bytes = bytes
|
||||
self.length = length
|
||||
self.freeWhenDone = freeWhenDone
|
||||
}
|
||||
|
||||
deinit {
|
||||
if freeWhenDone {
|
||||
unsafeBitCast(bytes, UnsafeMutablePointer<RawByte>.self).dealloc(length)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - DatabaseValueConvertible
|
||||
|
||||
/// Blob adopts DatabaseValueConvertible and SQLiteStatementConvertible.
|
||||
extension Blob : DatabaseValueConvertible, SQLiteStatementConvertible {
|
||||
|
||||
/**
|
||||
Returns an instance initialized from a raw SQLite statement pointer.
|
||||
|
||||
- parameter sqliteStatement: A pointer to a SQLite statement.
|
||||
- parameter index: The column index.
|
||||
*/
|
||||
public convenience init(sqliteStatement: SQLiteStatement, index: Int32) {
|
||||
// Metal Blob does not copy and does not take ownership of the statement bytes.
|
||||
let bytes = sqlite3_column_blob(sqliteStatement, index)
|
||||
let length = sqlite3_column_bytes(sqliteStatement, index)
|
||||
self.init(bytesNoCopy: bytes, length: Int(length), freeWhenDone: false)!
|
||||
}
|
||||
|
||||
/// Returns a value that can be stored in the database.
|
||||
public var databaseValue: DatabaseValue {
|
||||
return DatabaseValue(blob: self)
|
||||
}
|
||||
|
||||
/**
|
||||
Returns the Blob contained in *databaseValue*, if any.
|
||||
|
||||
- parameter databaseValue: A DatabaseValue.
|
||||
- returns: An optional Blob.
|
||||
*/
|
||||
public static func fromDatabaseValue(databaseValue: DatabaseValue) -> Blob? {
|
||||
switch databaseValue.storage {
|
||||
case .Blob(let blob):
|
||||
return blob
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - BlobImpl
|
||||
|
||||
// The protocol for Blob underlying implementation
|
||||
protocol BlobImpl {
|
||||
var bytes: UnsafePointer<Void> { get }
|
||||
var length: Int { get }
|
||||
}
|
||||
|
||||
|
||||
// MARK: - CustomString
|
||||
|
||||
/// DatabaseValue adopts CustomStringConvertible.
|
||||
extension Blob : CustomStringConvertible {
|
||||
/// A textual representation of `self`.
|
||||
public var description: String {
|
||||
return "Blob(\(length) bytes)"
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Equatable
|
||||
|
||||
/// DatabaseValue adopts Equatable.
|
||||
public func ==(lhs: Blob, rhs: Blob) -> Bool {
|
||||
guard lhs.length == rhs.length else {
|
||||
return false
|
||||
}
|
||||
return memcmp(lhs.bytes, rhs.bytes, lhs.length) == 0
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import Foundation
|
||||
|
||||
// MARK: - DatabaseValue
|
||||
|
||||
/**
|
||||
@@ -39,8 +41,12 @@ public struct DatabaseValue : Equatable {
|
||||
/**
|
||||
Returns a DatabaseValue storing a Blob.
|
||||
*/
|
||||
public init(blob: Blob) {
|
||||
self.storage = .Blob(blob)
|
||||
public init(data: NSData) {
|
||||
if data.length == 0 {
|
||||
self.storage = .Null
|
||||
} else {
|
||||
self.storage = .Blob(data)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +56,7 @@ public struct DatabaseValue : Equatable {
|
||||
Returns the wrapped value.
|
||||
|
||||
If not nil (for the database NULL), its type is guaranteed to be one of the
|
||||
following: Int64, Double, String, and Blob.
|
||||
following: Int64, Double, String, and NSData.
|
||||
|
||||
let value = databaseValue.value()
|
||||
*/
|
||||
@@ -71,8 +77,8 @@ public struct DatabaseValue : Equatable {
|
||||
return double
|
||||
case .String(let string):
|
||||
return string
|
||||
case .Blob(let blob):
|
||||
return blob
|
||||
case .Blob(let data):
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,7 +147,7 @@ public struct DatabaseValue : Equatable {
|
||||
case String(Swift.String)
|
||||
|
||||
/// The BLOB storage class, wrapping a Blob.
|
||||
case Blob(GRDB.Blob)
|
||||
case Blob(NSData)
|
||||
|
||||
init(sqliteStatement: SQLiteStatement, index: Int) {
|
||||
switch sqlite3_column_type(sqliteStatement, Int32(index)) {
|
||||
@@ -157,7 +163,7 @@ public struct DatabaseValue : Equatable {
|
||||
case SQLITE_BLOB:
|
||||
let bytes = sqlite3_column_blob(sqliteStatement, Int32(index))
|
||||
let length = sqlite3_column_bytes(sqliteStatement, Int32(index))
|
||||
self = .Blob(GRDB.Blob(bytes: bytes, length: Int(length))!) // copy bytes
|
||||
self = .Blob(NSData(bytes: bytes, length: Int(length))) // copy bytes
|
||||
default:
|
||||
fatalError("Unexpected SQLite column type")
|
||||
}
|
||||
@@ -245,8 +251,8 @@ extension DatabaseValue : CustomStringConvertible {
|
||||
return String(double)
|
||||
case .String(let string):
|
||||
return String(reflecting: string)
|
||||
case .Blob(let blob):
|
||||
return blob.description
|
||||
case .Blob(let data):
|
||||
return data.description
|
||||
}
|
||||
}
|
||||
}
|
||||
+59
-2
@@ -1,3 +1,5 @@
|
||||
import Foundation
|
||||
|
||||
/**
|
||||
A database row.
|
||||
*/
|
||||
@@ -118,7 +120,7 @@ public final class Row: CollectionType {
|
||||
- returns: An optional *Value*.
|
||||
*/
|
||||
public func value<Value: DatabaseValueConvertible>(atIndex index: Int) -> Value? {
|
||||
return Value.fromDatabaseValue(impl.databaseValue(atIndex: index))
|
||||
return impl.databaseValue(atIndex: index).value()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,7 +148,7 @@ public final class Row: CollectionType {
|
||||
}
|
||||
} else {
|
||||
// Detached row
|
||||
return Value.fromDatabaseValue(impl.databaseValue(atIndex: index))
|
||||
return impl.databaseValue(atIndex: index).value()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -338,6 +340,39 @@ public final class Row: CollectionType {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Extracting NSData
|
||||
|
||||
/**
|
||||
Returns the optional NSData stored at index *index*.
|
||||
|
||||
The returned data does not owns its bytes, and must not be used longer than
|
||||
the row's lifetime.
|
||||
|
||||
- parameter index: The index of a column.
|
||||
- returns: An optional NSData.
|
||||
*/
|
||||
public func dataNoCopy(atIndex index:Int) -> NSData? {
|
||||
return impl.dataNoCopy(atIndex: index)
|
||||
}
|
||||
|
||||
/**
|
||||
Returns the optional NSData stored at column *name*.
|
||||
|
||||
The returned data does not owns its bytes, and must not be used longer than
|
||||
the row's lifetime.
|
||||
|
||||
- parameter name: A column name.
|
||||
- returns: An NSData.
|
||||
*/
|
||||
public func dataNoCopy(named columnName: String) -> NSData? {
|
||||
if let index = impl.indexForColumn(named: columnName) {
|
||||
return dataNoCopy(atIndex: index)
|
||||
} else {
|
||||
fatalError("No such column: \(String(reflecting: columnName))")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Extracting DatabaseValue
|
||||
|
||||
/**
|
||||
@@ -614,6 +649,10 @@ public final class Row: CollectionType {
|
||||
return databaseDictionary.count
|
||||
}
|
||||
|
||||
func dataNoCopy(atIndex index:Int) -> NSData? {
|
||||
return databaseValue(atIndex: index).value()
|
||||
}
|
||||
|
||||
func databaseValue(atIndex index: Int) -> DatabaseValue {
|
||||
return databaseDictionary[databaseDictionary.startIndex.advancedBy(index)].1
|
||||
}
|
||||
@@ -655,6 +694,10 @@ public final class Row: CollectionType {
|
||||
return columnNames.count
|
||||
}
|
||||
|
||||
func dataNoCopy(atIndex index:Int) -> NSData? {
|
||||
return databaseValue(atIndex: index).value()
|
||||
}
|
||||
|
||||
func databaseValue(atIndex index: Int) -> DatabaseValue {
|
||||
return databaseValues[index]
|
||||
}
|
||||
@@ -691,6 +734,15 @@ public final class Row: CollectionType {
|
||||
return Int(sqlite3_column_count(sqliteStatement))
|
||||
}
|
||||
|
||||
func dataNoCopy(atIndex index:Int) -> NSData? {
|
||||
guard sqlite3_column_type(sqliteStatement, Int32(index)) != SQLITE_NULL else {
|
||||
return nil
|
||||
}
|
||||
let bytes = sqlite3_column_blob(sqliteStatement, Int32(index))
|
||||
let length = sqlite3_column_bytes(sqliteStatement, Int32(index))
|
||||
return NSData(bytesNoCopy: UnsafeMutablePointer(bytes), length: Int(length), freeWhenDone: false)
|
||||
}
|
||||
|
||||
func databaseValue(atIndex index: Int) -> DatabaseValue {
|
||||
return DatabaseValue(sqliteStatement: sqliteStatement, index: index)
|
||||
}
|
||||
@@ -720,6 +772,10 @@ public final class Row: CollectionType {
|
||||
fatalError("Empty row has no column")
|
||||
}
|
||||
|
||||
func dataNoCopy(atIndex index:Int) -> NSData? {
|
||||
fatalError("Empty row has no column")
|
||||
}
|
||||
|
||||
func columnName(atIndex index: Int) -> String {
|
||||
fatalError("Empty row has no column")
|
||||
}
|
||||
@@ -756,6 +812,7 @@ extension Row: CustomStringConvertible {
|
||||
protocol RowImpl {
|
||||
var count: Int { get }
|
||||
func databaseValue(atIndex index: Int) -> DatabaseValue
|
||||
func dataNoCopy(atIndex index:Int) -> NSData?
|
||||
func columnName(atIndex index: Int) -> String
|
||||
func indexForColumn(named name: String) -> Int? // This method MUST be case-insensitive.
|
||||
func detachedRow(row: Row) -> Row // The row argument has the receiver as an impl.
|
||||
|
||||
@@ -2,11 +2,7 @@
|
||||
extension NSData : DatabaseValueConvertible {
|
||||
/// Returns a value that can be stored in the database.
|
||||
public var databaseValue: DatabaseValue {
|
||||
if let blob = Blob(data: self) {
|
||||
return DatabaseValue(blob: blob)
|
||||
} else {
|
||||
return .Null
|
||||
}
|
||||
return DatabaseValue(data: self)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -18,101 +14,11 @@ extension NSData : DatabaseValueConvertible {
|
||||
- returns: An optional NSData.
|
||||
*/
|
||||
public static func fromDatabaseValue(databaseValue: DatabaseValue) -> Self? {
|
||||
if let blob = Blob.fromDatabaseValue(databaseValue) {
|
||||
return self.init(data: NSData(bytes: blob.bytes, length: blob.length))
|
||||
} else {
|
||||
switch databaseValue.storage {
|
||||
case .Blob(let data):
|
||||
return self.init(data: data)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Blob support for NSData
|
||||
extension Blob {
|
||||
|
||||
/**
|
||||
Creates a Blob from NSData.
|
||||
|
||||
Returns nil if and only if *data* is nil or zero-length (SQLite can't store
|
||||
empty blobs).
|
||||
|
||||
The data is *copied*.
|
||||
|
||||
- parameter data: An NSData
|
||||
*/
|
||||
public convenience init?(data: NSData?) {
|
||||
guard let data = data where data.length > 0 else { // oddly enough, if data.length is not tested here, we have a runtime crash.
|
||||
return nil
|
||||
}
|
||||
self.init(bytes: data.bytes, length: data.length)
|
||||
}
|
||||
|
||||
/**
|
||||
Returns an NSData.
|
||||
|
||||
The data is *copied*. See also the dataNoCopy property.
|
||||
*/
|
||||
public var data: NSData {
|
||||
return NSData(bytes: bytes, length: length)
|
||||
}
|
||||
|
||||
/**
|
||||
Returns an NSData.
|
||||
|
||||
The returned data does not owns its bytes, and must not be used longer than
|
||||
the blob's lifetime.
|
||||
|
||||
To get a long-lived NSData, see the data property.
|
||||
*/
|
||||
public var dataNoCopy: NSData {
|
||||
return NSData(bytesNoCopy: UnsafeMutablePointer(bytes), length: length, freeWhenDone: false)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Row support for NSData
|
||||
extension Row {
|
||||
|
||||
/**
|
||||
Returns the optional NSData stored at index *index*.
|
||||
|
||||
The returned data does not owns its bytes, and must not be used longer than
|
||||
the row's lifetime.
|
||||
|
||||
- parameter index: The index of a column.
|
||||
- returns: An optional NSData.
|
||||
*/
|
||||
public func dataNoCopy(atIndex index:Int) -> NSData? {
|
||||
return (value(atIndex: index) as Blob?)?.dataNoCopy
|
||||
}
|
||||
|
||||
/**
|
||||
Returns the optional NSData stored at column *name*.
|
||||
|
||||
The returned data does not owns its bytes, and must not be used longer than
|
||||
the row's lifetime.
|
||||
|
||||
- parameter name: A column name.
|
||||
- returns: An NSData.
|
||||
*/
|
||||
public func dataNoCopy(named name:String) -> NSData? {
|
||||
return (value(named: name) as Blob?)?.dataNoCopy
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// DatabaseValue support for NSData
|
||||
extension DatabaseValue {
|
||||
|
||||
/**
|
||||
Returns an optional NSData.
|
||||
|
||||
The returned data does not owns its bytes, and must not be used longer than
|
||||
the original row's lifetime.
|
||||
|
||||
To get a long-lived NSData, use the regular value extraction method:
|
||||
`databaseValue.value() as NSData?`.
|
||||
*/
|
||||
public var dataNoCopy: NSData? {
|
||||
return (value() as Blob?)?.dataNoCopy
|
||||
}
|
||||
}
|
||||
@@ -46,8 +46,8 @@ extension Row {
|
||||
dictionary[columnName] = NSNumber(double: double)
|
||||
case .String(let string):
|
||||
dictionary[columnName] = string as NSString
|
||||
case .Blob(let blob):
|
||||
dictionary[columnName] = blob.data
|
||||
case .Blob(let data):
|
||||
dictionary[columnName] = data
|
||||
}
|
||||
}
|
||||
return dictionary
|
||||
|
||||
Reference in new issue
Block a user