|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\Database\Live\Postgre; |
| 15 | + |
| 16 | +use CodeIgniter\Database\Postgre\Connection; |
| 17 | +use CodeIgniter\Test\CIUnitTestCase; |
| 18 | +use Config\Database; |
| 19 | +use PHPUnit\Framework\Attributes\Group; |
| 20 | + |
| 21 | +/** |
| 22 | + * @internal |
| 23 | + */ |
| 24 | +#[Group('DatabaseLive')] |
| 25 | +final class InsertIDTest extends CIUnitTestCase |
| 26 | +{ |
| 27 | + protected function setUp(): void |
| 28 | + { |
| 29 | + parent::setUp(); |
| 30 | + |
| 31 | + $this->db = Database::connect($this->DBGroup); |
| 32 | + |
| 33 | + if ($this->db->DBDriver !== 'Postgre') { |
| 34 | + $this->markTestSkipped('This test is only for Postgre.'); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public function testInsertIDWithApostropheInColumnName(): void |
| 39 | + { |
| 40 | + $tableName = 'temp_insertid_test'; |
| 41 | + $columnName = "id'col"; |
| 42 | + |
| 43 | + $this->db->query( |
| 44 | + 'CREATE TEMPORARY TABLE ' . $this->db->escapeIdentifiers($tableName) . ' (' |
| 45 | + . $this->db->escapeIdentifiers($columnName) . ' SERIAL PRIMARY KEY, "name" VARCHAR(255))', |
| 46 | + ); |
| 47 | + |
| 48 | + try { |
| 49 | + $this->db->query( |
| 50 | + 'INSERT INTO ' . $this->db->escapeIdentifiers($tableName) . ' ("name") VALUES (?)', |
| 51 | + ['Test Name'], |
| 52 | + ); |
| 53 | + |
| 54 | + /** @var Connection $db */ |
| 55 | + $db = $this->db; |
| 56 | + |
| 57 | + $id = $db->insertID($tableName, $columnName); |
| 58 | + |
| 59 | + $this->assertSame(1, $id); |
| 60 | + } finally { |
| 61 | + $this->db->query('DROP TABLE IF EXISTS ' . $this->db->escapeIdentifiers($tableName)); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments