Skip to content

Commit

Permalink
fix: primary_key value
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Jan 26, 2024
1 parent c86ddc5 commit 284ac97
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
5 changes: 4 additions & 1 deletion system/Database/SQLite3/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,10 @@ protected function _fieldData(string $table): array
$retVal[$i]->type = $query[$i]->type;
$retVal[$i]->max_length = null;
$retVal[$i]->default = $query[$i]->dflt_value;
$retVal[$i]->primary_key = $query[$i]->pk ?? false;
// "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
59 changes: 59 additions & 0 deletions tests/system/Database/Live/SQLite3/GetFieldDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,63 @@ public function testGetFieldData(): void
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,
],
]),
json_encode($fields)
);
}
}

0 comments on commit 284ac97

Please sign in to comment.