More SQL command (drop table, drop database)

delete, select & insert upgraded 
More utility function (node to array, arrayToNode)
XMLDB special move command
PHP Unit Test
This commit is contained in:
Chouchen
2010-10-11 14:03:38 +00:00
parent 0b54511565
commit 69b9eb42e2
126 changed files with 42747 additions and 178 deletions

260
XMLDB.php
View File

@@ -10,25 +10,45 @@ class XMLDB{
// Selected table
protected $_table;
// XPATH
// XPATH of the doc
protected $_xpath;
// Content of the XML DB File
protected $_doc;
// Name of the main root
protected $_databaseName;
// Name of each table root
protected $_tableName;
// Name of each item inside tables
protected $_itemName;
// Encoding used for the XML
protected $_encoding;
// Node buffered
protected $_buffer;
/**
* Constructor
* @param $file string path to the file to read/create
* @param $pk string name of the primary key
* @param $createIfNotExist bool create the file if it doesn't exist
*/
public function __construct($file, $pk = "id", $createIfNotExist = false){
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->_primaryKey = $pk;
$this->_file = $file;
$this->_doc = new DOMDocument;
$this->_buffer = null;
$this->_databaseName = $databaseName;
$this->_itemName = $itemName;
$this->_tableName = $tableName;
$this->_encoding = $encoding;
$this->_primaryKey = $pk;
$this->_file = $file;
$this->_doc = new DOMDocument;
$this->_doc->preserveWhiteSpace = false;
$this->_doc->formatOutput = true;
if($this->_doc->load($this->_file)){
@@ -38,27 +58,39 @@ class XMLDB{
if($createIfNotExist){
$this->createDatabase($file);
}else{
$this->_file = null;
$this->_doc = null;
$this->xpath = null;
$this->_file = null;
$this->_doc = null;
$this->xpath = null;
}
}
}
public function createDatabase($file){
$this->_file = $file;
$this->_doc = DOMDocument::loadXML('<?xml version="1.0" encoding="utf-8"?>
<Database>
</Database>');
$this->_xpath = new DOMXpath($this->_doc);
$this->_file = $file;
$this->_doc = DOMDocument::loadXML('<?xml version="1.0" encoding="' . $this->_encoding . '"?>
<' . $this->_databaseName . '>
</' . $this->_databaseName . '>');
$this->_xpath = new DOMXpath($this->_doc);
return $this->save();
}
public function dropDatabase($definitely = false){
if($definitely){
unlink($this->_file);
}else{
$this->createDatabase($this->_file);
}
}
public function createTable($name){
if($name == '*' || $this->tableAlreadyExists($name))
return false;
else
return $this->insertNode(array('name'=>'table', 'attributes'=>array('name'=>$name)));
return $this->insert(array('name'=>$this->_tableName, 'attributes'=>array('name'=>$name)));
}
public function dropTable($table){
return $this->delete($table);
}
/**
@@ -77,11 +109,6 @@ class XMLDB{
return false;
}
public function selectTable($name, $format = 'node'){
return $this->select($name, null, null, null, $format);
}
public function isLoaded(){
if($this->_doc != null)
return true;
@@ -105,6 +132,14 @@ class XMLDB{
return $this->_xpath;
}
public function setBuffer($node){
$this->_buffer = $node;
}
public function getBuffer($buffer){
return $this->_buffer;
}
/**
* Saving the DB file
*/
@@ -119,43 +154,88 @@ class XMLDB{
}
}
private function getResult($request, $format){
switch($format){
case "node":
return $request;
break;
case "count":
return $request->length;
break;
case "array":
default:
return $this->requestToArray($request);
}
}
// TODO checking $request
public function requestToArray($request){
private function requestToArray($request){
$return = array();
$number = 0;
foreach($request as $element){
/*if($childName != null && $childValue != null)
$element = $element->parentNode;*/
$elementValue = $element->attributes->item(0)->value;
$return[$elementValue]['attributes'] = array();
$return[$elementValue]['childs'] = array();
$return[$number]['name'] = $this->_itemName;
$return[$number]['attributes'] = array($this->_primaryKey => $elementValue);
$return[$number]['childs'] = array();
//Retrieving Attributes
$attributes = $element->attributes;
$length = $attributes->length;
for ($i = 1; $i <= $length; $i++) {
$return[$elementValue]['attributes'][$attributes->item($i)->name] = $attributes->item($i)->value;
for ($i = 0; $i <= $length; $i++) {
if($attributes->item($i)->name != '')
$return[$number]['attributes'][$attributes->item($i)->name] = $attributes->item($i)->value;
}
// Retrivieving childs
$nodes = $element->childNodes;
$length = $nodes->length;
for ($i = 1; $i <= $length; $i++) {
$return[$elementValue]['childs'][$nodes->item($i)->nodeName] = $nodes->item($i)->nodeValue;
for ($i = 0; $i <= $length; $i++) {
if($nodes->item($i)->nodeName != '')
$return[$number]['childs'][$nodes->item($i)->nodeName] = $nodes->item($i)->nodeValue;
}
$number++;
}
return $return;
}
private function arrayToNode($node){
if(!is_array($node) || !in_array($node['name'], array($this->_tableName, $this->_itemName)))
return;
$element = $this->_doc->createElement($node['name']);
if(isset($node['attributes'])){
foreach($node['attributes'] as $attributeName=>$attributeValue){
if($attributeName != '')
$element->setAttribute($attributeName, $attributeValue);
}
}
if(isset($node['childs'])){
foreach($node['childs'] as $childName=>$childValue){
if($childName != ''){
$newElement = $this->_doc->createElement($childName, $childValue);
$element->appendChild($newElement);
}
}
}
return $element;
}
/**
* Shortcuts for select
*/
public function selectTable($name, $format = 'node'){
return $this->select($name, null, null, null, $format);
}
public function selectFromAttribute($table, $attributes, $format = 'array'){
return $this->select($table, null, $attributes, null, $format);
}
public function selectFromChilds($table, $childs, $format = 'array'){
}
public function selectFromChildren($table, $childs, $format = 'array'){
return $this->select($table, null, null, $childs, $format);
}
public function selectFromPK($table, $pk, $format = "array"){
return $this->select($table, $pk, null, null, $format);
}
@@ -168,43 +248,24 @@ class XMLDB{
* @param $attributes array name/value of the attribute
* @return array
*/
public function select($from, $id = null, $attributes = null, $childs = null, $format = 'array'){
if (!$from) {
throw new Exception('uhoh, no table selected');
}
if($id != null){
private function select($from, $id = null, $attributes = null, $childs = null, $format = 'array'){
if($id != null && !is_array($id)){
$attribute = '[@' . $this->_primaryKey . ' = "' . $id . '"]';
}
if($attributes != null){
if($attributes != null && is_array($attributes)){
foreach($attributes as $attributeName=>$attributeValue)
$attribute .= '[@' . $attributeName . ' = "' . $attributeValue . '"]';
}
if($childs != null){
if($childs != null && is_array($childs)){
foreach($childs as $childName=>$childValue)
$child .= '[' . $childName . '="' . $childValue . '"]';
/*$child .= '/' . $childName . '[.="' . $childValue . '"]';
if(count($childs)>1){
$child = str_replace('/', '', $child);
$child = '/'.$child;
}*/
}
if($from == '*')
$request = $this->_xpath->query('//item'.$attribute.$child);
else
$request = $this->_xpath->query('//table[@name = "'.$from.'"]/item'.$attribute.$child);
$request = $this->_xpath->query('//' . $this->_tableName . '[@name = "'.$from.'"]/'.$this->_itemName.$attribute.$child);
switch($format){
case "node":
$return = $request;
break;
case "count":
$return = $request->length;
break;
case "array":
default:
$return = $this->requestToArray($request);
}
return $return;
return $this->getResult($request, $format);
}
/**
@@ -214,7 +275,7 @@ class XMLDB{
if (!$from || !$xpath) {
throw new Exception('uhoh, no table selected');
}
$request = $this->_xpath->query('//table[@name = "'.$from.'"]/'.$xpath);
$request = $this->_xpath->query('//' . $this->_tableName . '[@name = "'.$from.'"]/'.$xpath);
switch($format){
case "node":
$return = $request;
@@ -237,25 +298,25 @@ class XMLDB{
* @param $position string 'before' or 'after'
* @return bool
*/
public function insertItem($id, $attributes = null, $childs = null, $table){
if($attributes == null)
$attributes = array($this->_primaryKey=>$id);
else
$attributes += array($this->_primaryKey=>$id);
if($this->tableAlreadyExists($table) && !$this->pkAlreadyExists($id))
return $this->insert(array('name'=>$this->_itemName, 'attributes'=>$attributes, 'childs'=>$childs), $table);
return false;
}
// TODO other $where and $position
public function insertNode($node, $table = null, $position = null){
if(!is_array($node) || !isset($node['name']) || !isset($node['attributes']))
// TODO $position
private function insert($node, $table = null, $position = null){
if(isset($node[0]))
$node = $node[0];
if(!is_array($node) || !isset($node['name']) || !isset($node['attributes'])){
return false;
}
// Creating the node from an array
$element = $this->_doc->createElement($node['name']);
if(isset($node['attributes'])){
foreach($node['attributes'] as $attributeName=>$attributeValue){
$element->setAttribute($attributeName, $attributeValue);
}
}
if(isset($node['childs'])){
foreach($node['childs'] as $childName=>$childValue){
$newElement = $this->_doc->createElement($childName, $childValue);
$element->appendChild($newElement);
}
}
$element = $this->arrayToNode($node);
// Inserting the node into the DB
// case : creation of a new table
@@ -266,10 +327,11 @@ class XMLDB{
if(!$this->tableAlreadyExists($table) || $this->pkAlreadyExists($node['attributes'][$this->_primaryKey], $table)){
return false;
}
$request = $this->_xpath->query('//table[@name = "'.$table.'"]');
$request = $this->_xpath->query('//' . $this->_tableName . '[@name = "'.$table.'"]');
$request->item(0)->appendChild($element);
}else
}else{
return false;
}
return $this->save();
}
@@ -315,23 +377,53 @@ class XMLDB{
return false;
}
public function deleteNode($table, $id = null, $attributes = null){
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
* @param $table name of the table in which the entry is
* @param $id $attributes array where condition(s)
* @return bool
*/
private function delete($table, $id = null, $attributes = null){
if($id != null && $attributes != null)
return false;
if($id != null)
$request = $this->select($table, $id, null, null, 'node')->item(0);
$request = $this->selectFromPK($table, $id, 'node')->item(0);
if($attributes != null)
$request = $this->select($table, null, array($attribute[0]=>$attribute[1]), null, 'node')->item(0);
$request->parentNode->removeChild($request);
$request = $this->selectFromAttribute($table, array($attribute[0]=>$attribute[1]), 'node')->item(0);
if($attributes == null && $id == null)
$request = $this->selectTable($table);
if($request == null)
return false;
try{
$request->parentNode->removeChild($request);
}catch(Exception $e){
echo $e->getMessage();
return false;
}
return $this->save();
}
public function moveNode($node, $from, $to){
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, $itemAfter = ''){
$this->_buffer = $node;
if($this->deleteNode($node)){
$nodeArray = $this->requestToArray($this->_buffer);
return $this->insert($nodeArray, $to);
}else
return false;
}
}