Skip to content

Commit

Permalink
DOC-8512 -- Apply swift code feedback (couchbase#471) (couchbase#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibsoln committed Aug 11, 2021
1 parent 098db47 commit 75f909d
Showing 1 changed file with 44 additions and 43 deletions.
87 changes: 44 additions & 43 deletions modules/swift/examples/code_snippets/SampleCodeTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2114,7 +2114,10 @@ class QueryResultSets {
// seedHotel()

// QUERY RESULT SET HANDLING EXAMPLES
// tag::query-syntax-all[
// tag::query-syntax-all[]
let db = try! Database(name: "hotel")
var hotels = [String:Any]()
var hotel:Hotel = Hotel.init()

let listQuery = QueryBuilder.select(SelectResult.all())
.from(DataSource.database( db))
Expand All @@ -2125,37 +2128,27 @@ class QueryResultSets {

do {

for (row) in try! listQuery.execute() {
for row in try! listQuery.execute() {

print(row.toDictionary())
let thisDocsProps =
row.dictionary(at: 0)?.toDictionary() // <.>

if let hotel = row.dictionary(forKey: "_doc") {
let docid = thisDocsProps!["id"] as! String

let hotelid = hotel["id"].string!
let name = thisDocsProps!["name"] as! String

hotels[hotelid] = hotel.toDictionary()
let type = thisDocsProps!["type"] as! String

let city = thisDocsProps!["city"] as! String

if let thisDocsProperties = hotel.toDictionary() as? [String:Any] {
let hotel = row.dictionary(at: 0)?.toDictionary() //<.>

let docid = thisDocsProperties["id"] as! String
let hotelId = hotel!["id"] as! String

let name = thisDocsProperties["name"] as! String

let type = thisDocsProperties["type"] as! String

let city = thisDocsProperties["city"] as! String

print("thisDocsProperties are: ", docid,name,type,city)

} // end if <.>

} // end if
hotels[hotelId] = hotel

} // end for
} catch let err {
print(err.localizedDescription)
// ... handle errors as required

} //end do-block

// end::query-access-all[]
Expand Down Expand Up @@ -2199,6 +2192,10 @@ class QueryResultSets {
//
func dontTestQueryProps () throws {
// tag::query-syntax-props[]
let db = try! Database(name: "hotel")
var hotels = [String:Any]()
var hotel:Hotel = Hotel.init()

let listQuery = QueryBuilder
.select(SelectResult.expression(Meta.id).as("metaId"),
SelectResult.expression(Expression.property("id")),
Expand All @@ -2212,32 +2209,33 @@ class QueryResultSets {
// tag::query-access-props[]
for (_, result) in try! listQuery.execute().enumerated() {

print(result.toDictionary())

if let thisDoc = result.toDictionary() as? [String:Any] {

let docid = thisDoc["metaId"] as! String

let hotelId = thisDoc["id"] as! String
let thisDoc = result.toDictionary() as? [String:Any] // <.>
// Store dictionary data in hotel object and save in array
hotel.id = thisDoc!["id"] as! String
hotel.name = thisDoc!["name"] as! String
hotel.city = thisDoc!["city"] as! String
hotel.type = thisDoc!["type"] as! String
hotels[hotel.id] = hotel

let name = thisDoc["name"] as! String

let city = thisDoc["city"] as! String

let type = thisDoc["type"] as! String

// ... process document properties as required
print("Result properties are: ", docid, hotelId,name, city, type)
}
}
// Use result content directly
let docid = result.string(forKey: "metaId")
let hotelId = result.string(forKey: "id")
let name = result.string(forKey: "name")
let city = result.string(forKey: "city")
let type = result.string(forKey: "type")

// ... process document properties as required
print("Result properties are: ", docid, hotelId,name, city, type)
} // end for
// end::query-access-props[]
}

//
func dontTestQueryCount () throws {

// tag::query-syntax-count-only[]
let db = try! Database(name: "hotel")
do {
let listQuery = QueryBuilder
.select(SelectResult.expression(Function.count(Expression.all())).as("mycount"))
Expand All @@ -2247,13 +2245,15 @@ class QueryResultSets {


// tag::query-access-count-only[]

for result in try! listQuery.execute() {
if let dict = result.toDictionary() as? [String: Int] {
let thiscount = dict["mycount"]! // <.>
print("There are ", thiscount, " rows")
}
}
let dict = result.toDictionary() as? [String: Int]
let thiscount = dict!["mycount"]! // <.>
print("There are ", thiscount, " rows")
} // end for

} // end do

} // end function

// end::query-access-count-only[]
Expand All @@ -2262,6 +2262,7 @@ class QueryResultSets {
func dontTestQueryId () throws {

// tag::query-syntax-id[]
let db = try! Database(name: "hotel")
let listQuery = QueryBuilder.select(SelectResult.expression(Meta.id).as("metaId"))
.from(DataSource.database(db))

Expand Down

0 comments on commit 75f909d

Please sign in to comment.