Skip to content

Commit

Permalink
Merge feature generate IDs distinct to table. closes #23
Browse files Browse the repository at this point in the history
  • Loading branch information
jpuck committed Oct 13, 2016
2 parents 389d691 + ea0d514 commit f9c058b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 17 deletions.
9 changes: 6 additions & 3 deletions src/Sources/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

abstract class DB extends Source {
use DDL;
protected $surrogateCount = 0;
protected $surrogateCount = [];
protected $statements = '';

public function __construct(PDO $uri = null, ...$options){
Expand Down Expand Up @@ -90,7 +90,10 @@ protected function insertData(Array $node, Array $schema, Array $query){

// generate surrogate key, or use DB identity
if(empty($this->options['identity'])){
$query[$this->options['surrogate']] = ++$this->surrogateCount;
$count =& $this->surrogateCount;
$table = $query[0];
$count[$table] = $count[$table] ?? 0;
$query[$this->options['surrogate']] = ++$count[$table];
}

// execute query
Expand Down Expand Up @@ -156,7 +159,7 @@ protected function insertExecute(Array &$query, String $primaryKey = null){
$query[$surrogate.'fk'] = $stmt->fetch(PDO::FETCH_ASSOC)[$surrogate];
$stmt->closeCursor();
} else {
$query[$surrogate.'fk'] = $this->surrogateCount;
$query[$surrogate.'fk'] = $this->surrogateCount[$query[0]];
}
}

Expand Down
65 changes: 51 additions & 14 deletions tests/MicrosoftDMLinsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,21 @@ public function testCanInsertXMLwithGeneratedSurrogateKeys(){
*/
public function testCanInsertXMLwithGeneratedCustomSurrogateKey(){
jp::CleanMsSQLdb(static::$pdo);
$opts = [
self::$pdo,
[
'stage' => false,
'surrogate' => 'test_sid',
'identity' => false,
]
];
$data = self::$dataDir;
$xml = new XML(file_get_contents("$data/xml/sample.xml"));
$db = new MicrosoftSQLServer(
self::$pdo,
['stage'=>false,'identity'=>false,'surrogate'=>'test_sid']
);
$db = new MicrosoftSQLServer(...$opts);

$ddl = $db->toSQL($xml->schema())['create'];
$sql = $db->toSQL($xml->schema());
$ddl = $sql['create'];
$dml = $sql['delete'];

$this->assertNotFalse(static::$pdo->exec($ddl));
$this->assertTrue($db->insert($xml));
Expand All @@ -112,23 +119,41 @@ public function testCanInsertXMLwithGeneratedCustomSurrogateKey(){
$maxid = $result['maxid'];
}

$this->assertSame('25',$maxid);
$this->assertSame('6',$maxid);

// test same values used (no identity continuation after delete)
$db = new MicrosoftSQLServer(...$opts);
$this->assertNotFalse(static::$pdo->exec($dml));
$this->assertTrue($db->insert($xml));

$sql = 'SELECT MAX(test_sid) AS maxid FROM DataRecordSAMPLE';
foreach (self::$pdo->query($sql) as $result){
$maxid = $result['maxid'];
}

$this->assertSame('6',$maxid);
}

/**
* @testdox Can insert XML with custom surrogate key
* @testdox Can insert XML with identity custom surrogate key
*/
public function testCanInsertXMLwithCustomSurrogateKey(){
public function testCanInsertXMLwithIdentityCustomSurrogateKey(){
jp::CleanMsSQLdb(static::$pdo);
$opts = [
self::$pdo,
[
'stage' => false,
'surrogate' => 'test_sid',
'identity' => true,
]
];
$data = self::$dataDir;
$xml = new XML(file_get_contents("$data/xml/sample.xml"));
$db = new MicrosoftSQLServer(self::$pdo, [
'stage' => false,
'surrogate' => 'test_sid',
'identity' => true,
]);
$db = new MicrosoftSQLServer(...$opts);

$ddl = $db->toSQL($xml->schema())['create'];
$sql = $db->toSQL($xml->schema());
$ddl = $sql['create'];
$dml = $sql['delete'];

$this->assertNotFalse(static::$pdo->exec($ddl));
$this->assertTrue($db->insert($xml));
Expand All @@ -139,5 +164,17 @@ public function testCanInsertXMLwithCustomSurrogateKey(){
}

$this->assertSame('6',$maxid);

// test identity doesn't restart
$db = new MicrosoftSQLServer(...$opts);
$this->assertNotFalse(static::$pdo->exec($dml));
$this->assertTrue($db->insert($xml));

$sql = 'SELECT MAX(test_sid) AS maxid FROM DataRecordSAMPLE';
foreach (self::$pdo->query($sql) as $result){
$maxid = $result['maxid'];
}

$this->assertSame('12',$maxid);
}
}

0 comments on commit f9c058b

Please sign in to comment.