|
Erfahrener Benutzer
Registriert seit: 02.12.2004
Ort: Remagen
Beiträge: 4.587
|
Hallo,
wir haben per Mail eine Antwort erhalten. Ja, ein wenig komisch .. war ein fehler von uns.
Hier mal die Antwort.
Zitat:
hallo jacoblange
habe mal ein php skript gemacht, das diese umwandlung vornimmt.
habs mit 1.2 gebraucht, braucht evtl. anpassungen. kannst gerne weiterbasteln.
gruss christian
voilà
PHP-Code:
<?php
/**
* This Script transforms a Clay database-model into a xml-database-model for Propel.
*
* It was developed according to the database.dtd version 1.7.
* Default attributes will be set if no other entry is given, except implied attributes - they will not be set.
*
* I didn't work out to transform the follwing information from the clay model to propel xml:
* - multiple schemes (use only one scheme)
* - description (use the remarks field for descriptions)
* - unique keys
*
* The data-types from clay will be transformed as follows:
* - INT -> INTEGER
* - BOOL -> BOOLEAN
* - LONGTEXT -> LONGVARCHAR
*
* The following can't be modelled in Clay, you have to add it manually:
* - database elements: external schemas
* - table elements: id-method-parameter, validator, vendor
* - column elements: inheritance, vendor
* - foreign key elements: vendor
* - index-column elements: vendor
*
* Ressources:
* - Eclipse Plugin "Clay Database Modelling" http://www.azzurri.jp
* - Propel "smart, easy object persistence" http://propel.phpdb.org
*
* Requirements:
* - php 5
*
* Usage:
* - copy your clay-model and this script file in an empty folder on your php5-webserver
* - configure the parameters in the script file (first lines after the text)
* - call clay2propel.php?file=claymodel.clay (replace claymodel.clay with the name of your clay-model file)
* - you'll get an xml file called "schema.xml" saved in the directory and displayed on your screen
*
* @author Christian Abegg, abegg.ch@gmail.com
* @version 0.2
*
* @param file = filename of Clay database-model
*
*/
// configuration ------------------------------------------------------------------------------
// database attribute values
$defaultIdMethod = "native"; // (native|none) "none"
$projectName = ""; //
$defaultfile = "clayfile.clay";
$defaultPhpNamingMethod = "underscore"; // (nochange|underscore|phpname) "underscore"
$heavyIndexing = "false"; // (true|false) "false"
// file functions + initialisation -------------------------------------------------------------
// make new xml file ($out)
$imp = new DOMImplementation;
$dtd = $imp->createDocumentType('database', '', 'propel.dtd');
$out = $imp->createDocument("", "", $dtd);
$out->encoding = 'UTF-8';
$out->standalone = false;
$out->formatOutput = TRUE;
// create and append database element
$database = $out->createElement('database');
$database->setAttribute('name', $projectName);
$database->setAttribute('defaultIdMethod', $defaultIdMethod);
$database->setAttribute('defaultPhpNamingMethod', $defaultPhpNamingMethod);
$database->setAttribute('heavyIndexing', $heavyIndexing);
$out->appendChild($database);
// copy input file
if ($_REQUEST['file']) {
if (!is_file($_REQUEST['file'])) {
die("Error: no valid file given: ".$_REQUEST['file']."<br>\n
Please specify a valid file in the url. For example:
<em>clay2propel.php?file=validFile.clay</em>");
}
}
else $_REQUEST['file'] = $defaultfile;
if (!copy($_REQUEST['file'], "in.xml")) {
die("Error: can't copy input file");
}
// open given file ($in)
$in = DOMDocument :: load("in.xml");
if (!$in)
die("Error: can't load input file");
// copy data out of given file --------------------------------------------------------------------
// get element "table-list" (trav stands for traversable)
$old_table_list_trav = $in->getElementsByTagName('schema');
$i = 0;
foreach ($old_table_list_trav as $item) {
if ($i == 0)
$old_table_list = $item;
else
die("Error: Do not use more than one schemas.");
$i ++;
}
// get elements "table"
$old_table_trav = $old_table_list->getElementsByTagName("table");
foreach ($old_table_trav as $old_table) {
// create new table element & set attributes for table ----------------------
$table = $out->createElement('table');
// DTD: name CDATA #REQUIRED
if ($old_table->getAttribute('name') == '')
die("Error: No table name given.");
$table->setAttribute('name', $old_table->getAttribute('name'));
// DTD: phpName CDATA #IMPLIED
// DTD: idMethod (native|autoincrement|sequence|none|null) "null"
// DTD: skipSql (true|false) "false"
$table->setAttribute('skipSql', 'false');
// DTD: abstract (true|false) "false"
$table->setAttribute('abstract', 'false');
// DTD: baseClass CDATA #IMPLIED
// DTD: basePeer CDATA #IMPLIED
// DTD: alias CDATA #IMPLIED
$alias = $old_table->getAttribute('alias');
if ($alias != '')
$table->setAttribute('alias', $alias);
// DTD: interface CDATA #IMPLIED
// DTD: phpNamingMethod (nochange|underscore|phpname) #IMPLIED
// DTD: heavyIndexing (true|false) #IMPLIED
// DTD: description CDATA #IMPLIED
$remarks = $old_table->getAttribute('remarks'); // to keep easy, do not use description field in clay
if ($remarks != '')
$table->setAttribute('description', $remarks);
// add column elements to table ----------------------
// DTD: <!ELEMENT table (column+,(foreign-key|index|unique|id-method-parameter|validator|vendor)*)>
$old_column_trav = $old_table->getElementsByTagName("column");
foreach ($old_column_trav as $old_column) {
// create new column element & set column attributes ----------------------
$column = $out->createElement('column');
// DTD: name CDATA #REQUIRED
$column->setAttribute('name', $old_column->getAttribute('name'));
// DTD: phpName CDATA #IMPLIED
// DTD: peerName CDATA #IMPLIED
// DTD: primaryKey (true|false) "false"
foreach ($old_table->getElementsByTagName('primary-key-column') as $old_pk_column) {
if ($old_pk_column->getAttribute('name') == $old_column->getAttribute('name')) {
$column->setAttribute('primaryKey', 'true');
}
}
if ($column->getAttribute('primaryKey') != 'true')
$column->setAttribute('primaryKey', 'false');
// DTD: required (true|false) "false"
$required = $old_column->getAttribute('mandatory');
if ($required == 'true')
$column->setAttribute('required', $required);
else
$column->setAttribute('required', 'false');
// DTD: size CDATA #IMPLIED
/* DTD: type (
BIT | TINYINT | SMALLINT | INTEGER | BIGINT | FLOAT
| REAL | NUMERIC | DECIMAL | CHAR | VARCHAR | LONGVARCHAR
| DATE | TIME | TIMESTAMP | BINARY | VARBINARY | LONGVARBINARY
| NULL | OTHER | PHP_OBJECT | DISTINCT | STRUCT | ARRAY
| BLOB | CLOB | REF | BOOLEANINT | BOOLEANCHAR
| DOUBLE | BOOLEAN
) "VARCHAR" */
$possibleFields = array("BIT", "TINYINT", "SMALLINT", "INTEGER", "BIGINT", "FLOAT",
"REAL", "NUMERIC", "DECIMAL", "CHAR", "VARCHAR", "LONGVARCHAR",
"DATE", "TIME", "TIMESTAMP", "BINARY", "VARBINARY", "LONGVARBINARY",
"NULL", "OTHER", "PHP_OBJECT", "DISTINCT", "STRUCT", "ARRAY",
"BLOB", "CLOB", "REF", "BOOLEANINT", "BOOLEANCHAR",
"DOUBLE", "BOOLEAN");
$old_column_type_trav = $old_column->getElementsByTagName("data-type");
$i = 0;
foreach ($old_column_type_trav as $old_column_type) {
if ($i != 0)
die("Error: Column \"".$old_column->getAttribute('name')."\" has more than one Data-Types.");
else {
// type
$type = $old_column_type->getAttribute('name');
// size
$old_column_type_variant_trav = $old_column_type->getElementsByTagName("variant");
foreach ($old_column_type_variant_trav as $old_column_type_variant) {
$size = $old_column_type_variant->getAttribute("precision-max");
if ($size > 0)
break;
}
}
}
// type
if (array_search($type, $possibleFields))
$column->setAttribute('type', $type);
else if ($type == "")
$column->setAttribute('type', "VARCHAR"); // default value
else if ($type == "INT")
$column->setAttribute('type', "INTEGER");
else if ($type == "BOOL")
$column->setAttribute('type', "BOOLEAN");
else if ($type == "LONGTEXT")
$column->setAttribute('type', "LONGVARCHAR");
else
die ("Error: Unsupported column-type: $type. Column: <strong>".$old_column->getAttribute('name'). "</strong> / Table: <strong>".$old_table->getAttribute('name')."</strong>");
// size
if ($size != '')
$column->setAttribute('size', $size);
// DTD: phpType (object|primitive) #IMPLIED
// DTD: scale CDATA #IMPLIED
// DTD: default CDATA #IMPLIED
// DTD: autoIncrement (true|false) "false"
$autoincrement = $old_column->getAttribute('auto-increment');
if ($autoincrement == "true") {
$column->setAttribute('autoIncrement', 'true');
} else
$column->setAttribute('autoIncrement', 'false');
// DTD: inheritance (single|false) "false"
$column->setAttribute('inheritance', 'false');
// DTD: inputValidator CDATA #IMPLIED
// DTD: phpNamingMethod (nochange|underscore|phpname) #IMPLIED
// DTD: description CDATA #IMPLIED
$remarks = $old_column->getAttribute('remarks'); // to keep easy, do not use description field in clay
if ($remarks != '')
$column->setAttribute('description', $remarks);
// DTD: lazyLoad (true|false) "false"
$column->setAttribute('lazyLoad', 'false');
// set column elements ----------------------
// DTD: ((inheritance|vendor)*) -> can't be modelled in Clay
// append to parent table element ----------------------
$table->appendChild($column);
}
// set other table elements to table ----------------------
// DTD: (foreign-key|index|unique|id-method-parameter|validator|vendor)*
// add foreign-key element to table
$old_fk_trav = $old_table->getElementsByTagName("foreign-key");
foreach ($old_fk_trav as $old_fk) {
$fk = $out->createElement('foreign-key');
// set element reference to foreign-key -----------------
$reference = $out->createElement('reference');
// set attributes to reference
$old_fk_column_trav = $old_fk->getElementsByTagName('foreign-key-column');
foreach ($old_fk_column_trav as $old_fk_column) {
// DTD: local CDATA #REQUIRED
$local = $old_fk_column->getAttribute('column-name');
if ($local != "")
$reference->setAttribute('local', $local);
else
die("Error: No local reference in foreign key spcified. Table: ".$old_table->getAttribute('name')." / FK: ").$old_fk->getAttribute('name');
// DTD: foreign CDATA #REQUIRED
$foreign = $old_fk_column->getAttribute('referenced-key-column-name');
if ($foreign != "")
$reference->setAttribute('foreign', $foreign);
else
die("Error: No foreign reference in foreign key spcified. Table: ".$old_table->getAttribute('name')." / FK: ").$old_fk->getAttribute('name');
}
$fk->appendChild($reference);
// set attributes to foreign-key -----------------
// DTD: foreignTable CDATA #REQUIRED
$foreignTable = $old_fk->getAttribute("referenced-table");
if ($foreignTable != "")
$fk->setAttribute('foreignTable', $foreignTable);
else
die("Error: Foreign-Key-Table not specified in table ".$old_table->getAttribute('name'));
// DTD: name CDATA #IMPLIED
$name = $old_fk->getAttribute("name");
if ($name != "")
$fk->setAttribute('name', $name);
// DTD: onDelete (cascade|setnull|restrict|none) "none"
$possibleValues = array ("cascade", "setnull", "restrict", "none");
$onDelete = str_replace(" ", "", strtolower($old_fk->getAttribute("on-delete")));
//echo strtolower($old_fk->getAttribute("on-delete"));
if (in_array($onDelete, $possibleValues))
$fk->setAttribute('onDelete', $onDelete);
else
$fk->setAttribute('onDelete', 'none');
$table->appendChild($fk);
}
// set index element to table
$old_index_trav = $old_table->getElementsByTagName('index');
foreach($old_index_trav as $old_index) {
$index = $out->createElement('index');
// set name attribute to index
// DTD: name CDATA #IMPLIED
$name = $old_index->getAttribute('name');
if ($name != '') $index->setAttribute('name', $name);
// set index-column element to index
$old_indexcolumn_trav = $old_index->getElementsByTagName('index-column');
foreach($old_indexcolumn_trav as $old_indexcolumn) {
$indexcolumn = $out->createElement('index-column');
// DTD: <!ELEMENT index-column (vendor*)> -> can't be modelled in Clay
// DTD: name CDATA #REQUIRED
$name = $old_indexcolumn->getAttribute('name');
if ($name != "") $indexcolumn->setAttribute('name', $name);
else die ("Error: Tablename for index not specified in table ".$old_table->getAttribute('name'));
// DTD: size CDATA #IMPLIED
// let's keep the script easy...
// information have to be found in the column-list
$index->appendChild($indexcolumn);
}
}
// set unique element to table
$old_unique_trav = $old_table->getElementsByTagName('unique-key');
foreach($old_unique_trav as $old_unique) {
$unique = $out->createElement('unique');
// set name attribute to unique
// DTD: name CDATA #IMPLIED
$name = $old_unique->getAttribute('name');
if ($name != '') $unique->setAttribute('name', $name);
// set unique-column element to unique
$old_uniquecolumn_trav = $old_unique->getElementsByTagName('unique-key-column');
foreach($old_uniquecolumn_trav as $old_uniquecolumn) {
$uniquecolumn = $out->createElement('unique-column');
// DTD: <!ELEMENT unique-column (vendor*)> -> can't be modelled in Clay
// DTD: name CDATA #REQUIRED
$name = $old_uniquecolumn->getAttribute('name');
if ($name != "") $uniquecolumn->setAttribute('name', $name);
else die ("Error: Tablename for unique column not specified in table ".$old_table->getAttribute('name'));
$unique->appendChild($uniquecolumn);
}
$table->appendChild($unique);
}
// set unique element to table
// set id-method-parameter to table -> can't be modelled in clay
// set validator element to table -> can't be modelled in clay
// set vendor element to table -> can't be modelled in clay
// append table entry to database ----------------------
$database->appendChild($table);
}
// output ------------------------------------------------------------------------------------------
// save output file to filesystem
$out->save("schema.xml");
// print out xml-document
header("Content-type: application/xml");
print $out->saveXML();
?>
|
|