Skip to content

Commit

Permalink
Update existing entries instead of DELETE/INSERT.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrubinsk committed Jul 29, 2016
1 parent 108f8dd commit 4265a98
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions sam/lib/Driver/Amavisd/Sql.php
Expand Up @@ -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' &&
Expand All @@ -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)',
Expand All @@ -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(
Expand Down

0 comments on commit 4265a98

Please sign in to comment.