+
+ # 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.query(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.
+ connection.close()
+ end
+
+ return (count > 0)
+ end
+