Skip to content

Commit

Permalink
[jan] Fix catching exceptions from Horde_Db.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Oct 22, 2014
1 parent a393118 commit d5dec92
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 41 deletions.
89 changes: 50 additions & 39 deletions framework/History/lib/Horde/History/Sql.php
Expand Up @@ -53,7 +53,7 @@ public function __construct($auth, Horde_Db_Adapter $db)
public function getActionTimestamp($guid, $action)
{
if (!is_string($guid) || !is_string($action)) {
throw new Horde_History_Exception('$guid and $action need to be strings!');
throw new InvalidArgumentException('$guid and $action need to be strings!');
}

try {
Expand Down Expand Up @@ -162,7 +162,11 @@ protected function _log(Horde_History_Log $history, array $attributes,
*/
public function _getHistory($guid)
{
$rows = $this->_db->selectAll('SELECT * FROM horde_histories WHERE object_uid = ?', array($guid));
try {
$rows = $this->_db->selectAll('SELECT * FROM horde_histories WHERE object_uid = ?', array($guid));
} catch (Horde_Db_Exception $e) {
throw new Horde_History_Exception($e);
}
return new Horde_History_Log($guid, $rows);
}

Expand Down Expand Up @@ -257,17 +261,21 @@ protected function _getByModSeq($start, $end, $filters = array(), $parent = null
);

// Add additional filters, if there are any.
if ($filters) {
foreach ($filters as $filter) {
$where[] = 'history_' . $filter['field'] . ' ' . $filter['op'] . ' ' . $this->_db->quote($filter['value']);
try {
if ($filters) {
foreach ($filters as $filter) {
$where[] = 'history_' . $filter['field'] . ' ' . $filter['op'] . ' ' . $this->_db->quote($filter['value']);
}
}
}

if ($parent) {
$where[] = 'object_uid LIKE ' . $this->_db->quote($parent . ':%');
}
if ($parent) {
$where[] = 'object_uid LIKE ' . $this->_db->quote($parent . ':%');
}

return $this->_db->selectAssoc('SELECT DISTINCT object_uid, history_id FROM horde_histories WHERE ' . implode(' AND ', $where));
return $this->_db->selectAssoc('SELECT DISTINCT object_uid, history_id FROM horde_histories WHERE ' . implode(' AND ', $where));
} catch (Horde_Db_Exception $e) {
throw new Horde_History_Exception($e);
}
}

/**
Expand All @@ -284,14 +292,18 @@ public function removeByNames(array $names)
}

$ids = array();
foreach ($names as $name) {
$ids[] = $this->_db->quote($name);
if ($this->_cache) {
$this->_cache->expire('horde:history:' . $name);
try {
foreach ($names as $name) {
$ids[] = $this->_db->quote($name);
if ($this->_cache) {
$this->_cache->expire('horde:history:' . $name);
}
}
}

$this->_db->delete('DELETE FROM horde_histories WHERE object_uid IN (' . implode(',', $ids) . ')');
$this->_db->delete('DELETE FROM horde_histories WHERE object_uid IN (' . implode(',', $ids) . ')');
} catch (Horde_Db_Exception $e) {
throw new Horde_History_Exception($e);
}
}

/**
Expand All @@ -310,31 +322,26 @@ public function removeByNames(array $names)
*/
public function getHighestModSeq($parent = null)
{
$sql = 'SELECT history_modseq FROM horde_histories';
if (!empty($parent)) {
$sql .= ' WHERE object_uid LIKE ' . $this->_db->quote($parent . ':%');
}
$sql .= ' ORDER BY history_modseq DESC';
$sql = $this->_db->addLimitOffset($sql, array('limit' => 1));

try {
$sql = 'SELECT history_modseq FROM horde_histories';
if (!empty($parent)) {
$sql .= ' WHERE object_uid LIKE ' . $this->_db->quote($parent . ':%');
}
$sql .= ' ORDER BY history_modseq DESC';
$sql = $this->_db->addLimitOffset($sql, array('limit' => 1));

$modseq = $this->_db->selectValue($sql);
} catch (Horde_Db_Exception $e) {
throw new Horde_History_Exception($e);
}
if (is_null($modseq) || $modseq === false) {
try {
if (is_null($modseq) || $modseq === false) {
$modseq = $this->_db->selectValue('SELECT MAX(history_modseq) FROM horde_histories_modseq');
} catch (Horde_Db_Exception $e) {
throw new Horde_History_Exception($e);
}
if (!empty($modseq)) {
return $modseq;
} else {
return false;
if (!empty($modseq)) {
return $modseq;
} else {
return false;
}
}
} catch (Horde_Db_Exception $e) {
throw new Horde_History_Exception($e);
}

return $modseq;
}

Expand Down Expand Up @@ -381,9 +388,13 @@ public function getLatestEntry($guid, $use_ts = false)
}
$query .= 'DESC LIMIT 1';

$row = $this->_db->selectOne($query, array($guid));
if (empty($row['history_id'])) {
return false;
try {
$row = $this->_db->selectOne($query, array($guid));
if (empty($row['history_id'])) {
return false;
}
} catch (Horde_Db_Exception $e) {
throw new Horde_History_Exception($e);
}

$log = new Horde_History_Log($guid, array($row));
Expand Down
4 changes: 2 additions & 2 deletions framework/History/package.xml
Expand Up @@ -21,7 +21,7 @@
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [jan] Fix catching exceptions from Horde_Db.
</notes>
<contents>
<dir baseinstalldir="/" name="/">
Expand Down Expand Up @@ -510,7 +510,7 @@ Converted to package.xml 2.0 for pear.horde.org
<date>2014-05-21</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [jan] Fix catching exceptions from Horde_Db.
</notes>
</release>
</changelog>
Expand Down

0 comments on commit d5dec92

Please sign in to comment.