Skip to content

Commit 8fc9503

Browse files
authored
Merge pull request #8459 from kenjis/fix-oci8-getFieldData-default
fix: [OCI8] getFieldData() returns incorrect `default` value
2 parents 9200417 + c12aa14 commit 8fc9503

3 files changed

Lines changed: 117 additions & 6 deletions

File tree

system/Database/OCI8/Connection.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,7 @@ protected function _fieldData(string $table): array
315315

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

318-
$default = $query[$i]->DATA_DEFAULT;
319-
if ($default === null && $query[$i]->NULLABLE === 'N') {
320-
$default = '';
321-
}
322-
$retval[$i]->default = $default;
318+
$retval[$i]->default = $query[$i]->DATA_DEFAULT;
323319
$retval[$i]->nullable = $query[$i]->NULLABLE === 'Y';
324320
}
325321

tests/system/Database/Live/ForgeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ public function testAddFields(): void
10581058
'name' => 'username',
10591059
'type' => 'VARCHAR2',
10601060
'max_length' => '255',
1061-
'default' => '',
1061+
'default' => null,
10621062
'nullable' => false,
10631063
],
10641064
2 => [
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Database\Live\OCI8;
15+
16+
use CodeIgniter\Database\Live\AbstractGetFieldDataTest;
17+
use Config\Database;
18+
19+
/**
20+
* @group DatabaseLive
21+
*
22+
* @internal
23+
*/
24+
final class GetFieldDataTest extends AbstractGetFieldDataTest
25+
{
26+
protected function createForge(): void
27+
{
28+
if ($this->db->DBDriver !== 'OCI8') {
29+
$this->markTestSkipped('This test is only for OCI8.');
30+
}
31+
32+
$this->forge = Database::forge($this->db);
33+
}
34+
35+
public function testGetFieldData(): void
36+
{
37+
$fields = $this->db->getFieldData('test1');
38+
39+
$data = [];
40+
41+
foreach ($fields as $obj) {
42+
$data[$obj->name] = $obj;
43+
}
44+
45+
$idDefault = $data['id']->default;
46+
$this->assertMatchesRegularExpression('/"ORACLE"."ISEQ\$\$_[0-9]+".nextval/', $idDefault);
47+
48+
$expected = json_decode(json_encode([
49+
(object) [
50+
'name' => 'id',
51+
'type' => 'NUMBER',
52+
'max_length' => '11',
53+
'default' => $idDefault, // The default value is not defined.
54+
// 'primary_key' => 1,
55+
'nullable' => false,
56+
],
57+
(object) [
58+
'name' => 'text_not_null',
59+
'type' => 'VARCHAR2',
60+
'max_length' => '64',
61+
'default' => null, // The default value is not defined.
62+
// 'primary_key' => 0,
63+
'nullable' => false,
64+
],
65+
(object) [
66+
'name' => 'text_null',
67+
'type' => 'VARCHAR2',
68+
'max_length' => '64',
69+
'default' => null, // The default value is not defined.
70+
// 'primary_key' => 0,
71+
'nullable' => true,
72+
],
73+
(object) [
74+
'name' => 'int_default_0',
75+
'type' => 'NUMBER',
76+
'max_length' => '11',
77+
'default' => '0 ', // int 0
78+
// 'primary_key' => 0,
79+
'nullable' => false,
80+
],
81+
(object) [
82+
'name' => 'text_default_null',
83+
'type' => 'VARCHAR2',
84+
'max_length' => '64',
85+
'default' => 'NULL ', // NULL value
86+
// 'primary_key' => 0,
87+
'nullable' => true,
88+
],
89+
(object) [
90+
'name' => 'text_default_text_null',
91+
'type' => 'VARCHAR2',
92+
'max_length' => '64',
93+
'default' => "'null' ", // string "null"
94+
// 'primary_key' => 0,
95+
'nullable' => false,
96+
],
97+
(object) [
98+
'name' => 'text_default_abc',
99+
'type' => 'VARCHAR2',
100+
'max_length' => '64',
101+
'default' => "'abc' ", // string "abc"
102+
// 'primary_key' => 0,
103+
'nullable' => false,
104+
],
105+
]), true);
106+
$names = array_column($expected, 'name');
107+
array_multisort($names, SORT_ASC, $expected);
108+
109+
$fields = json_decode(json_encode($fields), true);
110+
$names = array_column($fields, 'name');
111+
array_multisort($names, SORT_ASC, $fields);
112+
113+
$this->assertSame($expected, $fields);
114+
}
115+
}

0 commit comments

Comments
 (0)