-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #160: Support multiple returning columns in MSSQL …
…driver
- Loading branch information
Showing
5 changed files
with
298 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of Cycle ORM package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Database\Driver\SQLServer\Query; | ||
|
||
use Cycle\Database\Driver\DriverInterface; | ||
use Cycle\Database\Driver\SQLServer\SQLServerDriver; | ||
use Cycle\Database\Exception\BuilderException; | ||
use Cycle\Database\Exception\ReadonlyConnectionException; | ||
use Cycle\Database\Injection\FragmentInterface; | ||
use Cycle\Database\Query\QueryParameters; | ||
use Cycle\Database\Query\ReturningInterface; | ||
use Cycle\Database\Query\InsertQuery; | ||
use Cycle\Database\Query\QueryInterface; | ||
use Cycle\Database\StatementInterface; | ||
|
||
class SQLServerInsertQuery extends InsertQuery implements ReturningInterface | ||
{ | ||
/** | ||
* @var SQLServerDriver|null | ||
*/ | ||
protected ?DriverInterface $driver = null; | ||
|
||
/** | ||
* @var list<FragmentInterface|non-empty-string> | ||
*/ | ||
protected array $returningColumns = []; | ||
|
||
public function withDriver(DriverInterface $driver, string $prefix = null): QueryInterface | ||
{ | ||
$driver instanceof SQLServerDriver or throw new BuilderException( | ||
'SQLServer InsertQuery can be used only with SQLServer driver' | ||
); | ||
|
||
return parent::withDriver($driver, $prefix); | ||
} | ||
|
||
public function returning(string|FragmentInterface ...$columns): self | ||
{ | ||
$columns === [] and throw new BuilderException('RETURNING clause should contain at least 1 column.'); | ||
|
||
$this->returningColumns = \array_values($columns); | ||
|
||
return $this; | ||
} | ||
|
||
public function run(): mixed | ||
{ | ||
if ($this->returningColumns === []) { | ||
return parent::run(); | ||
} | ||
|
||
$params = new QueryParameters(); | ||
$queryString = $this->sqlStatement($params); | ||
|
||
$this->driver->isReadonly() and throw ReadonlyConnectionException::onWriteStatementExecution(); | ||
|
||
$result = $this->driver->query($queryString, $params->getParameters()); | ||
|
||
try { | ||
if (\count($this->returningColumns) === 1) { | ||
return $result->fetchColumn(); | ||
} | ||
return $result->fetch(StatementInterface::FETCH_ASSOC); | ||
} finally { | ||
$result->close(); | ||
} | ||
} | ||
|
||
public function getTokens(): array | ||
{ | ||
return parent::getTokens() + [ | ||
'return' => $this->returningColumns, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
tests/Database/Unit/Driver/SQLServer/Query/SQLServerInsertQueryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Database\Tests\Unit\Driver\SQLServer\Query; | ||
|
||
use Cycle\Database\Driver\DriverInterface; | ||
use Cycle\Database\Driver\SQLServer\Query\SQLServerInsertQuery; | ||
use Cycle\Database\Exception\BuilderException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SQLServerInsertQueryTest extends TestCase | ||
{ | ||
public function testWithDriverException(): void | ||
{ | ||
$insert = new SQLServerInsertQuery(); | ||
|
||
$this->expectException(BuilderException::class); | ||
$insert->withDriver($this->createMock(DriverInterface::class)); | ||
} | ||
} |