Merge remote branch 'origin/master'
[phpmyadmin/dkf.git] / libraries / export / codegen.php
blobe0898934e8c8115a3ae578fc06e88aca1478a841
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-Codegen
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * This gets executed twice so avoid a notice
15 if (! defined('CG_FORMAT_NHIBERNATE_CS')) {
16 define("CG_FORMAT_NHIBERNATE_CS", "NHibernate C# DO");
17 define("CG_FORMAT_NHIBERNATE_XML", "NHibernate XML");
19 define("CG_HANDLER_NHIBERNATE_CS_BODY", "handleNHibernateCSBody");
20 define("CG_HANDLER_NHIBERNATE_XML_BODY", "handleNHibernateXMLBody");
23 $CG_FORMATS = array(CG_FORMAT_NHIBERNATE_CS, CG_FORMAT_NHIBERNATE_XML);
24 $CG_HANDLERS = array(CG_HANDLER_NHIBERNATE_CS_BODY, CG_HANDLER_NHIBERNATE_XML_BODY);
26 /**
29 if (isset($plugin_list)) {
30 $plugin_list['codegen'] = array(
31 'text' => 'CodeGen',
32 'extension' => 'cs',
33 'mime_type' => 'text/cs',
34 'options' => array(
35 array('type' => 'hidden', 'name' => 'data'),
36 array('type' => 'select', 'name' => 'format', 'text' => __('Format'), 'values' => $CG_FORMATS),
38 'options_text' => __('Options'),
40 } else {
42 /**
43 * Set of functions used to build exports of tables
46 /**
47 * Outputs comment
49 * @param string Text of comment
51 * @return bool Whether it suceeded
53 function PMA_exportComment($text)
55 return TRUE;
58 /**
59 * Outputs export footer
61 * @return bool Whether it suceeded
63 * @access public
65 function PMA_exportFooter()
67 return TRUE;
70 /**
71 * Outputs export header
73 * @return bool Whether it suceeded
75 * @access public
77 function PMA_exportHeader()
79 return TRUE;
82 /**
83 * Outputs database header
85 * @param string Database name
87 * @return bool Whether it suceeded
89 * @access public
91 function PMA_exportDBHeader($db)
93 return TRUE;
96 /**
97 * Outputs database footer
99 * @param string Database name
101 * @return bool Whether it suceeded
103 * @access public
105 function PMA_exportDBFooter($db)
107 return TRUE;
111 * Outputs create database database
113 * @param string Database name
115 * @return bool Whether it suceeded
117 * @access public
119 function PMA_exportDBCreate($db)
121 return TRUE;
125 * Outputs the content of a table in NHibernate format
127 * @param string the database name
128 * @param string the table name
129 * @param string the end of line sequence
130 * @param string the url to go back in case of error
131 * @param string SQL query for obtaining data
133 * @return bool Whether it suceeded
135 * @access public
137 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
139 global $CG_FORMATS, $CG_HANDLERS;
140 $format = cgGetOption("format");
141 $index = array_search($format, $CG_FORMATS);
142 if ($index >= 0)
143 return PMA_exportOutputHandler($CG_HANDLERS[$index]($db, $table, $crlf));
144 return PMA_exportOutputHandler(sprintf("%s is not supported.", $format));
149 * @package phpMyAdmin-Export-Codegen
151 class TableProperty
153 public $name;
154 public $type;
155 public $nullable;
156 public $key;
157 public $defaultValue;
158 public $ext;
159 function __construct($row)
161 $this->name = trim($row[0]);
162 $this->type = trim($row[1]);
163 $this->nullable = trim($row[2]);
164 $this->key = trim($row[3]);
165 $this->defaultValue = trim($row[4]);
166 $this->ext = trim($row[5]);
168 function getPureType()
170 $pos=strpos($this->type, "(");
171 if ($pos > 0)
172 return substr($this->type, 0, $pos);
173 return $this->type;
175 function isNotNull()
177 return $this->nullable == "NO" ? "true" : "false";
179 function isUnique()
181 return $this->key == "PRI" || $this->key == "UNI" ? "true" : "false";
183 function getDotNetPrimitiveType()
185 if (strpos($this->type, "int") === 0) return "int";
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 "bool";
192 if (strpos($this->type, "datetime") === 0) return "DateTime";
193 return "unknown";
195 function getDotNetObjectType()
197 if (strpos($this->type, "int") === 0) return "Int32";
198 if (strpos($this->type, "long") === 0) return "Long";
199 if (strpos($this->type, "char") === 0) return "String";
200 if (strpos($this->type, "varchar") === 0) return "String";
201 if (strpos($this->type, "text") === 0) return "String";
202 if (strpos($this->type, "longtext") === 0) return "String";
203 if (strpos($this->type, "tinyint") === 0) return "Boolean";
204 if (strpos($this->type, "datetime") === 0) return "DateTime";
205 return "Unknown";
207 function getIndexName()
209 if (strlen($this->key)>0)
210 return "index=\"" . $this->name . "\"";
211 return "";
213 function isPK()
215 return $this->key=="PRI";
217 function format($pattern)
219 $text=$pattern;
220 $text=str_replace("#name#", $this->name, $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 $text=str_replace("#ucfirstName#", ucfirst($this->name), $text);
225 $text=str_replace("#dotNetPrimitiveType#", $this->getDotNetPrimitiveType(), $text);
226 $text=str_replace("#dotNetObjectType#", $this->getDotNetObjectType(), $text);
227 $text=str_replace("#indexName#", $this->getIndexName(), $text);
228 return $text;
232 function handleNHibernateCSBody($db, $table, $crlf)
234 $lines=array();
235 $result=PMA_DBI_query(sprintf("DESC %s.%s", PMA_backquote($db), PMA_backquote($table)));
236 if ($result)
238 $tableProperties=array();
239 while ($row = PMA_DBI_fetch_row($result))
240 $tableProperties[] = new TableProperty($row);
241 $lines[] = "using System;";
242 $lines[] = "using System.Collections;";
243 $lines[] = "using System.Collections.Generic;";
244 $lines[] = "using System.Text;";
245 $lines[] = "namespace ".ucfirst($db);
246 $lines[] = "{";
247 $lines[] = " #region ".ucfirst($table);
248 $lines[] = " public class ".ucfirst($table);
249 $lines[] = " {";
250 $lines[] = " #region Member Variables";
251 foreach ($tableProperties as $tablePropertie)
252 $lines[] = $tablePropertie->format(" protected #dotNetPrimitiveType# _#name#;");
253 $lines[] = " #endregion";
254 $lines[] = " #region Constructors";
255 $lines[] = " public ".ucfirst($table)."() { }";
256 $temp = array();
257 foreach ($tableProperties as $tablePropertie)
258 if (! $tablePropertie->isPK())
259 $temp[] = $tablePropertie->format("#dotNetPrimitiveType# #name#");
260 $lines[] = " public ".ucfirst($table)."(".implode(", ", $temp).")";
261 $lines[] = " {";
262 foreach ($tableProperties as $tablePropertie)
263 if (! $tablePropertie->isPK())
264 $lines[] = $tablePropertie->format(" this._#name#=#name#;");
265 $lines[] = " }";
266 $lines[] = " #endregion";
267 $lines[] = " #region Public Properties";
268 foreach ($tableProperties as $tablePropertie)
269 $lines[] = $tablePropertie->format(" public virtual #dotNetPrimitiveType# _#ucfirstName#\n {\n get {return _#name#;}\n set {_#name#=value;}\n }");
270 $lines[] = " #endregion";
271 $lines[] = " }";
272 $lines[] = " #endregion";
273 $lines[] = "}";
274 PMA_DBI_free_result($result);
276 return implode("\n", $lines);
279 function handleNHibernateXMLBody($db, $table, $crlf)
281 $lines=array();
282 $lines[] = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
283 $lines[] = "<hibernate-mapping xmlns=\"urn:nhibernate-mapping-2.2\" namespace=\"".ucfirst($db)."\" assembly=\"".ucfirst($db)."\">";
284 $lines[] = " <class name=\"".ucfirst($table)."\" table=\"".$table."\">";
285 $result = PMA_DBI_query(sprintf("DESC %s.%s", PMA_backquote($db), PMA_backquote($table)));
286 if ($result)
288 $tableProperties = array();
289 while ($row = PMA_DBI_fetch_row($result))
290 $tableProperties[] = new TableProperty($row);
291 foreach ($tableProperties as $tablePropertie)
293 if ($tablePropertie->isPK())
294 $lines[] = $tablePropertie->format(" <id name=\"#ucfirstName#\" type=\"#dotNetObjectType#\" unsaved-value=\"0\">\n <column name=\"#name#\" sql-type=\"#type#\" not-null=\"#notNull#\" unique=\"#unique#\" index=\"PRIMARY\"/>\n <generator class=\"native\" />\n </id>");
295 else
296 $lines[] = $tablePropertie->format(" <property name=\"#ucfirstName#\" type=\"#dotNetObjectType#\">\n <column name=\"#name#\" sql-type=\"#type#\" not-null=\"#notNull#\" #indexName#/>\n </property>");
298 PMA_DBI_free_result($result);
300 $lines[]=" </class>";
301 $lines[]="</hibernate-mapping>";
302 return implode("\n", $lines);
305 function cgGetOption($optionName)
307 global $what;
308 return $GLOBALS[$what . "_" . $optionName];