contrib/benitlux: let the iterators close the sqlite statements
authorAlexis Laferrière <alexis.laf@xymus.net>
Fri, 8 Apr 2016 15:08:41 +0000 (11:08 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Fri, 8 Apr 2016 15:38:08 +0000 (11:38 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

contrib/benitlux/src/benitlux_db.nit
contrib/benitlux/src/benitlux_social.nit

index 73739d9..81ec6bf 100644 (file)
@@ -134,8 +134,13 @@ class BenitluxDB
        do
                var stmt = select("ROWID, name, desc FROM beers WHERE ROWID = {id}")
                if stmt == null then return null
-               for row in stmt do return row.to_beer
-               return null
+
+               var res = null
+               for row in stmt do
+                       res = row.to_beer
+                       break
+               end
+               return res
        end
 
        # Days where `beer` was available, all known days if `beer == null`
index ae409a8..70ee217 100644 (file)
@@ -110,6 +110,7 @@ GROUP BY beer0, beer1""") else
                        var user_id = row[0].to_i
                        var token = new_token(user_id)
                        var u = new User(user_id, row[1].to_s)
+                       stmt.close
                        return new LoginResult(u, token)
                end
                return null
@@ -153,8 +154,12 @@ GROUP BY beer0, beer1""") else
                # TODO update token timestamp and platform/client hint of last connection.
                # These informations could help detect malicious access to the account.
 
-               for row in stmt do return row[0].to_i
-               return null
+               var res = null
+               for row in stmt do
+                       res = row[0].to_i
+                       break
+               end
+               return res
        end
 
        # Get `User` data from the integer `id`
@@ -163,8 +168,12 @@ GROUP BY beer0, beer1""") else
                var stmt = select("name FROM users WHERE ROWID = {id}")
                assert stmt != null
 
-               for row in stmt do return new User(id, row[0].to_s)
-               return null
+               var res = null
+               for row in stmt do
+                       res = new User(id, row[0].to_s)
+                       break
+               end
+               return res
        end
 
        # Try to sign up a new user, return `true` on success
@@ -211,8 +220,12 @@ GROUP BY beer0, beer1""") else
                var b = beer_from_id(beer)
                if b == null then return null
 
-               for row in stmt do return new BeerStats(b, row[0].to_f, row[1].to_i)
-               return null
+               var res = null
+               for row in stmt do
+                       res = new BeerStats(b, row[0].to_f, row[1].to_i)
+                       break
+               end
+               return res
        end
 
        # Fetch the most recent rating left by `user_id` about `beer`
@@ -220,8 +233,13 @@ GROUP BY beer0, beer1""") else
        do
                var stmt = select("rating FROM reviews WHERE author = {user_id} AND beer = {beer} ORDER BY ROWID DESC LIMIT 1")
                assert stmt != null else print_error "Select 'rating' failed with: {error or else "?"}"
-               for row in stmt do return row[0].to_i
-               return null
+
+               var res = null
+               for row in stmt do
+                       res = row[0].to_i
+                       break
+               end
+               return res
        end
 
        # Register that `user_from` follows `user_to`
@@ -247,7 +265,8 @@ GROUP BY beer0, beer1""") else
                assert stmt != null else
                        print_error "Select 'follows' failed with: {error or else "?"}"
                end
-               for row in stmt do return true
+
+               for row in stmt.iterator.to_a do return true
                return false
        end