Skip to content

Commit

Permalink
Merge pull request #8459 from kenjis/fix-oci8-getFieldData-default
Browse files Browse the repository at this point in the history
fix: [OCI8] getFieldData() returns incorrect `default` value
  • Loading branch information
kenjis authored Jan 30, 2024
2 parents 9200417 + c12aa14 commit 8fc9503
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 6 deletions.
6 changes: 1 addition & 5 deletions system/Database/OCI8/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,7 @@ protected function _fieldData(string $table): array

$retval[$i]->max_length = $length;

$default = $query[$i]->DATA_DEFAULT;
if ($default === null && $query[$i]->NULLABLE === 'N') {
$default = '';
}
$retval[$i]->default = $default;
$retval[$i]->default = $query[$i]->DATA_DEFAULT;
$retval[$i]->nullable = $query[$i]->NULLABLE === 'Y';
}

Expand Down
2 changes: 1 addition & 1 deletion tests/system/Database/Live/ForgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ public function testAddFields(): void
'name' => 'username',
'type' => 'VARCHAR2',
'max_length' => '255',
'default' => '',
'default' => null,
'nullable' => false,
],
2 => [
Expand Down
115 changes: 115 additions & 0 deletions tests/system/Database/Live/OCI8/GetFieldDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Database\Live\OCI8;

use CodeIgniter\Database\Live\AbstractGetFieldDataTest;
use Config\Database;

/**
* @group DatabaseLive
*
* @internal
*/
final class GetFieldDataTest extends AbstractGetFieldDataTest
{
protected function createForge(): void
{
if ($this->db->DBDriver !== 'OCI8') {
$this->markTestSkipped('This test is only for OCI8.');
}

$this->forge = Database::forge($this->db);
}

public function testGetFieldData(): void
{
$fields = $this->db->getFieldData('test1');

$data = [];

foreach ($fields as $obj) {
$data[$obj->name] = $obj;
}

$idDefault = $data['id']->default;
$this->assertMatchesRegularExpression('/"ORACLE"."ISEQ\$\$_[0-9]+".nextval/', $idDefault);

$expected = json_decode(json_encode([
(object) [
'name' => 'id',
'type' => 'NUMBER',
'max_length' => '11',
'default' => $idDefault, // The default value is not defined.
// 'primary_key' => 1,
'nullable' => false,
],
(object) [
'name' => 'text_not_null',
'type' => 'VARCHAR2',
'max_length' => '64',
'default' => null, // The default value is not defined.
// 'primary_key' => 0,
'nullable' => false,
],
(object) [
'name' => 'text_null',
'type' => 'VARCHAR2',
'max_length' => '64',
'default' => null, // The default value is not defined.
// 'primary_key' => 0,
'nullable' => true,
],
(object) [
'name' => 'int_default_0',
'type' => 'NUMBER',
'max_length' => '11',
'default' => '0 ', // int 0
// 'primary_key' => 0,
'nullable' => false,
],
(object) [
'name' => 'text_default_null',
'type' => 'VARCHAR2',
'max_length' => '64',
'default' => 'NULL ', // NULL value
// 'primary_key' => 0,
'nullable' => true,
],
(object) [
'name' => 'text_default_text_null',
'type' => 'VARCHAR2',
'max_length' => '64',
'default' => "'null' ", // string "null"
// 'primary_key' => 0,
'nullable' => false,
],
(object) [
'name' => 'text_default_abc',
'type' => 'VARCHAR2',
'max_length' => '64',
'default' => "'abc' ", // string "abc"
// 'primary_key' => 0,
'nullable' => false,
],
]), true);
$names = array_column($expected, 'name');
array_multisort($names, SORT_ASC, $expected);

$fields = json_decode(json_encode($fields), true);
$names = array_column($fields, 'name');
array_multisort($names, SORT_ASC, $fields);

$this->assertSame($expected, $fields);
}
}

0 comments on commit 8fc9503

Please sign in to comment.