From 4265a9803e0bc6a2eb619e43b78ad7df093939cc Mon Sep 17 00:00:00 2001 From: Michael J Rubinsky Date: Fri, 29 Jul 2016 17:50:50 -0400 Subject: [PATCH] Update existing entries instead of DELETE/INSERT. --- sam/lib/Driver/Amavisd/Sql.php | 45 ++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/sam/lib/Driver/Amavisd/Sql.php b/sam/lib/Driver/Amavisd/Sql.php index 106d1918352..9c98618edc3 100644 --- a/sam/lib/Driver/Amavisd/Sql.php +++ b/sam/lib/Driver/Amavisd/Sql.php @@ -157,23 +157,7 @@ public function retrieve() */ public function store($defaults = false) { - /* Check if the policy already exists. */ - $policyID = $this->_lookupPolicyID(); - - /* Delete existing policy. */ - if ($policyID !== false) { - try { - $this->_db->delete( - sprintf('DELETE FROM %s WHERE %s = ?', - $this->_mapNameToTable('policies'), - $this->_mapAttributeToField('policies', 'name')), - array($this->_user)); - } catch (Horde_Db_Exception $e) { - throw new Sam_Exception($e); - } - } - - /* Insert new policy (everything but whitelists and blacklists). */ + /* Generate new policy (everything but whitelists and blacklists). */ $insertKeys = $insertVals = array(); foreach ($this->_options as $attribute => $value) { if ($attribute != 'whitelist_from' && @@ -182,7 +166,12 @@ public function store($defaults = false) $insertVals[] = strlen($value) ? $value : null; } } - if (count($insertKeys)) { + + /* Check if the policy already exists. */ + $policyID = $this->_lookupPolicyID(); + + if ($policyID === false && count($insertKeys)) { + // Create new policy for user. try { $this->_db->insert( sprintf('INSERT INTO %s (%s, %s) VALUES (%s)', @@ -194,11 +183,25 @@ public function store($defaults = false) } catch (Horde_Db_Exception $e) { throw new Sam_Exception($e); } + $policyID = $this->_lookupPolicyID(); + } elseif ($policyID && count($insertKeys)) { + // Update user's policy. + $update = array(); + foreach ($insertKeys as $value) { + $update[] = $this->_mapAttributeToField('policies', $value) . ' = ?'; + } + try { + $this->_db->update( + sprintf('UPDATE %s SET %s WHERE %s', + $this->_mapNameToTable('policies'), + implode(', ', $update), + $this->_mapAttributeToField('policies', 'id') . ' = ?'), + array_merge($insertVals, array($policyID))); + } catch (Horde_Db_Exception $e) { + throw new Sam_Exception($e); + } } - /* Get the new policy id for the recipients table. */ - $policyID = $this->_lookupPolicyID(); - /* Update recipients with new policy id. */ try { $this->_db->update(