1

Adding defer close for rows

This commit is contained in:
Alex Yatskov 2015-03-25 19:25:14 +09:00
parent 5dad43ce55
commit 6747b87b46
3 changed files with 15 additions and 12 deletions

View File

@ -105,27 +105,27 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) {
} }
func getCategories(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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer rows.Close() defer categoryRows.Close()
var categories []jsonCategory var categories []jsonCategory
for rows.Next() { for categoryRows.Next() {
var ( var (
description string description string
id int id int
) )
if err := rows.Scan(&description, &id); err != nil { if err := categoryRows.Scan(&description, &id); err != nil {
log.Fatal(err) log.Fatal(err)
} }
categories = append(categories, jsonCategory{description, id}) categories = append(categories, jsonCategory{description, id})
} }
if err := rows.Err(); err != nil { if err := categoryRows.Err(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -209,10 +209,10 @@ func accessReview(rw http.ResponseWriter, req *http.Request) {
return 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 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.Url = "http://www.tripadvisor.com" + reply.Url
reply.Success = true reply.Success = true
} }

View File

@ -65,7 +65,7 @@
$('#categories').append($(template({categories: categories})).hide().fadeIn()); $('#categories').append($(template({categories: categories})).hide().fadeIn());
$('#categories input:radio').unbind().change(function() { $('#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() { $('#categories button').unbind().click(function() {

11
util.go
View File

@ -159,6 +159,7 @@ func computeRecordPopularity(entries records, context queryContext) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer historyRows.Close()
var groupSum float64 var groupSum float64
var groupCount int var groupCount int
@ -173,6 +174,7 @@ func computeRecordPopularity(entries records, context queryContext) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer groupRows.Close()
recordProfile := make(featureMap) recordProfile := make(featureMap)
for groupRows.Next() { for groupRows.Next() {
@ -203,18 +205,19 @@ func computeRecordPopularity(entries records, context queryContext) {
} }
func getRecords(context queryContext) records { 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer recordRows.Close()
var entries []record var entries []record
for rows.Next() { for recordRows.Next() {
var name, url, closestStn string var name, url, closestStn string
var delicious, accomodating, affordable, atmospheric, latitude, longitude, distanceToStn float64 var delicious, accomodating, affordable, atmospheric, latitude, longitude, distanceToStn float64
var accessCount, id int var accessCount, id int
rows.Scan( recordRows.Scan(
&name, &name,
&url, &url,
&delicious, &delicious,
@ -245,7 +248,7 @@ func getRecords(context queryContext) records {
entries = append(entries, entry) entries = append(entries, entry)
} }
if err := rows.Err(); err != nil { if err := recordRows.Err(); err != nil {
log.Fatal(err) log.Fatal(err)
} }