From: Michael Orlitzky Date: Wed, 4 Nov 2015 17:07:16 +0000 (-0500) Subject: Stop pretending that we'll ever work with another DBMS. X-Git-Tag: 0.0.1~25 X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=20b843bddcd73833d41f98ff79d92ef59bb4d81e;p=mailshears.git Stop pretending that we'll ever work with another DBMS. --- diff --git a/lib/common/agendav_plugin.rb b/lib/common/agendav_plugin.rb index 032d057..9af7b01 100644 --- a/lib/common/agendav_plugin.rb +++ b/lib/common/agendav_plugin.rb @@ -25,30 +25,19 @@ module AgendavPlugin # 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 diff --git a/lib/common/davical_plugin.rb b/lib/common/davical_plugin.rb index cd34a98..363e08e 100644 --- a/lib/common/davical_plugin.rb +++ b/lib/common/davical_plugin.rb @@ -30,28 +30,18 @@ module DavicalPlugin # 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 @@ -61,33 +51,22 @@ module DavicalPlugin 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 diff --git a/lib/common/errors.rb b/lib/common/errors.rb index 7a2fbba..6a9927a 100644 --- a/lib/common/errors.rb +++ b/lib/common/errors.rb @@ -1,9 +1,3 @@ -# 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 diff --git a/lib/common/postfixadmin_plugin.rb b/lib/common/postfixadmin_plugin.rb index b984f6c..3a5494b 100644 --- a/lib/common/postfixadmin_plugin.rb +++ b/lib/common/postfixadmin_plugin.rb @@ -22,27 +22,17 @@ module PostfixadminPlugin 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 @@ -51,26 +41,16 @@ module PostfixadminPlugin 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 @@ -78,28 +58,17 @@ module PostfixadminPlugin 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 @@ -110,27 +79,17 @@ module PostfixadminPlugin # 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 diff --git a/lib/common/roundcube_plugin.rb b/lib/common/roundcube_plugin.rb index eb450f0..948a5b9 100644 --- a/lib/common/roundcube_plugin.rb +++ b/lib/common/roundcube_plugin.rb @@ -28,27 +28,16 @@ module RoundcubePlugin # 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 @@ -57,30 +46,19 @@ module RoundcubePlugin 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 diff --git a/lib/mv/plugins/agendav.rb b/lib/mv/plugins/agendav.rb index 0324e6d..ad1e9c2 100644 --- a/lib/mv/plugins/agendav.rb +++ b/lib/mv/plugins/agendav.rb @@ -21,25 +21,14 @@ class AgendavMv 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 diff --git a/lib/mv/plugins/davical.rb b/lib/mv/plugins/davical.rb index bd66df8..20c8a32 100644 --- a/lib/mv/plugins/davical.rb +++ b/lib/mv/plugins/davical.rb @@ -27,26 +27,14 @@ class DavicalMv 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 diff --git a/lib/mv/plugins/postfixadmin.rb b/lib/mv/plugins/postfixadmin.rb index 338be05..659b9b7 100644 --- a/lib/mv/plugins/postfixadmin.rb +++ b/lib/mv/plugins/postfixadmin.rb @@ -39,30 +39,18 @@ class PostfixadminMv 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 diff --git a/lib/mv/plugins/roundcube.rb b/lib/mv/plugins/roundcube.rb index 4b3ebcb..7f345a4 100644 --- a/lib/mv/plugins/roundcube.rb +++ b/lib/mv/plugins/roundcube.rb @@ -20,26 +20,14 @@ class RoundcubeMv 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 diff --git a/lib/rm/plugins/agendav.rb b/lib/rm/plugins/agendav.rb index bebab61..6d92c26 100644 --- a/lib/rm/plugins/agendav.rb +++ b/lib/rm/plugins/agendav.rb @@ -17,26 +17,14 @@ class AgendavRm 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 diff --git a/lib/rm/plugins/davical.rb b/lib/rm/plugins/davical.rb index 3526dcf..77b06b4 100644 --- a/lib/rm/plugins/davical.rb +++ b/lib/rm/plugins/davical.rb @@ -20,26 +20,14 @@ class DavicalRm 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 diff --git a/lib/rm/plugins/postfixadmin.rb b/lib/rm/plugins/postfixadmin.rb index eddf939..103912b 100644 --- a/lib/rm/plugins/postfixadmin.rb +++ b/lib/rm/plugins/postfixadmin.rb @@ -23,32 +23,21 @@ class PostfixadminRm # 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 @@ -63,26 +52,14 @@ class PostfixadminRm 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 @@ -91,32 +68,22 @@ class PostfixadminRm 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 diff --git a/lib/rm/plugins/roundcube.rb b/lib/rm/plugins/roundcube.rb index a315e77..c25874e 100644 --- a/lib/rm/plugins/roundcube.rb +++ b/lib/rm/plugins/roundcube.rb @@ -26,26 +26,14 @@ class RoundcubeRm # 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