Added autoincrement ( Closes #1 )
Added getAttribute & getChildValue ( Closes #3 )
This commit is contained in:
parent
69b9eb42e2
commit
f73fe22483
91
XMLDB.php
91
XMLDB.php
@ -38,9 +38,9 @@ class XMLDB{
|
|||||||
* @param $createIfNotExist bool create the file if it doesn't exist
|
* @param $createIfNotExist bool create the file if it doesn't exist
|
||||||
*/
|
*/
|
||||||
public function __construct($file, $pk = "id", $createIfNotExist = false, $databaseName = "Database", $tableName = "table", $itemName = "item", $encoding = "utf-8"){
|
public function __construct($file, $pk = "id", $createIfNotExist = false, $databaseName = "Database", $tableName = "table", $itemName = "item", $encoding = "utf-8"){
|
||||||
ini_set("display_errors", "off");
|
/*ini_set("display_errors", "off");
|
||||||
ini_set("log_errors", "on");
|
ini_set("log_errors", "on");
|
||||||
ini_set('error_log', $_SERVER['DOCUMENT_ROOT'].'/test/XMLDB/XMLDB.log');
|
ini_set('error_log', $_SERVER['DOCUMENT_ROOT'].'/test/XMLDB/XMLDB.log');*/
|
||||||
$this->_buffer = null;
|
$this->_buffer = null;
|
||||||
$this->_databaseName = $databaseName;
|
$this->_databaseName = $databaseName;
|
||||||
$this->_itemName = $itemName;
|
$this->_itemName = $itemName;
|
||||||
@ -82,12 +82,16 @@ class XMLDB{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createTable($name){
|
public function createTable($name, $autoincrement = false, $aiDefaultValue = 0){
|
||||||
if($name == '*' || $this->tableAlreadyExists($name))
|
if($name == '*' || $this->tableAlreadyExists($name))
|
||||||
return false;
|
return false;
|
||||||
|
else{
|
||||||
|
if($autoincrement)
|
||||||
|
return $this->insert(array('name'=>$this->_tableName, 'attributes'=>array('name'=>$name, 'autoincrement'=>'true', 'aivalue'=>$aiDefaultValue)));
|
||||||
else
|
else
|
||||||
return $this->insert(array('name'=>$this->_tableName, 'attributes'=>array('name'=>$name)));
|
return $this->insert(array('name'=>$this->_tableName, 'attributes'=>array('name'=>$name)));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function dropTable($table){
|
public function dropTable($table){
|
||||||
return $this->delete($table);
|
return $this->delete($table);
|
||||||
@ -102,6 +106,26 @@ class XMLDB{
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function isTableAI($table){
|
||||||
|
if($this->tableAlreadyExists($table)){
|
||||||
|
$table = $this->selectTable($table);
|
||||||
|
$ai = $this->getAttribute('autoincrement', $table);
|
||||||
|
if($ai == 'true')
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function updateTableAIValue($table){
|
||||||
|
if($this->tableAlreadyExists($table)){
|
||||||
|
$table = $this->selectTable($table);
|
||||||
|
$newValue = $table->item(0)->getAttribute('aivalue')+1;
|
||||||
|
$table->item(0)->setAttribute('aivalue', $newValue);
|
||||||
|
return $newValue;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public function pkAlreadyExists($pk, $table = '*'){
|
public function pkAlreadyExists($pk, $table = '*'){
|
||||||
if($this->selectFromPK($table, $pk , 'count') > 0){
|
if($this->selectFromPK($table, $pk , 'count') > 0){
|
||||||
return true;
|
return true;
|
||||||
@ -140,6 +164,20 @@ class XMLDB{
|
|||||||
return $this->_buffer;
|
return $this->_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getNewIncrement($table){
|
||||||
|
/* the old way
|
||||||
|
$field_count = $this->selectAllFromTable($table, 'count');
|
||||||
|
$nb = 1;
|
||||||
|
$tableArray = $this->selectAllFromTable($table, 'array');
|
||||||
|
//$test = $xmla->note;
|
||||||
|
for($i=0;$i<$field_count;$i++){
|
||||||
|
if($nb <= (int)$tableArray[$i]['attributes'][$this->_primaryKey]) $nb = (int)$tableArray[$i]['attributes'][$this->_primaryKey]+1;
|
||||||
|
}
|
||||||
|
return $nb;
|
||||||
|
*/
|
||||||
|
return $this->updateTableAIValue($table);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saving the DB file
|
* Saving the DB file
|
||||||
*/
|
*/
|
||||||
@ -223,6 +261,22 @@ class XMLDB{
|
|||||||
return $element;
|
return $element;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getAttribute($attribute, $node){
|
||||||
|
if($node->length == 1){
|
||||||
|
return $node->item(0)->getAttribute($attribute);
|
||||||
|
}else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getChildValue($child, $node){
|
||||||
|
if($node->length == 1){
|
||||||
|
$nodeArray = $this->requestToArray($node);
|
||||||
|
if(isset($nodeArray[0]['childs'][$child]))
|
||||||
|
return $nodeArray[0]['childs'][$child];
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shortcuts for select
|
* Shortcuts for select
|
||||||
@ -230,14 +284,17 @@ class XMLDB{
|
|||||||
public function selectTable($name, $format = 'node'){
|
public function selectTable($name, $format = 'node'){
|
||||||
return $this->select($name, null, null, null, $format);
|
return $this->select($name, null, null, null, $format);
|
||||||
}
|
}
|
||||||
|
public function selectAllFromTable($name, $format = 'node'){
|
||||||
|
return $this->select($name, null, null, null, $format, '/'.$this->_itemName);
|
||||||
|
}
|
||||||
public function selectFromAttribute($table, $attributes, $format = 'array'){
|
public function selectFromAttribute($table, $attributes, $format = 'array'){
|
||||||
return $this->select($table, null, $attributes, null, $format);
|
return $this->select($table, null, $attributes, null, $format,'/'.$this->_itemName);
|
||||||
}
|
}
|
||||||
public function selectFromChildren($table, $childs, $format = 'array'){
|
public function selectFromChildren($table, $childs, $format = 'array'){
|
||||||
return $this->select($table, null, null, $childs, $format);
|
return $this->select($table, null, null, $childs, $format, '/'.$this->_itemName);
|
||||||
}
|
}
|
||||||
public function selectFromPK($table, $pk, $format = "array"){
|
public function selectFromPK($table, $pk, $format = "array"){
|
||||||
return $this->select($table, $pk, null, null, $format);
|
return $this->select($table, $pk, null, null, $format, '/'.$this->_itemName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -248,7 +305,7 @@ class XMLDB{
|
|||||||
* @param $attributes array name/value of the attribute
|
* @param $attributes array name/value of the attribute
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function select($from, $id = null, $attributes = null, $childs = null, $format = 'array'){
|
private function select($from, $id = null, $attributes = null, $childs = null, $format = 'array', $item = ''){
|
||||||
if($id != null && !is_array($id)){
|
if($id != null && !is_array($id)){
|
||||||
$attribute = '[@' . $this->_primaryKey . ' = "' . $id . '"]';
|
$attribute = '[@' . $this->_primaryKey . ' = "' . $id . '"]';
|
||||||
}
|
}
|
||||||
@ -263,7 +320,7 @@ class XMLDB{
|
|||||||
if($from == '*')
|
if($from == '*')
|
||||||
$request = $this->_xpath->query('//item'.$attribute.$child);
|
$request = $this->_xpath->query('//item'.$attribute.$child);
|
||||||
else
|
else
|
||||||
$request = $this->_xpath->query('//' . $this->_tableName . '[@name = "'.$from.'"]/'.$this->_itemName.$attribute.$child);
|
$request = $this->_xpath->query('//' . $this->_tableName . '[@name = "'.$from.'"]'.$item.$attribute.$child);
|
||||||
|
|
||||||
return $this->getResult($request, $format);
|
return $this->getResult($request, $format);
|
||||||
}
|
}
|
||||||
@ -298,12 +355,18 @@ class XMLDB{
|
|||||||
* @param $position string 'before' or 'after'
|
* @param $position string 'before' or 'after'
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function insertItem($id, $attributes = null, $childs = null, $table){
|
public function insertItem($id = null, $attributes = null, $childs = null, $table){
|
||||||
|
if($id == null && $this->isTableAI($table)){
|
||||||
|
$id = $this->getNewIncrement($table);
|
||||||
|
}
|
||||||
|
else if(($id == null && !$this->isTableAI($table)) || ($id != null && $this->isTableAI($table)))
|
||||||
|
return false;
|
||||||
|
|
||||||
if($attributes == null)
|
if($attributes == null)
|
||||||
$attributes = array($this->_primaryKey=>$id);
|
$attributes = array($this->_primaryKey=>$id);
|
||||||
else
|
else
|
||||||
$attributes += array($this->_primaryKey=>$id);
|
$attributes += array($this->_primaryKey=>$id);
|
||||||
if($this->tableAlreadyExists($table) && !$this->pkAlreadyExists($id))
|
if($this->tableAlreadyExists($table) && !$this->pkAlreadyExists($id, $table))
|
||||||
return $this->insert(array('name'=>$this->_itemName, 'attributes'=>$attributes, 'childs'=>$childs), $table);
|
return $this->insert(array('name'=>$this->_itemName, 'attributes'=>$attributes, 'childs'=>$childs), $table);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -343,8 +406,8 @@ class XMLDB{
|
|||||||
* @param $forceInsert bool
|
* @param $forceInsert bool
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function updateNodeAttribute($table, $oldAttribute, $newAttribute, $forceInsert = false){
|
public function updateItemAttribute($table, $oldAttribute, $newAttribute, $forceInsert = false){
|
||||||
$request = $this->select($table, null, array($oldAttribute[0]=>$oldAttribute[1]), null, 'node');
|
$request = $this->select($table, null, array($oldAttribute[0]=>$oldAttribute[1]), null, 'node', '/'.$this->_itemName);
|
||||||
if($request->length == 1){
|
if($request->length == 1){
|
||||||
if(!$forceInsert){
|
if(!$forceInsert){
|
||||||
$request->item(0)->setAttribute($oldAttribute[0],$newAttribute[1]);
|
$request->item(0)->setAttribute($oldAttribute[0],$newAttribute[1]);
|
||||||
@ -363,8 +426,8 @@ class XMLDB{
|
|||||||
* @param $value string new value of the node
|
* @param $value string new value of the node
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function updateNodeValue($table, $attribute = null, $child = null, $value){
|
public function updateItemValue($table, $attribute = null, $child = null, $value){
|
||||||
$request = $this->select($table, null, array($attribute[0]=>$attribute[1]), null, 'node');
|
$request = $this->select($table, null, array($attribute[0]=>$attribute[1]), null, 'node', '/'.$this->_itemName);
|
||||||
//$request = $this->_xpath->query('//'.$node.'[@' . $attribute[0] . ' = "' . $attribute[1] . '"]');
|
//$request = $this->_xpath->query('//'.$node.'[@' . $attribute[0] . ' = "' . $attribute[1] . '"]');
|
||||||
if($request->length == 1){
|
if($request->length == 1){
|
||||||
$request = $request->item(0);
|
$request = $request->item(0);
|
||||||
|
@ -25,7 +25,7 @@ class TestOfLogging extends UnitTestCase {
|
|||||||
$log->message('Trying to insert item "test" into "table1"');
|
$log->message('Trying to insert item "test" into "table1"');
|
||||||
$this->assertTrue($xmldbtest->insertItem('test', array('pwet'=>'cacahuete'), array('visibility'=>'true', 'x'=>'33'), 'table1'));
|
$this->assertTrue($xmldbtest->insertItem('test', array('pwet'=>'cacahuete'), array('visibility'=>'true', 'x'=>'33'), 'table1'));
|
||||||
$log->message('Trying to do several select');
|
$log->message('Trying to do several select');
|
||||||
$this->assertEqual($xmldbtest->selectTable('table1', 'count'), 8);
|
$this->assertEqual($xmldbtest->selectAllFromTable('table1', 'count'), 8);
|
||||||
$this->assertEqual($xmldbtest->selectFromPK('table1', 'weather', 'count'), 1);
|
$this->assertEqual($xmldbtest->selectFromPK('table1', 'weather', 'count'), 1);
|
||||||
$this->assertEqual($xmldbtest->selectFromAttribute('table1', array('id'=>'weather'), 'count'),1);
|
$this->assertEqual($xmldbtest->selectFromAttribute('table1', array('id'=>'weather'), 'count'),1);
|
||||||
$this->assertEqual($xmldbtest->selectFromChildren('table1',array('visibility'=>'true'),'count'), 6);
|
$this->assertEqual($xmldbtest->selectFromChildren('table1',array('visibility'=>'true'),'count'), 6);
|
||||||
@ -35,8 +35,8 @@ class TestOfLogging extends UnitTestCase {
|
|||||||
$this->assertFalse($xmldbtest->insertItem('test', null, array('visibility'=>'true', 'x'=>'33'), 'table1'));
|
$this->assertFalse($xmldbtest->insertItem('test', null, array('visibility'=>'true', 'x'=>'33'), 'table1'));
|
||||||
$this->assertTrue($xmldbtest->insertItem('test2', null, array('visibility'=>'true', 'x'=>'33'), 'table1'));
|
$this->assertTrue($xmldbtest->insertItem('test2', null, array('visibility'=>'true', 'x'=>'33'), 'table1'));
|
||||||
$log->message('Trying to do several updates');
|
$log->message('Trying to do several updates');
|
||||||
$this->assertTrue($xmldbtest->updateNodeAttribute('table1', array('id', 'links'), array('id', 'zelda')));
|
$this->assertTrue($xmldbtest->updateItemAttribute('table1', array('id', 'links'), array('id', 'zelda')));
|
||||||
$this->assertTrue($xmldbtest->updateNodeValue('table1', array('id', 'notes'), null, 'booga!'));
|
$this->assertTrue($xmldbtest->updateItemValue('table1', array('id', 'notes'), null, 'booga!'));
|
||||||
$log->message('Trying to delete the item "clock" from "table1"');
|
$log->message('Trying to delete the item "clock" from "table1"');
|
||||||
//var_dump($xmldbtest->selectFromPK('table1', 'clock', 'node'), true);
|
//var_dump($xmldbtest->selectFromPK('table1', 'clock', 'node'), true);
|
||||||
//$this->assertTrue($xmldbtest->deleteNode($xmldbtest->selectFromPK('table1', 'clock', 'node')));
|
//$this->assertTrue($xmldbtest->deleteNode($xmldbtest->selectFromPK('table1', 'clock', 'node')));
|
||||||
@ -48,6 +48,36 @@ class TestOfLogging extends UnitTestCase {
|
|||||||
$log->message('Trying to erase a DB');
|
$log->message('Trying to erase a DB');
|
||||||
$xmldbtest1->dropDatabase(true);
|
$xmldbtest1->dropDatabase(true);
|
||||||
|
|
||||||
|
$xmldbtest2 = new XMLDB('database2.xml');
|
||||||
|
$this->assertTrue($xmldbtest2->isTableAI('table1'));
|
||||||
|
$this->assertTrue($xmldbtest2->insertItem(null, null, null,'table1'));
|
||||||
|
$this->assertEqual($xmldbtest2->getChildValue('sex', $xmldbtest2->selectFromPK('table1', '3', 'node')), 'M');
|
||||||
|
$this->assertTrue($xmldbtest2->createTable('table3', true, 2));
|
||||||
|
$personnes = array(
|
||||||
|
3 => array(
|
||||||
|
'nom' => 'Desmidt',
|
||||||
|
'prenom' => 'clément'
|
||||||
|
),
|
||||||
|
4 => array(
|
||||||
|
'nom' => 'Chou',
|
||||||
|
'prenom' => 'Chen'
|
||||||
|
),
|
||||||
|
5 => array(
|
||||||
|
'nom' => 'Votocek',
|
||||||
|
'prenom' => 'Sophie'
|
||||||
|
),
|
||||||
|
6 => array(
|
||||||
|
'nom' => 'Dupond',
|
||||||
|
'prenom' => 'Jean'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
foreach($personnes as $personne){
|
||||||
|
$this->assertTrue($xmldbtest2->insertItem(null, null, $personne, 'table3'));
|
||||||
|
}
|
||||||
|
$this->assertTrue($xmldbtest2->deleteITem('table3', '4'));
|
||||||
|
$this->assertTrue($xmldbtest2->insertItem(null, null, array('visibility'=>'true', 'x'=>'31'), 'table3'));
|
||||||
|
//$this->assertTrue($xmldbtest2->insertItem(null, null, array('visibility'=>'true', 'x'=>'33'), 'table3'));
|
||||||
|
//$this->assertEqual($xmldbtest2->getAttribute('aivalue', $xmldbtest2->selectTable('table3')), 5);
|
||||||
/*$log->message('Trying to select w/o anything');
|
/*$log->message('Trying to select w/o anything');
|
||||||
$xmldbtest->select();*/
|
$xmldbtest->select();*/
|
||||||
}
|
}
|
||||||
|
16
database2.xml
Normal file
16
database2.xml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Database>
|
||||||
|
<table name="table1" autoincrement="true" aivalue="5">
|
||||||
|
<item id="1">
|
||||||
|
<name>Sophie</name>
|
||||||
|
<age>27</age>
|
||||||
|
<sex>F</sex>
|
||||||
|
</item>
|
||||||
|
<item id="3">
|
||||||
|
<name>Clement</name>
|
||||||
|
<age>28</age>
|
||||||
|
<sex>M</sex>
|
||||||
|
<religion>atheist</religion>
|
||||||
|
</item>
|
||||||
|
</table>
|
||||||
|
</Database>
|
Loading…
Reference in New Issue
Block a user