@@ -2103,6 +2103,9 @@ class QueryResultSets {
2103
2103
2104
2104
// QUERY RESULT SET HANDLING EXAMPLES
2105
2105
// tag::query-syntax-all[]
2106
+ let db = try ! Database ( name: " hotel " )
2107
+ var hotels = [ String: Any] ( )
2108
+ var hotel : Hotel = Hotel . init ( )
2106
2109
2107
2110
let listQuery = QueryBuilder . select ( SelectResult . all ( ) )
2108
2111
. from ( DataSource . database ( db) )
@@ -2113,45 +2116,38 @@ class QueryResultSets {
2113
2116
2114
2117
do {
2115
2118
2116
- for ( row) in try ! listQuery. execute ( ) {
2119
+ for row in try ! listQuery. execute ( ) {
2117
2120
2118
- print ( row. toDictionary ( ) )
2121
+ let thisDocsProps =
2122
+ row. dictionary ( at: 0 ) ? . toDictionary ( ) // <.>
2119
2123
2120
- if let hotel = row . dictionary ( forKey : " _doc " ) { // <.>
2124
+ let docid = thisDocsProps! [ " id " ] as! String
2121
2125
2122
- let hotelid = hotel [ " id " ] . string!
2126
+ let name = thisDocsProps! [ " name " ] as! String
2123
2127
2124
- hotels [ hotelid ] = hotel . toDictionary ( )
2128
+ let type = thisDocsProps! [ " type " ] as! String
2125
2129
2130
+ let city = thisDocsProps![ " city " ] as! String
2126
2131
2127
- if let thisDocsProperties =
2128
- hotel. toDictionary ( ) as? [ String : Any ] { // <.>
2132
+ let hotel = row. dictionary ( at: 0 ) ? . toDictionary ( ) //<.>
2129
2133
2130
- let docid = thisDocsProperties [ " id " ] as! String
2134
+ let hotelId = hotel! [ " id " ] as! String
2131
2135
2132
- let name = thisDocsProperties [ " name " ] as! String
2133
-
2134
- let type = thisDocsProperties [ " type " ] as! String
2135
-
2136
- let city = thisDocsProperties [ " city " ] as! String
2137
-
2138
- print ( " thisDocsProperties are: " , docid, name, type, city)
2139
-
2140
- } // end if
2141
-
2142
- } // end if
2136
+ hotels [ hotelId] = hotel
2143
2137
2144
2138
} // end for
2145
- } catch let err {
2146
- print ( err. localizedDescription)
2147
- // ... handle errors as required
2139
+
2148
2140
} //end do-block
2149
2141
2150
2142
// end::query-access-all[]
2151
2143
2152
2144
//
2153
2145
func dontTestQueryProps ( ) throws {
2154
2146
// tag::query-syntax-props[]
2147
+ let db = try ! Database ( name: " hotel " )
2148
+ var hotels = [ String: Any] ( )
2149
+ var hotel : Hotel = Hotel . init ( )
2150
+
2155
2151
let listQuery = QueryBuilder
2156
2152
. select ( SelectResult . expression ( Meta . id) . as ( " metaId " ) ,
2157
2153
SelectResult . expression ( Expression . property ( " id " ) ) ,
@@ -2165,32 +2161,33 @@ class QueryResultSets {
2165
2161
// tag::query-access-props[]
2166
2162
for (_, result) in try ! listQuery. execute ( ) . enumerated ( ) {
2167
2163
2168
- print ( result. toDictionary ( ) )
2169
-
2170
- if let thisDoc = result. toDictionary ( ) as? [ String : Any ] {
2171
-
2172
- let docid = thisDoc [ " metaId " ] as! String
2173
-
2174
- let hotelId = thisDoc [ " id " ] as! String
2175
2164
2176
- let name = thisDoc [ " name " ] as! String
2165
+ let thisDoc = result. toDictionary ( ) as? [ String : Any ] // <.>
2166
+ // Store dictionary data in hotel object and save in array
2167
+ hotel. id = thisDoc![ " id " ] as! String
2168
+ hotel. name = thisDoc![ " name " ] as! String
2169
+ hotel. city = thisDoc![ " city " ] as! String
2170
+ hotel. type = thisDoc![ " type " ] as! String
2171
+ hotels [ hotel. id] = hotel
2177
2172
2178
- let city = thisDoc [ " city " ] as! String
2179
-
2180
- let type = thisDoc [ " type " ] as! String
2181
-
2182
- // ... process document properties as required
2183
- print ( " Result properties are: " , docid, hotelId, name, city, type)
2184
- }
2185
- }
2173
+ // Use result content directly
2174
+ let docid = result. string ( forKey: " metaId " )
2175
+ let hotelId = result. string ( forKey: " id " )
2176
+ let name = result. string ( forKey: " name " )
2177
+ let city = result. string ( forKey: " city " )
2178
+ let type = result. string ( forKey: " type " )
2186
2179
2180
+ // ... process document properties as required
2181
+ print ( " Result properties are: " , docid, hotelId, name, city, type)
2182
+ } // end for
2187
2183
// end::query-access-props[]
2188
2184
}
2189
2185
2190
2186
//
2191
2187
func dontTestQueryCount ( ) throws {
2192
2188
2193
2189
// tag::query-syntax-count-only[]
2190
+ let db = try ! Database ( name: " hotel " )
2194
2191
do {
2195
2192
let listQuery = QueryBuilder
2196
2193
. select ( SelectResult . expression ( Function . count ( Expression . all ( ) ) ) . as ( " mycount " ) )
@@ -2200,13 +2197,15 @@ class QueryResultSets {
2200
2197
2201
2198
2202
2199
// tag::query-access-count-only[]
2200
+
2203
2201
for result in try ! listQuery. execute ( ) {
2204
- if let dict = result. toDictionary ( ) as? [ String : Int ] {
2205
- let thiscount = dict [ " mycount " ] ! // <.>
2206
- print ( " There are " , thiscount, " rows " )
2207
- }
2208
- }
2202
+ let dict = result. toDictionary ( ) as? [ String : Int ]
2203
+ let thiscount = dict! [ " mycount " ] ! // <.>
2204
+ print ( " There are " , thiscount, " rows " )
2205
+ } // end for
2206
+
2209
2207
} // end do
2208
+
2210
2209
} // end function
2211
2210
2212
2211
// end::query-access-count-only[]
@@ -2215,6 +2214,7 @@ class QueryResultSets {
2215
2214
func dontTestQueryId ( ) throws {
2216
2215
2217
2216
// tag::query-syntax-id[]
2217
+ let db = try ! Database ( name: " hotel " )
2218
2218
let listQuery = QueryBuilder . select ( SelectResult . expression ( Meta . id) . as ( " metaId " ) )
2219
2219
. from ( DataSource . database ( db) )
2220
2220
0 commit comments