Skip to content

Commit

Permalink
insert row order fix
Browse files Browse the repository at this point in the history
  • Loading branch information
supun-io committed Mar 14, 2023
1 parent 9182338 commit cd46caf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Clickhouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public function pingThrow() : void
public function insert(string $table, array $columns, array ...$rows) : mixed
{

$columnNames = implode(', ', array_keys($columns));
$columnNames = array_keys($columns);
sort($columnNames);
$columnNames = implode(',', $columnNames);

$bindings = [];
$placeholders = [];
Expand All @@ -90,6 +92,8 @@ public function insert(string $table, array $columns, array ...$rows) : mixed
);
}

ksort($row);

foreach ($row as $columnName => $value) {
$key = 'r' . $i . '_' . $columnName;
$bindings[$key] = $value;
Expand Down
32 changes: 31 additions & 1 deletion tests/InsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,34 @@
]
);

})->throws(ClickhouseException::class, 'Expected 4 columns, got 3');
})->throws(ClickhouseException::class, 'Expected 4 columns, got 3');

# bug
it('inserts correctly when the row key order is different', function() {

addUsersTable();

$clickhouse = test()->clickhouse;

$clickhouse->insert('users',
[
'id' => 'UInt32',
'created_at' => 'DateTime',
'name' => 'String',
'age' => 'UInt8',
],
[
'age' => 10,
'created_at' => '2021-01-01 00:00:00',
'name' => 'John',
'id' => 1
]
);

$response = $clickhouse->select('SELECT * FROM users');
$row = $response->first();

expect($row['id'])->toBe(1)
->and($row['age'])->toBe(10);

});

0 comments on commit cd46caf

Please sign in to comment.