From c563e86eeaf6d3ecf889480200a40de9ddf44689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 7 Oct 2015 08:08:26 +0200 Subject: [PATCH] README.md --- README.md | 109 ++++++++++++++++++------------------------------------ 1 file changed, 37 insertions(+), 72 deletions(-) diff --git a/README.md b/README.md index 6a6fbb7fc..9c6998bc3 100644 --- a/README.md +++ b/README.md @@ -531,6 +531,43 @@ Here is the support provided by GRDB.swift for the various [date formats](https: > > Yet, this format may not fit your needs. We provide [below](#custom-value-types) some sample code for storing dates as timestamps. You can adapt it for your application. +Declare DATETIME columns in your tables: + +```swift +try db.execute( + "CREATE TABLE persons (" + + "creationDate DATETIME, " + + "...)") +``` + +Store NSDate into the database: + +```swift +let creationDate = NSDate() +try db.execute("INSERT INTO persons (creationDate, ...) " + + "VALUES (?, ...)", + arguments: [creationDate, ...]) +``` + +Extract NSDate from the database: + +```swift +let row = Row.fetchOne(db, "SELECT creationDate, ...")! +let date: NSDate = row.value(named: "creationDate") + +NSDate.fetch(db, "SELECT ...") // DatabaseSequence +NSDate.fetchAll(db, "SELECT ...") // [NSDate] +NSDate.fetchOne(db, "SELECT ...") // NSDate? +``` + +See [Value Queries](#value-queries) for more detail on value fetching. + + +#### NSDateComponents + +NSDateComponents is indirectly supported, through the **DatabaseDateComponents** helper type. + +DatabaseDateComponents reads date components from all [date formats supported by SQLite](https://www.sqlite.org/lang_datefunc.html), and stores them in the format of your choice, from HH:MM to YYYY-MM-DD HH:MM:SS.SSS. Declare DATETIME columns in your tables: @@ -541,54 +578,6 @@ try db.execute( "...)") ``` -Store NSDate into the database: - -```swift -let birthDate = NSDate() -try db.execute("INSERT INTO persons (birthDate, ...) " + - "VALUES (?, ...)", - arguments: [birthDate, ...]) -``` - -Extract NSDate from the database: - -```swift -let row = Row.fetchOne(db, "SELECT birthDate, ...")! -let date: NSDate = row.value(named: "birthDate") - -NSDate.fetch(db, "SELECT ...") // DatabaseSequence -NSDate.fetchAll(db, "SELECT ...") // [NSDate] -NSDate.fetchOne(db, "SELECT ...") // NSDate? -``` - -See [Value Queries](#value-queries) for more detail on value fetching. - -Use NSDate in a Record (see [Fetching Records](#fetching-records) for more information): - -```swift -class Person : Record { - var birthDate: NSDate? - - override var storedDatabaseDictionary: [String: DatabaseValueConvertible?] { - return ["birthDate": birthDate, ...] - } - - override func updateFromRow(row: Row) { - if let dbv = row["birthDate"] { birthDate = dbv.value() } - ... - } -} -``` - -One could reasonably wonder if NSDate is a suitable type for a birth date. Well, NSDateComponents has built-in support in GRDB as well: - - -#### NSDateComponents - -NSDateComponents is indirectly supported, through the **DatabaseDateComponents** helper type. - -DatabaseDateComponents reads date components from all [date formats supported by SQLite](https://www.sqlite.org/lang_datefunc.html), and stores them in the format of your choice, from HH:MM to YYYY-MM-DD HH:MM:SS.SSS. - Store NSDateComponents into the database: ```swift @@ -619,30 +608,6 @@ DatabaseDateComponents.fetchOne(db, "SELECT ...") // DatabaseDateComponents? See [Value Queries](#value-queries) for more detail on value fetching. -Use NSDateComponents in a Record (see [Fetching Records](#fetching-records) for more information): - -```swift -class Person : Record { - var birthDateComponents: NSDateComponents? - - override var storedDatabaseDictionary: [String: DatabaseValueConvertible?] { - // Store birth date as YYYY-MM-DD: - let dbComponents = DatabaseDateComponents( - birthDateComponents, - format: .YMD) - return ["birthDate": dbComponents, ...] - } - - override func updateFromRow(row: Row) { - if let dbv = row["birthDate"] { - let dbComponents = dbv.value() as DatabaseDateComponents? - birthDateComponents = dbComponents?.dateComponents - } - ... - } -} -``` - ### Swift Enums