Skip to content

Commit dc9f7f6

Browse files
authored
Merge pull request #124 from alpha0010/master
Add support for 'GROUP N BY'
2 parents d6fc276 + 5a9a118 commit dc9f7f6

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/SphinxQL.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ class SphinxQL
8282
*/
8383
protected $group_by = array();
8484

85+
/**
86+
* When not null changes 'GROUP BY' to 'GROUP N BY'
87+
*
88+
* @var null|int
89+
*/
90+
protected $group_n_by = null;
91+
8592
/**
8693
* ORDER BY array
8794
*
@@ -561,7 +568,11 @@ public function compileSelect()
561568
$query .= $this->compileMatch().$this->compileWhere();
562569

563570
if (!empty($this->group_by)) {
564-
$query .= 'GROUP BY '.implode(', ', $this->group_by).' ';
571+
$query .= 'GROUP ';
572+
if ($this->group_n_by !== null) {
573+
$query .= $this->group_n_by.' ';
574+
}
575+
$query .= 'BY '.implode(', ', $this->group_by).' ';
565576
}
566577

567578
if (!empty($this->within_group_order_by)) {
@@ -999,6 +1010,21 @@ public function groupBy($column)
9991010
return $this;
10001011
}
10011012

1013+
/**
1014+
* GROUP N BY clause (SphinxQL-specific)
1015+
* Changes 'GROUP BY' into 'GROUP N BY'
1016+
*
1017+
* @param int $n Number of items per group
1018+
*
1019+
* @return SphinxQL
1020+
*/
1021+
public function groupNBy($n)
1022+
{
1023+
$this->group_n_by = (int) $n;
1024+
1025+
return $this;
1026+
}
1027+
10021028
/**
10031029
* WITHIN GROUP ORDER BY clause (SphinxQL-specific)
10041030
* Adds to the previously added columns
@@ -1351,6 +1377,7 @@ public function reset()
13511377
$this->where = array();
13521378
$this->match = array();
13531379
$this->group_by = array();
1380+
$this->group_n_by = null;
13541381
$this->within_group_order_by = array();
13551382
$this->having = array();
13561383
$this->order_by = array();
@@ -1382,6 +1409,7 @@ public function resetMatch()
13821409
public function resetGroupBy()
13831410
{
13841411
$this->group_by = array();
1412+
$this->group_n_by = null;
13851413

13861414
return $this;
13871415
}

tests/SphinxQL/SphinxQLTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,42 @@ public function testWithinGroupOrderBy()
711711
$this->assertEquals('16', $result[0]['id']);
712712
}
713713

714+
public function testGroupNBy()
715+
{
716+
$query = SphinxQL::create(self::$conn)->select()
717+
->from('rt')
718+
->groupBy('gid');
719+
$this->assertEquals(
720+
'SELECT * FROM rt GROUP BY gid',
721+
$query->compile()->getCompiled()
722+
);
723+
724+
$query->groupNBy(3);
725+
$this->assertEquals(
726+
'SELECT * FROM rt GROUP 3 BY gid',
727+
$query->compile()->getCompiled()
728+
);
729+
730+
$query->resetGroupBy();
731+
$this->assertEquals(
732+
'SELECT * FROM rt',
733+
$query->compile()->getCompiled()
734+
);
735+
736+
$query->groupBy('gid');
737+
$this->assertEquals(
738+
'SELECT * FROM rt GROUP BY gid',
739+
$query->compile()->getCompiled()
740+
);
741+
742+
$query->resetGroupBy()
743+
->groupNBy(3);
744+
$this->assertEquals(
745+
'SELECT * FROM rt',
746+
$query->compile()->getCompiled()
747+
);
748+
}
749+
714750
public function testOffset()
715751
{
716752
$this->refill();

0 commit comments

Comments
 (0)