Skip to content
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
1 change: 1 addition & 0 deletions docs/changelogs/v4.7.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Release Date: Unreleased
- **CORS:** Fixed a bug where `Cors::hasResponseHeaders()` matched header substrings via `str_contains()`, causing false positives when existing exposed headers shared a prefix with configured exposed headers.
- **Database:** Fixed a bug where `SQLSRV\Builder::_upsertBatch()` concatenated `$table` unescaped into `Object_ID()`, causing syntax errors for table names with quotes or special characters.
- **Database:** Fixed a bug where `SQLSRV\Forge::_alterTable()` interpolated table and column names without escaping in default constraint lookups.
- **Database:** Fixed a bug where `SQLite3\Connection::_foreignKeyData()` did not escape table identifiers in `PRAGMA foreign_key_list`, causing syntax errors on table names containing hyphens or schemas.
- **Database:** Fixed a bug where `SQLite3\Table::dropIndexes()` used single-quoted string literals instead of escaped identifiers in `DROP INDEX IF EXISTS`, causing syntax errors on index names containing quotes or special characters.
- **HTTP:** Fixed a bug where `UploadedFile::move()` and `UploadedFile::store()` on Windows could store a file under a different name than `getName()` reported, or write its contents to an NTFS alternate data stream. Filenames ending in a period or space or containing a colon are now rejected, including explicitly supplied names.
- **Images:** Fixed a bug in `ImageMagickHandler::save()` where saving without an extension or with an unsupported extension threw an `ImagickException` instead of falling back to the image's format.
Expand Down
8 changes: 7 additions & 1 deletion system/Database/SQLite3/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,13 @@ protected function _foreignKeyData(string $table): array
return [];
}

$query = $this->query("PRAGMA foreign_key_list({$table})")->getResult();
$parts = explode('.', $table);
$tableName = array_pop($parts);
Comment thread
paulbalandan marked this conversation as resolved.
Resolved
$schema = array_pop($parts);
$pragma = ($schema === null || $schema === '' ? '' : $this->escapeIdentifier($schema) . '.')
. 'foreign_key_list(' . $this->escape($tableName) . ')';

$query = $this->query('PRAGMA ' . $pragma)->getResult();
$indexes = [];

