Fixes #149
[akelos.git] / lib / AkConverters / AkDBDesignerToAkelosDatabaseDesign.php
blob8cf6120337a64fbdd7b3f484594224e9e001de52
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 // +----------------------------------------------------------------------+
5 // | Akelos Framework - http://www.akelos.org |
6 // +----------------------------------------------------------------------+
7 // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
8 // | Released under the GNU Lesser General Public License, see LICENSE.txt|
9 // +----------------------------------------------------------------------+
11 /**
12 * @package ActiveSupport
13 * @subpackage Converters
14 * @author Bermi Ferrer <bermi a.t akelos c.om>
15 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
16 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
18 class AkDBDesignerToAkelosDatabaseDesign
20 var $_parser;
21 var $_stack = array();
22 var $_errors = array();
23 var $db_schema = array();
24 var $current_table;
26 function AkDBDesignerToAkelosDatabaseDesign()
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);
35 function addError($error)
37 $this->_errors[] = $error.' on line '.$this->getCurrentLine();
40 function getCurrentLine()
42 return xml_get_current_line_number($this->_parser) + $this->_startLine;
45 function hasErrors(&$xhtml)
47 return count($this->getErrors()) > 0;
50 function getErrors()
52 return array_unique($this->_errors);
55 function showErrors()
57 echo '<ul><li>'.join("</li>\n<li>", $this->getErrors()).'</li></ul>';
60 function convert()
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)));
66 foreach ($this->db_schema as $table=>$create_text){
67 $this->db_schema[$table] = rtrim($create_text,", \n");
70 return $this->db_schema;
74 function tagOpen($parser, $tag, $attributes)
76 if(!empty($attributes['Tablename'])){
77 $this->current_table = $attributes['Tablename'];
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";
91 function getDataType($type)
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',
109 14 => 'date',
110 15 => 'datetime',
111 16 => 'timestamp',
112 17 => 'time',
113 18 => 'integer',
115 19 => 'string',
116 20 => 'string',
118 21 => 'boolean',
119 22 => 'boolean',
121 23 => 'binary',
122 24 => 'binary',
123 25 => 'binary',
124 26 => 'binary',
126 27 => 'text',
127 28 => 'text',
128 29 => 'text',
129 30 => 'text');
131 return empty($dbdesigner_data_types[$type]) ? 'string' : $dbdesigner_data_types[$type];
135 function tagClose($parser, $tag)