Skip to content
This repository was archived by the owner on Sep 5, 2025. It is now read-only.
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
44 changes: 44 additions & 0 deletions src/ResultHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Keboola\QueryApi;

class ResultHelper
{
/**
* @param array{
* "columns": array<int, array{
* "name": string,
* "type": "text",
* }>,
* "data": array<array<int, string>>,
* } $responseData
* @return array{
* "columns": array<int, array{
* "name": string,
* "type": "text",
* }>,
* "data": array<array<string, string>>,
* }
*/
public static function mapColumnNamesIntoData(array $responseData): array
{
$data = $responseData['data'];
$columnNames = array_column($responseData['columns'], 'name');

$transformedData = [];
foreach ($data as $row) {
assert(is_array($row));
$transformedRow = [];
foreach ($row as $index => $value) {
if (isset($columnNames[$index])) {
$transformedRow[$columnNames[$index]] = $value;
}
}
$transformedData[] = $transformedRow;
}
$responseData['data'] = $transformedData;
return $responseData;
}
}
3 changes: 1 addition & 2 deletions tests/Functional/QueryServiceFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,9 @@ public function testQueryJobWithInvalidSQL(): void
assert(is_array($statements));
$this->assertCount(1, $statements);

// The statement remains in 'waiting' status because the job failed before execution
$statement = $statements[0];
assert(is_array($statement));
$this->assertEquals('completed', $statement['status']);
$this->assertEquals('failed', $statement['status']);
assert(is_string($statement['query']));
$this->assertEquals('SELECT * FROM non_existent_table_12345', $statement['query']);
}
Expand Down
47 changes: 47 additions & 0 deletions tests/Phpunit/ResultHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Keboola\QueryApi\Tests\Phpunit;

use Keboola\QueryApi\ResultHelper;
use PHPUnit\Framework\TestCase;

class ResultHelperTest extends TestCase
{
public function testMapColumnNamesIntoData(): void
{
$input = [
'columns' => [
['name' => 'id', 'type' => 'text'],
['name' => 'name', 'type' => 'text'],
['name' => 'city', 'type' => 'text'],
],
'data' => [
['1', 'Alice', 'Prague'],
['2', 'Bob', 'Liberec'],
['3', 'Charlie', 'Brno', 'EXTRA'],
],
];

$expected = [
'columns' => [
['name' => 'id', 'type' => 'text'],
['name' => 'name', 'type' => 'text'],
['name' => 'city', 'type' => 'text'],
],
'data' => [
['id' => '1', 'name' => 'Alice', 'city' => 'Prague'],
['id' => '2', 'name' => 'Bob', 'city' => 'Liberec'],
['id' => '3', 'name' => 'Charlie', 'city' => 'Brno'],
],
];

$actual = ResultHelper::mapColumnNamesIntoData($input);

// Columns should be preserved unchanged
$this->assertSame($expected['columns'], $actual['columns']);
// Data rows should be mapped by column names
$this->assertSame($expected['data'], $actual['data']);
}
}