lib,test: replace connection query() method with sync_exec{,_params}.
[mailshears.git] / lib / mv / plugins / roundcube.rb
index 4d8196461ca570a8e51a5aa0880365dda8d950f5..e2126f75f244bd5288647ec41ff7610917b90b42 100644 (file)
@@ -30,16 +30,15 @@ class RoundcubeMv
     raise NonexistentUserError.new(src.to_s()) if not user_exists(src)
     raise UserAlreadyExistsError.new(dst.to_s()) if user_exists(dst)
 
-    sql_queries = ['UPDATE users SET username = $1 WHERE username = $2;']
-
-    connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
-                                @db_name, @db_user, @db_pass)
-
-    sql_queries.each do |sql_query|
-      connection.query(sql_query, [dst.to_s(), src.to_s()])
+    sql_query = 'UPDATE users SET username = $1 WHERE username = $2;'
+
+    connection = PG::Connection.new(@db_hash)
+    begin
+      connection.sync_exec_params(sql_query, [dst.to_s(), src.to_s()])
+    ensure
+      # Make sure the connection gets closed even if the query explodes.
+      connection.close()
     end
-
-    connection.close()
   end
 
 end