Radical changes in the request way

This commit is contained in:
Chouchen 2010-10-22 16:11:01 +00:00
parent 697f87a188
commit 1c01937a15

View File

@ -1,6 +1,23 @@
<? <?
class XMLDB{ class XMLDB{
const SELECT = 'select';
const INSERT = 'insert';
const DELETE = 'delete';
const UPDATE = 'update';
/**
* Request
*/
protected $current_request;
protected $operation;
protected $from;
protected $setID;
protected $setChild;
protected $whereID;
protected $whereChildren;
protected $position;
// Path and name to the file // Path and name to the file
protected $_file; protected $_file;
@ -31,6 +48,8 @@ class XMLDB{
// Node buffered // Node buffered
protected $_buffer; protected $_buffer;
/** /**
* Constructor * Constructor
* @param $file string path to the file to read/create * @param $file string path to the file to read/create
@ -38,9 +57,6 @@ 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("log_errors", "on");
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;
@ -65,13 +81,17 @@ class XMLDB{
} }
} }
public function __destruct(){
$this->commit();
}
public function createDatabase($file){ public function createDatabase($file){
$this->_file = $file; $this->_file = $file;
$this->_doc = DOMDocument::loadXML('<?xml version="1.0" encoding="' . $this->_encoding . '"?> $this->_doc = DOMDocument::loadXML('<?xml version="1.0" encoding="' . $this->_encoding . '"?>
<' . $this->_databaseName . '> <' . $this->_databaseName . '>
</' . $this->_databaseName . '>'); </' . $this->_databaseName . '>');
$this->_xpath = new DOMXpath($this->_doc); $this->_xpath = new DOMXpath($this->_doc);
return $this->save(); return $this->commit();
} }
public function dropDatabase($definitely = false){ public function dropDatabase($definitely = false){
@ -82,33 +102,19 @@ class XMLDB{
} }
} }
public function createTable($name, $autoincrement = false, $aiDefaultValue = 0){ /**
if($name == '*' || $this->tableAlreadyExists($name))
return false;
else{
if($autoincrement)
return $this->insert(array('name'=>$this->_tableName, 'attributes'=>array('name'=>$name, 'autoincrement'=>'true', 'aivalue'=>$aiDefaultValue)));
else
return $this->insert(array('name'=>$this->_tableName, 'attributes'=>array('name'=>$name)));
}
}
public function dropTable($table){
return $this->delete($table);
}
/**
* @return bool * @return bool
*/ */
public function tableAlreadyExists($tableName){ public function tableAlreadyExists($tableName){
if($this->selectTable($tableName, 'count') >= 1) $request = $this->_xpath->query('//' . $this->_tableName . '[@name = "'.$tableName.'"]');
if($this->getResult($request, 'count') >= 1)
return true; return true;
return false; return false;
} }
public function isTableAI($table){ public function isTableAI($tableName){
if($this->tableAlreadyExists($table)){ if($this->tableAlreadyExists($tableName)){
$table = $this->selectTable($table); $table = $this->_xpath->query('//' . $this->_tableName . '[@name = "'.$tableName.'"]');
$ai = $this->getAttribute('autoincrement', $table); $ai = $this->getAttribute('autoincrement', $table);
if($ai == 'true') if($ai == 'true')
return true; return true;
@ -116,35 +122,14 @@ class XMLDB{
return false; return false;
} }
private function updateTableAIValue($table){ public function isLoaded(){
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 = '*'){
if($this->selectFromPK($table, $pk , 'count') > 0){
return true;
}
return false;
}
public function isLoaded(){
if($this->_doc != null) if($this->_doc != null)
return true; return true;
else else
return false; return false;
} }
public function count($from, $id = null, $attributes = null, $childs = null){ public function setPrimaryKey($pk){
return $this->select($from, $id, $attributes, $childs, 'count');
}
public function setPrimaryKey($pk){
$this->_primaryKey = $pk; $this->_primaryKey = $pk;
} }
@ -164,24 +149,10 @@ 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
*/ */
public function save(){ public function commit(){
if($this->_doc != null && $this->_file != null){ if($this->_doc != null && $this->_file != null){
$this->_doc->preserveWhiteSpace = false; $this->_doc->preserveWhiteSpace = false;
$this->_doc->formatOutput = true; $this->_doc->formatOutput = true;
@ -192,6 +163,44 @@ class XMLDB{
} }
} }
public function createTable($name, $autoincrement = false, $aiDefaultValue = 0){
if($name == '*' || $this->tableAlreadyExists($name))
return false;
else{
if($autoincrement)
return $this->_insert(array('name'=>$this->_tableName, 'attributes'=>array('name'=>$name, 'autoincrement'=>'true', 'aivalue'=>$aiDefaultValue)));
else
return $this->_insert(array('name'=>$this->_tableName, 'attributes'=>array('name'=>$name)));
}
}
public function dropTable($table){
return $this->_delete($table);
}
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;
}
private function _getNewIncrement($table){
return $this->updateTableAIValue($table);
}
public function pkAlreadyExists($pk, $table = '*'){
if($this->selectFromPK($table, $pk , 'count') > 0){
return true;
}
return false;
}
private function getResult($request, $format){ private function getResult($request, $format){
switch($format){ switch($format){
@ -207,8 +216,93 @@ class XMLDB{
} }
} }
// TODO checking $request public function select(array $what = array('*')){
private function requestToArray($request){ $this->current_request = self::SELECT;
$this->operation = $what;
}
public function insert(array $what = null){
$this->current_request = self::INSERT;
if($what != null)
$this->operation = $what;
else
throw new Exception('You must indicate something to insert');
}
public function delete(){
$this->current_request = self::DELETE;
}
// TODO
public function update(string $from){
$this->current_request = self::UPDATE;
$this->from = $from;
}
public function from(string $from = '*'){
if($this->tableAlreadyExists($from))
$this->from = $from;
else
$this->from = '*';
}
public function set(array $setID = null, array $setChild = null){
$this->setID = $setID;
$this->setChild = $setChild;
}
public function where(string $whereID = null, array $whereChildren = null){
$this->whereID = $whereID;
$this->whereChildren = $whereChildren;
}
public function toPosition(int $to){
$this->position = $to;
}
public function query(){
return $this->_prepareStmt();
}
private function _prepareStmt(){
switch($this->current_request()){
case self::SELECT:
return $this->_select($this->what, $this->from, $this->whereID, $this->whereChildren);
break;
case self::INSERT:
return $this->_insert($this->operation, $this->from, $this->position);
break;
case self::DELETE:
return $this->_delete($this->from, $this->whereID);
break;
case self::UPDATE:
return $this->_update($this->from, $this->setID, $this->setChild, $this->whereID, $this->whereChildren);
break;
default:
throw new Exception('no request detected.');
break;
}
}
private function _getAttribute($attribute, $node){
if($node->length == 1){
return $node->item(0)->getAttribute($attribute);
}else{
return false;
}
}
private function _getChildValue($child, $node){
$nodeArray = array();
if($node->length == 1){
$nodeArray = $this->_requestToArray($node);
if(isset($nodeArray[0]['childs'][$child]))
return $nodeArray[0]['childs'][$child];
}
return false;
}
private function _requestToArray($request){
$return = array(); $return = array();
$number = 0; $number = 0;
@ -240,9 +334,38 @@ class XMLDB{
return $return; return $return;
} }
private function arrayToNode($node){ private function _update($from, $setID = null, $setChild = null, $whereID = null, $whereChildren = null){
/*if(isset($node[0])) $node = $this->_select(array('*'), $from, $whereID, $whereChildren);
$node = $node[0];*/ if($node != null){
$this->updateChildValue($from, $node, $setChild, $forceInsert);
$this->updateItemAttribute($from, $node, $newAttribute, $forceInsert);
}else{
return false;
}
}
private function _select(array $what, string $from, string $id = null, $childs = null, $item = ''){
$attribute = '';
$child = '';
if($id != null && !is_array($id)){
$attribute = '[@' . $this->_primaryKey . ' = "' . $id . '"]';
}
if($childs != null && is_array($childs)){
foreach($childs as $childName=>$childValue)
$child .= '[' . $childName . '="' . $childValue . '"]';
}
if($from == '*')
$request = $this->_xpath->query('//item'.$attribute.$child);
else
$request = $this->_xpath->query('//' . $this->_tableName . '[@name = "'.$from.'"]'.$item.$attribute.$child);
if($what == '*')
return $request;
else{
return $this->_getChildValue($what, $request);
}
}
private function _arrayToNode($node){
if(!is_array($node) || !in_array($node['name'], array($this->_tableName, $this->_itemName))) if(!is_array($node) || !in_array($node['name'], array($this->_tableName, $this->_itemName)))
return; return;
$element = $this->_doc->createElement($node['name']); $element = $this->_doc->createElement($node['name']);
@ -263,92 +386,7 @@ 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
*/
public function selectTable($name, $format = 'node'){
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'){
return $this->select($table, null, $attributes, null, $format,'/'.$this->_itemName);
}
public function selectFromChildren($table, $childs, $format = 'array'){
return $this->select($table, null, null, $childs, $format, '/'.$this->_itemName);
}
public function selectFromPK($table, $pk, $format = "array"){
return $this->select($table, $pk, null, null, $format, '/'.$this->_itemName);
}
/**
* Allows you to get an array of the node you're looking for based on a "where" search thanks to child or attribute value
* @param $from string Name of the table
* @param $id string value of the primary key
* @param $childs array name/value of the child node
* @param $attributes array name/value of the attribute
* @return array
*/
private function select($from, $id = null, $attributes = null, $childs = null, $format = 'array', $item = ''){
if($id != null && !is_array($id)){
$attribute = '[@' . $this->_primaryKey . ' = "' . $id . '"]';
}
if($attributes != null && is_array($attributes)){
foreach($attributes as $attributeName=>$attributeValue)
$attribute .= '[@' . $attributeName . ' = "' . $attributeValue . '"]';
}
if($childs != null && is_array($childs)){
foreach($childs as $childName=>$childValue)
$child .= '[' . $childName . '="' . $childValue . '"]';
}
if($from == '*')
$request = $this->_xpath->query('//item'.$attribute.$child);
else
$request = $this->_xpath->query('//' . $this->_tableName . '[@name = "'.$from.'"]'.$item.$attribute.$child);
return $this->getResult($request, $format);
}
/**
* Same as above, but user can build his own xpath
*/
public function select_xpath($from, $xpath, $format = 'array'){
if (!$from || !$xpath) {
throw new Exception('uhoh, no table selected');
}
$request = $this->_xpath->query('//' . $this->_tableName . '[@name = "'.$from.'"]/'.$xpath);
switch($format){
case "node":
$return = $request;
break;
case "count":
$return = $request->length;
break;
case "array":
default:
$return = $this->requestToArray($request);
}
return $return;
}
/** /**
* Allows you to insert a node into your DB thanks to an array * Allows you to insert a node into your DB thanks to an array
@ -359,7 +397,7 @@ class XMLDB{
*/ */
public function insertItem($id = null, $attributes = null, $childs = null, $table){ public function insertItem($id = null, $attributes = null, $childs = null, $table){
if($id == null && $this->isTableAI($table)){ if($id == null && $this->isTableAI($table)){
$id = $this->getNewIncrement($table); $id = $this->_getNewIncrement($table);
} }
else if(($id == null && !$this->isTableAI($table)) || ($id != null && $this->isTableAI($table))) else if(($id == null && !$this->isTableAI($table)) || ($id != null && $this->isTableAI($table)))
return false; return false;
@ -369,19 +407,19 @@ class XMLDB{
else else
$attributes += array($this->_primaryKey=>$id); $attributes += array($this->_primaryKey=>$id);
if($this->tableAlreadyExists($table) && !$this->pkAlreadyExists($id, $table)) 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;
} }
// TODO $position // TODO $position
private function insert($node, $table = null, $position = null){ private function _insert(array $node, string $table = null, int $position = null){
if(isset($node[0])) if(isset($node[0]))
$node = $node[0]; $node = $node[0];
if(!is_array($node) || !isset($node['name']) || !isset($node['attributes'])){ if(!is_array($node) || !isset($node['name']) || !isset($node['attributes'])){
return false; throw new Exception('The node is not well formated.');
} }
// Creating the node from an array // Creating the node from an array
$element = $this->arrayToNode($node); $element = $this->_arrayToNode($node);
// Inserting the node into the DB // Inserting the node into the DB
// case : creation of a new table // case : creation of a new table
@ -404,7 +442,7 @@ class XMLDB{
}else{ }else{
return false; return false;
} }
return $this->save(); return $this->commit();
} }
/** /**
@ -423,7 +461,7 @@ class XMLDB{
}else{ }else{
$request->item(0)->setAttribute($newAttribute[0],$newAttribute[1]); $request->item(0)->setAttribute($newAttribute[0],$newAttribute[1]);
} }
return $this->save(); return $this->commit();
} }
else else
return false; return false;
@ -443,7 +481,7 @@ class XMLDB{
$newText = new DOMText($value); $newText = new DOMText($value);
$request->removeChild($request->firstChild); $request->removeChild($request->firstChild);
$request->appendChild($newText); $request->appendChild($newText);
return $this->save(); return $this->commit();
} }
else else
return false; return false;
@ -465,33 +503,23 @@ class XMLDB{
} }
//$request = $node->getElementsByTagName($child)->item(0); //$request = $node->getElementsByTagName($child)->item(0);
if($node->replaceChild($newChild, $old_element_childNodes->item($index))) if($node->replaceChild($newChild, $old_element_childNodes->item($index)))
return $this->save(); return $this->commit();
} }
return false; return false;
} }
public function deleteItem($table, $id = null, $attributes = null){
if($id == null && $attributes == null)
return false;
if($id != null)
return $this->delete($table, $id);
return $this->delete($table, null, $attributes);
}
/** /**
* Delete an entry * Delete an entry
* @param $table name of the table in which the entry is * @param $table name of the table in which the entry is
* @param $id $attributes array where condition(s) * @param $id $attributes array where condition(s)
* @return bool * @return bool
*/ */
private function delete($table, $id = null, $attributes = null){ private function _delete($table, $id = null){
if($id != null && $attributes != null)
return false;
if($id != null) if($id != null)
$request = $this->selectFromPK($table, $id, 'node')->item(0); $request = $this->_select (array('*'), $table, $id, null);
if($attributes != null) //$request = $this->selectFromPK($table, $id, 'node')->item(0);
$request = $this->selectFromAttribute($table, array($attribute[0]=>$attribute[1]), 'node')->item(0); else
if($attributes == null && $id == null) $request = $this->_select (array('*'), $table);
$request = $this->selectTable($table);
if($request == null) if($request == null)
return false; return false;
try{ try{
@ -500,22 +528,14 @@ class XMLDB{
echo $e->getMessage(); echo $e->getMessage();
return false; return false;
} }
return $this->save(); return $this->commit();
}
public function deleteNode($node){
if($node == null)
return false;
$node = $node->item(0);
$node->parentNode->removeChild($node);
return $this->save();
} }
public function move($node, $to, $position = null){ public function move($node, $to, $position = null){
$this->_buffer = $node; $this->_buffer = $node;
if($this->deleteNode($node)){ if($this->deleteNode($node)){
$nodeArray = $this->requestToArray($this->_buffer); $nodeArray = $this->requestToArray($this->_buffer);
return $this->insert($nodeArray, $to, $position); return $this->_insert($nodeArray, $to, $position);
}else }else
return false; return false;
} }