diff --git a/system/Database/OCI8/Connection.php b/system/Database/OCI8/Connection.php index 8cb7e3a4a766..58c33536a41c 100644 --- a/system/Database/OCI8/Connection.php +++ b/system/Database/OCI8/Connection.php @@ -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'; } diff --git a/tests/system/Database/Live/ForgeTest.php b/tests/system/Database/Live/ForgeTest.php index 626660ee7246..1cb5c6e4d41b 100644 --- a/tests/system/Database/Live/ForgeTest.php +++ b/tests/system/Database/Live/ForgeTest.php @@ -1058,7 +1058,7 @@ public function testAddFields(): void 'name' => 'username', 'type' => 'VARCHAR2', 'max_length' => '255', - 'default' => '', + 'default' => null, 'nullable' => false, ], 2 => [ diff --git a/tests/system/Database/Live/OCI8/GetFieldDataTest.php b/tests/system/Database/Live/OCI8/GetFieldDataTest.php new file mode 100644 index 000000000000..fcb585842a0d --- /dev/null +++ b/tests/system/Database/Live/OCI8/GetFieldDataTest.php @@ -0,0 +1,115 @@ + + * + * 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); + } +}