Skip to content

Commit

Permalink
Merge 73e6e4a into a063521
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw committed Nov 6, 2019
2 parents a063521 + 73e6e4a commit e31883b
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
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);
}
}
48 changes: 47 additions & 1 deletion tests/MySQLGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ public function testDescendingBackwardExclusive()
/**
* @test
*/
public function testBelongsToMany()
public function testBelongsToManyOrderByPivot()
{
$cursor = ['pivot_id' => 2];

Expand Down Expand Up @@ -476,4 +476,50 @@ public function testBelongsToMany()
)
', $builder->toSql());
}

/**
* @test
*/
public function testBelongsToManyOrderBySource()
{
$cursor = ['posts.id' => 2];

$tag = new Tag();
$tag->id = 1;
$tag->exists = true;

$builder = $tag->posts()->withPivot('id')
->lampager()
->forward()->limit(3)
->orderBy('posts.id')
->seekable()
->build($cursor);

$this->assertSqlEquals('
(
select * from `posts`
inner join `post_tag` on `posts`.`id` = `post_tag`.`post_id`
where `post_tag`.`tag_id` = ? AND (
`posts`.`id` < ?
)
order by `posts`.`id` desc
limit 1
)
union all
(
select
`posts`.*,
`post_tag`.`tag_id` as `pivot_tag_id`,
`post_tag`.`post_id` as `pivot_post_id`,
`post_tag`.`id` as `pivot_id`
from `posts`
inner join `post_tag` on `posts`.`id` = `post_tag`.`post_id`
where `post_tag`.`tag_id` = ? AND (
`posts`.`id` >= ?
)
order by `posts`.`id` asc
limit 4
)
', $builder->toSql());
}
}
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 e31883b

Please sign in to comment.