Skip to content
Permalink
Browse files

Implemented ORDER BY

  • Loading branch information...
lorenzo committed Jan 6, 2013
1 parent 694cd68 commit 3250af2336693e2bb0e5696bc69e5b178c82d90c
@@ -170,7 +170,8 @@ public function sql() {
$this->_replaceArrays();
}
$conjunction = $this->_conjunction;
return '(' . implode(" $conjunction ", $this->_conditions) . ')';
$template = ($this->count() === 1) ? '%s' : '(%s)';
return sprintf($template, implode(" $conjunction ", $this->_conditions));
}
public function traverse($callable) {
@@ -44,7 +44,6 @@ class Query implements IteratorAggregate {
'where' => ' WHERE %s',
'group' => ' GROUP BY %s ',
'having' => ' HAVING %s ',
'order' => ' ORDER BY %s',
'limit' => ' LIMIT %s',
'offset' => ' , %d'
];
@@ -303,11 +302,30 @@ public function orWhere($conditions, $types = []) {
return $this;
}
public function order($field, $direction = 'ASC') {
$this->_parts['order'] += [$field => $direction];
public function order($clause, $overwrite = false) {
$order = $this->_parts['order'];
if ($overwrite) {
$order = [];
}
if (!is_array($clause)) {
$clause = [$clause];
}
$order = array_merge($order, $clause);
$this->_parts['order'] = $order;
$this->_dirty = true;
return $this;
}
protected function _buildOrderPart($parts) {
$order = [];
foreach ($parts as $k => $direction) {
$order[] = is_numeric($k) ? $direction : sprintf('%s %s', $k, $direction);
}
return sprintf (' ORDER BY %s', implode(', ', $order));
}
public function group($field) {
$this->_parts['group'][] = $field;
return $this;
@@ -929,4 +929,73 @@ public function testSelectWhereNot() {
$this->assertEquals(['id' => 2], $result->fetch('assoc'));
}
/**
* Tests order() method both with simple fields and expressions
*
* @return void
**/
public function testSelectOrderBy() {
$statement = $this->_insertDateRecords();
$query = new Query($this->connection);
$result = $query
->select(['id'])
->from('dates')
->order(['id' => 'desc'])
->execute();
$this->assertEquals(['id' => 3], $result->fetch('assoc'));
$this->assertEquals(['id' => 2], $result->fetch('assoc'));
$this->assertEquals(['id' => 1], $result->fetch('assoc'));
$result = $query->order(['id' => 'asc'])->execute();
$this->assertEquals(['id' => 1], $result->fetch('assoc'));
$this->assertEquals(['id' => 2], $result->fetch('assoc'));
$this->assertEquals(['id' => 3], $result->fetch('assoc'));
$result = $query->order(['name' => 'asc'])->execute();
$this->assertEquals(['id' => 1], $result->fetch('assoc'));
$this->assertEquals(['id' => 2], $result->fetch('assoc'));
$this->assertEquals(['id' => 3], $result->fetch('assoc'));
$result = $query->order(['name' => 'asc'], true)->execute();
$this->assertEquals(['id' => 2], $result->fetch('assoc'));
$this->assertEquals(['id' => 1], $result->fetch('assoc'));
$this->assertEquals(['id' => 3], $result->fetch('assoc'));
$statement->bindValue(1, 4, 'integer');
$statement->bindValue(2, 'Chuck Norris');
$statement->bindValue(3, new \DateTime('2012-12-21 12:00'), 'datetime');
$statement->bindValue(4, 'N');
$statement->execute();
$statement->bindValue(1, 5, 'integer');
$statement->bindValue(2, 'Chuck Norris');
$statement->bindValue(3, new \DateTime('2012-12-20 12:00'), 'datetime');
$statement->bindValue(4, 'N');
$statement->execute();
$result = $query->order(['name' => 'asc', 'posted' => 'desc', 'visible' => 'asc'], true)
->execute();
$this->assertEquals(['id' => 2], $result->fetch('assoc'));
$this->assertEquals(['id' => 4], $result->fetch('assoc'));
$this->assertEquals(['id' => 1], $result->fetch('assoc'));
$this->assertEquals(['id' => 5], $result->fetch('assoc'));
$this->assertEquals(['id' => 3], $result->fetch('assoc'));
$expression = $query->newExpr()->add(['(id + :offset) % 2 = 0']);
$expression->bind(':offset', 1, null);
$result = $query->order([$expression, 'id' => 'desc'], true)->execute();
$this->assertEquals(['id' => 4], $result->fetch('assoc'));
$this->assertEquals(['id' => 2], $result->fetch('assoc'));
$this->assertEquals(['id' => 5], $result->fetch('assoc'));
$this->assertEquals(['id' => 3], $result->fetch('assoc'));
$this->assertEquals(['id' => 1], $result->fetch('assoc'));
$result = $query->order($expression, true)->order(['id' => 'asc'])->execute();
$this->assertEquals(['id' => 2], $result->fetch('assoc'));
$this->assertEquals(['id' => 4], $result->fetch('assoc'));
$this->assertEquals(['id' => 1], $result->fetch('assoc'));
$this->assertEquals(['id' => 3], $result->fetch('assoc'));
$this->assertEquals(['id' => 5], $result->fetch('assoc'));
}
}

0 comments on commit 3250af2

Please sign in to comment.
You can’t perform that action at this time.