foreach ($query as $row) {
Expand Down
156 changes: 156 additions & 0 deletions tests/system/Database/Live/SQLite3/GetForeignKeyDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Database\Live\SQLite3;

use CodeIgniter\Test\CIUnitTestCase;
use Config\Database;
use PHPUnit\Framework\Attributes\Group;

/**
* @internal
*/
#[Group('DatabaseLive')]
final class GetForeignKeyDataTest extends CIUnitTestCase
{
protected function setUp(): void
{
parent::setUp();

$this->db = Database::connect($this->DBGroup);
if ($this->db->DBDriver !== 'SQLite3') {
$this->markTestSkipped('This test is only for SQLite3.');
}

$config = [
'DBDriver' => 'SQLite3',
'database' => 'database.db',
'DBDebug' => true,
];
$this->db = db_connect($config, false);
}

public function testGetForeignKeyDataWithSpecialTableName(): void
{
$this->db->query('CREATE TABLE `test-parent` (id INTEGER PRIMARY KEY)');
$this->db->query('CREATE TABLE `test-child` (id INTEGER PRIMARY KEY, parent_id INTEGER, FOREIGN KEY (parent_id) REFERENCES `test-parent`(id))');

$foreignKeys = $this->db->getForeignKeyData('test-child');
Comment thread
paulbalandan marked this conversation as resolved.
Resolved

$this->db->query('DROP TABLE IF EXISTS `test-child`');
$this->db->query('DROP TABLE IF EXISTS `test-parent`');

$this->assertCount(1, $foreignKeys);
$firstKey = reset($foreignKeys);
$this->assertSame('test-child', $firstKey->table_name);
$this->assertSame(['parent_id'], $firstKey->column_name);
$this->assertSame('test-parent', $firstKey->foreign_table_name);
$this->assertSame(['id'], $firstKey->foreign_column_name);
}

public function testGetForeignKeyDataWithAttachedDatabase(): void
{
$this->db->query('ATTACH DATABASE ":memory:" AS `test_schema`');

$this->db->query('CREATE TABLE `test_schema`.`attached-parent` (id INTEGER PRIMARY KEY)');
$this->db->query('CREATE TABLE `test_schema`.`attached-child` (id INTEGER PRIMARY KEY, p_id INTEGER, FOREIGN KEY (p_id) REFERENCES `attached-parent`(id))');

$foreignKeys = $this->db->getForeignKeyData('test_schema.attached-child');

$this->db->query('DROP TABLE IF EXISTS `test_schema`.`attached-child`');
$this->db->query('DROP TABLE IF EXISTS `test_schema`.`attached-parent`');
$this->db->query('DETACH DATABASE `test_schema`');

$this->assertCount(1, $foreignKeys);
$firstKey = reset($foreignKeys);
$this->assertSame('test_schema.attached-child', $firstKey->table_name);
$this->assertSame(['p_id'], $firstKey->column_name);
$this->assertSame('attached-parent', $firstKey->foreign_table_name);
$this->assertSame(['id'], $firstKey->foreign_column_name);
}

public function testGetForeignKeyDataWithParenthesesInTableName(): void
{
$this->db->query('CREATE TABLE "parent(v1)" (id INTEGER PRIMARY KEY)');
$this->db->query('CREATE TABLE "child(v2)" (id INTEGER PRIMARY KEY, parent_id INTEGER, FOREIGN KEY (parent_id) REFERENCES "parent(v1)"(id))');

$foreignKeys = $this->db->getForeignKeyData('child(v2)');

$this->db->query('DROP TABLE IF EXISTS "child(v2)"');
$this->db->query('DROP TABLE IF EXISTS "parent(v1)"');

$this->assertCount(1, $foreignKeys);
$firstKey = reset($foreignKeys);
$this->assertSame('child(v2)', $firstKey->table_name);
$this->assertSame(['parent_id'], $firstKey->column_name);
$this->assertSame('parent(v1)', $firstKey->foreign_table_name);
$this->assertSame(['id'], $firstKey->foreign_column_name);
}

public function testGetForeignKeyDataWithQuotesAndSpacesInTableName(): void
{
$this->db->query('CREATE TABLE "parent table" (id INTEGER PRIMARY KEY)');
$this->db->query('CREATE TABLE "child\'quote with space" (id INTEGER PRIMARY KEY, p_id INTEGER, FOREIGN KEY (p_id) REFERENCES "parent table"(id))');

$foreignKeys = $this->db->getForeignKeyData("child'quote with space");

$this->db->query('DROP TABLE IF EXISTS "child\'quote with space"');
$this->db->query('DROP TABLE IF EXISTS "parent table"');

$this->assertCount(1, $foreignKeys);
$firstKey = reset($foreignKeys);
$this->assertSame("child'quote with space", $firstKey->table_name);
$this->assertSame(['p_id'], $firstKey->column_name);
$this->assertSame('parent table', $firstKey->foreign_table_name);
$this->assertSame(['id'], $firstKey->foreign_column_name);
}

public function testGetForeignKeyDataWithNumericAndReservedWordTableName(): void
{
$this->db->query('CREATE TABLE "order" (id INTEGER PRIMARY KEY)');
$this->db->query('CREATE TABLE "12345" (id INTEGER PRIMARY KEY, order_id INTEGER, FOREIGN KEY (order_id) REFERENCES "order"(id))');

$foreignKeys = $this->db->getForeignKeyData('12345');

$this->db->query('DROP TABLE IF EXISTS "12345"');
$this->db->query('DROP TABLE IF EXISTS "order"');

$this->assertCount(1, $foreignKeys);
$firstKey = reset($foreignKeys);
$this->assertSame('12345', $firstKey->table_name);
$this->assertSame(['order_id'], $firstKey->column_name);
$this->assertSame('order', $firstKey->foreign_table_name);
$this->assertSame(['id'], $firstKey->foreign_column_name);
}

public function testGetForeignKeyDataWithAttachedDatabaseSpecialIdentifiers(): void
{
$this->db->query('ATTACH DATABASE ":memory:" AS "schema-db(1)"');

$this->db->query('CREATE TABLE "schema-db(1)"."parent(v1)" (id INTEGER PRIMARY KEY)');
$this->db->query('CREATE TABLE "schema-db(1)"."child(v2)" (id INTEGER PRIMARY KEY, p_id INTEGER, FOREIGN KEY (p_id) REFERENCES "parent(v1)"(id))');

$foreignKeys = $this->db->getForeignKeyData('schema-db(1).child(v2)');

$this->db->query('DROP TABLE IF EXISTS "schema-db(1)"."child(v2)"');
$this->db->query('DROP TABLE IF EXISTS "schema-db(1)"."parent(v1)"');
$this->db->query('DETACH DATABASE "schema-db(1)"');

$this->assertCount(1, $foreignKeys);
$firstKey = reset($foreignKeys);
$this->assertSame('schema-db(1).child(v2)', $firstKey->table_name);
$this->assertSame(['p_id'], $firstKey->column_name);
$this->assertSame('parent(v1)', $firstKey->foreign_table_name);
$this->assertSame(['id'], $firstKey->foreign_column_name);
}
}
Loading