Stop pretending that we'll ever work with another DBMS.
authorMichael Orlitzky <michael@orlitzky.com>
Wed, 4 Nov 2015 17:07:16 +0000 (12:07 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Wed, 4 Nov 2015 17:07:16 +0000 (12:07 -0500)
13 files changed:
lib/common/agendav_plugin.rb
lib/common/davical_plugin.rb
lib/common/errors.rb
lib/common/postfixadmin_plugin.rb
lib/common/roundcube_plugin.rb
lib/mv/plugins/agendav.rb
lib/mv/plugins/davical.rb
lib/mv/plugins/postfixadmin.rb
lib/mv/plugins/roundcube.rb
lib/rm/plugins/agendav.rb
lib/rm/plugins/davical.rb
lib/rm/plugins/postfixadmin.rb
lib/rm/plugins/roundcube.rb

index 032d057c24d8e25c899ea672ae5fca50a711a8ff..9af7b01519b40aca2870176948ed4f0ac6f950cb 100644 (file)
@@ -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
 
index cd34a982529f70c09e156567e34cbcab97b7fc8e..363e08ef298cfc402aaf051ad486fedd3f553323 100644 (file)
@@ -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
 
index 7a2fbba565c035c7584972c3de211b0acf4ce4d2..6a9927af8070e561e21896c85ad7073ff00fb32e 100644 (file)
@@ -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
index b984f6cf66a5b2e5d66155124577f8c6a2c6eaef..3a5494bced822593e7a63f7a65c4422a02f3fb75 100644 (file)
@@ -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
 
index eb450f00059d038776046dc7604417e1de3c367e..948a5b91b5577dc5ebd3c45e24ad150b99556c97 100644 (file)
@@ -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
 
index 0324e6dc9e5003141ccf7ec6c8aacda1b3eb7130..ad1e9c2bfcf9f88b6fb67cf893a4c0ecb4d0348a 100644 (file)
@@ -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
index bd66df8bd8d979a3dd6aa74c4fed6f43a658deaf..20c8a32af50d668fe8ccba1f690091cbddcafe9a 100644 (file)
@@ -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
index 338be052f63affe1bb8deab2041cd994994cfbb6..659b9b7d7166f98d2974cdc53159098cc9a9e8af 100644 (file)
@@ -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
 
 
index 4b3ebcb5d20084d8c342445d2b5566a98324d6d5..7f345a4a70bb73094d02110ce8f6ff05d6a315e5 100644 (file)
@@ -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
index bebab618b06f190e979fb79c632c7239a409cccf..6d92c26d6fb054d9de44ba5d30b60abec76c2f8d 100644 (file)
@@ -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
index 3526dcff2fa4f9fc57950c32ac50e34c44d6872c..77b06b4777714a50a7ea18aa15e2116389128b2b 100644 (file)
@@ -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
 
 
index eddf93976981a5d031772cb3398001346d0fbc12..103912b648e71ce8edf02a6be0745d76cded190b 100644 (file)
@@ -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
 
index a315e77f49b2ec0c9b17c09d228ee55db8fa8459..c25874ef856beda9deda824d0d5d31a77d5fb4a3 100644 (file)
@@ -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