Skip to content

Commit

Permalink
Add: WHERE IN (?, ?, ...)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pym committed Mar 10, 2014
1 parent 6b8f570 commit 2de2d81
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/Pym/Table.php
Expand Up @@ -53,6 +53,16 @@ public function getQuery()
return $this->buildSQL();
}

public function getQueryParams()
{
$flattenQueryParams = [];
array_walk_recursive($this->queryParams, function($a) use (&$flattenQueryParams) {
$flattenQueryParams[] = $a;
});

return $flattenQueryParams;
}

protected function escapeColumns(array $columns) {
array_walk($columns, function(&$value) {
$escapeColumn = function($value, $noAutoAlias = false) {
Expand Down Expand Up @@ -159,13 +169,18 @@ protected function buildWhere()
$operator = '=';
break;
}
$currentWhereCollection[] = sprintf('%s %s ?', $key, $operator);
$currentWhereCollection[] = sprintf('%s %s %s',
$key,
$operator,
strtoupper($operator) === 'IN' ? '('.implode(', ', array_fill(0, count($value), '?')).')' : '?')
;
}

$currentWhereString = implode(sprintf(' %s ', $where['operator']), $currentWhereCollection);
if ($where['operator'] === 'OR') $currentWhereString = "($currentWhereString)";

$whereList[] = $currentWhereString;

$this->queryParams = array_merge($this->queryParams, array_values($where['collection']));
}

Expand Down Expand Up @@ -240,17 +255,17 @@ public function fetchAll(array $where = array())
{
if (count($where)) $this->where($where);

return $this->db->fetchAll($this->getQuery(), $this->queryParams);
return $this->db->fetchAll($this->getQuery(), $this->getQueryParams());
}

public function fetchAllKeyPair()
{
return $this->db->executeQuery($this->getQuery(), $this->queryParams)->fetchAll(\PDO::FETCH_KEY_PAIR);
return $this->db->executeQuery($this->getQuery(), $this->getQueryParams())->fetchAll(\PDO::FETCH_KEY_PAIR);
}

public function fetchAllKeyPairs()
{
$results = $this->db->executeQuery($this->getQuery(), $this->queryParams)->fetchAll(\PDO::FETCH_GROUP|\PDO::FETCH_ASSOC);
$results = $this->db->executeQuery($this->getQuery(), $this->getQueryParams())->fetchAll(\PDO::FETCH_GROUP|\PDO::FETCH_ASSOC);

return array_map('reset', $results);
}
Expand All @@ -259,13 +274,13 @@ public function fetchAssoc(array $where = array())
{
if (count($where)) $this->where($where);

return $this->db->fetchAssoc($this->getQuery(), $this->queryParams);
return $this->db->fetchAssoc($this->getQuery(), $this->getQueryParams());
}

public function executeQuery($query, array $queryParams = array())
{
if (count($queryParams) === 0) {
$queryParams = $this->queryParams;
$queryParams = $this->getQueryParams();
}

return $this->db->executeQuery($query, $queryParams);
Expand Down Expand Up @@ -306,5 +321,4 @@ public function drop()
{
return $this->db->query(sprintf('DELETE FROM `%s`', $this->tableName));
}

}
8 changes: 8 additions & 0 deletions tests/TableTest.php
Expand Up @@ -100,6 +100,14 @@ public function testWhereLike()
$this->assertEquals($this->testTable->getQuery(), 'SELECT * FROM `user` u WHERE name = ? AND username LIKE ?');
}

public function testWhereIn()
{
$this->testTable->where(['value' => ['IN' => ['1', '2' ,'3']]]);

$this->assertEquals($this->testTable->getQuery(), 'SELECT * FROM `user` u WHERE value IN (?, ?, ?)');
$this->assertEquals($this->testTable->getQueryParams(), ['1', '2', '3']);
}

public function testLimit()
{
$this->testTable->limit(10);
Expand Down

0 comments on commit 2de2d81

Please sign in to comment.