/** * Checks if all columns of a xml description of all tables of a database matches the database * * Warning: if ( $change && $strictlyColumns ) it will DROP not described columns !!! * @access private * * @param string|array $tablesNames Name(s) of tables to dump * @param boolean $withContent FALSE: only structure, TRUE: also content * @return CBSimpleXMLElement */ function dumpTableToXml($tablesNames, $withContent = true) { $db = new CBSimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><database version="1" />'); foreach ((array) $tablesNames as $tableName) { $table = $db->addChild('table'); $table->addAttribute('name', $tableName); $table->addAttribute('class', ''); $table->addAttribute('strict', 'false'); $table->addAttribute('drop', 'never'); // Columns: $allColumns = $this->getAllTableColumns($tableName); $columns = $table->addChild('columns'); foreach ($allColumns as $colEntry) { $colTypeUnsigned = explode(' ', $colEntry->Type); if (count($colTypeUnsigned) == 1) { $colTypeUnsigned[1] = null; } $column = $columns->addChild('column'); $column->addAttribute('name', $colEntry->Field); $column->addAttribute('type', 'sql:' . $colTypeUnsigned[0]); if ($colTypeUnsigned[1] === 'unsigned') { $column->addAttribute('unsigned', $colTypeUnsigned[1] === 'unsigned' ? 'true' : 'false'); } if ($colEntry->Null === 'YES') { $column->addAttribute('null', $colEntry->Null === 'YES' ? 'true' : 'false'); } if ($colEntry->Default !== null) { if ($colEntry->Null === 'YES') { $column->addAttribute('default', $colEntry->Default); } else { $defaultDefaultTypes = $this->defaultValuesOfTypes($this->mysqlToXmlsql('sql:' . $colTypeUnsigned[0])); if (!in_array($colEntry->Default, $defaultDefaultTypes)) { $column->addAttribute('default', $colEntry->Default); } } } if (strpos($colEntry->Extra, 'auto_increment') !== false) { $tableStatus = $this->_db->getTableStatus($tableName); if (isset($tableStatus[0]->Auto_increment)) { $lastAuto_increment = $tableStatus[0]->Auto_increment; } else { $lastAuto_increment = '100'; } $column->addAttribute('auto_increment', $lastAuto_increment); } } // Indexes: $indexes = $table->addChild('indexes'); $primaryIndex = null; $allIndexes = $this->getAllTableIndexes($tableName); foreach ($allIndexes as $indexName => $sequenceInIndexArray) { $type = $sequenceInIndexArray[1]['type']; $using = $sequenceInIndexArray[1]['using']; $index = $indexes->addChild('index'); $index->addAttribute('name', $indexName); if ($type != '') { $index->addAttribute('type', $type); } if ($using != 'btree') { $index->addAttribute('using', $using); } foreach ($sequenceInIndexArray as $indexAttributes) { $column = $index->addChild('column'); $column->addAttribute('name', $indexAttributes['name']); if ($indexAttributes['size']) { $column->addAttribute('size', $indexAttributes['size']); } if ($indexAttributes['ordering'] != 'A') { $column->addAttribute('ordering', $indexAttributes['ordering']); } } if ($type == 'primary') { $primaryIndex = $index; } } // Content: if ($withContent) { $allRows = $this->loadRows($tableName, null, null, null); if (count($allRows) > 0) { $rows = $table->addChild('rows'); $primaryNames = null; if ($primaryIndex !== null) { foreach ($primaryIndex->children() as $column) { if ($column->name() == 'column') { $primaryNames[] = $column->attributes('name'); } } } foreach ($allRows as $rowData) { $row = $rows->addChild('row'); // missing primary key here: $rowIndexName = array(); $rowIndexValue = array(); $rowIndexValueType = array(); foreach (get_object_vars($rowData) as $fieldDataName => $fieldDataValue) { if ($fieldDataName[0] != '_') { $typeColumn = $columns->getChildByNameAttributes('column', array('name' => $fieldDataName)); $fieldDataValueType = 'const:' . $this->mysqlToXmlsql($typeColumn->attributes('type')); $field = $row->addChild('field'); if ($fieldDataValue === null) { $fieldDataValue = 'NULL'; $fieldDataValueType = 'const:null'; } $field->addAttribute('name', $fieldDataName); $field->addAttribute('value', $fieldDataValue); $field->addAttribute('valuetype', $fieldDataValueType); if (in_array($fieldDataName, $primaryNames)) { $field->addAttribute('strict', 'true'); $rowIndexName[] = $fieldDataName; $rowIndexValue[] = $fieldDataValue; $rowIndexValueType[] = $fieldDataValueType; } } } $row->addAttribute('index', implode(' ', $rowIndexName)); $row->addAttribute('value', implode(' ', $rowIndexValue)); $row->addAttribute('valuetype', implode(' ', $rowIndexValueType)); } } } } return $db; }