| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
class AkDBDesignerToAkelosDatabaseDesign |
|---|
| 19 |
{ |
|---|
| 20 |
public $_parser; |
|---|
| 21 |
public $_stack = array(); |
|---|
| 22 |
public $_errors = array(); |
|---|
| 23 |
public $db_schema = array(); |
|---|
| 24 |
public $current_table; |
|---|
| 25 |
|
|---|
| 26 |
public function AkDBDesignerToAkelosDatabaseDesign() |
|---|
| 27 |
{ |
|---|
| 28 |
$this->_parser = xml_parser_create(); |
|---|
| 29 |
xml_set_object($this->_parser, &$this); |
|---|
| 30 |
xml_set_element_handler($this->_parser, 'tagOpen', 'tagClose'); |
|---|
| 31 |
xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false); |
|---|
| 32 |
} |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
public function addError($error) |
|---|
| 36 |
{ |
|---|
| 37 |
$this->_errors[] = $error.' on line '.$this->getCurrentLine(); |
|---|
| 38 |
} |
|---|
| 39 |
|
|---|
| 40 |
public function getCurrentLine() |
|---|
| 41 |
{ |
|---|
| 42 |
return xml_get_current_line_number($this->_parser) + $this->_startLine; |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
public function hasErrors(&$xhtml) |
|---|
| 46 |
{ |
|---|
| 47 |
return count($this->getErrors()) > 0; |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
public function getErrors() |
|---|
| 51 |
{ |
|---|
| 52 |
return array_unique($this->_errors); |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
public function showErrors() |
|---|
| 56 |
{ |
|---|
| 57 |
echo '<ul><li>'.join("</li>\n<li>", $this->getErrors()).'</li></ul>'; |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
public function convert() |
|---|
| 61 |
{ |
|---|
| 62 |
if (!xml_parse($this->_parser, $this->source)) { |
|---|
| 63 |
$this->addError(Ak::t('DBDesigner file is not well-formed.').' '.xml_error_string(xml_get_error_code($this->_parser))); |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
foreach ($this->db_schema as $table=>$create_text){ |
|---|
| 67 |
$this->db_schema[$table] = rtrim($create_text,", \n"); |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
return $this->db_schema; |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
public function tagOpen($parser, $tag, $attributes) |
|---|
| 75 |
{ |
|---|
| 76 |
if(!empty($attributes['Tablename'])){ |
|---|
| 77 |
$this->current_table = $attributes['Tablename']; |
|---|
| 78 |
} |
|---|
| 79 |
if(!empty($attributes['ColName']) && !empty($this->current_table)){ |
|---|
| 80 |
$this->db_schema[$this->current_table] = empty($this->db_schema[$this->current_table]) ? '' : $this->db_schema[$this->current_table]; |
|---|
| 81 |
$this->db_schema[$this->current_table] .= |
|---|
| 82 |
$attributes['ColName'].' '. |
|---|
| 83 |
$this->getDataType($attributes['idDatatype']).$attributes['DatatypeParams']. |
|---|
| 84 |
(empty($attributes['PrimaryKey']) ? '' : ' primary'). |
|---|
| 85 |
(empty($attributes['NotNull']) ? '' : ' not null'). |
|---|
| 86 |
(empty($attributes['AutoInc']) ? '' : ' auto increment'). |
|---|
| 87 |
(empty($attributes['DefaultValue']) ? '' : ' default='.$attributes['DefaultValue']).",\n"; |
|---|
| 88 |
} |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
public function getDataType($type) |
|---|
| 92 |
{ |
|---|
| 93 |
(int)$type = $type; |
|---|
| 94 |
$dbdesigner_data_types = array( |
|---|
| 95 |
1 => 'integer', |
|---|
| 96 |
2 => 'integer', |
|---|
| 97 |
3 => 'integer', |
|---|
| 98 |
4 => 'integer', |
|---|
| 99 |
5 => 'integer', |
|---|
| 100 |
6 => 'integer', |
|---|
| 101 |
7 => 'float', |
|---|
| 102 |
8 => 'float', |
|---|
| 103 |
9 => 'float', |
|---|
| 104 |
10 => 'float', |
|---|
| 105 |
11 => 'float', |
|---|
| 106 |
12 => 'float', |
|---|
| 107 |
13 => 'float', |
|---|
| 108 |
|
|---|
| 109 |
14 => 'date', |
|---|
| 110 |
15 => 'datetime', |
|---|
| 111 |
16 => 'timestamp', |
|---|
| 112 |
17 => 'time', |
|---|
| 113 |
18 => 'integer', |
|---|
| 114 |
|
|---|
| 115 |
19 => 'string', |
|---|
| 116 |
20 => 'string', |
|---|
| 117 |
|
|---|
| 118 |
21 => 'boolean', |
|---|
| 119 |
22 => 'boolean', |
|---|
| 120 |
|
|---|
| 121 |
23 => 'binary', |
|---|
| 122 |
24 => 'binary', |
|---|
| 123 |
25 => 'binary', |
|---|
| 124 |
26 => 'binary', |
|---|
| 125 |
|
|---|
| 126 |
27 => 'text', |
|---|
| 127 |
28 => 'text', |
|---|
| 128 |
29 => 'text', |
|---|
| 129 |
30 => 'text'); |
|---|
| 130 |
|
|---|
| 131 |
return empty($dbdesigner_data_types[$type]) ? 'string' : $dbdesigner_data_types[$type]; |
|---|
| 132 |
|
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
public function tagClose($parser, $tag) |
|---|
| 136 |
{ |
|---|
| 137 |
} |
|---|
| 138 |
} |
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
?> |
|---|
| 142 |
|
|---|