Flatten the SQL result set so that the array difference actually works.
[mailshears.git] / src / postfixadmin_db.rb
1 require 'postgres'
2
3 class PostfixadminDb
4
5 def initialize(db_host,
6 db_port,
7 db_opts,
8 db_tty,
9 db_name,
10 db_user,
11 db_pass)
12
13 @db_host = db_host
14 @db_port = db_port
15 @db_opts = db_opts
16 @db_tty = db_tty
17 @db_name = db_name
18 @db_user = db_user
19 @db_pass = db_pass
20 end
21
22
23 def get_accounts_from_db()
24 # Just assume PostgreSQL for now.
25 begin
26 connection = PGconn.connect(@db_host,
27 @db_port,
28 @db_opts,
29 @db_tty,
30 @db_name,
31 @db_user,
32 @db_pass)
33
34 # If address = goto, then the alias basically says, "really
35 # deliver to that address; it's not an alias."
36 sql_query = 'SELECT address FROM alias WHERE address = goto;'
37 result = connection.query(sql_query)
38 connection.close()
39 rescue PGError => e
40 # But pretend like we're database-agnostic in case we ever are.
41 raise DatabaseError.new(e)
42 end
43
44 # The database query returns an array of rows. Since we only asked
45 # for one column (address), we can flatten the result into an
46 # array of addresses.
47 return result.flatten
48 end
49
50 end