Skip to content

fix: [SQLite3] getFieldData() returns incorrect primary_key values #8460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions system/Database/SQLite3/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,14 @@ protected function _fieldData(string $table): array
for ($i = 0, $c = count($query); $i < $c; $i++) {
$retVal[$i] = new stdClass();

$retVal[$i]->name = $query[$i]->name;
$retVal[$i]->type = $query[$i]->type;
$retVal[$i]->max_length = null;
$retVal[$i]->default = $query[$i]->dflt_value;
$retVal[$i]->primary_key = isset($query[$i]->pk) && (bool) $query[$i]->pk;
$retVal[$i]->name = $query[$i]->name;
$retVal[$i]->type = $query[$i]->type;
$retVal[$i]->max_length = null;
$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;
}

Expand Down
8 changes: 4 additions & 4 deletions tests/system/Database/Live/ForgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -986,31 +986,31 @@ public function testAddFields(): void
'type' => 'INTEGER',
'max_length' => null,
'default' => null,
'primary_key' => true,
'primary_key' => 1,
'nullable' => true,
],
1 => [
'name' => 'username',
'type' => 'VARCHAR',
'max_length' => null,
'default' => null,
'primary_key' => false,
'primary_key' => 0,
'nullable' => false,
],
2 => [
'name' => 'name',
'type' => 'VARCHAR',
'max_length' => null,
'default' => null,
'primary_key' => false,
'primary_key' => 0,
'nullable' => true,
],
3 => [
'name' => 'active',
'type' => 'INTEGER',
'max_length' => null,
'default' => '0',
'primary_key' => false,
'primary_key' => 0,
'nullable' => false,
],
];
Expand Down
73 changes: 66 additions & 7 deletions tests/system/Database/Live/SQLite3/GetFieldDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,55 +47,114 @@ public function testGetFieldData(): void
'type' => 'INTEGER',
'max_length' => null,
'default' => null, // The default value is not defined.
'primary_key' => true,
'primary_key' => 1,
'nullable' => true,
],
(object) [
'name' => 'text_not_null',
'type' => 'VARCHAR',
'max_length' => null,
'default' => null, // The default value is not defined.
'primary_key' => false,
'primary_key' => 0,
'nullable' => false,
],
(object) [
'name' => 'text_null',
'type' => 'VARCHAR',
'max_length' => null,
'default' => null, // The default value is not defined.
'primary_key' => false,
'primary_key' => 0,
'nullable' => true,
],
(object) [
'name' => 'int_default_0',
'type' => 'INT',
'max_length' => null,
'default' => '0', // int 0
'primary_key' => false,
'primary_key' => 0,
'nullable' => false,
],
(object) [
'name' => 'text_default_null',
'type' => 'VARCHAR',
'max_length' => null,
'default' => 'NULL', // NULL value
'primary_key' => false,
'primary_key' => 0,
'nullable' => true,
],
(object) [
'name' => 'text_default_text_null',
'type' => 'VARCHAR',
'max_length' => null,
'default' => "'null'", // string "null"
'primary_key' => false,
'primary_key' => 0,
'nullable' => false,
],
(object) [
'name' => 'text_default_abc',
'type' => 'VARCHAR',
'max_length' => null,
'default' => "'abc'", // string "abc"
'primary_key' => false,
'primary_key' => 0,
'nullable' => false,
],
]),
json_encode($fields)
);
}

protected function createTableCompositePrimaryKey()
{
$this->forge->dropTable('test1', true);

$this->forge->addField([
'pk1' => [
'type' => 'VARCHAR',
'constraint' => 64,
],
'pk2' => [
'type' => 'VARCHAR',
'constraint' => 64,
],
'text' => [
'type' => 'VARCHAR',
'constraint' => 64,
],
]);
$this->forge->addPrimaryKey(['pk1', 'pk2']);
$this->forge->createTable('test1');
}

public function testGetFieldDataCompositePrimaryKey(): void
{
$this->createTableCompositePrimaryKey();

$fields = $this->db->getFieldData('test1');

$this->assertJsonStringEqualsJsonString(
json_encode([
(object) [
'name' => 'pk1',
'type' => 'VARCHAR',
'max_length' => null,
'default' => null,
'primary_key' => 1,
'nullable' => false,
],
(object) [
'name' => 'pk2',
'type' => 'VARCHAR',
'max_length' => null,
'default' => null,
'primary_key' => 1,
'nullable' => false,
],
(object) [
'name' => 'text',
'type' => 'VARCHAR',
'max_length' => null,
'default' => null,
'primary_key' => 0,
'nullable' => false,
],
]),
Expand Down
12 changes: 6 additions & 6 deletions user_guide_src/source/database/metadata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ supplying the table name:
The following data is available from this function if supported by your
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 MySQL and SQLite3)
- nullable - boolean ``true`` if the column is nullable, otherwise boolean ``false``
- default - the default value
- ``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

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

Expand Down