Skip to content

Commit

Permalink
Support qualified cursor (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw committed Nov 6, 2019
1 parent 51c3d6b commit 2d40b1b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ class Processor extends ArrayProcessor
{
use HasSnakeAliases;

/**
* Return comparable value from a row.
*
* @param mixed $row
* @param string $column
* @return int|string
*/
protected function field($row, $column)
{
return parent::field($row, static::dropTablePrefix($column));
}

/**
* Slice rows, like PHP function array_slice().
*
Expand Down Expand Up @@ -62,4 +74,17 @@ 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 static function dropTablePrefix($column)
{
$segments = explode('.', $column);

return end($segments);
}
}
38 changes: 38 additions & 0 deletions tests/MySQLGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,42 @@ public function testDescendingBackwardExclusive()
) `temporary_table`
', $this->toSql($builder));
}

/**
* @test
*/
public function testQualifiedColumnOrder()
{
$cursor = ['posts.updated_at' => '', 'posts.created_at' => '', 'posts.id' => ''];
$builder = lampager(ORM::forTable('posts')->where_equal('posts.user_id', 2))
->forward()->limit(3)
->order_by_asc('posts.updated_at')
->order_by_asc('posts.created_at')
->order_by_asc('posts.id')
->seekable()
->build($cursor);
$this->assertSqlEquals('
SELECT * FROM (
SELECT * FROM `posts`
WHERE `posts`.`user_id` = ? AND (
`posts`.`updated_at` = ? AND `posts`.`created_at` = ? AND `posts`.`id` < ? OR
`posts`.`updated_at` = ? AND `posts`.`created_at` < ? OR
`posts`.`updated_at` < ?
)
ORDER BY `posts`.`updated_at` DESC, `posts`.`created_at` DESC, `posts`.`id` DESC
LIMIT 1
) `temporary_table`
UNION ALL
SELECT * FROM (
SELECT * FROM `posts`
WHERE `posts`.`user_id` = ? AND (
`posts`.`updated_at` = ? AND `posts`.`created_at` = ? AND `posts`.`id` >= ? OR
`posts`.`updated_at` = ? AND `posts`.`created_at` > ? OR
`posts`.`updated_at` > ?
)
ORDER BY `posts`.`updated_at` ASC, `posts`.`created_at` ASC, `posts`.`id` ASC
LIMIT 4
) `temporary_table`
', $this->toSql($builder));
}
}
26 changes: 26 additions & 0 deletions tests/ProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,4 +435,30 @@ public function testDescendingBackwardExclusive()
->paginate(['id' => '3', 'updated_at' => '2017-01-01 10:00:00'])
);
}

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

0 comments on commit 2d40b1b

Please sign in to comment.