Adding some constraints about PK

This commit is contained in:
Chouchen 2010-10-08 16:18:41 +00:00
parent 0e49600951
commit 0b54511565
3 changed files with 27 additions and 10 deletions

View File

View File

@ -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();
}

View File

@ -66,8 +66,18 @@ else
echo 'Testing select visibility = true and x = 32 (experimental - should find 1)<br/>';
$result = $xmldbtest->selectFromChilds('table1',array('visibility'=>'true', 'x'=>'32'));
echo count($result).' results<br/><br/>';
echo '<br/>Testing insertNode that already exist (should do "ko")<br/>';
if($xmldbtest->insertNode(array('name'=>'item', 'attributes'=>array('id'=>'test'), 'childs'=>array('visibility'=>'true', 'x'=>'33')), 'table1'))
echo "ok<br/><br/>";
else
echo "ko<br/><br/>";
echo '<br/>Testing insertNode that doesn\'t exist (should do "ok")<br/>';
if($xmldbtest->insertNode(array('name'=>'item', 'attributes'=>array('id'=>'test2'), 'childs'=>array('visibility'=>'true', 'x'=>'33')), 'table1'))
echo "ok<br/><br/>";
else
echo "ko<br/><br/>";
echo '<br/>Testing updatingNodeAttribute with no insert (should be ok and change the item "links" into "zelda" haha)<br/>';
if($xmldbtest->updateNodeAttribute('table1', array('id', 'links'), array('id', 'zelda')))