<?php
/**************************************|
| dia2sql.php v.: 0.2 |
|--------------------------------------|
| Daniel Afonso Heisler |
| CPD - UNIVATES |
| hide@address.com |
|**************************************/
class dia2sql
{
var $UML = array( 0 => 'Class', // implemented
1 => 'Association', // implemented
2 => 'Generalization',
3 => 'Dependency',
4 => 'Ralizes',
5 => 'Implements',
6 => 'Note',
7 => 'Constraint',
8 => 'SmallPackage',
9 => 'LargePackage',
10 => 'Actor',
11 => 'Usecase',
12 => 'Lifeline',
14 => 'Object',
15 => 'Message',
16 => 'Component',
17 => 'Node',
18 => 'Classicon',
19 => 'State',
20 => 'Branch'
);
var $file;
var $string;
var $struct;
var $sql;
var $path = 'sql';
Function dia2sql($file)
{
$this->file = $file;
$this->Generate();
$this->ColectStruct();
}
Function Generate()
{
$fp = fopen($this->file,'r');
while (!feof($fp))
$this->string[] = fgets($fp,4096);
fclose($fp);
}
Function ColectStruct()
{
foreach ( $this->string as $s )
{
// UML types definition
$key_idobject = true ? stristr($s,'object type') : false;
if ( $key_idobject )
{
$id = substr($s,strpos($s,'id')+4,strrpos($s,'">')-strpos($s,'id')-4);
$type_struct = substr($s,strpos($s,'UML')+6,strrpos($s,'version')-strpos($s,'UML')-8);
$this->struct[$id]['Type'] = $type_struct;
$end_obj = false;
}
// Type Class
if ( $key_class )
if ( stristr($s,'object') )
$key_class = false;
if ( stristr($s,'UML - Class') || $key_class )
{
$key_class = true;
if ( $name_key && !$attributes_key)
$name = substr( $s, strpos($s,'#')+1, strrpos($s,'#')-strpos($s,'#')-1 );
$name_key = stristr($s,'name="name"') ? true : false;
if ( $name )
{
if ( stristr($s,'composite type="umlattribute"') )
$attributes_key = true;
if ( stristr($s,'composite>') )
$attributes_key = false;
if ( $attributes_key )
{
// fields
if ( $field_key )
$field = substr( $s, strpos($s,'#')+1, strrpos($s,'#')-strpos($s,'#')-1 );
$field_key = stristr($s,'name="name"') ? true : false;
// types
if ( $type_key )
{
$type = substr( $s, strpos($s,'#')+1, strrpos($s,'#')-strpos($s,'#')-1 );
// default type is text
if ( !$type )
$type = 'text';
}
$type_key = stristr($s,'name="type"') ? true : false;
// values
if ( $value_key )
{
$value = substr( $s, strpos($s,'#')+1, strrpos($s,'#')-strpos($s,'#')-1 );
if ( !$value || stristr($value,'string/>') )
$value = ' ';
}
$value_key = stristr($s,'name="value"') ? true : false;
// sequences
if ( $sequence_key )
{
$sequence = substr( $s, strpos($s,'"')+1, strrpos($s,'"')-strpos($s,'"')-1 );
}
$sequence_key = stristr($s,'name="class_scope"') ? true : false;
// type of field (primary key, refrences ... )
if ( $type_field_key )
{
$type_field = substr( $s, strpos($s,'"')+1, strrpos($s,'"')-strpos($s,'"')-1 );
$primary_key = !$primary_key ? 'false' : $primary_key;
$index = !$index ? 'false' : $index;
$index_unique = !$index_unique ? 'false' : $index_unique;
switch ( $type_field )
{
case '1' :
$index_unique = 'true';
break;
case '2' :
$primary_key = 'true';
break;
case '3' :
$index = 'true';
break;
}
}
$type_field_key = stristr($s,'name="visibility"') ? true : false;
if ( $id && $name && $field && $type && $value && $sequence )
{
$this->struct[$id][$name][$field]['type'] = $type;
$this->struct[$id][$name][$field]['value'] = $value;
$this->struct[$id][$name][$field]['sequence'] = $sequence;
$this->struct[$id][$name][$field]['primary_key'] = $primary_key;
$this->struct[$id][$name][$field]['index'] = $index;
$this->struct[$id][$name][$field]['index_unique'] = $index_unique;
unset($field,$type,$value,$sequence,$index,$primary_key,$index,$index_unique);
}
}
}
}
// Type Association
if ( $key_association )
if ( stristr($s,'object') )
$key_association = false;
if ( stristr($s,'UML - Association') || $key_association )
{
$key_association = true;
if ( $key_con )
if ( stristr($s,'connections') )
$key_con = false;
if ( $key_direction )
$direction = substr( $s, strpos($s,'val=')+5, strrpos($s,'"')-strpos($s,'val=')-5 );
$key_direction = stristr($s,'name="direction"') ? true : false;
if ( stristr($s,'connection=') || $key_con )
{
$key_con = true;
$handle = substr( $s, strpos($s,'handle')+8, 1 );
$to = substr( $s, strpos($s,'to')+4, strpos($s,'connection=')-strpos($s,'to')-6 );
$connection = substr( $s, strpos($s,'connection=')+12, strrpos($s,'"')-strpos($s,'connection=')-12 );
$this->struct[$id][$handle]['to'] = $to;
$this->struct[$id][$handle]['connection'] = $connection;
$this->struct[$id]['direction'] = $direction;
}
} // if association
} // foreach
$this->GenerateClass();
} // function
Function GenerateClass()
{
if ( ! file_exists($this->path) )
mkdir ( $this->path , 0777 );
foreach ( $this->struct as $id => $s )
{
$key_class = false;
foreach ( $s as $name => $array )
{
if ( $key_class )
{
$key_class = false;
$this->sql[$id] .= "----------------------------------------------------------------------\n" .
"-- --\n" .
"--\n" .
"-- Table: $name\n" .
"-- Purpose:\n" .
"--\n" .
"-- --\n" .
"----------------------------------------------------------------------\n\n";
foreach ( $array as $field => $types )
{
if ( $types['sequence'] == 'true' )
{
$this->sql[$id] .= "-- DROP SEQUENCE seq_{$field};\n" .
"CREATE SEQUENCE seq_{$field};\n\n";
}
}
$this->sql[$id] .= "-- DROP TABLE $name;\n" .
"CREATE TABLE $name \n" .
"(\n";
foreach ( $array as $field => $types )
{
if ( strlen($types['type']) > $count_t )
$count_t = strlen($types['type']);
if ( strlen($field) > $count_c )
$count_c = strlen($field);
}
unset($e);
$count=8;
foreach ( $array as $field => $types )
{
$this->sql[$id] .= " $field";
for ( $x=0; $x < ($count_c-(strlen($field))); $x++ )
$this->sql[$id] .= ' ';
$this->sql[$id] .= " {$types['type']}";
for ( $x=0; $x < ($count_t-(strlen($types['type']))); $x++ )
$this->sql[$id] .= ' ';
if ( $types['sequence'] == 'true' )
$default = 'default nextval(\'seq_' . $field . '\')';
elseif ( stristr($types['value'],'not') || stristr($types['value'],'null') )
$default = $types['value'];
else
$default = 'default ' . $types['value'];
if ( $types['primary_key'] == 'true' )
{
$this->sql[$id] .= " primary key\n";
for ( $x=0; $x < ($count_t+$count_c+8); $x++ )
$this->sql[$id] .= ' ';
}
$references = $this->CheckAssociation($id,$count++,$name,$field);
$count++;
$e++;
if ( $references )
{
$this->sql[$id] .= " $references,\n";
}
else
{
if ( $e >= count($array) )
{
if ( ( $types['value'] && $types['value']!=' ' ) || $types['sequence'] == 'true' )
$this->sql[$id] .= " $default\n);\n";
else
$this->sql[$id] .= " null\n);\n";
}
else
{
if ( ( $types['value'] && $types['value']!=' ' ) || $types['sequence'] == 'true' )
$this->sql[$id] .= " $default,\n";
else
$this->sql[$id] .= " null,\n";
}
}
}
unset($fil,$fil1,$name_index,$name_index1);
foreach ( $array as $field => $types )
{
if ( $types['index_unique'] == 'true' )
{
$this->sql[$id] .= "\n-- DROP INDEX {$field}_idx;\n" .
"CREATE UNIQUE INDEX {$field}_idx ON {$name}($field);\n";
$fil[] = $field;
}
if ( $types['index'] == 'true' )
{
$this->sql[$id] .= "\n-- DROP INDEX {$field}_idx;\n" .
"CREATE INDEX {$field}_idx ON {$name}($field);\n";
$fil1[] = $field;
}
}
if ( count($fil) > 1 )
{
$i=0;
foreach ( $fil as $fl )
{
if ( $i < 1 )
$string = "($fl";
elseif ( $i < count($fil) )
$string .= ",$fl";
$i++;
$name_index .= $fl . '_';
}
$this->sql[$id] .= "\n-- DROP INDEX {$name_index}idx;\n" .
"CREATE UNIQUE INDEX {$name_index}idx ON {$name}$string);\n";
}
if ( count($fil1) > 1 )
{
$i=0;
foreach ( $fil1 as $fl1 )
{
if ( $i < 1 )
$string = "($fl1";
elseif ( $i < count($fil1) )
$string .= ",$fl1";
$i++;
$name_index1 .= $fl1 . '_';
}
$this->sql[$id] .= "\n-- DROP INDEX {$name_index1}idx;\n" .
"CREATE INDEX {$name_index1}idx ON {$name}$string);\n";
}
$this->GenerateFile( $name . '.sql',$this->sql[$id]);
}
elseif ( $name == 'Type' && $array == 'Class' )
{
$key_class = true;
}
} // foreach
}
}
Function CheckAssociation($id_table,$id_field,$name_table,$name_field)
{
foreach ( $this->struct as $id => $i )
{
if ( $i['Type'] == 'Association' )
{
switch ( $i['direction'] )
{
case '1' :
if ( $i['0']['to'] == $id_table && ( $i['0']['connection'] == $id_field || $i['0']['connection'] == ($id_field+1) ) )
{
foreach ( $this->struct["{$i['1']['to']}"] as $id_ => $i_ )
{
if ( is_array($i_) )
{
$e=8;
foreach ( $i_ as $iss_ => $rrr )
{
if ( ( $e == $i['1']['connection'] ) || ( ($e+1) == $i['1']['connection'] ) )
return "references {$id_}({$iss_})";
$e+=2;
}
}
}
}
break;
case '2' :
if ( $i['1']['to'] == $id_table && ( $i['1']['connection'] == $id_field || $i['1']['connection'] == ($id_field+1) ) )
{
foreach ( $this->struct["{$i['0']['to']}"] as $id_ => $i_ )
{
if ( is_array($i_) )
{
$e=8;
foreach ( $i_ as $iss_ => $rrr )
{
if ( ( $e == $i['0']['connection'] ) || ( ($e+1) == $i['0']['connection'] ) )
return "references {$id_}({$iss_})";
$e+=2;
}
}
}
}
break;
case '' :
return null;
break;
} // switch
}
}
} // function
Function GenerateFile($file,$content)
{
$fp = fopen( $this->path . '/' . $file, 'w' );
fputs ( $fp, $content );
fclose( $fp );
}
}
?>