#
users = []
- # 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 prefs)'
- sql_query += 'UNION'
- sql_query += '(SELECT user_from FROM shared);'
-
- connection.query(sql_query) do |result|
- users = 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)
+ connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+ @db_name, @db_user, @db_pass)
+
+ sql_query = '(SELECT username FROM prefs)'
+ sql_query += 'UNION'
+ sql_query += '(SELECT user_from FROM shared);'
+
+ connection.query(sql_query) do |result|
+ users = result.field_values('username')
end
+ connection.close()
+
return users.map{ |u| User.new(u) }
end
#
usernames = []
- begin
- connection = PGconn.connect(@db_host,
- @db_port,
- @db_opts,
- @db_tty,
- @db_name,
- @db_user,
- @db_pass)
-
- # User #1 is the super-user, and not tied to an email address.
- sql_query = "SELECT username FROM usr WHERE user_no > 1"
-
- connection.query(sql_query) do |result|
- usernames = result.field_values('username')
- end
+ connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+ @db_name, @db_user, @db_pass)
+
+ # User #1 is the super-user, and not tied to an email address.
+ sql_query = 'SELECT username FROM usr WHERE user_no > 1'
- connection.close()
- rescue PGError => e
- # Pretend like we're database-agnostic in case we ever are.
- raise DatabaseError.new(e)
+ connection.query(sql_query) do |result|
+ usernames = result.field_values('username')
end
+ connection.close()
+
return usernames.map{ |u| User.new(u) }
end
def get_principal_id(user)
principal_id = nil
- begin
- connection = PGconn.connect(@db_host,
- @db_port,
- @db_opts,
- @db_tty,
- @db_name,
- @db_user,
- @db_pass)
-
- sql_query = "SELECT principal.principal_id "
- sql_query += "FROM (principal INNER JOIN usr "
- sql_query += " ON principal.user_no = usr.user_no) "
- sql_query += "WHERE usr.username = $1;"
-
- connection.query(sql_query, [user.to_s()]) do |result|
- if result.num_tuples > 0
- principal_id = result[0]['principal_id']
- end
- end
+ connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+ @db_name, @db_user, @db_pass)
- connection.close()
+ sql_query = 'SELECT principal.principal_id '
+ sql_query += 'FROM (principal INNER JOIN usr '
+ sql_query += ' ON principal.user_no = usr.user_no) '
+ sql_query += 'WHERE usr.username = $1;'
- rescue PGError => e
- # Pretend like we're database-agnostic in case we ever are.
- raise DatabaseError.new(e)
+ connection.query(sql_query, [user.to_s()]) do |result|
+ if result.num_tuples > 0
+ principal_id = result[0]['principal_id']
+ end
end
+ connection.close()
+
return principal_id
end
-# A generalization of PGError, and whatever MySQL and the other
-# databases might eventually use.
-class DatabaseError < StandardError
-end
-
-
# Username is syntactically invalid.
class InvalidUserError < StandardError
end
def list_domains()
domains = []
- # Just assume PostgreSQL for now.
- begin
- connection = PGconn.connect(@db_host,
- @db_port,
- @db_opts,
- @db_tty,
- @db_name,
- @db_user,
- @db_pass)
-
- # 'ALL' is a magic domain, and we don't want it.
- sql_query = "SELECT domain FROM domain WHERE domain <> 'ALL';"
- connection.query(sql_query) do |result|
- domains = result.field_values('domain')
- end
- connection.close()
- rescue PGError => e
- # But pretend like we're database-agnostic in case we ever are.
- raise DatabaseError.new(e)
+ connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+ @db_name, @db_user, @db_pass)
+
+ # 'ALL' is a magic domain, and we don't want it.
+ sql_query = "SELECT domain FROM domain WHERE domain <> 'ALL';"
+ connection.query(sql_query) do |result|
+ domains = result.field_values('domain')
end
+ connection.close()
+
return domains.map{ |d| Domain.new(d) }
end
def list_users()
users = []
- # 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;'
- connection.query(sql_query) do |result|
- users = result.field_values('username')
- end
- connection.close()
- rescue PGError => e
- # But pretend like we're database-agnostic in case we ever are.
- raise DatabaseError.new(e)
+ connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+ @db_name, @db_user, @db_pass)
+
+ sql_query = 'SELECT username FROM mailbox;'
+ connection.query(sql_query) do |result|
+ users = result.field_values('username')
end
+ connection.close()
+
return users.map{ |u| User.new(u) }
end
def list_domains_users(domains)
usernames = []
- # 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.map{|d| d.to_s()}) 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)
+ 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.map{|d| d.to_s()}) do |result|
+ usernames = result.field_values('username')
end
+ connection.close()
+
return usernames.map{ |u| User.new(u) }
end
#
aliases = []
- # 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 address,goto FROM alias;'
- results = connection.query(sql_query)
- results.each do |row|
- aliases << row # row should be a hash
- end
- connection.close()
- rescue PGError => e
- # But pretend like we're database-agnostic in case we ever are.
- raise DatabaseError.new(e)
+ 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|
+ aliases << row # row should be a hash
end
+ connection.close()
+
return aliases
end
# is public because it is useful in testing.
usernames = []
- # 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 users;"
- connection.query(sql_query) do |result|
- usernames = result.field_values('username')
- end
+ connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+ @db_name, @db_user, @db_pass)
- connection.close()
- rescue PGError => e
- # Pretend like we're database-agnostic in case we ever are.
- raise DatabaseError.new(e)
+ sql_query = 'SELECT username FROM users;'
+ connection.query(sql_query) do |result|
+ usernames = result.field_values('username')
end
+ connection.close()
+
return usernames.map{ |u| User.new(u) }
end
def get_user_id(user)
user_id = nil
- begin
- connection = PGconn.connect(@db_host,
- @db_port,
- @db_opts,
- @db_tty,
- @db_name,
- @db_user,
- @db_pass)
-
- sql_query = "SELECT user_id FROM users WHERE username = $1;"
-
- connection.query(sql_query, [user.to_s()]) do |result|
- if result.num_tuples > 0
- user_id = result[0]['user_id']
- end
- end
+ connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+ @db_name, @db_user, @db_pass)
- connection.close()
+ sql_query = 'SELECT user_id FROM users WHERE username = $1;'
- rescue PGError => e
- # Pretend like we're database-agnostic in case we ever are.
- raise DatabaseError.new(e)
+ connection.query(sql_query, [user.to_s()]) do |result|
+ if result.num_tuples > 0
+ user_id = result[0]['user_id']
+ end
end
+ connection.close()
+
return user_id
end
sql_queries << 'UPDATE shared SET user_from = $1 WHERE user_from = $2;'
sql_queries << 'UPDATE shared SET user_which = $1 WHERE user_which = $2;'
- begin
- connection = PGconn.connect(@db_host,
- @db_port,
- @db_opts,
- @db_tty,
- @db_name,
- @db_user,
- @db_pass)
-
- sql_queries.each do |sql_query|
- connection.query(sql_query, [dst.to_s(), src.to_s()])
- end
-
- connection.close()
-
- rescue PGError => e
- # Pretend like we're database-agnostic in case we ever are.
- raise DatabaseError.new(e)
+ connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+ @db_name, @db_user, @db_pass)
+
+ sql_queries.each do |sql_query|
+ connection.query(sql_query, [dst.to_s(), src.to_s()])
end
+
+ connection.close()
end
end
sql_queries = ['UPDATE usr SET username = $1 WHERE username = $2']
- begin
- connection = PGconn.connect(@db_host,
- @db_port,
- @db_opts,
- @db_tty,
- @db_name,
- @db_user,
- @db_pass)
-
- sql_queries.each do |sql_query|
- connection.query(sql_query, [dst.to_s(), src.to_s()])
- end
-
- connection.close()
-
- rescue PGError => e
- # Pretend like we're database-agnostic in case we ever are.
- raise DatabaseError.new(e)
+ connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+ @db_name, @db_user, @db_pass)
+
+ sql_queries.each do |sql_query|
+ connection.query(sql_query, [dst.to_s(), src.to_s()])
end
+ connection.close()
end
end
sql_queries = [mailbox_query, alias_query1, alias_query2]
- begin
- connection = PGconn.connect(@db_host,
- @db_port,
- @db_opts,
- @db_tty,
- @db_name,
- @db_user,
- @db_pass)
-
- sql_queries.each do |sql_query|
- varchar = 1043 # from pg_type.h
- params = [{:value => dst.to_s(), :type => varchar},
- {:value => dst.domainpart(), :type => varchar},
- {:value => dst.localpart(), :type => varchar},
- {:value => src.to_s(), :type => varchar}]
- connection.query(sql_query, params)
- end
-
- connection.close()
-
- rescue PGError => e
- # Pretend like we're database-agnostic in case we ever are.
- raise DatabaseError.new(e)
- end
+ connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+ @db_name, @db_user, @db_pass)
+
+ sql_queries.each do |sql_query|
+ varchar = 1043 # from pg_type.h
+ params = [{:value => dst.to_s(), :type => varchar},
+ {:value => dst.domainpart(), :type => varchar},
+ {:value => dst.localpart(), :type => varchar},
+ {:value => src.to_s(), :type => varchar}]
+
+ connection.query(sql_query, params)
+ connection.close()
end
sql_queries = ['UPDATE users SET username = $1 WHERE username = $2;']
- begin
- connection = PGconn.connect(@db_host,
- @db_port,
- @db_opts,
- @db_tty,
- @db_name,
- @db_user,
- @db_pass)
-
- sql_queries.each do |sql_query|
- connection.query(sql_query, [dst.to_s(), src.to_s()])
- end
-
- connection.close()
-
- rescue PGError => e
- # Pretend like we're database-agnostic in case we ever are.
- raise DatabaseError.new(e)
+ connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+ @db_name, @db_user, @db_pass)
+
+ sql_queries.each do |sql_query|
+ connection.query(sql_query, [dst.to_s(), src.to_s()])
end
+ connection.close()
end
end
sql_queries = ['DELETE FROM prefs WHERE username = $1;']
sql_queries << 'DELETE FROM shared WHERE user_from = $1;'
- begin
- connection = PGconn.connect(@db_host,
- @db_port,
- @db_opts,
- @db_tty,
- @db_name,
- @db_user,
- @db_pass)
-
- sql_queries.each do |sql_query|
- connection.query(sql_query, [user.to_s()])
- end
-
- connection.close()
-
- rescue PGError => e
- # Pretend like we're database-agnostic in case we ever are.
- raise DatabaseError.new(e)
+ connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+ @db_name, @db_user, @db_pass)
+
+ sql_queries.each do |sql_query|
+ connection.query(sql_query, [user.to_s()])
end
+ connection.close()
end
end
sql_queries = ['DELETE FROM usr WHERE username = $1']
- begin
- connection = PGconn.connect(@db_host,
- @db_port,
- @db_opts,
- @db_tty,
- @db_name,
- @db_user,
- @db_pass)
-
- sql_queries.each do |sql_query|
- connection.query(sql_query, [user.to_s()])
- end
-
- connection.close()
-
- rescue PGError => e
- # Pretend like we're database-agnostic in case we ever are.
- raise DatabaseError.new(e)
+ connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+ @db_name, @db_user, @db_pass)
+
+ sql_queries.each do |sql_query|
+ connection.query(sql_query, [user.to_s()])
end
+ connection.close()
end
# Should be handled by a trigger, according to PostfixAdmin code.
sql_queries << 'DELETE FROM vacation_notification WHERE on_vacation = $1;'
- begin
- connection = PGconn.connect(@db_host,
- @db_port,
- @db_opts,
- @db_tty,
- @db_name,
- @db_user,
- @db_pass)
-
- sql_queries.each do |sql_query|
- connection.query(sql_query, [user.to_s()])
- end
+ connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+ @db_name, @db_user, @db_pass)
- # The earlier alias update query will leave things like
- # "foo@example.com,,bar@example.com" in the "goto" column. Now
- # we fix it. We don't do it in the loop because query() craps
- # out on the superfluous parameter.
- sql_query = "UPDATE alias SET goto=REPLACE(goto, ',,', ',');"
- connection.query(sql_query)
+ sql_queries.each do |sql_query|
+ connection.query(sql_query, [user.to_s()])
+ end
- connection.close()
+ # The earlier alias update query will leave things like
+ # "foo@example.com,,bar@example.com" in the "goto" column. Now
+ # we fix it. We don't do it in the loop because query() craps
+ # out on the superfluous parameter.
+ sql_query = "UPDATE alias SET goto=REPLACE(goto, ',,', ',');"
+ connection.query(sql_query)
- rescue PGError => e
- # Pretend like we're database-agnostic in case we ever are.
- raise DatabaseError.new(e)
- end
+ connection.close()
end
sql_queries << 'DELETE FROM vacation WHERE domain = $1;'
sql_queries << 'DELETE FROM domain WHERE domain = $1;'
- begin
- connection = PGconn.connect(@db_host,
- @db_port,
- @db_opts,
- @db_tty,
- @db_name,
- @db_user,
- @db_pass)
-
- sql_queries.each do |sql_query|
- connection.query(sql_query, [domain.to_s()])
- end
-
- connection.close()
+ connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+ @db_name, @db_user, @db_pass)
- rescue PGError => e
- # Pretend like we're database-agnostic in case we ever are.
- raise DatabaseError.new(e)
+ sql_queries.each do |sql_query|
+ connection.query(sql_query, [domain.to_s()])
end
+ connection.close()
end
def domain_exists(domain)
count = 0
- # 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 COUNT(domain) as count FROM domain WHERE domain = $1;'
- connection.query(sql_query, [domain.to_s()]) do |result|
- return false if result.ntuples() < 1
- begin
- count = result.getvalue(0,0).to_i()
- return false if count.nil?
- rescue StandardError
- return false
- end
+ connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+ @db_name, @db_user, @db_pass)
+
+ sql_query = 'SELECT COUNT(domain) as count FROM domain WHERE domain = $1;'
+ connection.query(sql_query, [domain.to_s()]) do |result|
+ return false if result.ntuples() < 1
+ begin
+ count = result.getvalue(0,0).to_i()
+ return false if count.nil?
+ rescue StandardError
+ return false
end
- connection.close()
- rescue PGError => e
- # But pretend like we're database-agnostic in case we ever are.
- raise DatabaseError.new(e)
end
+ connection.close()
+
return (count > 0)
end
# This query is of course necessary with any DBMS:
sql_queries = ['DELETE FROM users WHERE user_id = $1::int;']
- begin
- connection = PGconn.connect(@db_host,
- @db_port,
- @db_opts,
- @db_tty,
- @db_name,
- @db_user,
- @db_pass)
-
- sql_queries.each do |sql_query|
- connection.query(sql_query, [user_id])
- end
-
- connection.close()
-
- rescue PGError => e
- # Pretend like we're database-agnostic in case we ever are.
- raise DatabaseError.new(e)
+ connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+ @db_name, @db_user, @db_pass)
+
+ sql_queries.each do |sql_query|
+ connection.query(sql_query, [user_id])
end
+ connection.close()
end
end