Stop pretending that we'll ever work with another DBMS.
[mailshears.git] / lib / rm / plugins / postfixadmin.rb
index 3883e866103e71dff7cf75ecb37681cebca7ca11..103912b648e71ce8edf02a6be0745d76cded190b 100644 (file)
@@ -9,11 +9,11 @@ class PostfixadminRm
   include RmPlugin
 
 
-  def delete_account(account)
-    raise NonexistentAccountError.new(account) if not user_exists(account)
+  def delete_user(user)
+    raise NonexistentUserError.new(user.to_s()) if not user_exists(user)
 
     sql_queries = ['DELETE FROM alias WHERE address = $1;']
-    # Wipe out any aliases pointed at our account.
+    # Wipe out any aliases pointed at our user.
     sql_queries << "UPDATE alias SET goto=REPLACE(goto, $1, '');"
     sql_queries << 'DELETE FROM mailbox WHERE username = $1;'
     sql_queries << 'DELETE FROM quota WHERE username = $1;'
@@ -23,37 +23,26 @@ class PostfixadminRm
     # Should be handled by a trigger, according to PostfixAdmin code.
     sql_queries << 'DELETE FROM vacation_notification WHERE on_vacation = $1;'
 
-    begin
-      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, [account])
-      end
+    connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+                                @db_name, @db_user, @db_pass)
 
-      # 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)
+    sql_queries.each do |sql_query|
+      connection.query(sql_query, [user.to_s()])
+    end
 
-      connection.close()
+    # 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)
 
-    rescue PGError => e
-      # Pretend like we're database-agnostic in case we ever are.
-      raise DatabaseError.new(e)
-    end
+    connection.close()
   end
 
 
   def delete_domain(domain)
-    raise NonexistentDomainError.new(domain) if not domain_exists(domain)
+    raise NonexistentDomainError.new(domain.to_s()) if not domain_exists(domain)
 
     sql_queries = ['DELETE FROM domain_admins WHERE domain = $1;']
     sql_queries << 'DELETE FROM alias WHERE domain = $1;'
@@ -63,26 +52,14 @@ class PostfixadminRm
     sql_queries << 'DELETE FROM vacation WHERE domain = $1;'
     sql_queries << 'DELETE FROM domain WHERE domain = $1;'
 
-    begin
-      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, [domain])
-      end
-
-      connection.close()
+    connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+                                @db_name, @db_user, @db_pass)
 
-    rescue PGError => e
-      # Pretend like we're database-agnostic in case we ever are.
-      raise DatabaseError.new(e)
+    sql_queries.each do |sql_query|
+      connection.query(sql_query, [domain.to_s()])
     end
 
+    connection.close()
   end
 
 
@@ -91,32 +68,22 @@ class PostfixadminRm
   def domain_exists(domain)
     count = 0
 
-    # Just assume PostgreSQL for now.
-    begin
-      connection = PGconn.connect(@db_host,
-                                  @db_port,
-                                  @db_opts,
-                                  @db_tty,
-                                  @db_name,
-                                  @db_user,
-                                  @db_pass)
-
-      sql_query = 'SELECT COUNT(domain) as count FROM domain WHERE domain = $1;'
-      connection.query(sql_query, [domain]) do |result|
-        return false if result.ntuples() < 1
-        begin
-          count = result.getvalue(0,0).to_i()
-          return false if count.nil?
-        rescue StandardError
-          return false
-        end
+    connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+                                @db_name, @db_user, @db_pass)
+
+    sql_query = 'SELECT COUNT(domain) as count FROM domain WHERE domain = $1;'
+    connection.query(sql_query, [domain.to_s()]) do |result|
+      return false if result.ntuples() < 1
+      begin
+        count = result.getvalue(0,0).to_i()
+        return false if count.nil?
+      rescue StandardError
+        return false
       end
-      connection.close()
-    rescue PGError => e
-      # But pretend like we're database-agnostic in case we ever are.
-      raise DatabaseError.new(e)
     end
 
+    connection.close()
+
     return (count > 0)
   end