+
+ # Get a list of all Postfixadmin aliases as a <tt>from => to</tt>
+ # hash. This is useful for testing, since aliases should be removed
+ # when either the "from user" or "to user" are removed.
+ #
+ # @return [Hash] all aliases known to Postfixadmin in the form of a
+ # <tt>from => to</tt> hash.
+ #
+ def list_aliases()
+ aliases = []
+
+ connection = PG::Connection.new(@db_hash)
+
+ sql_query = 'SELECT address,goto FROM alias;'
+
+ begin
+ results = connection.sync_exec(sql_query)
+ results.each do |row|
+ # row should be a hash
+ aliases << row
+ end
+ ensure
+ # Make sure the connection gets closed even if the query explodes.
+ connection.close()
+ end
+
+ return aliases
+ end
+
+
+ # A fast implementation of the "does this domain exist?"
+ # operation. It only queries the database for the existence of
+ # *domain* rather than a list of all domains (which is the default
+ # implementation).
+ #
+ # @param domain [Domain] the domain whose existence is in question.
+ #
+ # @return [Boolean] true if *domain* exists in the Postfixadmin
+ # database and false otherwise.
+ #
+ def domain_exists(domain)
+ count = 0
+
+ connection = PG::Connection.new(@db_hash)
+
+ sql_query = 'SELECT COUNT(domain) as count FROM domain WHERE domain = $1;'
+
+ begin
+ connection.sync_exec_params(sql_query, [domain.to_s()]) do |result|
+ return false if result.ntuples() < 1
+ count = result.getvalue(0,0).to_i()
+
+ return false if count.nil?
+ end
+ ensure
+ # Make sure the connection gets closed even if the query explodes.