2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used to build NHibernate dumps of tables
6 * @package phpMyAdmin-Export-Codegen
9 if (! defined('PHPMYADMIN')) {
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
);
30 if (isset($plugin_list)) {
31 $plugin_list['codegen'] = array(
34 'mime_type' => 'text/cs',
36 array('type' => 'hidden', 'name' => 'data'),
37 array('type' => 'select', 'name' => 'format', 'text' => 'strFormat', 'values' => $CG_FORMATS),
39 'options_text' => 'strOptions',
44 * Set of functions used to build exports of tables
50 * @param string Text of comment
52 * @return bool Whether it suceeded
54 function PMA_exportComment($text)
60 * Outputs export footer
62 * @return bool Whether it suceeded
66 function PMA_exportFooter()
72 * Outputs export header
74 * @return bool Whether it suceeded
78 function PMA_exportHeader()
84 * Outputs database header
86 * @param string Database name
88 * @return bool Whether it suceeded
92 function PMA_exportDBHeader($db)
98 * Outputs database footer
100 * @param string Database name
102 * @return bool Whether it suceeded
106 function PMA_exportDBFooter($db)
112 * Outputs create database database
114 * @param string Database name
116 * @return bool Whether it suceeded
120 function PMA_exportDBCreate($db)
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
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);
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
158 public $defaultValue;
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
, "(");
173 return substr($this->type
, 0, $pos);
178 return $this->nullable
== "NO" ?
"true" : "false";
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";
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";
208 function getIndexName()
210 if (strlen($this->key
)>0)
211 return "index=\"" . $this->name
. "\"";
216 return $this->key
=="PRI";
218 function format($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);
233 function handleNHibernateCSBody($db, $table, $crlf)
236 $result=PMA_DBI_query(sprintf("DESC %s.%s", PMA_backquote($db), PMA_backquote($table)));
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);
248 $lines[] = " #region ".ucfirst($table);
249 $lines[] = " public class ".ucfirst($table);
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)."() { }";
258 foreach ($tableProperties as $tablePropertie)
259 if (! $tablePropertie->isPK())
260 $temp[] = $tablePropertie->format("#dotNetPrimitiveType# #name#");
261 $lines[] = " public ".ucfirst($table)."(".implode(", ", $temp).")";
263 foreach ($tableProperties as $tablePropertie)
264 if (! $tablePropertie->isPK())
265 $lines[] = $tablePropertie->format(" this._#name#=#name#;");
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";
273 $lines[] = " #endregion";
275 PMA_DBI_free_result($result);
277 return implode("\n", $lines);
280 function handleNHibernateXMLBody($db, $table, $crlf)
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)));
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>");
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)
309 return $GLOBALS[$what . "_" . $optionName];