@@ -2114,7 +2114,10 @@ class QueryResultSets {
2114
2114
// seedHotel()
2115
2115
2116
2116
// QUERY RESULT SET HANDLING EXAMPLES
2117
- // tag::query-syntax-all[
2117
+ // tag::query-syntax-all[]
2118
+ let db = try ! Database ( name: " hotel " )
2119
+ var hotels = [ String: Any] ( )
2120
+ var hotel : Hotel = Hotel . init ( )
2118
2121
2119
2122
let listQuery = QueryBuilder . select ( SelectResult . all ( ) )
2120
2123
. from ( DataSource . database ( db) )
@@ -2125,37 +2128,27 @@ class QueryResultSets {
2125
2128
2126
2129
do {
2127
2130
2128
- for ( row) in try ! listQuery. execute ( ) {
2131
+ for row in try ! listQuery. execute ( ) {
2129
2132
2130
- print ( row. toDictionary ( ) )
2133
+ let thisDocsProps =
2134
+ row. dictionary ( at: 0 ) ? . toDictionary ( ) // <.>
2131
2135
2132
- if let hotel = row . dictionary ( forKey : " _doc " ) {
2136
+ let docid = thisDocsProps! [ " id " ] as! String
2133
2137
2134
- let hotelid = hotel [ " id " ] . string!
2138
+ let name = thisDocsProps! [ " name " ] as! String
2135
2139
2136
- hotels [ hotelid ] = hotel . toDictionary ( )
2140
+ let type = thisDocsProps! [ " type " ] as! String
2137
2141
2142
+ let city = thisDocsProps![ " city " ] as! String
2138
2143
2139
- if let thisDocsProperties = hotel . toDictionary ( ) as? [ String : Any ] {
2144
+ let hotel = row . dictionary ( at : 0 ) ? . toDictionary ( ) //<.>
2140
2145
2141
- let docid = thisDocsProperties [ " id " ] as! String
2146
+ let hotelId = hotel! [ " id " ] as! String
2142
2147
2143
- let name = thisDocsProperties [ " name " ] as! String
2144
-
2145
- let type = thisDocsProperties [ " type " ] as! String
2146
-
2147
- let city = thisDocsProperties [ " city " ] as! String
2148
-
2149
- print ( " thisDocsProperties are: " , docid, name, type, city)
2150
-
2151
- } // end if <.>
2152
-
2153
- } // end if
2148
+ hotels [ hotelId] = hotel
2154
2149
2155
2150
} // end for
2156
- } catch let err {
2157
- print ( err. localizedDescription)
2158
- // ... handle errors as required
2151
+
2159
2152
} //end do-block
2160
2153
2161
2154
// end::query-access-all[]
@@ -2199,6 +2192,10 @@ class QueryResultSets {
2199
2192
//
2200
2193
func dontTestQueryProps ( ) throws {
2201
2194
// tag::query-syntax-props[]
2195
+ let db = try ! Database ( name: " hotel " )
2196
+ var hotels = [ String: Any] ( )
2197
+ var hotel : Hotel = Hotel . init ( )
2198
+
2202
2199
let listQuery = QueryBuilder
2203
2200
. select ( SelectResult . expression ( Meta . id) . as ( " metaId " ) ,
2204
2201
SelectResult . expression ( Expression . property ( " id " ) ) ,
@@ -2212,32 +2209,33 @@ class QueryResultSets {
2212
2209
// tag::query-access-props[]
2213
2210
for (_, result) in try ! listQuery. execute ( ) . enumerated ( ) {
2214
2211
2215
- print ( result. toDictionary ( ) )
2216
-
2217
- if let thisDoc = result. toDictionary ( ) as? [ String : Any ] {
2218
-
2219
- let docid = thisDoc [ " metaId " ] as! String
2220
2212
2221
- let hotelId = thisDoc [ " id " ] as! String
2213
+ let thisDoc = result. toDictionary ( ) as? [ String : Any ] // <.>
2214
+ // Store dictionary data in hotel object and save in array
2215
+ hotel. id = thisDoc![ " id " ] as! String
2216
+ hotel. name = thisDoc![ " name " ] as! String
2217
+ hotel. city = thisDoc![ " city " ] as! String
2218
+ hotel. type = thisDoc![ " type " ] as! String
2219
+ hotels [ hotel. id] = hotel
2222
2220
2223
- let name = thisDoc [ " name " ] as! String
2224
-
2225
- let city = thisDoc [ " city " ] as! String
2226
-
2227
- let type = thisDoc [ " type " ] as! String
2228
-
2229
- // ... process document properties as required
2230
- print ( " Result properties are: " , docid, hotelId, name, city, type)
2231
- }
2232
- }
2221
+ // Use result content directly
2222
+ let docid = result. string ( forKey: " metaId " )
2223
+ let hotelId = result. string ( forKey: " id " )
2224
+ let name = result. string ( forKey: " name " )
2225
+ let city = result. string ( forKey: " city " )
2226
+ let type = result. string ( forKey: " type " )
2233
2227
2228
+ // ... process document properties as required
2229
+ print ( " Result properties are: " , docid, hotelId, name, city, type)
2230
+ } // end for
2234
2231
// end::query-access-props[]
2235
2232
}
2236
2233
2237
2234
//
2238
2235
func dontTestQueryCount ( ) throws {
2239
2236
2240
2237
// tag::query-syntax-count-only[]
2238
+ let db = try ! Database ( name: " hotel " )
2241
2239
do {
2242
2240
let listQuery = QueryBuilder
2243
2241
. select ( SelectResult . expression ( Function . count ( Expression . all ( ) ) ) . as ( " mycount " ) )
@@ -2247,13 +2245,15 @@ class QueryResultSets {
2247
2245
2248
2246
2249
2247
// tag::query-access-count-only[]
2248
+
2250
2249
for result in try ! listQuery. execute ( ) {
2251
- if let dict = result. toDictionary ( ) as? [ String : Int ] {
2252
- let thiscount = dict [ " mycount " ] ! // <.>
2253
- print ( " There are " , thiscount, " rows " )
2254
- }
2255
- }
2250
+ let dict = result. toDictionary ( ) as? [ String : Int ]
2251
+ let thiscount = dict! [ " mycount " ] ! // <.>
2252
+ print ( " There are " , thiscount, " rows " )
2253
+ } // end for
2254
+
2256
2255
} // end do
2256
+
2257
2257
} // end function
2258
2258
2259
2259
// end::query-access-count-only[]
@@ -2262,6 +2262,7 @@ class QueryResultSets {
2262
2262
func dontTestQueryId ( ) throws {
2263
2263
2264
2264
// tag::query-syntax-id[]
2265
+ let db = try ! Database ( name: " hotel " )
2265
2266
let listQuery = QueryBuilder . select ( SelectResult . expression ( Meta . id) . as ( " metaId " ) )
2266
2267
. from ( DataSource . database ( db) )
2267
2268
0 commit comments