Fixed: Not selecting a datalabel used to issue a notice(undefined offset)
[phpmyadmin/ammaryasirr.git] / libraries / export / codegen.php
blobb8b35d5a1ce2b203c0bbad81365bbe626cab7ac0
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build NHibernate dumps of tables
6 * @package phpMyAdmin-Export
7 * @subpackage Codegen
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
14 * This gets executed twice so avoid a notice
16 if (! defined('CG_FORMAT_NHIBERNATE_CS')) {
17 define("CG_FORMAT_NHIBERNATE_CS", "NHibernate C# DO");
18 define("CG_FORMAT_NHIBERNATE_XML", "NHibernate XML");
20 define("CG_HANDLER_NHIBERNATE_CS_BODY", "handleNHibernateCSBody");
21 define("CG_HANDLER_NHIBERNATE_XML_BODY", "handleNHibernateXMLBody");
24 $CG_FORMATS = array(CG_FORMAT_NHIBERNATE_CS, CG_FORMAT_NHIBERNATE_XML);
25 $CG_HANDLERS = array(CG_HANDLER_NHIBERNATE_CS_BODY, CG_HANDLER_NHIBERNATE_XML_BODY);
27 /**
30 if (isset($plugin_list)) {
31 $plugin_list['codegen'] = array(
32 'text' => 'CodeGen',
33 'extension' => 'cs',
34 'mime_type' => 'text/cs',
35 'options' => array(
36 array('type' => 'begin_group', 'name' => 'general_opts'),
37 array('type' => 'hidden', 'name' => 'structure_or_data'),
38 array('type' => 'select', 'name' => 'format', 'text' => __('Format:'), 'values' => $CG_FORMATS),
39 array('type' => 'end_group')
41 'options_text' => __('Options'),
43 } else {
45 /**
46 * Set of functions used to build exports of tables
49 /**
50 * Outputs export footer
52 * @return bool Whether it suceeded
54 * @access public
56 function PMA_exportFooter()
58 return true;
61 /**
62 * Outputs export header
64 * @return bool Whether it suceeded
66 * @access public
68 function PMA_exportHeader()
70 return true;
73 /**
74 * Outputs database header
76 * @param string $db Database name
77 * @return bool Whether it suceeded
79 * @access public
81 function PMA_exportDBHeader($db)
83 return true;
86 /**
87 * Outputs database footer
89 * @param string $db Database name
90 * @return bool Whether it suceeded
92 * @access public
94 function PMA_exportDBFooter($db)
96 return true;
99 /**
100 * Outputs CREATE DATABASE statement
102 * @param string $db Database name
103 * @return bool Whether it suceeded
105 * @access public
107 function PMA_exportDBCreate($db)
109 return true;
113 * Outputs the content of a table in NHibernate format
115 * @param string $db database name
116 * @param string $table table name
117 * @param string $crlf the end of line sequence
118 * @param string $error_url the url to go back in case of error
119 * @param string $sql_query SQL query for obtaining data
120 * @return bool Whether it suceeded
122 * @access public
124 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
126 global $CG_FORMATS, $CG_HANDLERS, $what;
127 $format = $GLOBALS[$what . '_format'];
128 if (isset($CG_FORMATS[$format])) {
129 return PMA_exportOutputHandler($CG_HANDLERS[$format]($db, $table, $crlf));
131 return PMA_exportOutputHandler(sprintf("%s is not supported.", $format));
136 * @package phpMyAdmin-Export
137 * @subpackage Codegen
139 class TableProperty
141 public $name;
142 public $type;
143 public $nullable;
144 public $key;
145 public $defaultValue;
146 public $ext;
147 function __construct($row)
149 $this->name = trim($row[0]);
150 $this->type = trim($row[1]);
151 $this->nullable = trim($row[2]);
152 $this->key = trim($row[3]);
153 $this->defaultValue = trim($row[4]);
154 $this->ext = trim($row[5]);
156 function getPureType()
158 $pos=strpos($this->type, "(");
159 if ($pos > 0)
160 return substr($this->type, 0, $pos);
161 return $this->type;
163 function isNotNull()
165 return $this->nullable == "NO" ? "true" : "false";
167 function isUnique()
169 return $this->key == "PRI" || $this->key == "UNI" ? "true" : "false";
171 function getDotNetPrimitiveType()
173 if (strpos($this->type, "int") === 0) return "int";
174 if (strpos($this->type, "long") === 0) return "long";
175 if (strpos($this->type, "char") === 0) return "string";
176 if (strpos($this->type, "varchar") === 0) return "string";
177 if (strpos($this->type, "text") === 0) return "string";
178 if (strpos($this->type, "longtext") === 0) return "string";
179 if (strpos($this->type, "tinyint") === 0) return "bool";
180 if (strpos($this->type, "datetime") === 0) return "DateTime";
181 return "unknown";
183 function getDotNetObjectType()
185 if (strpos($this->type, "int") === 0) return "Int32";
186 if (strpos($this->type, "long") === 0) return "Long";
187 if (strpos($this->type, "char") === 0) return "String";
188 if (strpos($this->type, "varchar") === 0) return "String";
189 if (strpos($this->type, "text") === 0) return "String";
190 if (strpos($this->type, "longtext") === 0) return "String";
191 if (strpos($this->type, "tinyint") === 0) return "Boolean";
192 if (strpos($this->type, "datetime") === 0) return "DateTime";
193 return "Unknown";
195 function getIndexName()
197 if (strlen($this->key)>0)
198 return "index=\"" . htmlspecialchars($this->name, ENT_COMPAT, 'UTF-8') . "\"";
199 return "";
201 function isPK()
203 return $this->key=="PRI";
205 function formatCs($text)
207 $text=str_replace("#name#", cgMakeIdentifier($this->name, false), $text);
208 return $this->format($text);
210 function formatXml($text)
212 $text=str_replace("#name#", htmlspecialchars($this->name, ENT_COMPAT, 'UTF-8'), $text);
213 $text=str_replace("#indexName#", $this->getIndexName(), $text);
214 return $this->format($text);
216 function format($text)
218 $text=str_replace("#ucfirstName#", cgMakeIdentifier($this->name), $text);
219 $text=str_replace("#dotNetPrimitiveType#", $this->getDotNetPrimitiveType(), $text);
220 $text=str_replace("#dotNetObjectType#", $this->getDotNetObjectType(), $text);
221 $text=str_replace("#type#", $this->getPureType(), $text);
222 $text=str_replace("#notNull#", $this->isNotNull(), $text);
223 $text=str_replace("#unique#", $this->isUnique(), $text);
224 return $text;
228 function cgMakeIdentifier($str, $ucfirst = true)
230 // remove unsafe characters
231 $str = preg_replace('/[^\p{L}\p{Nl}_]/u', '', $str);
232 // make sure first character is a letter or _
233 if (!preg_match('/^\pL/u', $str)) {
234 $str = '_' . $str;
236 if ($ucfirst) {
237 $str = ucfirst($str);
239 return $str;
242 function handleNHibernateCSBody($db, $table, $crlf)
244 $lines=array();
245 $result=PMA_DBI_query(sprintf('DESC %s.%s', PMA_backquote($db), PMA_backquote($table)));
246 if ($result) {
247 $tableProperties=array();
248 while ($row = PMA_DBI_fetch_row($result)) {
249 $tableProperties[] = new TableProperty($row);
251 PMA_DBI_free_result($result);
252 $lines[] = 'using System;';
253 $lines[] = 'using System.Collections;';
254 $lines[] = 'using System.Collections.Generic;';
255 $lines[] = 'using System.Text;';
256 $lines[] = 'namespace ' . cgMakeIdentifier($db);
257 $lines[] = '{';
258 $lines[] = ' #region ' . cgMakeIdentifier($table);
259 $lines[] = ' public class ' . cgMakeIdentifier($table);
260 $lines[] = ' {';
261 $lines[] = ' #region Member Variables';
262 foreach ($tableProperties as $tablePropertie) {
263 $lines[] = $tablePropertie->formatCs(' protected #dotNetPrimitiveType# _#name#;');
265 $lines[] = ' #endregion';
266 $lines[] = ' #region Constructors';
267 $lines[] = ' public ' . cgMakeIdentifier($table).'() { }';
268 $temp = array();
269 foreach ($tableProperties as $tablePropertie) {
270 if (! $tablePropertie->isPK()) {
271 $temp[] = $tablePropertie->formatCs('#dotNetPrimitiveType# #name#');
274 $lines[] = ' public ' . cgMakeIdentifier($table) . '(' . implode(', ', $temp) . ')';
275 $lines[] = ' {';
276 foreach ($tableProperties as $tablePropertie) {
277 if (! $tablePropertie->isPK()) {
278 $lines[] = $tablePropertie->formatCs(' this._#name#=#name#;');
281 $lines[] = ' }';
282 $lines[] = ' #endregion';
283 $lines[] = ' #region Public Properties';
284 foreach ($tableProperties as $tablePropertie) {
285 $lines[] = $tablePropertie->formatCs(''
286 . ' public virtual #dotNetPrimitiveType# #ucfirstName#' . "\n"
287 . ' {' . "\n"
288 . ' get {return _#name#;}' . "\n"
289 . ' set {_#name#=value;}' . "\n"
290 . ' }'
293 $lines[] = ' #endregion';
294 $lines[] = ' }';
295 $lines[] = ' #endregion';
296 $lines[] = '}';
298 return implode("\n", $lines);
301 function handleNHibernateXMLBody($db, $table, $crlf)
303 $lines = array();
304 $lines[] = '<?xml version="1.0" encoding="utf-8" ?' . '>';
305 $lines[] = '<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" '
306 . 'namespace="' . cgMakeIdentifier($db) . '" '
307 . 'assembly="' . cgMakeIdentifier($db) . '">';
308 $lines[] = ' <class '
309 . 'name="' . cgMakeIdentifier($table) . '" '
310 . 'table="' . cgMakeIdentifier($table) . '">';
311 $result = PMA_DBI_query(sprintf("DESC %s.%s", PMA_backquote($db), PMA_backquote($table)));
312 if ($result) {
313 while ($row = PMA_DBI_fetch_row($result)) {
314 $tablePropertie = new TableProperty($row);
315 if ($tablePropertie->isPK())
316 $lines[] = $tablePropertie->formatXml(''
317 . ' <id name="#ucfirstName#" type="#dotNetObjectType#" unsaved-value="0">' . "\n"
318 . ' <column name="#name#" sql-type="#type#" not-null="#notNull#" unique="#unique#" index="PRIMARY"/>' . "\n"
319 . ' <generator class="native" />' . "\n"
320 . ' </id>');
321 else
322 $lines[] = $tablePropertie->formatXml(''
323 . ' <property name="#ucfirstName#" type="#dotNetObjectType#">' . "\n"
324 . ' <column name="#name#" sql-type="#type#" not-null="#notNull#" #indexName#/>' . "\n"
325 . ' </property>');
327 PMA_DBI_free_result($result);
329 $lines[] = ' </class>';
330 $lines[] = '</hibernate-mapping>';
331 return implode("\n", $lines);