Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query: Determine omitted & target columns correctly #139

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Query.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check failure on line 1 in src/Query.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Ignored error pattern #^Parameter \#1 \$object of method SplObjectStorage\<object,mixed\>\:\:attach\(\) expects object, mixed given\.$# in path /home/runner/work/ipl-orm/ipl-orm/src/Query.php was not matched in reported errors.

Check failure on line 1 in src/Query.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Ignored error pattern #^Parameter \#1 \$object of method SplObjectStorage\<object,mixed\>\:\:contains\(\) expects object, mixed given\.$# in path /home/runner/work/ipl-orm/ipl-orm/src/Query.php was not matched in reported errors.

Check failure on line 1 in src/Query.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Ignored error pattern #^Parameter \#1 \$objectOrClass of class ReflectionClass constructor expects class\-string\<T of object\>\|T of object, string given\.$# in path /home/runner/work/ipl-orm/ipl-orm/src/Query.php was not matched in reported errors.

Check failure on line 1 in src/Query.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Ignored error pattern #^Parameter \#1 \$object of method SplObjectStorage\<object,mixed\>\:\:attach\(\) expects object, mixed given\.$# in path /home/runner/work/ipl-orm/ipl-orm/src/Query.php was not matched in reported errors.

Check failure on line 1 in src/Query.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Ignored error pattern #^Parameter \#1 \$object of method SplObjectStorage\<object,mixed\>\:\:contains\(\) expects object, mixed given\.$# in path /home/runner/work/ipl-orm/ipl-orm/src/Query.php was not matched in reported errors.

Check failure on line 1 in src/Query.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Ignored error pattern #^Parameter \#1 \$objectOrClass of class ReflectionClass constructor expects class\-string\<T of object\>\|T of object, string given\.$# in path /home/runner/work/ipl-orm/ipl-orm/src/Query.php was not matched in reported errors.

Check failure on line 1 in src/Query.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Ignored error pattern #^Parameter \#1 \$object of method SplObjectStorage\<object,mixed\>\:\:attach\(\) expects object, mixed given\.$# in path /home/runner/work/ipl-orm/ipl-orm/src/Query.php was not matched in reported errors.

Check failure on line 1 in src/Query.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Ignored error pattern #^Parameter \#1 \$object of method SplObjectStorage\<object,mixed\>\:\:contains\(\) expects object, mixed given\.$# in path /home/runner/work/ipl-orm/ipl-orm/src/Query.php was not matched in reported errors.

Check failure on line 1 in src/Query.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Ignored error pattern #^Parameter \#1 \$objectOrClass of class ReflectionClass constructor expects class\-string\<T of object\>\|T of object, string given\.$# in path /home/runner/work/ipl-orm/ipl-orm/src/Query.php was not matched in reported errors.

namespace ipl\Orm;

Expand Down Expand Up @@ -135,7 +135,7 @@
throw new InvalidArgumentException('Argument $class must be a string');
}

if (! (new ReflectionClass($class))->newInstanceWithoutConstructor() instanceof ResultSet) {

Check failure on line 138 in src/Query.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #1 $argument of class ReflectionClass constructor expects class-string<T of object>|T of object, string given.

Check failure on line 138 in src/Query.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #1 $argument of class ReflectionClass constructor expects class-string<T of object>|T of object, string given.

Check failure on line 138 in src/Query.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Parameter #1 $argument of class ReflectionClass constructor expects class-string<T of object>|T of object, string given.
throw new InvalidArgumentException(
$class . ' must be an instance of ' . ResultSet::class
);
Expand Down Expand Up @@ -454,7 +454,18 @@
foreach ($resolved as $target) {
$targetColumns = $resolved[$target]->getArrayCopy();
if (isset($omitted[$target])) {
$targetColumns = array_diff($targetColumns, $omitted[$target]->getArrayCopy());
$toExclude = $omitted[$target]->getArrayCopy();
$targetColumns = array_filter($targetColumns, function ($column, $alias) use ($toExclude) {
if (is_string($alias) && isset($toExclude[$alias])) {
return false;
}

if (is_string($alias)) {
return ! in_array($alias, $toExclude, true);
}

return ! in_array($column, $toExclude, true);
}, ARRAY_FILTER_USE_BOTH);
}

if (! empty($customAliases)) {
Expand Down Expand Up @@ -733,9 +744,9 @@
$columnStorage = new SplObjectStorage();

foreach ($columns as list($target, $alias, $column)) {
if (! $columnStorage->contains($target)) {

Check failure on line 747 in src/Query.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #1 $obj of method SplObjectStorage<object,mixed>::contains() expects object, mixed given.

Check failure on line 747 in src/Query.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #1 $obj of method SplObjectStorage<object,mixed>::contains() expects object, mixed given.

Check failure on line 747 in src/Query.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Parameter #1 $obj of method SplObjectStorage<object,mixed>::contains() expects object, mixed given.
$resolved = new ArrayObject();
$columnStorage->attach($target, $resolved);

Check failure on line 749 in src/Query.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #1 $obj of method SplObjectStorage<object,mixed>::attach() expects object, mixed given.

Check failure on line 749 in src/Query.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #1 $obj of method SplObjectStorage<object,mixed>::attach() expects object, mixed given.

Check failure on line 749 in src/Query.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Parameter #1 $obj of method SplObjectStorage<object,mixed>::attach() expects object, mixed given.
} else {
$resolved = $columnStorage[$target];
}
Expand Down
49 changes: 49 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ipl\Orm\Exception\InvalidRelationException;
use ipl\Orm\Query;
use ipl\Orm\ResolvedExpression;
use ipl\Sql\Expression;
use ipl\Tests\Sql\TestCase;

Expand Down Expand Up @@ -401,6 +402,32 @@ public function testWithColumnsSupportsExpressions()
);
}

public function testWithColumnsSupportsAliasedExpressions()
{
$model = new class () extends User {
public function getColumns(): array
{
return ['username', 'aliased_expr' => new Expression('1')];
}
};

$query = (new Query())
->setModel($model)
->columns('username')
->withColumns('aliased_expr');

$this->assertEquals(
[
'user.username',
'aliased_expr' => new ResolvedExpression(
new Expression('1'),
$query->getResolver()->requireAndResolveColumns(['aliased_expr'])
),
],
$query->assembleSelect()->getColumns()
);
}

public function testWithColumnsHandlesCustomAliasesCorrectly()
{
$query = (new Query())
Expand Down Expand Up @@ -486,6 +513,28 @@ public function testWithoutColumnsOverridesColumnsAndWithColumns()
);
}

public function testWithoutColumnsExcludesAliasedExpressions()
{
$model = new class () extends User {
public function getColumns(): array
{
return [
'username',
'aliased_expr' => new Expression('1')
];
}
};

$query = (new Query())
->setModel($model)
->withoutColumns(['aliased_expr', 'id']);

$this->assertSame(
['user.username'],
$query->assembleSelect()->getColumns()
);
}

public function testWithoutColumnsDoesNotWorkWithExpressions()
{
$expression = new Expression('1');
Expand Down
Loading