Skip to content

Commit

Permalink
Merge pull request #5279 from paulbalandan/pgsql-close
Browse files Browse the repository at this point in the history
Fix compatibility of `PgSql\Result` on closing the result instance
  • Loading branch information
paulbalandan authored Nov 3, 2021
2 parents c0bb804 + eb1b621 commit af247f4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion system/Database/BasePreparedQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ abstract public function _getResult();
*/
public function close()
{
if (! is_object($this->statement)) {
if (! is_object($this->statement) || ! method_exists($this->statement, 'close')) {
return;
}

Expand Down
55 changes: 34 additions & 21 deletions tests/system/Database/Live/PreparedQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use CodeIgniter\Database\Query;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use Tests\Support\Database\Seeds\CITestSeeder;

/**
* @group DatabaseLive
Expand All @@ -25,19 +26,38 @@ final class PreparedQueryTest extends CIUnitTestCase
{
use DatabaseTestTrait;

protected $refresh = true;
protected $seed = 'Tests\Support\Database\Seeds\CITestSeeder';
protected $seed = CITestSeeder::class;

/**
* @var BasePreparedQuery|null
*/
private $query;

protected function setUp(): void
{
parent::setUp();
$this->query = null;
}

protected function tearDown(): void
{
parent::tearDown();

if ($this->query !== null) {
$this->query->close();
}
}

public function testPrepareReturnsPreparedQuery()
{
$query = $this->db->prepare(static function ($db) {
$this->query = $this->db->prepare(static function ($db) {
return $db->table('user')->insert([
'name' => 'a',
'email' => '[email protected]',
]);
});

$this->assertInstanceOf(BasePreparedQuery::class, $query);
$this->assertInstanceOf(BasePreparedQuery::class, $this->query);

$ec = $this->db->escapeChar;
$pre = $this->db->DBPrefix;
Expand All @@ -54,20 +74,19 @@ public function testPrepareReturnsPreparedQuery()
} else {
$expected = "INSERT INTO {$ec}{$pre}user{$ec} ({$ec}name{$ec}, {$ec}email{$ec}) VALUES ({$placeholders})";
}
$this->assertSame($expected, $query->getQueryString());

$query->close();
$this->assertSame($expected, $this->query->getQueryString());
}

public function testPrepareReturnsManualPreparedQuery()
{
$query = $this->db->prepare(static function ($db) {
$this->query = $this->db->prepare(static function ($db) {
$sql = "INSERT INTO {$db->DBPrefix}user (name, email, country) VALUES (?, ?, ?)";

return (new Query($db))->setQuery($sql);
});

$this->assertInstanceOf(BasePreparedQuery::class, $query);
$this->assertInstanceOf(BasePreparedQuery::class, $this->query);

$pre = $this->db->DBPrefix;

Expand All @@ -78,33 +97,29 @@ public function testPrepareReturnsManualPreparedQuery()
}

$expected = "INSERT INTO {$pre}user (name, email, country) VALUES ({$placeholders})";
$this->assertSame($expected, $query->getQueryString());

$query->close();
$this->assertSame($expected, $this->query->getQueryString());
}

public function testExecuteRunsQueryAndReturnsResultObject()
{
$query = $this->db->prepare(static function ($db) {
$this->query = $this->db->prepare(static function ($db) {
return $db->table('user')->insert([
'name' => 'a',
'email' => '[email protected]',
'country' => 'x',
]);
});

$query->execute('foo', '[email protected]', 'US');
$query->execute('bar', '[email protected]', 'GB');
$this->query->execute('foo', '[email protected]', 'US');
$this->query->execute('bar', '[email protected]', 'GB');

$this->seeInDatabase($this->db->DBPrefix . 'user', ['name' => 'foo', 'email' => '[email protected]']);
$this->seeInDatabase($this->db->DBPrefix . 'user', ['name' => 'bar', 'email' => '[email protected]']);

$query->close();
}

public function testExecuteRunsQueryAndReturnsManualResultObject()
{
$query = $this->db->prepare(static function ($db) {
$this->query = $this->db->prepare(static function ($db) {
$sql = "INSERT INTO {$db->DBPrefix}user (name, email, country) VALUES (?, ?, ?)";

if ($db->DBDriver === 'SQLSRV') {
Expand All @@ -114,12 +129,10 @@ public function testExecuteRunsQueryAndReturnsManualResultObject()
return (new Query($db))->setQuery($sql);
});

$query->execute('foo', '[email protected]', '');
$query->execute('bar', '[email protected]', '');
$this->query->execute('foo', '[email protected]', '');
$this->query->execute('bar', '[email protected]', '');

$this->seeInDatabase($this->db->DBPrefix . 'user', ['name' => 'foo', 'email' => '[email protected]']);
$this->seeInDatabase($this->db->DBPrefix . 'user', ['name' => 'bar', 'email' => '[email protected]']);

$query->close();
}
}

0 comments on commit af247f4

Please sign in to comment.