Skip to content

Commit

Permalink
Refactor/improve sqlite error behavior (#41)
Browse files Browse the repository at this point in the history
* - properly handing through errors in user repo

* - properly handing through errors
  • Loading branch information
winterMate authored Oct 1, 2019
1 parent 4725a00 commit 8222891
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 39 deletions.
26 changes: 15 additions & 11 deletions maas-server/items/item_repository_sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ func (r *ItemRepoSqlite3) Get(itemID uint32) (Item, error) {
rows, err := r.db.Query("SELECT ID, name, cost FROM items WHERE id=?", itemID)
if err == nil {
for rows.Next() {
rows.Scan(&item.ID, &item.Name, &item.Cost)
err = rows.Scan(&item.ID, &item.Name, &item.Cost)
break
}
rows.Close()
err = rows.Close()
}

return item, err
Expand All @@ -45,10 +45,12 @@ func (r *ItemRepoSqlite3) List() ([]Item, error) {
var id uint32
var name string
var cost int32
rows.Scan(&id, &name, &cost)
items = append(items, Item{ID: id, Name: name, Cost: cost})
err = rows.Scan(&id, &name, &cost)
if err == nil {
items = append(items, Item{ID: id, Name: name, Cost: cost})
}
}
rows.Close()
err = rows.Close()
}

return items, err
Expand All @@ -64,12 +66,14 @@ func (r *ItemRepoSqlite3) Save(item Item) (Item, error) {
stmt, err := r.db.Prepare("INSERT INTO items (name, cost) VALUES (?, ?)")
if err == nil {
res, err := stmt.Exec(item.Name, item.Cost)
id, err := res.LastInsertId()
if err == nil {
//evil cast of int64 => uint32 .... TODO solve this better
returnedItem = Item{ID: uint32(id), Name: item.Name, Cost: item.Cost}
id, err := res.LastInsertId()
if err == nil {
//evil cast of int64 => uint32 .... TODO solve this better
returnedItem = Item{ID: uint32(id), Name: item.Name, Cost: item.Cost}
}
err = stmt.Close()
}
stmt.Close()
}
} else {
stmt, err := r.db.Prepare("UPDATE items SET name=?, cost=? WHERE ID=?")
Expand All @@ -78,7 +82,7 @@ func (r *ItemRepoSqlite3) Save(item Item) (Item, error) {
if err == nil {
returnedItem = item
}
stmt.Close()
err = stmt.Close()
}
}
}
Expand All @@ -93,7 +97,7 @@ func (r *ItemRepoSqlite3) Delete(itemID uint32) (Item, error) {
stmt, err := r.db.Prepare("DELETE FROM items WHERE ID=?")
if err == nil {
_, err = stmt.Exec(item.ID)
stmt.Close()
err = stmt.Close()
}
}
return item, err
Expand Down
16 changes: 9 additions & 7 deletions maas-server/items/item_stats_repository_sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ func (r *ItemStatsRepoSqlite3) Get(itemID uint32) (ItemStats, error) {
rows, err := r.db.Query("SELECT itemID, consumed FROM items_stats WHERE itemID=?", itemID)
if err == nil {
for rows.Next() {
rows.Scan(&itemStats.ItemID, &itemStats.Consumed)
err = rows.Scan(&itemStats.ItemID, &itemStats.Consumed)
break
}
rows.Close()
err = rows.Close()
}

return itemStats, err
Expand All @@ -48,7 +48,7 @@ func (r *ItemStatsRepoSqlite3) CountConsumption(itemID uint32, consumed uint32)
if err == nil {
returnedItemStats = ItemStats{ItemID: itemID, Consumed: consumed}
}
stmt.Close()
err = stmt.Close()
}
} else {
stmt, err := r.db.Prepare("UPDATE items_stats SET consumed = consumed + ? WHERE itemID=?")
Expand All @@ -57,7 +57,7 @@ func (r *ItemStatsRepoSqlite3) CountConsumption(itemID uint32, consumed uint32)
if err == nil {
returnedItemStats = ItemStats{ItemID: itemID, Consumed: consumed}
}
stmt.Close()
err = stmt.Close()
}
}
}
Expand All @@ -74,11 +74,13 @@ func (r *ItemStatsRepoSqlite3) List() ([]ItemStats, error) {
var id uint32
var consumed uint32

rows.Scan(&id, &consumed)
err = rows.Scan(&id, &consumed)

itemsStats = append(itemsStats, ItemStats{ItemID: id, Consumed: consumed})
if err == nil {
itemsStats = append(itemsStats, ItemStats{ItemID: id, Consumed: consumed})
}
}
rows.Close()
err = rows.Close()
}

return itemsStats, err
Expand Down
13 changes: 7 additions & 6 deletions maas-server/users/user_items_stats_repository_sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ func (r *UserItemsStatsRepoSqlite3) Get(userID uint32) ([]items.ItemStats, error
var itemid uint32
var consumed uint32

rows.Scan(&itemid, &consumed)

itemsStats = append(itemsStats, items.ItemStats{ItemID: itemid, Consumed: consumed})
err = rows.Scan(&itemid, &consumed)
if err == nil {
itemsStats = append(itemsStats, items.ItemStats{ItemID: itemid, Consumed: consumed})
}
}
rows.Close()
err = rows.Close()
}

return itemsStats, err
Expand All @@ -50,14 +51,14 @@ func (r *UserItemsStatsRepoSqlite3) CountConsumption(userID uint32, itemID uint3
stmt, err := r.db.Prepare("INSERT INTO user_items_stats (userID, itemID, consumed) VALUES (?, ?, ?)")
if err == nil {
_, err = stmt.Exec(userID, itemID, consumed)
err = stmt.Close()
}
stmt.Close()
} else {
stmt, err := r.db.Prepare("UPDATE user_items_stats SET consumed = consumed + ? WHERE userID=? AND itemID=?")
if err == nil {
_, err = stmt.Exec(consumed, userID, itemID)
err = stmt.Close()
}
stmt.Close()
}
}
return err
Expand Down
33 changes: 18 additions & 15 deletions maas-server/users/user_repository_sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (r *UserRepoSqlite3) Get(userID uint32) (User, error) {
rows.Scan(&user.ID, &user.Username, &user.Password, &user.Credits, &user.admin)
break
}
rows.Close()
err = rows.Close()
}

return user, err
Expand All @@ -45,7 +45,7 @@ func (r *UserRepoSqlite3) GetByUsername(username string) (User, error) {
rows.Scan(&user.ID, &user.Username, &user.Password, &user.Credits, &user.admin)
break
}
rows.Close()
err = rows.Close()
}

return user, err
Expand All @@ -64,16 +64,17 @@ func (r *UserRepoSqlite3) List() ([]User, error) {
var credits int32
var adminInt uint32

rows.Scan(&id, &username, &password, &credits, &adminInt)
err = rows.Scan(&id, &username, &password, &credits, &adminInt)
if err == nil {
var adminBool bool
if adminInt == 1 {
adminBool = true
}

var adminBool bool
if adminInt == 1 {
adminBool = true
users = append(users, User{ID: id, Username: username, Password: password, Credits: credits, admin: adminBool})
}

users = append(users, User{ID: id, Username: username, Password: password, Credits: credits, admin: adminBool})
}
rows.Close()
err = rows.Close()
}

return users, err
Expand All @@ -93,12 +94,14 @@ func (r *UserRepoSqlite3) Save(user User) (User, error) {
adminInt = 1
}
res, err := stmt.Exec(user.Username, user.Password, user.Credits, adminInt)
id, err := res.LastInsertId()
if err == nil {
//evil cast of int64 => uint32 .... TODO solve this better
returnedUser = User{ID: uint32(id), Username: user.Username, Password: user.Password, Credits: user.Credits, admin: user.IsAdmin()}
id, err := res.LastInsertId()
if err == nil {
//evil cast of int64 => uint32 .... TODO solve this better
returnedUser = User{ID: uint32(id), Username: user.Username, Password: user.Password, Credits: user.Credits, admin: user.IsAdmin()}
}
err = stmt.Close()
}
stmt.Close()
}
} else {
stmt, err := r.db.Prepare("UPDATE users SET username=?, password=?, credits=?, admin=? WHERE ID=?")
Expand All @@ -111,7 +114,7 @@ func (r *UserRepoSqlite3) Save(user User) (User, error) {
if err == nil {
returnedUser = user
}
stmt.Close()
err = stmt.Close()
}
}
}
Expand All @@ -126,7 +129,7 @@ func (r *UserRepoSqlite3) Delete(userID uint32) (User, error) {
stmt, err := r.db.Prepare("DELETE FROM users WHERE ID=?")
if err == nil {
_, err = stmt.Exec(user.ID)
stmt.Close()
err = stmt.Close()
}
}
return user, err
Expand Down

0 comments on commit 8222891

Please sign in to comment.