Skip to content

Commit 76be59d

Browse files
authored
Fix ordering by multiple columns in DbalLimitOffsetExtractor (#1818)
1 parent 01ca07f commit 76be59d

2 files changed

Lines changed: 75 additions & 3 deletions

File tree

src/adapter/etl-adapter-doctrine/src/Flow/ETL/Adapter/Doctrine/DbalLimitOffsetExtractor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static function table(
4444
->from($table->name);
4545

4646
foreach ($orderBy as $order) {
47-
$queryBuilder = $queryBuilder->orderBy($order->column, $order->order->name);
47+
$queryBuilder = $queryBuilder->addOrderBy($order->column, $order->order->name);
4848
}
4949

5050
return new self(

src/adapter/etl-adapter-doctrine/tests/Flow/ETL/Adapter/Doctrine/Tests/Integration/DbalLimitOffsetExtractorTest.php

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,84 @@
55
namespace Flow\ETL\Adapter\Doctrine\Tests\Integration;
66

77
use function Flow\ETL\Adapter\Doctrine\from_dbal_limit_offset;
8-
use Flow\ETL\Adapter\Doctrine\Table;
8+
use function Flow\ETL\DSL\{data_frame, flow_context, from_array};
9+
use Doctrine\DBAL\Schema\Column;
10+
use Doctrine\DBAL\Types\{TextType, Type, Types};
11+
use Flow\ETL\Adapter\Doctrine\{DbalLoader, DbalTypesDetector, Order, OrderBy, Table, TypesMap};
912
use Flow\ETL\Adapter\Doctrine\Tests\IntegrationTestCase;
13+
use Flow\ETL\{Config, Rows};
14+
use Flow\Types\Type\Native\{IntegerType, StringType};
1015

1116
final class DbalLimitOffsetExtractorTest extends IntegrationTestCase
1217
{
13-
public function test_creating_limit_offset_extractor_for_table_without_oder_by() : void
18+
public function test_creating_limit_offset_extractor_for_table() : void
19+
{
20+
$this->pgsqlDatabaseContext->createTable((new \Doctrine\DBAL\Schema\Table(
21+
$table = 'flow_doctrine_order_by_test',
22+
[
23+
new Column('id', Type::getType(Types::INTEGER), ['notnull' => true]),
24+
new Column('code', Type::getType(Types::INTEGER), ['notnull' => true]),
25+
],
26+
))
27+
->setPrimaryKey(['id']));
28+
29+
$customTypesMap = new TypesMap([
30+
StringType::class => TextType::class,
31+
IntegerType::class => \Doctrine\DBAL\Types\IntegerType::class,
32+
]);
33+
34+
$customConverter = new DbalTypesDetector($customTypesMap);
35+
36+
$loader = (new DbalLoader($table, $this->postgresqlConnectionParams()))
37+
->withTypesDetector($customConverter);
38+
39+
(data_frame())
40+
->read(from_array([
41+
['id' => 1, 'code' => 100],
42+
['id' => 2, 'code' => 100],
43+
['id' => 3, 'code' => 200],
44+
]))
45+
->load($loader)
46+
->run();
47+
48+
$extractor = from_dbal_limit_offset(
49+
$this->pgsqlDatabaseContext->connection(),
50+
new Table('flow_doctrine_order_by_test', ['id', 'code']),
51+
[
52+
new OrderBy('code', Order::DESC),
53+
new OrderBy('id', Order::ASC),
54+
]
55+
);
56+
57+
self::assertSame(
58+
[
59+
[
60+
[
61+
'id' => 3,
62+
'code' => 200,
63+
],
64+
],
65+
[
66+
[
67+
'id' => 1,
68+
'code' => 100,
69+
],
70+
],
71+
[
72+
[
73+
'id' => 2,
74+
'code' => 100,
75+
],
76+
],
77+
],
78+
\array_map(
79+
static fn (Rows $r) => $r->toArray(),
80+
\iterator_to_array($extractor->extract(flow_context(Config::builder()->putInputIntoRows()->build())))
81+
)
82+
);
83+
}
84+
85+
public function test_creating_limit_offset_extractor_for_table_without_order_by() : void
1486
{
1587
$this->expectExceptionMessage('There must be at least one column to order by, zero given');
1688

0 commit comments

Comments
 (0)