Inner Documentation

This commit is contained in:
Gwendal Roué committed 2015-07-05 00:42:34 +02:00
1 parent e11cb86981
commit 052b75872a
2 files changed
+94 -44

No files matched your search

+68 -36
View File
@@ -14,29 +14,40 @@ protocol BindingsImpl {
public struct Bindings {
let impl: BindingsImpl
// Bindings([SQLiteValueConvertible?])
//
// Supported usage:
//
// db.execute("INSERT ... (?,?,?)",
// bindings: Bindings(rowModel.databaseDictionary.values))
public init<Sequence: SequenceType where Sequence.Generator.Element == Optional<SQLiteValueConvertible>>(_ array: Sequence) {
impl = BindingsArrayImpl(array: Array(array))
}
// Bindings([SQLiteValueConvertible])
//
// No known usage yet.
public init<Sequence: SequenceType where Sequence.Generator.Element == SQLiteValueConvertible>(_ array: Sequence) {
impl = BindingsArrayImpl(array: array.map { $0 })
}
// Bindings(NSArray)
//
// This is a convenience initializer.
//
// Without it, the following code won't compile:
//
// let statement = try db.updateStatement("INSERT INTO persons (name, age) VALUES (?, ?)")
// let persons = [
// ["Arthur", 41],
// ["Barbara"],
// ]
// for person in persons {
// statement.clearBindings()
// statement.bind(Bindings(person)) // Error
// try statement.execute()
// }
public init(_ array: NSArray) {
// This is a convenience initializer.
//
// Without it, the following code won't compile:
//
// let statement = try db.updateStatement("INSERT INTO persons (name, age) VALUES (?, ?)")
// let persons = [
// ["Arthur", 41],
// ["Barbara"],
// ]
// for person in persons {
// statement.clearBindings()
// statement.bind(Bindings(person)) // Error
// try statement.execute()
// }
var values = [SQLiteValueConvertible?]()
for item in array {
values.append(Bindings.valueFromAnyObject(item))
@@ -44,25 +55,33 @@ public struct Bindings {
self.init(values)
}
// Bindings([String: SQLiteValueConvertible?])
//
// Supported usage: DictionaryLiteralConvertible adoption:
//
// db.execute("INSERT ... (:name, :age)",
// bindings: ["name"; "Arthur", "age": 41])
public init(_ dictionary: [String: SQLiteValueConvertible?]) {
impl = BindingsDictionaryImpl(dictionary: dictionary)
}
// Bindings(NSDictionary)
//
// This is a convenience initializer.
//
// Without it, the following code won't compile:
//
// let statement = try db.updateStatement("INSERT INTO persons (name, age) VALUES (:name, :age)")
// let persons = [
// ["name": "Arthur", "age": 41],
// ["name": "Barbara"],
// ]
// for person in persons {
// statement.clearBindings()
// statement.bind(Bindings(person)) // Error
// try statement.execute()
// }
public init(_ dictionary: NSDictionary) {
// This is a convenience initializer.
//
// Without it, the following code won't compile:
//
// let statement = try db.updateStatement("INSERT INTO persons (name, age) VALUES (:name, :age)")
// let persons = [
// ["name": "Arthur", "age": 41],
// ["name": "Barbara"],
// ]
// for person in persons {
// statement.clearBindings()
// statement.bind(Bindings(person)) // Error
// try statement.execute()
// }
var values = [String: SQLiteValueConvertible?]()
for (key, item) in dictionary {
if let key = key as? String {
@@ -74,10 +93,18 @@ public struct Bindings {
self.init(values)
}
// Supported uage: Statement.bindings property
//
// let statement = db.UpdateStatement("INSERT INTO persons (name, age) VALUES (?,?)"
// statement.bindings = ["Arthur", 41]
// statement.execute()
func bindInStatement(statement: Statement) {
impl.bindInStatement(statement)
}
// Supported usage: loading of RowModel by multiple-columns primary keys:
//
// let person = db.fetchOne(Person.self, primaryKey: Bindings)
func dictionary(defaultColumnNames defaultColumnNames: [String]?) -> [String: SQLiteValueConvertible?] {
return impl.dictionary(defaultColumnNames: defaultColumnNames)
}
@@ -122,16 +149,15 @@ public struct Bindings {
}
}
// IMPLEMENTATION NOTE:
//
// NSNumber, NSString, NSNull can't adopt SQLiteValueConvertible because
// Swift 2 won't make it possible.
//
// This is why this method exists. As a convenience for init(NSArray)
// and init(NSDictionary), themselves conveniences for the library user.
private static func valueFromAnyObject(object: AnyObject) -> SQLiteValueConvertible? {
// IMPLEMENTATION NOTE:
//
// NSNumber, NSString, NSNull can't adopt SQLiteValueConvertible because
// Swift 2 won't make it possible.
//
// This is why this method exists. As a convenience for init(NSArray)
// and init(NSDictionary), themselves conveniences for the library user.
switch object {
case let value as SQLiteValueConvertible:
return value
@@ -178,12 +204,18 @@ public struct Bindings {
}
extension Bindings : ArrayLiteralConvertible {
// Supported usage:
//
// db.selectRows("SELECT ...", bindings: ["Arthur", 41])
public init(arrayLiteral elements: SQLiteValueConvertible?...) {
self.init(elements)
}
}
extension Bindings : DictionaryLiteralConvertible {
// Supported usage:
//
// db.selectRows("SELECT ...", bindings: ["name": "Arthur", "age": 41])
public init(dictionaryLiteral elements: (String, SQLiteValueConvertible?)...) {
var dictionary = [String: SQLiteValueConvertible?]()
for (key, value) in elements {
+26 -8
View File
@@ -28,6 +28,8 @@ public struct Row: CollectionType {
}
}
// Used by RowModel so that it can call RowModel.updateFromDatabaseRow()
// to set the ID after an insertion.
public init(sqliteDictionary: [String: SQLiteValue]) {
self.impl = DictionaryRowImpl(sqliteDictionary: sqliteDictionary)
}
@@ -35,18 +37,22 @@ public struct Row: CollectionType {
// MARK: - Values
// if row.hasColumn("name") { self.name = row.value(named:"name") }
public func hasColumn(name: String) -> Bool {
return impl.sqliteDictionary.indexForKey(name) != nil
}
// if row.value(atIndex:0) == nil { ... }
public func value(atIndex index: Int) -> SQLiteValueConvertible? {
return impl.sqliteValueAtIndex(index).value()
}
// let name:String? = row.value(atIndex: 0)
public func value<Value: SQLiteValueConvertible>(atIndex index: Int) -> Value? {
return impl.sqliteValueAtIndex(index).value()
}
// if row.value(named: "name") == nil { ... }
public func value(named columnName: String) -> SQLiteValueConvertible? {
if let index = impl.indexForColumnNamed(columnName) {
return impl.sqliteValueAtIndex(index).value()
@@ -55,6 +61,7 @@ public struct Row: CollectionType {
}
}
// let name:String? = row.value(named: "name")
public func value<Value: SQLiteValueConvertible>(named columnName: String) -> Value? {
if let index = impl.indexForColumnNamed(columnName) {
return impl.sqliteValueAtIndex(index).value()
@@ -63,7 +70,7 @@ public struct Row: CollectionType {
}
}
// For tests
// For tests.
func sqliteValue(atIndex index: Int) -> SQLiteValue {
return impl.sqliteValueAtIndex(index)
}
@@ -71,9 +78,9 @@ public struct Row: CollectionType {
// MARK: - CollectionType
// TODO: test the row as collection
// Use a custom index, so that we eventually can provide a subscript(Int)
// Row needs an index type in order to adopt CollectionType.
//
// We use a custom index, so that we eventually can provide a subscript(Int)
// that returns a SQLiteValueConvertible.
public struct RowIndex: ForwardIndexType {
let index: Int
@@ -87,18 +94,22 @@ public struct Row: CollectionType {
}
}
// Required by Row adoption of CollectionType
public func generate() -> IndexingGenerator<Row> {
return IndexingGenerator(self)
}
// Required by Row adoption of CollectionType
public var startIndex: RowIndex {
return Index(0)
}
// Required by Row adoption of CollectionType
public var endIndex: RowIndex {
return Index(impl.count)
}
// Required by Row adoption of CollectionType
public subscript(index: RowIndex) -> (String, SQLiteValue) {
return (
self.impl.columnNameAtIndex(index.index),
@@ -108,6 +119,7 @@ public struct Row: CollectionType {
// MARK: - DictionaryRowImpl
// Implements a Rows on a top of a dictionary [String: SQLiteValue]
private struct DictionaryRowImpl: RowImpl {
let sqliteDictionary: [String: SQLiteValue]
@@ -139,8 +151,10 @@ public struct Row: CollectionType {
// MARK: - SafeRowImpl
// SafeRowImpl can be safely accessed after sqlite3_step() and sqlite3_finalize() has been called.
// It preserves the column ordering of the statement.
// Implements a Rows on a top of a statement.
//
// It makes Array(rowSequence) work: as the sequence is iterated,
// SafeRowImpl *copies* statement results.
private struct SafeRowImpl : RowImpl {
let sqliteValues: [SQLiteValue]
let columnNames: [String]
@@ -177,8 +191,11 @@ public struct Row: CollectionType {
// MARK: - UnsafeRowImpl
// UnsafeRowImpl can not be safely accessed after sqlite3_step() or sqlite3_finalize() has been called.
// It preserves the column ordering of the statement.
// Implements a Rows on a top of a statement.
//
// It can't make Array(rowSequence) work: as the sequence is iterated,
// UnsafeRowImpl *does not* copy statement results, and those results are
// lost.
private struct UnsafeRowImpl : RowImpl {
let statement: SelectStatement
@@ -213,6 +230,7 @@ public struct Row: CollectionType {
}
}
// Required by Row adoption of CollectionType
public func ==(lhs: Row.RowIndex, rhs: Row.RowIndex) -> Bool {
return lhs.index == rhs.index
}