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 @@ -18,6 +18,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 `Postgre`, `OCI8`, and `SQLSRV` `Forge::_dropKeyAsConstraint()` interpolated table, constraint, and schema names without escaping.
- **Database:** Fixed a bug where `Postgre\Connection::insertID()` interpolated table, column, and sequence names without escaping.
- **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\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.
Expand Down
4 changes: 2 additions & 2 deletions system/Database/Postgre/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ public function insertID()
$sql = 'SELECT LASTVAL() AS ins_id';
} elseif ($table !== null) {
if ($column !== null && $v >= '8.0') {
$sql = "SELECT pg_get_serial_sequence('{$table}', '{$column}') AS seq";
$sql = 'SELECT pg_get_serial_sequence(' . $this->escape($table) . ', ' . $this->escape($column) . ') AS seq';
$query = $this->query($sql);
$query = $query->getRow();
$seq = $query->seq;
Expand All @@ -498,7 +498,7 @@ public function insertID()
$seq = $table;
}

$sql = "SELECT CURRVAL('{$seq}') AS ins_id";
$sql = 'SELECT CURRVAL(' . $this->escape($seq) . ') AS ins_id';
} else {
return pg_last_oid($this->resultID);
}
Expand Down
64 changes: 64 additions & 0 deletions tests/system/Database/Live/Postgre/InsertIDTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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\Postgre;

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

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

$this->db = Database::connect($this->DBGroup);

if ($this->db->DBDriver !== 'Postgre') {
$this->markTestSkipped('This test is only for Postgre.');
}
}

public function testInsertIDWithApostropheInColumnName(): void
{
$tableName = 'temp_insertid_test';
$columnName = "id'col";

$this->db->query(
'CREATE TEMPORARY TABLE ' . $this->db->escapeIdentifiers($tableName) . ' ('
. $this->db->escapeIdentifiers($columnName) . ' SERIAL PRIMARY KEY, "name" VARCHAR(255))',
);

try {
$this->db->query(
'INSERT INTO ' . $this->db->escapeIdentifiers($tableName) . ' ("name") VALUES (?)',
['Test Name'],
);

/** @var Connection $db */
$db = $this->db;

$id = $db->insertID($tableName, $columnName);

$this->assertSame(1, $id);
} finally {
$this->db->query('DROP TABLE IF EXISTS ' . $this->db->escapeIdentifiers($tableName));
}
}
}
Loading