Skip to content

Commit

Permalink
Support qualified cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw committed Nov 6, 2019
1 parent a063521 commit 9d302c7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function process(Query $query, $rows)
*/
protected function field($row, $column)
{
$column = $this->dropTablePrefix($column);
if ($this->builder instanceof BelongsToMany && strpos($column, 'pivot_') === 0) {
return $this->pivotField($row, substr($column, 6), $this->pivotAccessor());
}
Expand Down Expand Up @@ -140,4 +141,24 @@ protected function defaultFormat($rows, array $meta, Query $query)
{
return new PaginationResult($rows, $meta);
}

/**
* Drop table prefix on column name.
*
* @param string $column
* @return string
*/
protected function dropTablePrefix(string $column)
{
if (!$this->builder) {
return $column;
}

// e.g.
// x -> "x" in Standard SQL
// -> [x] in MS SQL
// -> `x` in MySQL
$q = $this->builder->getConnection()->getQueryGrammar()->wrap('x')[0];
return preg_replace("/^(?:(?:{$q}[^{$q}]*?{$q}|\w*?)\.)*?(?:{$q}([^{$q}]*){$q}|(\w*))$/", '$1$2', $column);
}
}
28 changes: 27 additions & 1 deletion tests/ProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ public function testDescendingBackwardExclusive()
/**
* @test
*/
public function testBelongsToMany()
public function testBelongsToManyOrderByPivot()
{
$this->assertResultSame(
[
Expand All @@ -460,6 +460,32 @@ public function testBelongsToMany()
);
}

/**
* @test
*/
public function testBelongsToManyOrderBySource()
{
$this->assertResultSame(
[
'records' => [
['id' => 2, 'updated_at' => '2017-01-01 11:00:00'],
['id' => 3, 'updated_at' => '2017-01-01 10:00:00'],
['id' => 4, 'updated_at' => '2017-01-01 11:00:00'],
],
'has_previous' => true,
'previous_cursor' => ['posts.id' => 1],
'has_next' => true,
'next_cursor' => ['posts.id' => 5],
],
Tag::find(1)->posts()->withPivot('id')
->lampager()
->forward()->limit(3)
->orderBy('posts.id')
->seekable()
->paginate(['posts.id' => 2])
);
}

/**
* @test
*/
Expand Down

0 comments on commit 9d302c7

Please sign in to comment.