From 692a6bc28d777952c8fdcb16fa05e670aa38af26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 4 Jul 2015 17:02:43 +0200 Subject: [PATCH] DatabaseDate -> DBDate --- GRDB Tests/GRDBTests.swift | 8 ++-- .../RowModel/PrimaryKeySQLiteRowIDTests.swift | 4 +- .../CustomSQLiteValueConvertibleTests.swift | 6 +-- README.md | 47 ++++++++++--------- 4 files changed, 35 insertions(+), 30 deletions(-) diff --git a/GRDB Tests/GRDBTests.swift b/GRDB Tests/GRDBTests.swift index 5272fb063..0b5c561d6 100644 --- a/GRDB Tests/GRDBTests.swift +++ b/GRDB Tests/GRDBTests.swift @@ -9,14 +9,14 @@ import XCTest import GRDB -struct DatabaseDate: SQLiteValueConvertible { +struct DBDate: SQLiteValueConvertible { - // MARK: - DatabaseDate <-> NSDate conversion + // MARK: - DBDate <-> NSDate conversion let date: NSDate // Define a failable initializer in order to consistently use nil as the - // NULL marker throughout the conversions NSDate <-> DatabaseDate <-> SQLite + // NULL marker throughout the conversions NSDate <-> DBDate <-> SQLite init?(_ date: NSDate?) { if let date = date { self.date = date @@ -25,7 +25,7 @@ struct DatabaseDate: SQLiteValueConvertible { } } - // MARK: - DatabaseDate <-> SQLiteValue conversion + // MARK: - DBDate <-> SQLiteValue conversion var sqliteValue: SQLiteValue { return .Real(date.timeIntervalSince1970) diff --git a/GRDB Tests/RowModel/PrimaryKeySQLiteRowIDTests.swift b/GRDB Tests/RowModel/PrimaryKeySQLiteRowIDTests.swift index feaa6b06e..2d5ee8819 100644 --- a/GRDB Tests/RowModel/PrimaryKeySQLiteRowIDTests.swift +++ b/GRDB Tests/RowModel/PrimaryKeySQLiteRowIDTests.swift @@ -28,7 +28,7 @@ class Person: RowModel { "id": id, "name": name, "age": age, - "creationTimestamp": DatabaseDate(creationDate), + "creationTimestamp": DBDate(creationDate), ] } @@ -37,7 +37,7 @@ class Person: RowModel { if row.hasColumn("name") { name = row.value(named: "name") } if row.hasColumn("age") { age = row.value(named: "age") } if row.hasColumn("creationTimestamp") { - let dbDate: DatabaseDate? = row.value(named: "creationTimestamp") + let dbDate: DBDate? = row.value(named: "creationTimestamp") creationDate = dbDate?.date } } diff --git a/GRDB Tests/SQLiteValueConvertible/CustomSQLiteValueConvertibleTests.swift b/GRDB Tests/SQLiteValueConvertible/CustomSQLiteValueConvertibleTests.swift index 3ab409be0..942e854a6 100644 --- a/GRDB Tests/SQLiteValueConvertible/CustomSQLiteValueConvertibleTests.swift +++ b/GRDB Tests/SQLiteValueConvertible/CustomSQLiteValueConvertibleTests.swift @@ -38,18 +38,18 @@ class CustomSQLiteValueConvertibleTests: GRDBTests { dateComponents.month = 09 dateComponents.day = 18 let date = calendar.dateFromComponents(dateComponents)! - try db.execute("INSERT INTO stuffs (creationTimestamp) VALUES (?)", bindings: [DatabaseDate(date)]) + try db.execute("INSERT INTO stuffs (creationTimestamp) VALUES (?)", bindings: [DBDate(date)]) } do { let row = db.fetchOneRow("SELECT creationTimestamp FROM stuffs")! - let date: DatabaseDate = row.value(atIndex: 0)! + let date: DBDate = row.value(atIndex: 0)! let year = calendar.component(NSCalendarUnit.Year, fromDate: date.date) XCTAssertEqual(year, 1973) } do { - let date = db.fetchOne(DatabaseDate.self, "SELECT creationTimestamp FROM stuffs")! + let date = db.fetchOne(DBDate.self, "SELECT creationTimestamp FROM stuffs")! let year = calendar.component(NSCalendarUnit.Year, fromDate: date.date) XCTAssertEqual(year, 1973) } diff --git a/README.md b/README.md index 55f5750a4..876688772 100644 --- a/README.md +++ b/README.md @@ -238,17 +238,17 @@ Your custom types can perform their own conversions to and from SQLite storage c A custom type that can be represented as a [SQLIte datatype](https://www.sqlite.org/datatype3.html) (INTEGER, REAL, TEXT, and BLOB) gets full support from GRDB.swift by adopting the `SQLiteValueConvertible` protocol. It can be used wherever the built-in types `Int`, `String`, etc. are used, without any limitation or caveat. -For example, let's define below the `DatabaseDate` type that stores and loads NSDates as timestamps: +For example, let's define below the `DBDate` type that stores and loads NSDates as timestamps: ```swift -struct DatabaseDate: SQLiteValueConvertible { +struct DBDate: SQLiteValueConvertible { - // MARK: - DatabaseDate <-> NSDate conversion + // MARK: - DBDate <-> NSDate conversion let date: NSDate // Define a failable initializer in order to consistently use nil as the - // NULL marker throughout the conversions NSDate <-> DatabaseDate <-> SQLite + // NULL marker throughout the conversions NSDate <-> DBDate <-> SQLite init?(_ date: NSDate?) { if let date = date { self.date = date @@ -257,7 +257,7 @@ struct DatabaseDate: SQLiteValueConvertible { } } - // MARK: - DatabaseDate <-> SQLiteValue conversion + // MARK: - DBDate <-> SQLiteValue conversion var sqliteValue: SQLiteValue { return .Real(date.timeIntervalSince1970) @@ -274,23 +274,28 @@ struct DatabaseDate: SQLiteValueConvertible { } } -// Write +dbQueue.inDatabase { db in -let date = NSDate() -try db.execute("INSERT INTO persons (timestamp, ...) " + - "VALUES (?, ...)", - bindings: [DatabaseDate(date), ...]) + // Write -// Read from row + let date = NSDate() + try db.execute("INSERT INTO persons (timestamp, ...) " + + "VALUES (?, ...)", + bindings: [DBDate(date), ...]) -let row = db.fetchOneRow("SELECT * FROM persons")! -let dbDate: DatabaseDate? = row.value(named: "timestamp") -let date = dbDate?.date + // Read from row -// Direct read + for rows in db.fetchRows("SELECT * FROM persons") { + let dbDate: DBDate? = row.value(named: "timestamp") + let date = dbDate?.date + } -let dbDate = db.fetchOne(DatabaseDate.self, "SELECT timestamp ...") -let date = dbDate?.date + // Direct read + + db.fetch(DBDate.self, "SELECT ...", bindings: ...) // AnySequence + db.fetchAll(DBDate.self, "SELECT ...", bindings: ...) // [DBDate?] + db.fetchOne(DBDate.self, "SELECT ...", bindings: ...) // DBDate? +} ``` The `sqliteValue` property feeds SQLite with its own food (NULL, INTEGER, REAL, TEXT or BLOB). Yet the *actual* value stored on the database depends on the *column affinity*. That value is usually the one you expect, fortunately. Yet, for reference: https://www.sqlite.org/datatype3.html#affinity. @@ -423,9 +428,9 @@ class Person : RowModel { if row.hasColumn("age") { age = row.value(named: "age") } // Int if row.hasColumn("name") { name = row.value(named: "name") } // String if row.hasColumn("creationTimestamp") { // NSDate - // The custom type DatabaseDate that we have declared above turns + // The custom type DBDate that we have declared above turns // out handy: - let dbDate: DatabaseDate? = row.value(named: "creationTimestamp") + let dbDate: DBDate? = row.value(named: "creationTimestamp") creationDate = dbDate?.date } } @@ -533,8 +538,8 @@ class Person : RowModel { "id": id, "name": name, "age": age, - // The custom type DatabaseDate has been declared above. - "creationTimestamp": DatabaseDate(creationDate), + // The custom type DBDate has been declared above. + "creationTimestamp": DBDate(creationDate), ] } }