diff --git a/XMLDB.log b/XMLDB.log
deleted file mode 100644
index e69de29..0000000
diff --git a/XMLDB.php b/XMLDB.php
index 726beb5..25dca7d 100644
--- a/XMLDB.php
+++ b/XMLDB.php
@@ -70,6 +70,13 @@ class XMLDB{
return false;
}
+ public function pkAlreadyExists($pk, $table = '*'){
+ if($this->selectFromPK($table, $pk , 'count') > 0){
+ return true;
+ }
+ return false;
+ }
+
public function selectTable($name, $format = 'node'){
return $this->select($name, null, null, null, $format);
@@ -165,8 +172,8 @@ class XMLDB{
if (!$from) {
throw new Exception('uhoh, no table selected');
}
- if($id != null && count($id) == 1){
- $attribute .= '[@' . $this->_primaryKey . ' = "' . $id . '"]';
+ if($id != null){
+ $attribute = '[@' . $this->_primaryKey . ' = "' . $id . '"]';
}
if($attributes != null){
foreach($attributes as $attributeName=>$attributeValue)
@@ -233,7 +240,7 @@ class XMLDB{
// TODO other $where and $position
public function insertNode($node, $table = null, $position = null){
- if(!is_array($node) || !isset($node['name']))
+ if(!is_array($node) || !isset($node['name']) || !isset($node['attributes']))
return false;
// Creating the node from an array
@@ -252,17 +259,17 @@ class XMLDB{
// Inserting the node into the DB
// case : creation of a new table
- if($table == null){
+ if($table == null && !$this->tableAlreadyExists($node['name'])){
$this->_doc->firstChild->appendChild($element);
- }else{
+ }else if($table != null){
// case : insertion into the end of table
- //TODO checking if PK already exists
- $request = $this->_xpath->query('//table[@name = "'.$table.'"]');
- if($request->length != 1){
+ if(!$this->tableAlreadyExists($table) || $this->pkAlreadyExists($node['attributes'][$this->_primaryKey], $table)){
return false;
}
+ $request = $this->_xpath->query('//table[@name = "'.$table.'"]');
$request->item(0)->appendChild($element);
- }
+ }else
+ return false;
return $this->save();
}
diff --git a/XMLDBtestUnit.php b/XMLDBtestUnit.php
index 8fde625..44400ac 100644
--- a/XMLDBtestUnit.php
+++ b/XMLDBtestUnit.php
@@ -66,8 +66,18 @@ else
echo 'Testing select visibility = true and x = 32 (experimental - should find 1)
';
$result = $xmldbtest->selectFromChilds('table1',array('visibility'=>'true', 'x'=>'32'));
echo count($result).' results
';
-
+ echo '
Testing insertNode that already exist (should do "ko")
';
+ if($xmldbtest->insertNode(array('name'=>'item', 'attributes'=>array('id'=>'test'), 'childs'=>array('visibility'=>'true', 'x'=>'33')), 'table1'))
+ echo "ok
";
+ else
+ echo "ko
";
+
+ echo '
Testing insertNode that doesn\'t exist (should do "ok")
';
+ if($xmldbtest->insertNode(array('name'=>'item', 'attributes'=>array('id'=>'test2'), 'childs'=>array('visibility'=>'true', 'x'=>'33')), 'table1'))
+ echo "ok
";
+ else
+ echo "ko
";
echo '
Testing updatingNodeAttribute with no insert (should be ok and change the item "links" into "zelda" haha)
';
if($xmldbtest->updateNodeAttribute('table1', array('id', 'links'), array('id', 'zelda')))