Skip to content

Commit

Permalink
Added designation type lastInsertId.
Browse files Browse the repository at this point in the history
  • Loading branch information
prwater committed Oct 18, 2019
1 parent 0db9fd7 commit 42185c3
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Helper/RoutineLoaderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private function extractDesignationType(): void
throw new RoutineLoaderException('Unable to find the designation type of the stored routine');
}

if (!in_array($this->designationType, ['none', 'row0', 'row1', 'rows', 'singleton0', 'singleton1']))
if (!in_array($this->designationType, ['lastInsertId', 'none', 'row0', 'row1', 'rows', 'singleton0', 'singleton1']))
{
throw new RoutineLoaderException("'%s' is not a valid designation type", $this->designationType);
}
Expand Down
3 changes: 3 additions & 0 deletions src/SqlitePdoDataLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,9 @@ public function insertRows(string $table, array $rows): void
* Returns the ID of the last inserted row.
*
* @return int
*
* @since 1.0.0
* @api
*/
public function lastInsertId(): int
{
Expand Down
43 changes: 43 additions & 0 deletions src/Wrapper/LastInsertIdWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);

namespace SetBased\Stratum\SqlitePdo\Wrapper;

/**
* Class for generating a wrapper method for a stored procedure that insert rows in a table with an auto increment key.
*/
class LastInsertIdWrapper extends Wrapper
{
//--------------------------------------------------------------------------------------------------------------------
/**
* @inheritdoc
*/
protected function getDocBlockReturnType(): string
{
return 'int';
}

//--------------------------------------------------------------------------------------------------------------------
/**
* @inheritdoc
*/
protected function getReturnTypeDeclaration(): string
{
return ': int';
}

//--------------------------------------------------------------------------------------------------------------------
/**
* @inheritdoc
*/
protected function writeResultHandler(): void
{
$this->codeStore->append('$this->executeNone($query);');
$this->codeStore->append('');
$this->codeStore->append('return $this->lastInsertId();');
}

//--------------------------------------------------------------------------------------------------------------------
}

//----------------------------------------------------------------------------------------------------------------------
4 changes: 4 additions & 0 deletions src/Wrapper/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public static function createRoutineWrapper(array $routine, PhpCodeStore $codeSt
{
switch ($routine['designation'])
{
case 'lastInsertId':
$wrapper = new LastInsertIdWrapper($routine, $codeStore, $nameMangler);
break;

case 'none':
$wrapper = new NoneWrapper($routine, $codeStore, $nameMangler);
break;
Expand Down
28 changes: 28 additions & 0 deletions test/LastInsertIdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

namespace SetBased\Stratum\SqlitePdo\Test;

/**
* Test cases for stored routines with designation type lastInsertId.
*/
class LastInsertIdTest extends DataLayerTestCase
{
//--------------------------------------------------------------------------------------------------------------------
/**
* Stored routine with designation type lastInsertId must returns IDs start with 1.
*/
public function test01()
{
$id = $this->dataLayer->tstTestLastIncrementId('Hello');
self::assertEquals(1, $id);

$id = $this->dataLayer->tstTestLastIncrementId('World');
self::assertEquals(2, $id);
}

//--------------------------------------------------------------------------------------------------------------------
}

//----------------------------------------------------------------------------------------------------------------------

21 changes: 21 additions & 0 deletions test/TestDataLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,27 @@ public function tstTestIllegalQuery(): array
return $this->executeRows($query);
}

//--------------------------------------------------------------------------------------------------------------------
/**
* Test case for designation type lastIncrementId.
*
* @param string|null $pTstTest Some value.
*
* @return int
*/
public function tstTestLastIncrementId(?string $pTstTest): int
{
$replace = [':p_tst_test' => $this->quoteString($pTstTest)];
$query = <<< EOT
insert into TST_LAST_INCREMENT_ID( tst_test )
values( :p_tst_test )
EOT;
$query = str_repeat(PHP_EOL, 7).strtr($query, $replace);
$this->executeNone($query);

return $this->lastInsertId();
}

//--------------------------------------------------------------------------------------------------------------------
/**
*
Expand Down
5 changes: 5 additions & 0 deletions test/ddl/0100_create_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,8 @@ values( 'spam'
;

-- ---------------------------------------------------------------------------------------------------------------------
drop table if exists TST_LAST_INCREMENT_ID;

create table TST_LAST_INCREMENT_ID( tst_test varchar );

-- ---------------------------------------------------------------------------------------------------------------------
9 changes: 9 additions & 0 deletions test/psql/tst_test_last_increment_id.psql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Test case for designation type lastIncrementId.
*
* @param varchar :p_tst_test Some value.
*
* @type lastInsertId
*/
insert into TST_LAST_INCREMENT_ID( tst_test )
values( :p_tst_test )

0 comments on commit 42185c3

Please sign in to comment.