Skip to content

Commit

Permalink
Merge feature ignore extra data. closes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
jpuck committed Oct 14, 2016
2 parents 4f27fea + e08fb23 commit d633198
Show file tree
Hide file tree
Showing 3 changed files with 799 additions and 11 deletions.
53 changes: 42 additions & 11 deletions src/Sources/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ protected function insertData(Array $node, Array $schema, Array $query){
foreach ($node['value'] as $key => $value){
// if grandchildren, then recurse
$key = Schematizer::stripNamespace($node['value'][$key]['name']);
if ($this->hasGrandChildren($key, $schema, $query)){
if ($this->hasGrandChildren($node['value'], $key, $schema, $query)){
$recurse []= $value;
} else {
$tmpa = $this->getAttributes($value, $query, $schema, $key);
$tmpb = $this->setValues($value['value'],$key,$query,$schema);
$tmpb = $this->setValues($value,$key,$query,$schema);
$primaryKey = $primaryKey ?? $tmpa ?? $tmpb;
}
}
} else {
$tmp = $this->setValues($node['value'],$name,$query,$schema);
$tmp = $this->setValues($node,$name,$query,$schema);
$primaryKey = $primaryKey ?? $tmp;
}
}
Expand Down Expand Up @@ -165,22 +165,49 @@ protected function insertExecute(Array &$query, String $primaryKey = null){
}
}

protected function setValues($value, String $name, Array &$query, Array $schema, String $prefix=''){
if (is_numeric($value) || !empty($value)){
$query[$prefix.$name] = $value;
if ($this->isPrimaryKey($name, $schema, $query)){
$primaryKey = $name;
protected function setValues(Array &$node, String $nsname, Array &$query, Array $schema, String $prefix=''){
$name = Schematizer::stripNamespace($nsname);
if($this->uses($node,$name,$query,$schema)){
$value = $node[$nsname] ?? $node['value'] ?? null;
if(is_numeric($value) || !empty($value)){
$query[$prefix.$name] = $value;
if($this->isPrimaryKey($name, $schema, $query)){
$primaryKey = $name;
}
}
}
return $primaryKey ?? null;
}

protected function uses(Array &$node, String $name, Array &$query, Array $schema) : Bool {
$stack = $this->walkSchema($schema, $query);

if(isset($stack['attributes'])){
foreach($stack['attributes'] as $key => $value){
if($key === $name){
$exists = true;
}
}
}

if(!empty($stack['elements'][$name])){
$exists = true;
}

// don't waste any more time exploring this node
if(empty($exists)){
unset($node[$name]);
return false;
}

return $exists;
}

protected function getAttributes(Array &$node, Array &$query, Array $schema, String $prefix=''){
// get the node attributes as column values
if (isset($node['attributes'])){
foreach ($node['attributes'] as $key => $value){
$key = Schematizer::stripNamespace($key);
$tmp = $this->setValues($value, $key, $query, $schema, $prefix);
$tmp = $this->setValues($node['attributes'], $key, $query, $schema, $prefix);
$primaryKey = $primaryKey ?? $tmp;
}
}
Expand Down Expand Up @@ -208,7 +235,11 @@ protected function walkSchema(Array $schema, Array $query) : Array {
];
}

protected function hasGrandChildren(String $name, Array $schema, Array $query){
protected function hasGrandChildren(Array &$node, String $name, Array $schema, Array $query){
if(!$this->uses($node, $name, $query, $schema)){
return false;
}

$elements = $this->walkSchema($schema, $query)['elements'];

// check if single leaf
Expand Down
19 changes: 19 additions & 0 deletions tests/MicrosoftDMLinsertTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
use jpuck\etl\Sources\DBMS\MicrosoftSQLServer;
use jpuck\etl\Data\XML;
use jpuck\etl\Schemata\Schema;
use jpuck\phpdev\Functions as jp;

/**
Expand Down Expand Up @@ -177,4 +178,22 @@ public function testCanInsertXMLwithIdentityCustomSurrogateKey(){

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

/**
* @testdox Can insert XML into DB ignoring extra data
*/
public function testCanInsertXMLintoDBignoringExtraData(){
jp::CleanMsSQLdb(static::$pdo);
$db = new MicrosoftSQLServer(self::$pdo,
['stage'=>false, 'identity'=>true]
);
$data = self::$dataDir;
$schema = new Schema("$data/schemata/sample.schema.reduced.php");
$xml = new XML(file_get_contents("$data/xml/sample.xml"), $schema);

$this->assertNotFalse(static::$pdo->exec(
$db->toSQL($schema)['create']
));
$this->assertTrue($db->insert($xml));
}
}
Loading

0 comments on commit d633198

Please sign in to comment.