Skip to content

Commit

Permalink
use table-spaced incrementing ID
Browse files Browse the repository at this point in the history
  • Loading branch information
jpuck committed Oct 13, 2016
1 parent d859b24 commit ea0d514
Showing 1 changed file with 6 additions and 3 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

0 comments on commit ea0d514

Please sign in to comment.