0001 // Row.swift 0002 // 0003 // The MIT License (MIT) 0004 // 0005 // Copyright (c) 2015 Formbound 0006 // 0007 // Permission is hereby granted, free of charge, to any person obtaining a copy 0008 // of this software and associated documentation files (the "Software"), to deal 0009 // in the Software without restriction, including without limitation the rights 0010 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 0011 // copies of the Software, and to permit persons to whom the Software is 0012 // furnished to do so, subject to the following conditions: 0013 // 0014 // The above copyright notice and this permission notice shall be included in all 0015 // copies or substantial portions of the Software. 0016 // 0017 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 0018 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 0019 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 0020 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 0021 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 0022 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 0023 // SOFTWARE. 0024 0025 @_exported import Data 0026 @_exported import String 0027 0028 public protocol RowType{ 0029 init(dataByFieldName: [String: Data?]) 0030 0031 var fieldNames: [String] { get } 0032 0033 var dataByFieldName: [String: Data?] { get } 0034 } 0035 0036 public struct Row
Row.swift:36 public struct Row: RowType, CustomStringConvertible {Row.swift:55 public extension RowType {: RowType, CustomStringConvertible { 0037 0038 public init(dataByFieldName: [String: Data?]) { 0039 self.dataByFieldName = dataByFieldName 0040 } 0041 0042 public var dataByFieldName: [String: Data?] 0043 0044 public var fieldNames: [String] { 0045 return Array(dataByFieldName.keys) 0046 } 0047 } 0048 0049 public enum RowError
Result.swift:46 func next() -> Row?Result.swift:50 public typealias Element = RowModel.swift:122 init(row: Row) throwsQuery.swift:233 public func fetch<T: Connection where T.ResultType.Generator.Element == Row>(connection: T) throws -> [ModelType] {Query.swift:237 public func first<T: Connection where T.ResultType.Generator.Element == Row>(connection: T) throws -> ModelType? {: ErrorType { 0050 case ExpectedField
Row.swift:92 throw RowError.ExpectedField(fieldNameCandidates.joinWithSeparator(", "))Row.swift:99 throw RowError.UnexpectedNilValue(fieldName)Row.swift:117 throw RowError.UnexpectedNilValue(fieldName)(String) 0051 case UnexpectedNilValue
Row.swift:92 throw RowError.ExpectedField(fieldNameCandidates.joinWithSeparator(", "))(String) 0052 case ConversionError(fieldName: String, type: Any.Type) 0053 } 0054 0055 public extension RowType { 0056 0057 // MARK: - Data 0058 0059 public func data(fieldName: String) throws -> Data? { 0060 0061 /* 0062 Supplying a fielName can done either 0063 1. Qualified, e.g. 'users.id' 0064 2. Non-qualified e.g. 'id' 0065 0066 A statement will cast qualified fields from 'users.id' to 'users__id' 0067 0068 Because of this, a given field name must be checked for three type of keys 0069 0070 */ 0071 var fieldNameCandidates = [fieldName] 0072 0073 let components = fieldName.split(".") 0074 if components.count == 2 { // The field name is qualified 0075 fieldNameCandidates += [ 0076 components.joinWithSeparator("__"), // Add candidate as 'users__id' 0077 components[1] // Add candidate as 'id' 0078 ] 0079 } 0080 0081 var data: Data?? 0082 0083 for fielNameCandidate in fieldNameCandidates { 0084 data = dataByFieldName[fielNameCandidate] 0085 0086 if data != nil { 0087 break 0088 } 0089 } 0090 0091 guard let result = data else { 0092 throw RowError.ExpectedField(fieldNameCandidates.joinWithSeparator(", ")) 0093 } 0094 return result 0095 } 0096 0097 public func data(fieldName: String) throws -> Data { 0098 guard let data: Data = try data(fieldName) else { 0099 throw RowError.UnexpectedNilValue(fieldName) 0100 } 0101 0102 return data 0103 } 0104 0105 // MARK: - ValueConvertible 0106 0107 public func value
Row.swift:99 throw RowError.UnexpectedNilValue(fieldName)Row.swift:117 throw RowError.UnexpectedNilValue(fieldName)<T: ValueConvertible>(fieldName: String) throws -> T? { 0108 guard let data: Data = try data(fieldName) else { 0109 return nil 0110 } 0111 0112 return try T(rawSQLValue: data) 0113 } 0114 0115 public func value
Row.swift:135 return try value(field.qualifiedName)<T: ValueConvertible>(fieldName: String) throws -> T { 0116 guard let data: Data = try data(fieldName) else { 0117 throw RowError.UnexpectedNilValue(fieldName) 0118 } 0119 0120 return try T(rawSQLValue: data) 0121 } 0122 0123 0124 // MARK: - Model field support 0125 0126 public func data<F: ModelFieldset>(field: F) throws -> Data? { 0127 return try data(field.qualifiedName) 0128 } 0129 0130 public func data
Row.swift:139 return try value(field.qualifiedName)<F: ModelFieldset>(field: F) throws -> Data { 0131 return try data(field.qualifiedName) 0132 } 0133 0134 public func value<T: ValueConvertible, F: ModelFieldset>(field: F) throws -> T? { 0135 return try value(field.qualifiedName) 0136 } 0137 0138 public func value<T: ValueConvertible, F: ModelFieldset>(field: F) throws -> T { 0139 return try value(field.qualifiedName) 0140 } 0141 0142 0143 public var description: String { 0144 var string: String = "" 0145 0146 let tab = "\t\t" 0147 0148 string += dataByFieldName.keys.joinWithSeparator(tab) 0149 string += "\n---------\n" 0150 string += dataByFieldName.values.map { 0151 value in 0152 0153 guard let value = value else { 0154 return "NULL" 0155 } 0156 0157 return value.description 0158 0159 }.joinWithSeparator(tab) 0160 0161 return string 0162 } 0163 }
Row.swift:59 public func data(fieldName: String) throws -> Data? {Row.swift:97 public func data(fieldName: String) throws -> Data {Row.swift:126 public func data<F: ModelFieldset>(field: F) throws -> Data? {