closed all fetched rows, db was getting locked

This commit is contained in:
Suraj Patil 2016-05-14 15:04:42 +05:30
parent da354c2038
commit 6721b2d993
4 changed files with 18 additions and 7 deletions

View File

@ -21,6 +21,7 @@ func GetFileName(token string) (string, error) {
sql := "select name from files where autoName=?" sql := "select name from files where autoName=?"
var fileName string var fileName string
rows := database.query(sql, fileName) rows := database.query(sql, fileName)
defer rows.Close()
if rows.Next() { if rows.Next() {
err := rows.Scan(&fileName) err := rows.Scan(&fileName)
if err != nil { if err != nil {
@ -47,6 +48,7 @@ func GetCategories(username string) []types.CategoryCount {
var categories []types.CategoryCount var categories []types.CategoryCount
var category types.CategoryCount var category types.CategoryCount
defer rows.Close()
for rows.Next() { for rows.Next() {
err := rows.Scan(&category.Name, &category.Count) err := rows.Scan(&category.Name, &category.Count)
if err != nil { if err != nil {
@ -54,7 +56,6 @@ func GetCategories(username string) []types.CategoryCount {
} }
categories = append(categories, category) categories = append(categories, category)
} }
rows.Close()
return categories return categories
} }
@ -64,6 +65,7 @@ func AddCategory(username, category string) error {
if err != nil { if err != nil {
return nil return nil
} }
log.Println("executing query to add category")
err = taskQuery("insert into category(name, user_id) values(?,?)", category, userID) err = taskQuery("insert into category(name, user_id) values(?,?)", category, userID)
return err return err
} }
@ -74,7 +76,7 @@ func GetCategoryByName(username, category string) int {
stmt := "select id from category where name=? and user_id = (select id from user where username=?)" stmt := "select id from category where name=? and user_id = (select id from user where username=?)"
rows := database.query(stmt, category, username) rows := database.query(stmt, category, username)
var categoryID int var categoryID int
defer rows.Close()
for rows.Next() { for rows.Next() {
err := rows.Scan(&categoryID) err := rows.Scan(&categoryID)
if err != nil { if err != nil {

View File

@ -248,6 +248,7 @@ func UpdateTask(id int, title, content, category string, priority int, username
//taskQuery encapsulates running multiple queries which don't do much things //taskQuery encapsulates running multiple queries which don't do much things
func taskQuery(sql string, args ...interface{}) error { func taskQuery(sql string, args ...interface{}) error {
log.Print("inside task query")
SQL := database.prepare(sql) SQL := database.prepare(sql)
tx := database.begin() tx := database.begin()
_, err = tx.Stmt(SQL).Exec(args...) _, err = tx.Stmt(SQL).Exec(args...)
@ -255,7 +256,12 @@ func taskQuery(sql string, args ...interface{}) error {
log.Println("taskQuery: ", err) log.Println("taskQuery: ", err)
tx.Rollback() tx.Rollback()
} else { } else {
tx.Commit() err = tx.Commit()
if err != nil {
log.Println(err)
return err
}
log.Println("Commit successful")
} }
return err return err
} }
@ -280,7 +286,7 @@ func SearchTask(username, query string) (types.Context, error) {
stmt := "select t.id, title, content, created_date, priority, c.name from task t, category c where t.user_id=? and c.id = t.cat_id and (title like '%" + query + "%' or content like '%" + query + "%') order by created_date desc" stmt := "select t.id, title, content, created_date, priority, c.name from task t, category c where t.user_id=? and c.id = t.cat_id and (title like '%" + query + "%' or content like '%" + query + "%') order by created_date desc"
rows := database.query(stmt, userID, query, query) rows := database.query(stmt, userID, query, query)
defer rows.Close()
for rows.Next() { for rows.Next() {
err := rows.Scan(&task.Id, &task.Title, &task.Content, &TaskCreated, &task.Priority, &task.Category) err := rows.Scan(&task.Id, &task.Title, &task.Content, &TaskCreated, &task.Priority, &task.Category)
if err != nil { if err != nil {
@ -327,6 +333,7 @@ func GetComments(username string) (map[int][]types.Comment, error) {
stmt := "select c.id, c.taskID, c.content, c.created from comments c, task t where t.id=c.taskID and c.user_id=?;" stmt := "select c.id, c.taskID, c.content, c.created from comments c, task t where t.id=c.taskID and c.user_id=?;"
rows := database.query(stmt, userID) rows := database.query(stmt, userID)
defer rows.Close()
for rows.Next() { for rows.Next() {
err := rows.Scan(&comment.ID, &taskID, &comment.Content, &created) err := rows.Scan(&comment.ID, &taskID, &comment.Content, &created)
if err != nil { if err != nil {
@ -337,7 +344,6 @@ func GetComments(username string) (map[int][]types.Comment, error) {
comment.Created = created.Format("Jan 2 2006 15:04:05") comment.Created = created.Format("Jan 2 2006 15:04:05")
commentMap[taskID] = append(commentMap[taskID], comment) commentMap[taskID] = append(commentMap[taskID], comment)
} }
rows.Close()
return commentMap, nil return commentMap, nil
} }

View File

@ -17,6 +17,7 @@ func ValidUser(username, password string) bool {
log.Print("validating user ", username) log.Print("validating user ", username)
rows := database.query(userSQL, username) rows := database.query(userSQL, username)
defer rows.Close()
if rows.Next() { if rows.Next() {
err := rows.Scan(&passwordFromDB) err := rows.Scan(&passwordFromDB)
if err != nil { if err != nil {
@ -37,12 +38,12 @@ func GetUserID(username string) (int, error) {
userSQL := "select id from user where username=?" userSQL := "select id from user where username=?"
rows := database.query(userSQL, username) rows := database.query(userSQL, username)
defer rows.Close()
if rows.Next() { if rows.Next() {
err := rows.Scan(&userID) err := rows.Scan(&userID)
if err != nil { if err != nil {
return -1, err return -1, err
} }
} }
rows.Close()
return userID, nil return userID, nil
} }

View File

@ -129,7 +129,9 @@ func AddCategoryFunc(w http.ResponseWriter, r *http.Request) {
category := r.Form.Get("category") category := r.Form.Get("category")
if strings.Trim(category, " ") != "" { if strings.Trim(category, " ") != "" {
username := sessions.GetCurrentUserName(r) username := sessions.GetCurrentUserName(r)
if err := db.AddCategory(username, category); err != nil { log.Println("adding category")
err := db.AddCategory(username, category)
if err != nil {
message = "Error adding category" message = "Error adding category"
http.Redirect(w, r, "/", http.StatusBadRequest) http.Redirect(w, r, "/", http.StatusBadRequest)
} else { } else {