Skip to content

Commit

Permalink
Add ORDERBY and GROUP capabilities
Browse files Browse the repository at this point in the history
Existing was ORDER and GROUPBY...
  • Loading branch information
trasher committed Sep 26, 2018
1 parent 3086c84 commit cb17289
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions inc/dbmysqliterator.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ function buildQuery ($table, $crit = "", $log = false) {
break;

case 'ORDER' :
case 'ORDERBY' :
$orderby = $val;
unset($crit[$key]);
break;
Expand All @@ -186,6 +187,7 @@ function buildQuery ($table, $crit = "", $log = false) {
unset($crit[$key]);
break;

case 'GROUP' :
case 'GROUPBY' :
$groupby = $val;
unset($crit[$key]);
Expand Down
41 changes: 41 additions & 0 deletions tests/units/DBmysqlIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,24 +200,45 @@ public function testFields() {


public function testOrder() {
$it = $this->it->execute('foo', ['ORDERBY' => 'bar']);
$this->string($it->getSql())->isIdenticalTo('SELECT * FROM `foo` ORDER BY `bar`');

$it = $this->it->execute('foo', ['ORDER' => 'bar']);
$this->string($it->getSql())->isIdenticalTo('SELECT * FROM `foo` ORDER BY `bar`');

$it = $this->it->execute('foo', ['ORDERBY' => '`baz`']);
$this->string($it->getSql())->isIdenticalTo('SELECT * FROM `foo` ORDER BY `baz`');

$it = $this->it->execute('foo', ['ORDER' => '`baz`']);
$this->string($it->getSql())->isIdenticalTo('SELECT * FROM `foo` ORDER BY `baz`');

$it = $this->it->execute('foo', ['ORDERBY' => 'bar ASC']);
$this->string($it->getSql())->isIdenticalTo('SELECT * FROM `foo` ORDER BY `bar` ASC');

$it = $this->it->execute('foo', ['ORDER' => 'bar ASC']);
$this->string($it->getSql())->isIdenticalTo('SELECT * FROM `foo` ORDER BY `bar` ASC');

$it = $this->it->execute('foo', ['ORDERBY' => 'bar DESC']);
$this->string($it->getSql())->isIdenticalTo('SELECT * FROM `foo` ORDER BY `bar` DESC');

$it = $this->it->execute('foo', ['ORDER' => 'bar DESC']);
$this->string($it->getSql())->isIdenticalTo('SELECT * FROM `foo` ORDER BY `bar` DESC');

$it = $this->it->execute('foo', ['ORDERBY' => ['`a`', 'b ASC', 'c DESC']]);
$this->string($it->getSql())->isIdenticalTo('SELECT * FROM `foo` ORDER BY `a`, `b` ASC, `c` DESC');

$it = $this->it->execute('foo', ['ORDER' => ['`a`', 'b ASC', 'c DESC']]);
$this->string($it->getSql())->isIdenticalTo('SELECT * FROM `foo` ORDER BY `a`, `b` ASC, `c` DESC');

$it = $this->it->execute('foo', ['ORDERBY' => 'bar, baz ASC']);
$this->string($it->getSql())->isIdenticalTo('SELECT * FROM `foo` ORDER BY `bar`, `baz` ASC');

$it = $this->it->execute('foo', ['ORDER' => 'bar, baz ASC']);
$this->string($it->getSql())->isIdenticalTo('SELECT * FROM `foo` ORDER BY `bar`, `baz` ASC');

$it = $this->it->execute('foo', ['ORDERBY' => 'bar DESC, baz ASC']);
$this->string($it->getSql())->isIdenticalTo('SELECT * FROM `foo` ORDER BY `bar` DESC, `baz` ASC');

$it = $this->it->execute('foo', ['ORDER' => 'bar DESC, baz ASC']);
$this->string($it->getSql())->isIdenticalTo('SELECT * FROM `foo` ORDER BY `bar` DESC, `baz` ASC');
}
Expand Down Expand Up @@ -449,11 +470,20 @@ public function testGroupBy() {
$it = $this->it->execute(['foo'], ['GROUPBY' => ['id']]);
$this->string($it->getSql())->isIdenticalTo('SELECT * FROM `foo` GROUP BY `id`');

$it = $this->it->execute(['foo'], ['GROUP' => ['id']]);
$this->string($it->getSql())->isIdenticalTo('SELECT * FROM `foo` GROUP BY `id`');

$it = $this->it->execute(['foo'], ['GROUPBY' => 'id']);
$this->string($it->getSql())->isIdenticalTo('SELECT * FROM `foo` GROUP BY `id`');

$it = $this->it->execute(['foo'], ['GROUP' => 'id']);
$this->string($it->getSql())->isIdenticalTo('SELECT * FROM `foo` GROUP BY `id`');

$it = $this->it->execute(['foo'], ['GROUPBY' => ['id', 'name']]);
$this->string($it->getSql())->isIdenticalTo('SELECT * FROM `foo` GROUP BY `id`, `name`');

$it = $this->it->execute(['foo'], ['GROUP' => ['id', 'name']]);
$this->string($it->getSql())->isIdenticalTo('SELECT * FROM `foo` GROUP BY `id`, `name`');
}

public function testNoFieldGroupBy() {
Expand All @@ -466,6 +496,17 @@ function () {
->withType(E_USER_ERROR)
->withMessage('Missing group by field')
->exists();

$this->when(
function () {
$it = $this->it->execute(['foo'], ['GROUP' => []]);
$this->string('SELECT * FROM `foo`', $it->getSql(), 'No group by field');
}
)->error()
->withType(E_USER_ERROR)
->withMessage('Missing group by field')
->exists();

}

public function testRange() {
Expand Down

0 comments on commit cb17289

Please sign in to comment.