db2 driver updates

This commit is contained in:
Mark Gregory
2026-11-11 10:48:53 +00:00
parent 2e51121c5b
commit d9e83bcbcb
24 changed files with 3750 additions and 15 deletions
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace Tests\Feature\Db2;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
abstract class Db2TestCase extends TestCase
{
protected function db()
{
return DB::connection('db2_local');
}
protected function setUp(): void
{
parent::setUp();
if (! extension_loaded('pdo_odbc')) {
$this->markTestSkipped('PDO_ODBC extension unavailable.');
}
if (! config('database.connections.db2_local')) {
$this->markTestSkipped('db2_local is not configured.');
}
try {
$this->db()->select(
'SELECT 1 AS "health" FROM SYSIBM.SYSDUMMY1'
);
} catch (\Throwable $e) {
$this->markTestSkipped(
'Db2 connection unavailable: ' . $e->getMessage()
);
}
}
}
+125
View File
@@ -0,0 +1,125 @@
<?php
namespace Tests\Feature\Db2;
use Illuminate\Support\Facades\DB;
class NullHandlingTest extends Db2TestCase
{
private string $fixtureTable = '';
protected function setUp(): void
{
parent::setUp();
// Each test owns a unique permanent table.
// Db2 catalog metadata can discover its columns.
$this->fixtureTable = 'DB2_TEST.DB2T_' .
strtoupper(bin2hex(random_bytes(6)));
$db = $this->db();
$db->statement("
CREATE TABLE {$this->fixtureTable} (
ID INTEGER NOT NULL PRIMARY KEY,
NULLABLE_TEXT VARCHAR(100),
NULLABLE_DATE DATE,
NULLABLE_TIME TIME,
NULLABLE_TIMESTAMP TIMESTAMP
)
");
$db->statement("
INSERT INTO {$this->fixtureTable}
(ID, NULLABLE_TEXT, NULLABLE_DATE,
NULLABLE_TIME, NULLABLE_TIMESTAMP)
VALUES (1, NULL, NULL, NULL, NULL)
");
$db->statement("
INSERT INTO {$this->fixtureTable}
(ID, NULLABLE_TEXT, NULLABLE_DATE,
NULLABLE_TIME, NULLABLE_TIMESTAMP)
VALUES (
2,
'',
DATE('2026-11-11'),
TIME('10:15:30'),
TIMESTAMP('2026-11-11 10:15:30')
)
");
}
protected function tearDown(): void
{
try {
if ($this->fixtureTable !== '') {
$this->db()->statement(
"DROP TABLE {$this->fixtureTable}"
);
}
} finally {
parent::tearDown();
}
}
public function test_null_values_are_restored(): void
{
$row = $this->db()->table($this->fixtureTable)
->where('ID', 1)
->first();
$this->assertNull($row->NULLABLE_TEXT);
$this->assertNull($row->NULLABLE_DATE);
$this->assertNull($row->NULLABLE_TIME);
$this->assertNull($row->NULLABLE_TIMESTAMP);
}
public function test_empty_string_is_not_converted_to_null(): void
{
$row = $this->db()->table($this->fixtureTable)
->where('ID', 2)
->first();
$this->assertSame('', $row->NULLABLE_TEXT);
}
public function test_non_null_temporal_values_survive(): void
{
$row = $this->db()->table($this->fixtureTable)
->where('ID', 2)
->first();
$this->assertNotNull($row->NULLABLE_DATE);
$this->assertNotNull($row->NULLABLE_TIME);
$this->assertNotNull($row->NULLABLE_TIMESTAMP);
}
public function test_generated_indicators_are_hidden(): void
{
$row = $this->db()->table($this->fixtureTable)
->where('ID', 1)
->first();
foreach (array_keys((array) $row) as $column) {
$this->assertDoesNotMatchRegularExpression(
'/^__db2_null_flag_\d+$/i',
$column
);
}
}
public function test_nullable_aliases_are_preserved(): void
{
$row = $this->db()->table($this->fixtureTable)
->select('NULLABLE_TEXT as aliased_text')
->where('ID', 1)
->first();
$this->assertTrue(
property_exists($row, 'aliased_text')
);
$this->assertNull($row->aliased_text);
}
}
@@ -0,0 +1,121 @@
<?php
namespace Tests\Feature\Db2;
use Illuminate\Support\Facades\DB;
class NullHandlingTest extends Db2TestCase
{
private const TABLE = 'DB2_LARAVEL_TEST_NULLS';
protected function setUp(): void
{
parent::setUp();
$db = $this->db();
$db->statement('
DECLARE GLOBAL TEMPORARY TABLE
SESSION.DB2_LARAVEL_TEST_NULLS (
ID INTEGER NOT NULL,
NULLABLE_TEXT VARCHAR(4096),
NULLABLE_DATE DATE,
NULLABLE_TIME TIME,
NULLABLE_TIMESTAMP TIMESTAMP
)
ON COMMIT PRESERVE ROWS
NOT LOGGED
');
$db->statement('
INSERT INTO SESSION.DB2_LARAVEL_TEST_NULLS
(ID, NULLABLE_TEXT, NULLABLE_DATE,
NULLABLE_TIME, NULLABLE_TIMESTAMP)
VALUES (1, NULL, NULL, NULL, NULL)
');
$db->statement("
INSERT INTO SESSION.DB2_LARAVEL_TEST_NULLS
(ID, NULLABLE_TEXT, NULLABLE_DATE,
NULLABLE_TIME, NULLABLE_TIMESTAMP)
VALUES (
2,
'',
DATE('2026-11-11'),
TIME('10:15:30'),
TIMESTAMP('2026-11-11 10:15:30')
)
");
}
protected function tearDown(): void
{
try {
$this->db()->statement(
'DROP TABLE SESSION.DB2_LARAVEL_TEST_NULLS'
);
} finally {
parent::tearDown();
}
}
public function test_null_values_are_restored(): void
{
$row = $this->db()->table('SESSION.' . self::TABLE)
->where('ID', 1)
->first();
$this->assertNull($row->NULLABLE_TEXT);
$this->assertNull($row->NULLABLE_DATE);
$this->assertNull($row->NULLABLE_TIME);
$this->assertNull($row->NULLABLE_TIMESTAMP);
}
public function test_empty_string_is_not_converted_to_null(): void
{
$row = $this->db()->table('SESSION.' . self::TABLE)
->where('ID', 2)
->first();
$this->assertSame('', $row->NULLABLE_TEXT);
}
public function test_non_null_temporal_values_survive(): void
{
$row = $this->db()->table('SESSION.' . self::TABLE)
->where('ID', 2)
->first();
$this->assertNotNull($row->NULLABLE_DATE);
$this->assertNotNull($row->NULLABLE_TIME);
$this->assertNotNull($row->NULLABLE_TIMESTAMP);
}
public function test_generated_indicators_are_hidden(): void
{
$row = $this->db()->table('SESSION.' . self::TABLE)
->where('ID', 1)
->first();
foreach (array_keys((array) $row) as $column) {
$this->assertDoesNotMatchRegularExpression(
'/^__db2_null_flag_\d+$/i',
$column
);
}
}
public function test_nullable_aliases_are_preserved(): void
{
$row = $this->db()->table('SESSION.' . self::TABLE)
->select('NULLABLE_TEXT as aliased_text')
->where('ID', 1)
->first();
$this->assertTrue(
property_exists($row, 'aliased_text')
);
$this->assertNull($row->aliased_text);
}
}
@@ -0,0 +1,121 @@
<?php
namespace Tests\Feature\Db2;
use Illuminate\Support\Facades\DB;
class NullHandlingTest extends Db2TestCase
{
private const TABLE = 'DB2_LARAVEL_TEST_NULLS';
protected function setUp(): void
{
parent::setUp();
$db = $this->db();
$db->statement('
DECLARE GLOBAL TEMPORARY TABLE
SESSION.DB2_LARAVEL_TEST_NULLS (
ID INTEGER NOT NULL,
NULLABLE_TEXT VARCHAR(100),
NULLABLE_DATE DATE,
NULLABLE_TIME TIME,
NULLABLE_TIMESTAMP TIMESTAMP
)
ON COMMIT PRESERVE ROWS
NOT LOGGED
');
$db->statement('
INSERT INTO SESSION.DB2_LARAVEL_TEST_NULLS
(ID, NULLABLE_TEXT, NULLABLE_DATE,
NULLABLE_TIME, NULLABLE_TIMESTAMP)
VALUES (1, NULL, NULL, NULL, NULL)
');
$db->statement("
INSERT INTO SESSION.DB2_LARAVEL_TEST_NULLS
(ID, NULLABLE_TEXT, NULLABLE_DATE,
NULLABLE_TIME, NULLABLE_TIMESTAMP)
VALUES (
2,
'',
DATE('2026-11-11'),
TIME('10:15:30'),
TIMESTAMP('2026-11-11 10:15:30')
)
");
}
protected function tearDown(): void
{
try {
$this->db()->statement(
'DROP TABLE SESSION.DB2_LARAVEL_TEST_NULLS'
);
} finally {
parent::tearDown();
}
}
public function test_null_values_are_restored(): void
{
$row = $this->db()->table('SESSION.' . self::TABLE)
->where('ID', 1)
->first();
$this->assertNull($row->NULLABLE_TEXT);
$this->assertNull($row->NULLABLE_DATE);
$this->assertNull($row->NULLABLE_TIME);
$this->assertNull($row->NULLABLE_TIMESTAMP);
}
public function test_empty_string_is_not_converted_to_null(): void
{
$row = $this->db()->table('SESSION.' . self::TABLE)
->where('ID', 2)
->first();
$this->assertSame('', $row->NULLABLE_TEXT);
}
public function test_non_null_temporal_values_survive(): void
{
$row = $this->db()->table('SESSION.' . self::TABLE)
->where('ID', 2)
->first();
$this->assertNotNull($row->NULLABLE_DATE);
$this->assertNotNull($row->NULLABLE_TIME);
$this->assertNotNull($row->NULLABLE_TIMESTAMP);
}
public function test_generated_indicators_are_hidden(): void
{
$row = $this->db()->table('SESSION.' . self::TABLE)
->where('ID', 1)
->first();
foreach (array_keys((array) $row) as $column) {
$this->assertDoesNotMatchRegularExpression(
'/^__db2_null_flag_\d+$/i',
$column
);
}
}
public function test_nullable_aliases_are_preserved(): void
{
$row = $this->db()->table('SESSION.' . self::TABLE)
->select('NULLABLE_TEXT as aliased_text')
->where('ID', 1)
->first();
$this->assertTrue(
property_exists($row, 'aliased_text')
);
$this->assertNull($row->aliased_text);
}
}
@@ -0,0 +1,138 @@
<?php
namespace Tests\Feature\Db2;
class PaginationIdentityTest extends Db2TestCase
{
private string $tableName = '';
protected function setUp(): void
{
parent::setUp();
$this->tableName = 'DB2_TEST.DB2T_' .
strtoupper(bin2hex(random_bytes(6)));
$this->db()->statement("
CREATE TABLE {$this->tableName} (
ID INTEGER GENERATED ALWAYS AS IDENTITY
(START WITH 1 INCREMENT BY 1),
NAME VARCHAR(100) NOT NULL,
PRIMARY KEY (ID)
)
");
foreach (range(1, 20) as $number) {
$this->db()->table($this->tableName)->insert([
'NAME' => sprintf('Item %02d', $number),
]);
}
}
protected function tearDown(): void
{
try {
if ($this->tableName !== '') {
$this->db()->statement(
"DROP TABLE {$this->tableName}"
);
}
} finally {
parent::tearDown();
}
}
public function test_limit_returns_requested_rows(): void
{
$rows = $this->db()->table($this->tableName)
->orderBy('ID')
->limit(5)
->get();
$this->assertCount(5, $rows);
$this->assertSame('Item 01', $rows[0]->NAME);
$this->assertSame('Item 05', $rows[4]->NAME);
}
public function test_offset_skips_correct_rows(): void
{
$rows = $this->db()->table($this->tableName)
->orderBy('ID')
->offset(10)
->limit(5)
->get();
$this->assertCount(5, $rows);
$this->assertSame('Item 11', $rows[0]->NAME);
$this->assertSame('Item 15', $rows[4]->NAME);
}
public function test_offset_without_limit(): void
{
$rows = $this->db()->table($this->tableName)
->orderBy('ID')
->offset(15)
->get();
$this->assertCount(5, $rows);
$this->assertSame('Item 16', $rows[0]->NAME);
}
public function test_standard_pagination(): void
{
$page = $this->db()->table($this->tableName)
->orderBy('ID')
->paginate(5, ['*'], 'page', 3);
$this->assertSame(20, $page->total());
$this->assertSame(4, $page->lastPage());
$this->assertSame(3, $page->currentPage());
$this->assertCount(5, $page->items());
$this->assertSame('Item 11', $page->items()[0]->NAME);
}
public function test_simple_pagination(): void
{
$page = $this->db()->table($this->tableName)
->orderBy('ID')
->simplePaginate(5, ['*'], 'page', 2);
$this->assertSame(2, $page->currentPage());
$this->assertCount(5, $page->items());
$this->assertSame('Item 06', $page->items()[0]->NAME);
$this->assertTrue($page->hasMorePages());
}
public function test_insert_get_id_returns_generated_identity(): void
{
$id = $this->db()->table($this->tableName)
->insertGetId(['NAME' => 'New Item']);
$this->assertEquals(21, $id);
$row = $this->db()->table($this->tableName)
->where('ID', $id)
->first();
$this->assertNotNull($row);
$this->assertSame('New Item', $row->NAME);
}
public function test_pagination_does_not_duplicate_rows(): void
{
$ids = [];
for ($page = 1; $page <= 4; $page++) {
$result = $this->db()->table($this->tableName)
->orderBy('ID')
->paginate(5, ['*'], 'page', $page);
foreach ($result->items() as $row) {
$ids[] = (int) $row->ID;
}
}
$this->assertCount(20, $ids);
$this->assertSame(range(1, 20), $ids);
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace Tests\Feature\Db2;
class RawQueryTest extends Db2TestCase
{
public function test_raw_query_columns_are_not_removed(): void
{
$row = $this->db()->selectOne("
SELECT
CAST('ordinary value' AS VARCHAR(30))
AS \"data\",
CAST('1' AS VARCHAR(1))
AS \"__db2_null_flag_0\"
FROM SYSIBM.SYSDUMMY1
");
$this->assertSame('ordinary value', $row->data);
$this->assertTrue(
property_exists($row, '__db2_null_flag_0')
);
$this->assertSame(
'1',
$row->__db2_null_flag_0
);
}
}
+217
View File
@@ -0,0 +1,217 @@
<?php
namespace Tests\Feature\Db2;
use Illuminate\Database\QueryException;
use RuntimeException;
class TransactionTest extends Db2TestCase
{
private string $tableName = '';
protected function setUp(): void
{
parent::setUp();
$this->tableName = 'DB2_TEST.DB2T_' .
strtoupper(bin2hex(random_bytes(6)));
$this->db()->statement("
CREATE TABLE {$this->tableName} (
ID INTEGER NOT NULL PRIMARY KEY,
NAME VARCHAR(100) NOT NULL
)
");
}
protected function tearDown(): void
{
try {
if ($this->tableName !== '') {
$db = $this->db();
while ($db->transactionLevel() > 0) {
$db->rollBack();
}
$db->statement("DROP TABLE {$this->tableName}");
}
} finally {
parent::tearDown();
}
}
private function insertRow(int $id, string $name): void
{
$this->db()->table($this->tableName)->insert([
'ID' => $id,
'NAME' => $name,
]);
}
private function rowCount(): int
{
return $this->db()->table($this->tableName)->count();
}
public function test_transaction_commit(): void
{
$this->db()->beginTransaction();
$this->insertRow(1, 'Committed');
$this->assertSame(1, $this->db()->transactionLevel());
$this->db()->commit();
$this->assertSame(0, $this->db()->transactionLevel());
$this->assertSame(1, $this->rowCount());
}
public function test_transaction_rollback(): void
{
$this->db()->beginTransaction();
$this->insertRow(1, 'Rolled back');
$this->db()->rollBack();
$this->assertSame(0, $this->db()->transactionLevel());
$this->assertSame(0, $this->rowCount());
}
public function test_transaction_closure_commits(): void
{
$result = $this->db()->transaction(function () {
$this->insertRow(1, 'Closure');
return 'success';
});
$this->assertSame('success', $result);
$this->assertSame(1, $this->rowCount());
$this->assertSame(0, $this->db()->transactionLevel());
}
public function test_transaction_closure_rolls_back_on_exception(): void
{
try {
$this->db()->transaction(function () {
$this->insertRow(1, 'Rollback');
throw new RuntimeException('Expected failure');
});
$this->fail('Expected exception was not thrown.');
} catch (RuntimeException $e) {
$this->assertSame('Expected failure', $e->getMessage());
}
$this->assertSame(0, $this->rowCount());
$this->assertSame(0, $this->db()->transactionLevel());
}
public function test_nested_transaction_commit(): void
{
$db = $this->db();
$db->beginTransaction();
$this->insertRow(1, 'Outer');
$db->beginTransaction();
$this->insertRow(2, 'Inner');
$this->assertSame(2, $db->transactionLevel());
$db->commit();
$this->assertSame(1, $db->transactionLevel());
$db->commit();
$this->assertSame(0, $db->transactionLevel());
$this->assertSame(2, $this->rowCount());
}
public function test_nested_rollback_preserves_outer_transaction(): void
{
$db = $this->db();
$db->beginTransaction();
$this->insertRow(1, 'Outer');
$db->beginTransaction();
$this->insertRow(2, 'Inner');
$db->rollBack();
$this->assertSame(1, $db->transactionLevel());
$this->assertSame(1, $this->rowCount());
$db->commit();
$this->assertSame(0, $db->transactionLevel());
$this->assertSame(1, $this->rowCount());
}
public function test_nested_exception_preserves_outer_changes(): void
{
$db = $this->db();
$db->beginTransaction();
$this->insertRow(1, 'Outer');
try {
$db->transaction(function () {
$this->insertRow(2, 'Inner');
throw new RuntimeException('Inner failure');
});
} catch (RuntimeException $e) {
$this->assertSame('Inner failure', $e->getMessage());
}
$this->assertSame(1, $db->transactionLevel());
$db->commit();
$this->assertSame(1, $this->rowCount());
}
public function test_sql_error_rolls_back_transaction(): void
{
$db = $this->db();
try {
$db->transaction(function () {
$this->insertRow(1, 'Valid');
$this->insertRow(1, 'Duplicate');
});
$this->fail('Expected duplicate-key exception.');
} catch (QueryException $e) {
$this->assertSame('23505', $e->getCode());
}
$this->assertSame(0, $db->transactionLevel());
$this->assertSame(0, $this->rowCount());
}
public function test_transaction_level_is_restored(): void
{
$db = $this->db();
$this->assertSame(0, $db->transactionLevel());
$db->beginTransaction();
$this->assertSame(1, $db->transactionLevel());
$db->beginTransaction();
$this->assertSame(2, $db->transactionLevel());
$db->rollBack();
$this->assertSame(1, $db->transactionLevel());
$db->rollBack();
$this->assertSame(0, $db->transactionLevel());
}
}
+258
View File
@@ -0,0 +1,258 @@
<?php
namespace Tests\Feature\Db2;
class UpsertTest extends Db2TestCase
{
private string $tableName = '';
protected function setUp(): void
{
parent::setUp();
$this->tableName = 'DB2_TEST.DB2T_' .
strtoupper(bin2hex(random_bytes(6)));
$this->db()->statement("
CREATE TABLE {$this->tableName} (
ID INTEGER NOT NULL,
CODE VARCHAR(30) NOT NULL,
NAME VARCHAR(100) NOT NULL,
STATUS VARCHAR(20) NOT NULL,
PRIMARY KEY (ID),
CONSTRAINT UQ_CODE UNIQUE (CODE)
)
");
$this->db()->table($this->tableName)->insert([
'ID' => 1,
'CODE' => 'A001',
'NAME' => 'Original',
'STATUS' => 'active',
]);
}
protected function tearDown(): void
{
try {
if ($this->tableName !== '') {
$this->db()->statement(
"DROP TABLE {$this->tableName}"
);
}
} finally {
parent::tearDown();
}
}
public function test_upsert_inserts_new_row(): void
{
$this->db()->table($this->tableName)->upsert(
[[
'ID' => 2,
'CODE' => 'A002',
'NAME' => 'Second',
'STATUS' => 'active',
]],
['ID'],
['NAME', 'STATUS']
);
$this->assertSame(
'Second',
$this->db()->table($this->tableName)
->where('ID', 2)->first()->NAME
);
}
public function test_upsert_updates_existing_row(): void
{
$this->db()->table($this->tableName)->upsert(
[[
'ID' => 1,
'CODE' => 'A001',
'NAME' => 'Updated',
'STATUS' => 'inactive',
]],
['ID'],
['NAME', 'STATUS']
);
$row = $this->db()->table($this->tableName)
->where('ID', 1)->first();
$this->assertSame('Updated', $row->NAME);
$this->assertSame('inactive', $row->STATUS);
}
public function test_upsert_multiple_rows(): void
{
$this->db()->table($this->tableName)->upsert(
[
[
'ID' => 1,
'CODE' => 'A001',
'NAME' => 'Changed',
'STATUS' => 'active',
],
[
'ID' => 2,
'CODE' => 'A002',
'NAME' => 'New',
'STATUS' => 'active',
],
],
['ID'],
['NAME']
);
$this->assertSame(
2,
$this->db()->table($this->tableName)->count()
);
$this->assertSame(
'Changed',
$this->db()->table($this->tableName)
->where('ID', 1)->first()->NAME
);
}
public function test_upsert_preserves_unselected_columns(): void
{
$this->db()->table($this->tableName)->upsert(
[[
'ID' => 1,
'CODE' => 'A001',
'NAME' => 'Changed',
'STATUS' => 'inactive',
]],
['ID'],
['NAME']
);
$row = $this->db()->table($this->tableName)
->where('ID', 1)->first();
$this->assertSame('Changed', $row->NAME);
$this->assertSame('active', $row->STATUS);
}
public function test_insert_or_ignore_inserts_new_row(): void
{
$this->db()->table($this->tableName)->insertOrIgnore([
'ID' => 2,
'CODE' => 'A002',
'NAME' => 'Second',
'STATUS' => 'active',
]);
$this->assertSame(
2,
$this->db()->table($this->tableName)->count()
);
}
public function test_insert_or_ignore_skips_duplicate(): void
{
$this->db()->table($this->tableName)->insertOrIgnore([
'ID' => 1,
'CODE' => 'A001',
'NAME' => 'Duplicate',
'STATUS' => 'inactive',
]);
$this->assertSame(
'Original',
$this->db()->table($this->tableName)
->where('ID', 1)->first()->NAME
);
}
public function test_insert_or_ignore_multiple_rows(): void
{
$this->db()->table($this->tableName)->insertOrIgnore([
[
'ID' => 1,
'CODE' => 'A001',
'NAME' => 'Duplicate',
'STATUS' => 'inactive',
],
[
'ID' => 2,
'CODE' => 'A002',
'NAME' => 'New',
'STATUS' => 'active',
],
]);
$this->assertSame(
2,
$this->db()->table($this->tableName)->count()
);
}
public function test_insert_or_ignore_duplicate_unique_code(): void
{
$this->db()->table($this->tableName)->insertOrIgnore([
'ID' => 99,
'CODE' => 'A001',
'NAME' => 'Different',
'STATUS' => 'inactive',
]);
$this->assertSame(
1,
$this->db()->table($this->tableName)->count()
);
$this->assertSame(
'Original',
$this->db()->table($this->tableName)
->where('ID', 1)->first()->NAME
);
}
public function test_insert_or_ignore_does_not_hide_check_violations(): void
{
$this->expectException(
\Illuminate\Database\QueryException::class
);
$this->db()->table($this->tableName)->insertOrIgnore([
'ID' => 99,
'CODE' => 'A099',
'NAME' => 'Invalid',
'STATUS' => null,
]);
}
public function test_update_or_insert(): void
{
$this->db()->table($this->tableName)->updateOrInsert(
['ID' => 1],
['NAME' => 'Updated']
);
$this->db()->table($this->tableName)->updateOrInsert(
['ID' => 2],
[
'CODE' => 'A002',
'NAME' => 'Inserted',
'STATUS' => 'active',
]
);
$this->assertSame(
'Updated',
$this->db()->table($this->tableName)
->where('ID', 1)->first()->NAME
);
$this->assertSame(
'Inserted',
$this->db()->table($this->tableName)
->where('ID', 2)->first()->NAME
);
}
}
@@ -0,0 +1,223 @@
<?php
namespace Tests\Feature\Db2;
class UpsertTest extends Db2TestCase
{
private string $tableName = '';
protected function setUp(): void
{
parent::setUp();
$this->tableName = 'DB2_TEST.DB2T_' .
strtoupper(bin2hex(random_bytes(6)));
$this->db()->statement("
CREATE TABLE {$this->tableName} (
ID INTEGER NOT NULL,
CODE VARCHAR(30) NOT NULL,
NAME VARCHAR(100) NOT NULL,
STATUS VARCHAR(20) NOT NULL,
PRIMARY KEY (ID),
CONSTRAINT UQ_CODE UNIQUE (CODE)
)
");
$this->db()->table($this->tableName)->insert([
'ID' => 1,
'CODE' => 'A001',
'NAME' => 'Original',
'STATUS' => 'active',
]);
}
protected function tearDown(): void
{
try {
if ($this->tableName !== '') {
$this->db()->statement(
"DROP TABLE {$this->tableName}"
);
}
} finally {
parent::tearDown();
}
}
public function test_upsert_inserts_new_row(): void
{
$this->db()->table($this->tableName)->upsert(
[[
'ID' => 2,
'CODE' => 'A002',
'NAME' => 'Second',
'STATUS' => 'active',
]],
['ID'],
['NAME', 'STATUS']
);
$this->assertSame(
'Second',
$this->db()->table($this->tableName)
->where('ID', 2)->first()->NAME
);
}
public function test_upsert_updates_existing_row(): void
{
$this->db()->table($this->tableName)->upsert(
[[
'ID' => 1,
'CODE' => 'A001',
'NAME' => 'Updated',
'STATUS' => 'inactive',
]],
['ID'],
['NAME', 'STATUS']
);
$row = $this->db()->table($this->tableName)
->where('ID', 1)->first();
$this->assertSame('Updated', $row->NAME);
$this->assertSame('inactive', $row->STATUS);
}
public function test_upsert_multiple_rows(): void
{
$this->db()->table($this->tableName)->upsert(
[
[
'ID' => 1,
'CODE' => 'A001',
'NAME' => 'Changed',
'STATUS' => 'active',
],
[
'ID' => 2,
'CODE' => 'A002',
'NAME' => 'New',
'STATUS' => 'active',
],
],
['ID'],
['NAME']
);
$this->assertSame(
2,
$this->db()->table($this->tableName)->count()
);
$this->assertSame(
'Changed',
$this->db()->table($this->tableName)
->where('ID', 1)->first()->NAME
);
}
public function test_upsert_preserves_unselected_columns(): void
{
$this->db()->table($this->tableName)->upsert(
[[
'ID' => 1,
'CODE' => 'A001',
'NAME' => 'Changed',
'STATUS' => 'inactive',
]],
['ID'],
['NAME']
);
$row = $this->db()->table($this->tableName)
->where('ID', 1)->first();
$this->assertSame('Changed', $row->NAME);
$this->assertSame('active', $row->STATUS);
}
public function test_insert_or_ignore_inserts_new_row(): void
{
$this->db()->table($this->tableName)->insertOrIgnore([
'ID' => 2,
'CODE' => 'A002',
'NAME' => 'Second',
'STATUS' => 'active',
]);
$this->assertSame(
2,
$this->db()->table($this->tableName)->count()
);
}
public function test_insert_or_ignore_skips_duplicate(): void
{
$this->db()->table($this->tableName)->insertOrIgnore([
'ID' => 1,
'CODE' => 'A001',
'NAME' => 'Duplicate',
'STATUS' => 'inactive',
]);
$this->assertSame(
'Original',
$this->db()->table($this->tableName)
->where('ID', 1)->first()->NAME
);
}
public function test_insert_or_ignore_multiple_rows(): void
{
$this->db()->table($this->tableName)->insertOrIgnore([
[
'ID' => 1,
'CODE' => 'A001',
'NAME' => 'Duplicate',
'STATUS' => 'inactive',
],
[
'ID' => 2,
'CODE' => 'A002',
'NAME' => 'New',
'STATUS' => 'active',
],
]);
$this->assertSame(
2,
$this->db()->table($this->tableName)->count()
);
}
public function test_update_or_insert(): void
{
$this->db()->table($this->tableName)->updateOrInsert(
['ID' => 1],
['NAME' => 'Updated']
);
$this->db()->table($this->tableName)->updateOrInsert(
['ID' => 2],
[
'CODE' => 'A002',
'NAME' => 'Inserted',
'STATUS' => 'active',
]
);
$this->assertSame(
'Updated',
$this->db()->table($this->tableName)
->where('ID', 1)->first()->NAME
);
$this->assertSame(
'Inserted',
$this->db()->table($this->tableName)
->where('ID', 2)->first()->NAME
);
}
}