- # 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 username FROM mailbox WHERE domain IN $1;'
-
- connection.query(sql_query, [domains]) do |result|
- usernames = result.field_values('username')
- end
-
- connection.close()
- rescue PGError => e
- # Pretend like we're database-agnostic in case we ever are.
- raise DatabaseError.new(e)
+ # The number of parameters that we'll pass into our prepared query
+ # is the number of domains that we're given. It's important that
+ # we have at least one domain here.
+ params = 1.upto(domains.length()).map{ |i| '$' + i.to_s() }.join(',')
+ sql_query = "SELECT username FROM mailbox WHERE domain IN (#{params});"
+
+ # Now replace each Domain with its string representation and pass
+ # those in as our individual parameters.
+ connection.query(sql_query, domains.map{ |d| d.to_s() }) do |result|
+ usernames = result.field_values('username')
+ end
+
+ connection.close()
+
+ return usernames.map{ |u| User.new(u) }
+ end
+
+
+ # 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 = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+ @db_name, @db_user, @db_pass)
+
+ sql_query = 'SELECT address,goto FROM alias;'
+ results = connection.query(sql_query)
+ results.each do |row|
+ # row should be a hash
+ aliases << row