Skip to content

Commit

Permalink
platform: added getPrimarySequenceName() method (bc break!)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Jan 10, 2016
1 parent ccbfd48 commit 91fe03f
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Platforms/CachedPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ public function getForeignKeys($table)
}


public function getPrimarySequenceName($table)
{
return $this->cache->load('seq.' . md5($table), function () use ($table) {
return $this->platform->getPrimarySequenceName($table);
});
}


public function clearCache()
{
$this->cache->clean();
Expand Down
9 changes: 9 additions & 0 deletions src/Platforms/IPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,13 @@ public function getColumns($table);
* @return array
*/
public function getForeignKeys($table);


/**
* Returns primary sequence name for the table.
* If not supported nor present, returns a null.
* @param string $table
* @return string|null
*/
public function getPrimarySequenceName($table);
}
6 changes: 6 additions & 0 deletions src/Platforms/MySqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,10 @@ public function getForeignKeys($table)
}
return $keys;
}


public function getPrimarySequenceName($table)
{
return null;
}
}
11 changes: 11 additions & 0 deletions src/Platforms/PostgreSqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,15 @@ public function getForeignKeys($table)
}
return $keys;
}


public function getPrimarySequenceName($table)
{
foreach ($this->getColumns($table) as $column) {
if ($column['is_primary']) {
return $column['sequence'];
}
}
return null;
}
}
5 changes: 5 additions & 0 deletions tests/cases/integration/platform.mysql.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ class PlatformMysqlTest extends IntegrationTestCase
], $keys);
}


public function testPrimarySequence()
{
Assert::null($this->connection->getPlatform()->getPrimarySequenceName('books'));
}
}


Expand Down
5 changes: 5 additions & 0 deletions tests/cases/integration/platform.pgsql.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ class PlatformPostgreTest extends IntegrationTestCase
], $keys);
}


public function testPrimarySequence()
{
Assert::same('books_id_seq', $this->connection->getPlatform()->getPrimarySequenceName('books'));
}
}


Expand Down
27 changes: 27 additions & 0 deletions tests/cases/unit/CachedPlatformTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,33 @@ class CachedPlatformTest extends TestCase
$cols = $this->platform->getForeignKeys('foo');
Assert::same($expectedFk, $cols);
}


public function testQueryPS()
{
$expectedPs = 'ps_name';
$this->storageMock
->shouldReceive('read')
->with("nextras.dbal.platform.b5ad707c2b9f71ed843ba3004e50b37d\x00bd00c4ff6d83b5c76532c1ed83cb7855")
->once()
->andReturnNull();
$this->storageMock
->shouldReceive('lock')
->with("nextras.dbal.platform.b5ad707c2b9f71ed843ba3004e50b37d\x00bd00c4ff6d83b5c76532c1ed83cb7855")
->once();
$this->storageMock
->shouldReceive('write')
->with(
"nextras.dbal.platform.b5ad707c2b9f71ed843ba3004e50b37d\x00bd00c4ff6d83b5c76532c1ed83cb7855",
$expectedPs,
[]
)
->once();
$this->platformMock->shouldReceive('getPrimarySequenceName')->with('foo')->once()->andReturn($expectedPs);

$cols = $this->platform->getPrimarySequenceName('foo');
Assert::same($expectedPs, $cols);
}
}


Expand Down

0 comments on commit 91fe03f

Please sign in to comment.