From 6747b87b463881828e07ce3ceb2aa0ccee94ef2a Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 25 Mar 2015 19:25:14 +0900 Subject: [PATCH] Adding defer close for rows --- server.go | 14 +++++++------- static/scripts/profile.js | 2 +- util.go | 11 +++++++---- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/server.go b/server.go index 7c8ad5e..f742b0d 100644 --- a/server.go +++ b/server.go @@ -105,27 +105,27 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) { } func getCategories(rw http.ResponseWriter, req *http.Request) { - rows, err := db.Query("SELECT description, id FROM categories") + categoryRows, err := db.Query("SELECT description, id FROM categories") if err != nil { log.Fatal(err) } - defer rows.Close() + defer categoryRows.Close() var categories []jsonCategory - for rows.Next() { + for categoryRows.Next() { var ( description string id int ) - if err := rows.Scan(&description, &id); err != nil { + if err := categoryRows.Scan(&description, &id); err != nil { log.Fatal(err) } categories = append(categories, jsonCategory{description, id}) } - if err := rows.Err(); err != nil { + if err := categoryRows.Err(); err != nil { log.Fatal(err) } @@ -209,10 +209,10 @@ func accessReview(rw http.ResponseWriter, req *http.Request) { return } - reviewRows := db.QueryRow("SELECT url FROM reviews WHERE id = (?) LIMIT 1", request.Id) + reviewRow := db.QueryRow("SELECT url FROM reviews WHERE id = (?) LIMIT 1", request.Id) var reply jsonAccessReply - if err := reviewRows.Scan(&reply.Url); err == nil { + if err := reviewRow.Scan(&reply.Url); err == nil { reply.Url = "http://www.tripadvisor.com" + reply.Url reply.Success = true } diff --git a/static/scripts/profile.js b/static/scripts/profile.js index cf1d8ea..60131e3 100644 --- a/static/scripts/profile.js +++ b/static/scripts/profile.js @@ -65,7 +65,7 @@ $('#categories').append($(template({categories: categories})).hide().fadeIn()); $('#categories input:radio').unbind().change(function() { - setProfileValue($(this).attr('data-categoryId'), this.value); + setProfileValue(parseInt($(this).attr('data-categoryId'), this.value)); }); $('#categories button').unbind().click(function() { diff --git a/util.go b/util.go index 99cd11f..3342d11 100644 --- a/util.go +++ b/util.go @@ -159,6 +159,7 @@ func computeRecordPopularity(entries records, context queryContext) { if err != nil { log.Fatal(err) } + defer historyRows.Close() var groupSum float64 var groupCount int @@ -173,6 +174,7 @@ func computeRecordPopularity(entries records, context queryContext) { if err != nil { log.Fatal(err) } + defer groupRows.Close() recordProfile := make(featureMap) for groupRows.Next() { @@ -203,18 +205,19 @@ func computeRecordPopularity(entries records, context queryContext) { } func getRecords(context queryContext) records { - rows, err := db.Query("SELECT name, url, delicious, accomodating, affordable, atmospheric, latitude, longitude, distanceToStn, closestStn, accessCount, id FROM reviews") + recordRows, err := db.Query("SELECT name, url, delicious, accomodating, affordable, atmospheric, latitude, longitude, distanceToStn, closestStn, accessCount, id FROM reviews") if err != nil { log.Fatal(err) } + defer recordRows.Close() var entries []record - for rows.Next() { + for recordRows.Next() { var name, url, closestStn string var delicious, accomodating, affordable, atmospheric, latitude, longitude, distanceToStn float64 var accessCount, id int - rows.Scan( + recordRows.Scan( &name, &url, &delicious, @@ -245,7 +248,7 @@ func getRecords(context queryContext) records { entries = append(entries, entry) } - if err := rows.Err(); err != nil { + if err := recordRows.Err(); err != nil { log.Fatal(err) }