]> gitweb.michael.orlitzky.com - mailshears.git/blob - src/postfixadmin_db.rb
f4e6feb5c999119535afff82f16ec9d9b83b51cb
[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_domains_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 sql_query = 'SELECT domain FROM domain;'
35 result = connection.query(sql_query)
36 connection.close()
37 rescue PGError => e
38 # But pretend like we're database-agnostic in case we ever are.
39 raise DatabaseError.new(e)
40 end
41
42 # The database query returns an array of rows. Since we only asked
43 # for one column (domain), we can flatten the result into an
44 # array of domains.
45 return result.flatten
46 end
47
48
49 def get_accounts_from_db()
50 # Just assume PostgreSQL for now.
51 begin
52 connection = PGconn.connect(@db_host,
53 @db_port,
54 @db_opts,
55 @db_tty,
56 @db_name,
57 @db_user,
58 @db_pass)
59
60 # If address = goto, then the alias basically says, "really
61 # deliver to that address; it's not an alias."
62 sql_query = 'SELECT username FROM mailbox;'
63 result = connection.query(sql_query)
64 connection.close()
65 rescue PGError => e
66 # But pretend like we're database-agnostic in case we ever are.
67 raise DatabaseError.new(e)
68 end
69
70 # The database query returns an array of rows. Since we only asked
71 # for one column (address), we can flatten the result into an
72 # array of addresses.
73 return result.flatten
74 end
75
76 end