Skip to content

Commit

Permalink
fix: change order of properties in stdClass returned by getFieldData(…
Browse files Browse the repository at this point in the history
…) to the same for all DB drivers
  • Loading branch information
kenjis committed Jan 30, 2024
1 parent 057bcbd commit f30768f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion system/Database/OCI8/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ protected function _fieldData(string $table): array

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

$retval[$i]->default = $query[$i]->DATA_DEFAULT;
$retval[$i]->nullable = $query[$i]->NULLABLE === 'Y';
$retval[$i]->default = $query[$i]->DATA_DEFAULT;
}

return $retval;
Expand Down
2 changes: 1 addition & 1 deletion system/Database/Postgre/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ protected function _fieldData(string $table): array

$retVal[$i]->name = $query[$i]->column_name;
$retVal[$i]->type = $query[$i]->data_type;
$retVal[$i]->max_length = $query[$i]->character_maximum_length > 0 ? $query[$i]->character_maximum_length : $query[$i]->numeric_precision;
$retVal[$i]->nullable = $query[$i]->is_nullable === 'YES';
$retVal[$i]->default = $query[$i]->column_default;
$retVal[$i]->max_length = $query[$i]->character_maximum_length > 0 ? $query[$i]->character_maximum_length : $query[$i]->numeric_precision;
}

return $retVal;
Expand Down
6 changes: 3 additions & 3 deletions system/Database/SQLSRV/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,15 +357,15 @@ protected function _fieldData(string $table): array
for ($i = 0, $c = count($query); $i < $c; $i++) {
$retVal[$i] = new stdClass();

$retVal[$i]->name = $query[$i]->COLUMN_NAME;
$retVal[$i]->type = $query[$i]->DATA_TYPE;
$retVal[$i]->default = $query[$i]->COLUMN_DEFAULT;
$retVal[$i]->name = $query[$i]->COLUMN_NAME;
$retVal[$i]->type = $query[$i]->DATA_TYPE;

$retVal[$i]->max_length = $query[$i]->CHARACTER_MAXIMUM_LENGTH > 0
? $query[$i]->CHARACTER_MAXIMUM_LENGTH
: $query[$i]->NUMERIC_PRECISION;

$retVal[$i]->nullable = $query[$i]->IS_NULLABLE !== 'NO';
$retVal[$i]->default = $query[$i]->COLUMN_DEFAULT;
}

return $retVal;
Expand Down
2 changes: 1 addition & 1 deletion system/Database/SQLite3/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,12 @@ protected function _fieldData(string $table): array
$retVal[$i]->name = $query[$i]->name;
$retVal[$i]->type = $query[$i]->type;
$retVal[$i]->max_length = null;
$retVal[$i]->nullable = isset($query[$i]->notnull) && ! (bool) $query[$i]->notnull;
$retVal[$i]->default = $query[$i]->dflt_value;
// "pk" (either zero for columns that are not part of the primary key,
// or the 1-based index of the column within the primary key).
// https://www.sqlite.org/pragma.html#pragma_table_info
$retVal[$i]->primary_key = ($query[$i]->pk === 0) ? 0 : 1;
$retVal[$i]->nullable = isset($query[$i]->notnull) && ! (bool) $query[$i]->notnull;
}

return $retVal;
Expand Down
32 changes: 16 additions & 16 deletions tests/system/Database/Live/ForgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -953,30 +953,30 @@ public function testAddFields(): void
0 => [
'name' => 'id',
'type' => 'integer',
'max_length' => '32',
'nullable' => false,
'default' => "nextval('db_forge_test_fields_id_seq'::regclass)",
'max_length' => '32',
],
1 => [
'name' => 'username',
'type' => 'character varying',
'max_length' => '255',
'nullable' => false,
'default' => null,
'max_length' => '255',
],
2 => [
'name' => 'name',
'type' => 'character varying',
'max_length' => '255',
'nullable' => true,
'default' => null,
'max_length' => '255',
],
3 => [
'name' => 'active',
'type' => 'integer',
'max_length' => '32',
'nullable' => false,
'default' => '0',
'max_length' => '32',
],
];
} elseif ($this->db->DBDriver === 'SQLite3') {
Expand All @@ -985,64 +985,64 @@ public function testAddFields(): void
'name' => 'id',
'type' => 'INTEGER',
'max_length' => null,
'nullable' => true,
'default' => null,
'primary_key' => 1,
'nullable' => true,
],
1 => [
'name' => 'username',
'type' => 'VARCHAR',
'max_length' => null,
'nullable' => false,
'default' => null,
'primary_key' => 0,
'nullable' => false,
],
2 => [
'name' => 'name',
'type' => 'VARCHAR',
'max_length' => null,
'nullable' => true,
'default' => null,
'primary_key' => 0,
'nullable' => true,
],
3 => [
'name' => 'active',
'type' => 'INTEGER',
'max_length' => null,
'nullable' => false,
'default' => '0',
'primary_key' => 0,
'nullable' => false,
],
];
} elseif ($this->db->DBDriver === 'SQLSRV') {
$expected = [
0 => [
'name' => 'id',
'type' => 'int',
'default' => null,
'max_length' => 10,
'nullable' => false,
'default' => null,
],
1 => [
'name' => 'username',
'type' => 'varchar',
'default' => null,
'max_length' => 255,
'nullable' => false,
'default' => null,
],
2 => [
'name' => 'name',
'type' => 'varchar',
'default' => null,
'max_length' => 255,
'nullable' => true,
'default' => null,
],
3 => [
'name' => 'active',
'type' => 'int',
'default' => '((0))', // Why?
'max_length' => 10,
'nullable' => false,
'default' => '((0))', // Why?
],
];
} elseif ($this->db->DBDriver === 'OCI8') {
Expand All @@ -1051,29 +1051,29 @@ public function testAddFields(): void
'name' => 'id',
'type' => 'NUMBER',
'max_length' => '11',
'default' => '"ORACLE"."ISEQ$$_80229".nextval', // Sequence id may change
'nullable' => false,
'default' => '"ORACLE"."ISEQ$$_80229".nextval', // Sequence id may change
],
1 => [
'name' => 'username',
'type' => 'VARCHAR2',
'max_length' => '255',
'default' => null,
'nullable' => false,
'default' => null,
],
2 => [
'name' => 'name',
'type' => 'VARCHAR2',
'max_length' => '255',
'default' => null,
'nullable' => true,
'default' => null,
],
3 => [
'name' => 'active',
'type' => 'NUMBER',
'max_length' => '11',
'default' => '0 ', // Why?
'nullable' => false,
'default' => '0 ', // Why?
],
];

Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/database/metadata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ database:
- ``name`` - column name
- ``type`` - the type of the column
- ``max_length`` - maximum length of the column
- ``primary_key`` - integer ``1`` if the column is a primary key (all integer ``1``, even if there are multiple primary keys), otherwise integer ``0`` (This field is currently only available for ``MySQLi`` and ``SQLite3``)
- ``nullable`` - boolean ``true`` if the column is nullable, otherwise boolean ``false``
- ``default`` - the default value
- ``primary_key`` - integer ``1`` if the column is a primary key (all integer ``1``, even if there are multiple primary keys), otherwise integer ``0`` (This field is currently only available for ``MySQLi`` and ``SQLite3``)

.. note:: Since v4.4.0, SQLSRV supported ``nullable``.

Expand Down

0 comments on commit f30768f

Please sign in to comment.