From ba4a729ba9be7b189a2f77397f25035af8dc9956 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 24 Sep 2015 08:35:35 +0200 Subject: [PATCH] Row documentation --- GRDB/Core/Row.swift | 132 ++++++++++++++++++++++++++++++-------------- 1 file changed, 91 insertions(+), 41 deletions(-) diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index bef2f0753..ffdf8439a 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -78,8 +78,6 @@ public class Row: CollectionType { Indexes span from 0 for the leftmost column to (row.count - 1) for the righmost column. - TODO: the doc below is obsolete. - The conversion returns nil if the fetched SQLite value is NULL, or can't be converted to the requested type: @@ -90,26 +88,11 @@ public class Row: CollectionType { **WARNING**: type casting requires a very careful use of the `as` operator (see [rdar://21676393](http://openradar.appspot.com/radar?id=4951414862249984)): - row.value(atIndex: 0)! as Int // OK: Int + row.value(atIndex: 0) as Int // OK: Int row.value(atIndex: 0) as Int? // OK: Int? row.value(atIndex: 0) as? Int // NO NO NO DON'T DO THAT! row.value(atIndex: 0) as! Int // NO NO NO DON'T DO THAT! - Your custom types that adopt the DatabaseValueConvertible protocol handle - their own conversion from raw SQLite values. Yet, here is the reference for - built-in types: - - SQLite value: | NULL INTEGER REAL TEXT BLOB - --------------|--------------------------------------------------------- - Bool | nil false if 0 false if 0.0 nil nil - Int | nil Int(*) Int(*) nil nil - Int64 | nil Int64 Int64(*) nil nil - Double | nil Double Double nil nil - String | nil nil nil String nil - Blob | nil nil nil nil Blob - - (*) Conversions to Int and Int64 crash if the value is too big. - - parameter index: The index of a column. - returns: An optional *Value*. */ @@ -118,7 +101,17 @@ public class Row: CollectionType { } /** - TODO + This method is an optimized specialization of Row.value(atIndex:) for types + that adopt both DatabaseValueConvertible and MetalType protocols. + + let value: Bool? = row.value(atIndex: 0) + let value: Int? = row.value(atIndex: 0) + let value: Double? = row.value(atIndex: 0) + + See the documentation of Row.value(atIndex:) for more information. + + - parameter index: The index of a column. + - returns: An optional *Value*. */ public func value>(atIndex index: Int) -> Value? { let sqliteStatement = self.sqliteStatement @@ -136,14 +129,45 @@ public class Row: CollectionType { } /** - TODO + Returns the value at given index, converted to the requested type. + + Indexes span from 0 for the leftmost column to (row.count - 1) for the + righmost column. + + Expect a crash if the SQLite value is NULL, or can't be converted to the + requested type. + + let value: Bool = row.value(atIndex: 0) + let value: Int = row.value(atIndex: 0) + let value: Double = row.value(atIndex: 0) + + **WARNING**: type casting requires a very careful use of the `as` operator + (see [rdar://21676393](http://openradar.appspot.com/radar?id=4951414862249984)): + + row.value(atIndex: 0) as Int // OK: Int + row.value(atIndex: 0) as Int? // OK: Int? + row.value(atIndex: 0) as? Int // NO NO NO DON'T DO THAT! + row.value(atIndex: 0) as! Int // NO NO NO DON'T DO THAT! + + - parameter index: The index of a column. + - returns: An optional *Value*. */ public func value(atIndex index: Int) -> Value { return Value.fromDatabaseValue(impl.databaseValue(atIndex: index))! } /** - TODO + This method is an optimized specialization of Row.value(atIndex:) for types + that adopt both DatabaseValueConvertible and MetalType protocols. + + let value: Bool = row.value(atIndex: 0) + let value: Int = row.value(atIndex: 0) + let value: Double = row.value(atIndex: 0) + + See the documentation of Row.value(atIndex:) for more information. + + - parameter index: The index of a column. + - returns: An optional *Value*. */ public func value>(atIndex index: Int) -> Value { let sqliteStatement = self.sqliteStatement @@ -156,7 +180,6 @@ public class Row: CollectionType { } } - /** Returns the value for the given column. @@ -199,26 +222,11 @@ public class Row: CollectionType { **WARNING**: type casting requires a very careful use of the `as` operator (see [rdar://21676393](http://openradar.appspot.com/radar?id=4951414862249984)): - row.value(named: "count")! as Int // OK: Int + row.value(named: "count") as Int // OK: Int row.value(named: "count") as Int? // OK: Int? row.value(named: "count") as! Int // NO NO NO DON'T DO THAT! row.value(named: "count") as? Int // NO NO NO DON'T DO THAT! - Your custom types that adopt the DatabaseValueConvertible protocol handle - their own conversion from raw SQLite values. Yet, here is the reference for - built-in types: - - SQLite value: | NULL INTEGER REAL TEXT BLOB - --------------|--------------------------------------------------------- - Bool | nil false if 0 false if 0.0 nil nil - Int | nil Int(*) Int(*) nil nil - Int64 | nil Int64 Int64(*) nil nil - Double | nil Double Double nil nil - String | nil nil nil String nil - Blob | nil nil nil nil Blob - - (*) Conversions to Int and Int64 crash if the value is too big. - - parameter name: A column name. - returns: An optional *Value*. */ @@ -228,7 +236,17 @@ public class Row: CollectionType { } /** - TODO + This method is an optimized specialization of Row.value(named:) for types + that adopt both DatabaseValueConvertible and MetalType protocols. + + let value: Bool? = row.value(named: "count") + let value: Int? = row.value(named: "count") + let value: Double? = row.value(named: "count") + + See the documentation of Row.value(named:) for more information. + + - parameter name: A column name. + - returns: An optional *Value*. */ public func value>(named columnName: String) -> Value? { let index = impl.indexForColumn(named: columnName)! @@ -236,7 +254,29 @@ public class Row: CollectionType { } /** - TODO + Returns the value for the given column, converted to the requested type. + + Expect a crash if the SQLite value is NULL, or can't be converted to the + requested type. + + let value: Bool? = row.value(named: "count") + let value: Int? = row.value(named: "count") + let value: Double? = row.value(named: "count") + + This method is case-insensitive. + + TODO: the doc below is obsolete. + + **WARNING**: type casting requires a very careful use of the `as` operator + (see [rdar://21676393](http://openradar.appspot.com/radar?id=4951414862249984)): + + row.value(named: "count") as Int // OK: Int + row.value(named: "count") as Int? // OK: Int? + row.value(named: "count") as! Int // NO NO NO DON'T DO THAT! + row.value(named: "count") as? Int // NO NO NO DON'T DO THAT! + + - parameter name: A column name. + - returns: An optional *Value*. */ public func value(named columnName: String) -> Value { let index = impl.indexForColumn(named: columnName)! @@ -244,7 +284,17 @@ public class Row: CollectionType { } /** - TODO + This method is an optimized specialization of Row.value(named:) for types + that adopt both DatabaseValueConvertible and MetalType protocols. + + let value: Bool = row.value(named: "count") + let value: Int = row.value(named: "count") + let value: Double = row.value(named: "count") + + See the documentation of Row.value(named:) for more information. + + - parameter name: A column name. + - returns: An optional *Value*. */ public func value>(named columnName: String) -> Value { let index = impl.indexForColumn(named: columnName)!