Skip to content

Commit

Permalink
Handle empty, invalid, and single-element array arguments without dyi…
Browse files Browse the repository at this point in the history
…ng, generating invalid SQL, or hitting a str_repeat error.
  • Loading branch information
Chuck Hagenbuch committed Sep 2, 2014
1 parent 7eeb857 commit a4bdab8
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions jonah/lib/Driver/Sql.php
Expand Up @@ -855,7 +855,11 @@ public function searchTags($names, $max = 10, $from = 0, $channel_id = array(),
*/
public function getTagNames($ids)
{
$sql = 'SELECT t.tag_name FROM jonah_tags as t WHERE t.tag_id IN(' . str_repeat('?,', count($ids) - 1) . '?)';
if (empty($ids)) {
return array();
}

$sql = 'SELECT t.tag_name FROM jonah_tags as t WHERE t.tag_id IN (' . implode(',', array_map(function($v) { return '?'; }, $ids)) . ')';
$tags = $this->_db->getCol($sql, 0, $ids);
if ($tags instanceof PEAR_Error) {
throw new Jonah_Exception($tags);
Expand All @@ -873,7 +877,11 @@ public function getTagNames($ids)
*/
public function getTagIds($names)
{
$sql = 'SELECT t.tag_name, t.tag_id FROM jonah_tags as t WHERE t.tag_name IN(' . str_repeat('?,', count($names) - 1) . '?)';
if (empty($names)) {
return array();
}

$sql = 'SELECT t.tag_name, t.tag_id FROM jonah_tags as t WHERE t.tag_name IN (' . implode(',', array_map(function($v) { return '?'; }, $names)) . ')';
$tags = $this->_db->getAssoc($sql, false, $names);
if ($tags instanceof PEAR_Error) {
throw new Jonah_Exception($tags);
Expand Down

0 comments on commit a4bdab8

Please sign in to comment.