3.3.2-rc1
[phpmyadmin/madhuracj.git] / libraries / export / codegen.php
blobe234f78a9473a6d9b228ac4f097bc567b0bee5cf
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 * @version $Id$
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' => 'hidden', 'name' => 'data'),
37 array('type' => 'select', 'name' => 'format', 'text' => 'strFormat', 'values' => $CG_FORMATS),
39 'options_text' => 'strOptions',
41 } else {
43 /**
44 * Set of functions used to build exports of tables
47 /**
48 * Outputs comment
50 * @param string Text of comment
52 * @return bool Whether it suceeded
54 function PMA_exportComment($text)
56 return TRUE;
59 /**
60 * Outputs export footer
62 * @return bool Whether it suceeded
64 * @access public
66 function PMA_exportFooter()
68 return TRUE;
71 /**
72 * Outputs export header
74 * @return bool Whether it suceeded
76 * @access public
78 function PMA_exportHeader()
80 return TRUE;
83 /**
84 * Outputs database header
86 * @param string Database name
88 * @return bool Whether it suceeded
90 * @access public
92 function PMA_exportDBHeader($db)
94 return TRUE;
97 /**
98 * Outputs database footer
100 * @param string Database name
102 * @return bool Whether it suceeded
104 * @access public
106 function PMA_exportDBFooter($db)
108 return TRUE;
112 * Outputs create database database
114 * @param string Database name
116 * @return bool Whether it suceeded
118 * @access public
120 function PMA_exportDBCreate($db)
122 return TRUE;
126 * Outputs the content of a table in NHibernate format
128 * @param string the database name
129 * @param string the table name
130 * @param string the end of line sequence
131 * @param string the url to go back in case of error
132 * @param string SQL query for obtaining data
134 * @return bool Whether it suceeded
136 * @access public
138 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
140 global $CG_FORMATS, $CG_HANDLERS;
141 $format = cgGetOption("format");
142 $index = array_search($format, $CG_FORMATS);
143 if ($index >= 0)
144 return PMA_exportOutputHandler($CG_HANDLERS[$index]($db, $table, $crlf));
145 return PMA_exportOutputHandler(sprintf("%s is not supported.", $format));
150 * @package phpMyAdmin-Export-Codegen
152 class TableProperty
154 public $name;
155 public $type;
156 public $nullable;
157 public $key;
158 public $defaultValue;
159 public $ext;
160 function __construct($row)
162 $this->name = trim($row[0]);
163 $this->type = trim($row[1]);
164 $this->nullable = trim($row[2]);
165 $this->key = trim($row[3]);
166 $this->defaultValue = trim($row[4]);
167 $this->ext = trim($row[5]);
169 function getPureType()
171 $pos=strpos($this->type, "(");
172 if ($pos > 0)
173 return substr($this->type, 0, $pos);
174 return $this->type;
176 function isNotNull()
178 return $this->nullable == "NO" ? "true" : "false";
180 function isUnique()
182 return $this->key == "PRI" || $this->key == "UNI" ? "true" : "false";
184 function getDotNetPrimitiveType()
186 if (strpos($this->type, "int") === 0) return "int";
187 if (strpos($this->type, "long") === 0) return "long";
188 if (strpos($this->type, "char") === 0) return "string";
189 if (strpos($this->type, "varchar") === 0) return "string";
190 if (strpos($this->type, "text") === 0) return "string";
191 if (strpos($this->type, "longtext") === 0) return "string";
192 if (strpos($this->type, "tinyint") === 0) return "bool";
193 if (strpos($this->type, "datetime") === 0) return "DateTime";
194 return "unknown";
196 function getDotNetObjectType()
198 if (strpos($this->type, "int") === 0) return "Int32";
199 if (strpos($this->type, "long") === 0) return "Long";
200 if (strpos($this->type, "char") === 0) return "String";
201 if (strpos($this->type, "varchar") === 0) return "String";
202 if (strpos($this->type, "text") === 0) return "String";
203 if (strpos($this->type, "longtext") === 0) return "String";
204 if (strpos($this->type, "tinyint") === 0) return "Boolean";
205 if (strpos($this->type, "datetime") === 0) return "DateTime";
206 return "Unknown";
208 function getIndexName()
210 if (strlen($this->key)>0)
211 return "index=\"" . $this->name . "\"";
212 return "";
214 function isPK()
216 return $this->key=="PRI";
218 function format($pattern)
220 $text=$pattern;
221 $text=str_replace("#name#", $this->name, $text);
222 $text=str_replace("#type#", $this->getPureType(), $text);
223 $text=str_replace("#notNull#", $this->isNotNull(), $text);
224 $text=str_replace("#unique#", $this->isUnique(), $text);
225 $text=str_replace("#ucfirstName#", ucfirst($this->name), $text);
226 $text=str_replace("#dotNetPrimitiveType#", $this->getDotNetPrimitiveType(), $text);
227 $text=str_replace("#dotNetObjectType#", $this->getDotNetObjectType(), $text);
228 $text=str_replace("#indexName#", $this->getIndexName(), $text);
229 return $text;
233 function handleNHibernateCSBody($db, $table, $crlf)
235 $lines=array();
236 $result=PMA_DBI_query(sprintf("DESC %s.%s", PMA_backquote($db), PMA_backquote($table)));
237 if ($result)
239 $tableProperties=array();
240 while ($row = PMA_DBI_fetch_row($result))
241 $tableProperties[] = new TableProperty($row);
242 $lines[] = "using System;";
243 $lines[] = "using System.Collections;";
244 $lines[] = "using System.Collections.Generic;";
245 $lines[] = "using System.Text;";
246 $lines[] = "namespace ".ucfirst($db);
247 $lines[] = "{";
248 $lines[] = " #region ".ucfirst($table);
249 $lines[] = " public class ".ucfirst($table);
250 $lines[] = " {";
251 $lines[] = " #region Member Variables";
252 foreach ($tableProperties as $tablePropertie)
253 $lines[] = $tablePropertie->format(" protected #dotNetPrimitiveType# _#name#;");
254 $lines[] = " #endregion";
255 $lines[] = " #region Constructors";
256 $lines[] = " public ".ucfirst($table)."() { }";
257 $temp = array();
258 foreach ($tableProperties as $tablePropertie)
259 if (! $tablePropertie->isPK())
260 $temp[] = $tablePropertie->format("#dotNetPrimitiveType# #name#");
261 $lines[] = " public ".ucfirst($table)."(".implode(", ", $temp).")";
262 $lines[] = " {";
263 foreach ($tableProperties as $tablePropertie)
264 if (! $tablePropertie->isPK())
265 $lines[] = $tablePropertie->format(" this._#name#=#name#;");
266 $lines[] = " }";
267 $lines[] = " #endregion";
268 $lines[] = " #region Public Properties";
269 foreach ($tableProperties as $tablePropertie)
270 $lines[] = $tablePropertie->format(" public virtual #dotNetPrimitiveType# _#ucfirstName#\n {\n get {return _#name#;}\n set {_#name#=value;}\n }");
271 $lines[] = " #endregion";
272 $lines[] = " }";
273 $lines[] = " #endregion";
274 $lines[] = "}";
275 PMA_DBI_free_result($result);
277 return implode("\n", $lines);
280 function handleNHibernateXMLBody($db, $table, $crlf)
282 $lines=array();
283 $lines[] = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
284 $lines[] = "<hibernate-mapping xmlns=\"urn:nhibernate-mapping-2.2\" namespace=\"".ucfirst($db)."\" assembly=\"".ucfirst($db)."\">";
285 $lines[] = " <class name=\"".ucfirst($table)."\" table=\"".$table."\">";
286 $result = PMA_DBI_query(sprintf("DESC %s.%s", PMA_backquote($db), PMA_backquote($table)));
287 if ($result)
289 $tableProperties = array();
290 while ($row = PMA_DBI_fetch_row($result))
291 $tableProperties[] = new TableProperty($row);
292 foreach ($tableProperties as $tablePropertie)
294 if ($tablePropertie->isPK())
295 $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>");
296 else
297 $lines[] = $tablePropertie->format(" <property name=\"#ucfirstName#\" type=\"#dotNetObjectType#\">\n <column name=\"#name#\" sql-type=\"#type#\" not-null=\"#notNull#\" #indexName#/>\n </property>");
299 PMA_DBI_free_result($result);
301 $lines[]=" </class>";
302 $lines[]="</hibernate-mapping>";
303 return implode("\n", $lines);
306 function cgGetOption($optionName)
308 global $what;
309 return $GLOBALS[$what . "_" . $optionName];