Skip to content

Commit 35e3664

Browse files
authored
fix: [SQLSRV] _getResult() return object for preparedQuery class (#6718)
* fix: _getResult() return object for preparedQuery class in sqlsrv
1 parent 1024cfc commit 35e3664

3 files changed

Lines changed: 20 additions & 13 deletions

File tree

system/Database/SQLSRV/PreparedQuery.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ class PreparedQuery extends BasePreparedQuery
2727
*/
2828
protected $parameters = [];
2929

30-
/**
31-
* The result boolean from a sqlsrv_execute.
32-
*
33-
* @var bool
34-
*/
35-
protected $result;
36-
3730
/**
3831
* Prepares the query against the database, and saves the connection
3932
* info necessary to execute the query later.
@@ -68,7 +61,7 @@ public function _prepare(string $sql, array $options = [])
6861

6962
/**
7063
* Takes a new set of data and runs it against the currently
71-
* prepared query. Upon success, will return a Results object.
64+
* prepared query.
7265
*/
7366
public function _execute(array $data): bool
7467
{
@@ -80,19 +73,17 @@ public function _execute(array $data): bool
8073
$this->parameters[$key] = $value;
8174
}
8275

83-
$this->result = sqlsrv_execute($this->statement);
84-
85-
return (bool) $this->result;
76+
return sqlsrv_execute($this->statement);
8677
}
8778

8879
/**
89-
* Returns the result object for the prepared query.
80+
* Returns the statement resource for the prepared query or false when preparing failed.
9081
*
9182
* @return mixed
9283
*/
9384
public function _getResult()
9485
{
95-
return $this->result;
86+
return $this->statement;
9687
}
9788

9889
/**

tests/system/Database/Live/PreparedQueryTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use CodeIgniter\Database\BasePreparedQuery;
1515
use CodeIgniter\Database\Query;
16+
use CodeIgniter\Database\ResultInterface;
1617
use CodeIgniter\Test\CIUnitTestCase;
1718
use CodeIgniter\Test\DatabaseTestTrait;
1819
use Tests\Support\Database\Seeds\CITestSeeder;
@@ -134,4 +135,18 @@ public function testExecuteRunsQueryAndReturnsManualResultObject()
134135
$this->seeInDatabase($this->db->DBPrefix . 'user', ['name' => 'foo', 'email' => 'foo@example.com']);
135136
$this->seeInDatabase($this->db->DBPrefix . 'user', ['name' => 'bar', 'email' => 'bar@example.com']);
136137
}
138+
139+
public function testExecuteSelectQueryAndCheckTypeAndResult()
140+
{
141+
$this->query = $this->db->prepare(static fn ($db) => $db->table('user')->select('name, email, country')->where([
142+
'name' => 'foo',
143+
])->get());
144+
145+
$result = $this->query->execute('Derek Jones');
146+
147+
$this->assertInstanceOf(ResultInterface::class, $result);
148+
149+
$expectedRow = ['name' => 'Derek Jones', 'email' => 'derek@world.com', 'country' => 'US'];
150+
$this->assertSame($expectedRow, $result->getRowArray());
151+
}
137152
}

user_guide_src/source/changelogs/v4.2.8.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ Bugs Fixed
3838

3939
- Fixed a bug when the ``CodeIgniter\HTTP\IncomingRequest::getPostGet()`` and ``CodeIgniter\HTTP\IncomingRequest::getGetPost()`` methods didn't return values from the other stream when ``index`` was set to ``null``.
4040
- Fixed a bug when ``binds`` weren't cleaned properly when calling ``CodeIgniter\Database\Postgre::replace()`` multiple times in the context.
41+
- Fixed a bug when the ``CodeIgniter\Database\SQLSRV\PreparedQuery::_getResult()`` was returning the bool value instead of the resource.
4142

4243
See the repo's `CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_ for a complete list of bugs fixed.

0 commit comments

Comments
 (0)