]> gitweb.michael.orlitzky.com - mailshears.git/blobdiff - lib/rm/plugins/postfixadmin.rb
lib,test: replace connection query() method with sync_exec{,_params}.
[mailshears.git] / lib / rm / plugins / postfixadmin.rb
index b950b7a76d6c27e9d80c1d9d0b0972427681baf3..958d42cbfd530e9fcb0ae6c7b1b83fa9b2c0ebb3 100644 (file)
@@ -31,10 +31,16 @@ class PostfixadminRm
 
     # But aliases don't need to point to a single user! If our user
     # was part of a multi-recipient alias, we want to remove our user
 
     # But aliases don't need to point to a single user! If our user
     # was part of a multi-recipient alias, we want to remove our user
-    # from the alias and leave the other recipients. If you're
-    # wondering about the leftover double-commas, look towards the end
-    # of the function.
-    sql_queries << "UPDATE alias SET goto=REPLACE(goto, $1, '');"
+    # from the alias and leave the other recipients.
+    #
+    # We want to delete the comma that precedes/follows the address,
+    # too.  Since the address to be replaced can appear at either the
+    # beginning or the end of the list (as well as in the middle), we
+    # have to try to fix both cases: comma before, and comma after.
+    comma_before = "CONCAT(',', $1)"
+    comma_after  = "CONCAT($1, ',')"
+    sql_queries << "UPDATE alias SET goto=REPLACE(goto, #{comma_before}, '');"
+    sql_queries << "UPDATE alias SET goto=REPLACE(goto, #{comma_after}, '');"
 
     sql_queries << 'DELETE FROM mailbox WHERE username = $1;'
     sql_queries << 'DELETE FROM quota WHERE username = $1;'
 
     sql_queries << 'DELETE FROM mailbox WHERE username = $1;'
     sql_queries << 'DELETE FROM quota WHERE username = $1;'
@@ -44,21 +50,18 @@ class PostfixadminRm
     # Should be handled by a trigger, according to PostfixAdmin code.
     sql_queries << 'DELETE FROM vacation_notification WHERE on_vacation = $1;'
 
     # Should be handled by a trigger, according to PostfixAdmin code.
     sql_queries << 'DELETE FROM vacation_notification WHERE on_vacation = $1;'
 
-    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, [user.to_s()])
+    connection = PG::Connection.new(@db_hash)
+
+    begin
+      sql_queries.each do |sql_query|
+        varchar = 1043 # from pg_type.h
+        params = [{:value => user.to_s(), :type => varchar}]
+        connection.sync_exec_params(sql_query, params)
+      end
+    ensure
+      # Make sure the connection gets closed even if a query explodes.
+      connection.close()
     end
     end
-
-    # The earlier alias update query will leave things like
-    # "foo@example.com,,bar@example.com" in the "goto" column. Now
-    # we fix it. We don't do it in the loop because query() craps
-    # out on the superfluous parameter.
-    sql_query = "UPDATE alias SET goto=REPLACE(goto, ',,', ',');"
-    connection.query(sql_query)
-
-    connection.close()
   end
 
 
   end
 
 
@@ -96,14 +99,16 @@ class PostfixadminRm
     sql_queries << 'DELETE FROM vacation WHERE domain = $1;'
     sql_queries << 'DELETE FROM domain WHERE domain = $1;'
 
     sql_queries << 'DELETE FROM vacation WHERE domain = $1;'
     sql_queries << 'DELETE FROM domain WHERE domain = $1;'
 
-    connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
-                                @db_name, @db_user, @db_pass)
+    connection = PG::Connection.new(@db_hash)
 
 
-    sql_queries.each do |sql_query|
-      connection.query(sql_query, [domain.to_s()])
+    begin
+      sql_queries.each do |sql_query|
+        connection.sync_exec_params(sql_query, [domain.to_s()])
+      end
+    ensure
+      # Make sure the connection gets closed even if a query explodes.
+      connection.close()
     end
     end
-
-    connection.close()
   end
 
 end
   end
 
 end