1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-06 14:16:39 +02:00

Merge pull request #209 from castamir/sqlsrv_insert

SqlsrvDriver::getInsertId() last inserted id is from last statement instead of last inserted row
This commit is contained in:
David Grudl
2016-02-29 23:37:17 +01:00
3 changed files with 34 additions and 1 deletions

View File

@@ -141,7 +141,7 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
*/
public function getInsertId($sequence)
{
$res = sqlsrv_query($this->connection, 'SELECT @@IDENTITY');
$res = sqlsrv_query($this->connection, 'SELECT SCOPE_IDENTITY()');
if (is_resource($res)) {
$row = sqlsrv_fetch_array($res, SQLSRV_FETCH_NUMERIC);
return $row[0];

View File

@@ -0,0 +1,28 @@
<?php
/**
* @dataProvider? ../databases.ini sqlsrv
*/
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
$conn = new Dibi\Connection($config);
$conn->loadFile(__DIR__ . "/data/sqlsrv.insert.sql");
for ($i = 1; $i <= 5; $i++) {
$conn->query('INSERT INTO %n DEFAULT VALUES', 'aaa');
Assert::equal($i, $conn->getInsertId());
}
$conn->query('INSERT INTO %n DEFAULT VALUES', 'aab');
Assert::equal(1, $conn->getInsertId());
$conn->query(
'CREATE TRIGGER %n ON %n AFTER INSERT AS INSERT INTO %n DEFAULT VALUES',
'UpdAAB', 'aab', 'aaa'
);
$conn->query('INSERT INTO %n DEFAULT VALUES', 'aab');
Assert::equal(2, $conn->getInsertId());

View File

@@ -0,0 +1,5 @@
IF OBJECT_ID('aaa', 'U') IS NOT NULL DROP TABLE aaa;
IF OBJECT_ID('aab', 'U') IS NOT NULL DROP TABLE aab;
CREATE TABLE aaa ( [id] int NOT NULL IDENTITY PRIMARY KEY )
CREATE TABLE aab ( [id] int NOT NULL IDENTITY PRIMARY KEY )