Skip to content

Commit

Permalink
fix unwrapped column aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
aarondfrancis committed May 15, 2024
1 parent 95fefe0 commit b4da72b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
14 changes: 10 additions & 4 deletions src/FastPaginate.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,16 @@ public static function getInnerSelectColumns($builder)
->pluck('column')
->filter()
->map(function ($column) use ($base) {
// Use the grammar to wrap them, so that our `str_contains`
// (further down) doesn't return any false positives.
return $base->grammar->wrap($column);
});
// Not everyone quotes their custom selects, which
// is totally reasonable. We'll look for both
// quoted and unquoted, as a kindness.
// See https://github.com/hammerstonedev/fast-paginate/pull/57
return [
$column,
$base->grammar->wrap($column)
];
})
->flatten(1);

return collect($base->columns)
->filter(function ($column) use ($orders, $base) {
Expand Down
19 changes: 3 additions & 16 deletions tests/Integration/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,27 +205,14 @@ public function selects_are_overwritten()
}

/** @test */
public function selects_are_preserved_if_used_in_order_by()
public function unquoted_selects_are_preserved_if_used_in_order_by()
{
$queries = $this->withQueriesLogged(function () use (&$results) {
$results = User::query()->selectRaw('(select 1 as computed_column)')->orderBy('computed_column')->fastPaginate();
$results = User::query()->selectRaw('(select 1) as computed_column')->orderBy('computed_column')->fastPaginate();
});

$this->assertEquals(
'select `users`.`id`, (select 1 as computed_column) from `users` order by `computed_column` asc limit 15 offset 0',
$queries[1]['query']
);
}

/** @test */
public function selects_are_preserved_if_used_in_where_constraint()
{
$queries = $this->withQueriesLogged(function () use (&$results) {
$results = User::query()->selectRaw('(select 1 as computed_column)')->where('computed_column', 1)->fastPaginate();
});

$this->assertEquals(
'select `users`.`id`, (select 1 as computed_column) from `users` where `computed_column` = 1 asc limit 15 offset 0',
'select `users`.`id`, (select 1) as computed_column from `users` order by `computed_column` asc limit 15 offset 0',
$queries[1]['query']
);
}
Expand Down

0 comments on commit b4da72b

Please sign in to comment.