2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used to build NHibernate dumps of tables
8 if (! defined('PHPMYADMIN')) {
12 // this gets executed twice so avoid a notice
13 if (! defined('CG_FORMAT_NHIBERNATE_CS')) {
14 define("CG_FORMAT_NHIBERNATE_CS", "NHibernate C# DO");
15 define("CG_FORMAT_NHIBERNATE_XML", "NHibernate XML");
17 define("CG_HANDLER_NHIBERNATE_CS_BODY", "handleNHibernateCSBody");
18 define("CG_HANDLER_NHIBERNATE_XML_BODY", "handleNHibernateXMLBody");
21 $CG_FORMATS = array(CG_FORMAT_NHIBERNATE_CS
, CG_FORMAT_NHIBERNATE_XML
);
22 $CG_HANDLERS = array(CG_HANDLER_NHIBERNATE_CS_BODY
, CG_HANDLER_NHIBERNATE_XML_BODY
);
27 if (isset($plugin_list)) {
28 $plugin_list['codegen'] = array(
31 'mime_type' => 'text/cs',
33 array('type' => 'hidden', 'name' => 'data'),
34 array('type' => 'select', 'name' => 'format', 'text' => 'strFormat', 'values' => $CG_FORMATS),
36 'options_text' => 'strOptions',
41 * Set of functions used to build exports of tables
47 * @param string Text of comment
49 * @return bool Whether it suceeded
51 function PMA_exportComment($text)
57 * Outputs export footer
59 * @return bool Whether it suceeded
63 function PMA_exportFooter()
69 * Outputs export header
71 * @return bool Whether it suceeded
75 function PMA_exportHeader()
81 * Outputs database header
83 * @param string Database name
85 * @return bool Whether it suceeded
89 function PMA_exportDBHeader($db)
95 * Outputs database footer
97 * @param string Database name
99 * @return bool Whether it suceeded
103 function PMA_exportDBFooter($db)
109 * Outputs create database database
111 * @param string Database name
113 * @return bool Whether it suceeded
117 function PMA_exportDBCreate($db)
123 * Outputs the content of a table in NHibernate format
125 * @param string the database name
126 * @param string the table name
127 * @param string the end of line sequence
128 * @param string the url to go back in case of error
129 * @param string SQL query for obtaining data
131 * @return bool Whether it suceeded
135 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
137 global $CG_FORMATS, $CG_HANDLERS;
138 $format = cgGetOption("format");
139 $index = array_search($format, $CG_FORMATS);
141 return PMA_exportOutputHandler($CG_HANDLERS[$index]($db, $table, $crlf));
142 return PMA_exportOutputHandler(sprintf("%s is not supported.", $format));
151 public $defaultValue;
153 function __construct($row)
155 $this->name
= trim($row[0]);
156 $this->type
= trim($row[1]);
157 $this->nullable
= trim($row[2]);
158 $this->key
= trim($row[3]);
159 $this->defaultValue
= trim($row[4]);
160 $this->ext
= trim($row[5]);
162 function getPureType()
164 $pos=strpos($this->type
, "(");
166 return substr($this->type
, 0, $pos);
171 return $this->nullable
== "NO" ?
"true" : "false";
175 return $this->key
== "PRI" ||
$this->key
== "UNI" ?
"true" : "false";
177 function getDotNetPrimitiveType()
179 if (strpos($this->type
, "int") === 0) return "int";
180 if (strpos($this->type
, "long") === 0) return "long";
181 if (strpos($this->type
, "char") === 0) return "string";
182 if (strpos($this->type
, "varchar") === 0) return "string";
183 if (strpos($this->type
, "text") === 0) return "string";
184 if (strpos($this->type
, "longtext") === 0) return "string";
185 if (strpos($this->type
, "tinyint") === 0) return "bool";
186 if (strpos($this->type
, "datetime") === 0) return "DateTime";
189 function getDotNetObjectType()
191 if (strpos($this->type
, "int") === 0) return "Int32";
192 if (strpos($this->type
, "long") === 0) return "Long";
193 if (strpos($this->type
, "char") === 0) return "String";
194 if (strpos($this->type
, "varchar") === 0) return "String";
195 if (strpos($this->type
, "text") === 0) return "String";
196 if (strpos($this->type
, "longtext") === 0) return "String";
197 if (strpos($this->type
, "tinyint") === 0) return "Boolean";
198 if (strpos($this->type
, "datetime") === 0) return "DateTime";
201 function getIndexName()
203 if (strlen($this->key
)>0)
204 return "index=\"" . $this->name
. "\"";
209 return $this->key
=="PRI";
211 function format($pattern)
214 $text=str_replace("#name#", $this->name
, $text);
215 $text=str_replace("#type#", $this->getPureType(), $text);
216 $text=str_replace("#notNull#", $this->isNotNull(), $text);
217 $text=str_replace("#unique#", $this->isUnique(), $text);
218 $text=str_replace("#ucfirstName#", ucfirst($this->name
), $text);
219 $text=str_replace("#dotNetPrimitiveType#", $this->getDotNetPrimitiveType(), $text);
220 $text=str_replace("#dotNetObjectType#", $this->getDotNetObjectType(), $text);
221 $text=str_replace("#indexName#", $this->getIndexName(), $text);
226 function handleNHibernateCSBody($db, $table, $crlf)
229 $result=PMA_DBI_query(sprintf("DESC %s.%s", PMA_backquote($db), PMA_backquote($table)));
232 $tableProperties=array();
233 while ($row = PMA_DBI_fetch_row($result))
234 $tableProperties[] = new TableProperty($row);
235 $lines[] = "using System;";
236 $lines[] = "using System.Collections;";
237 $lines[] = "using System.Collections.Generic;";
238 $lines[] = "using System.Text;";
239 $lines[] = "namespace ".ucfirst($db);
241 $lines[] = " #region ".ucfirst($table);
242 $lines[] = " public class ".ucfirst($table);
244 $lines[] = " #region Member Variables";
245 foreach ($tableProperties as $tablePropertie)
246 $lines[] = $tablePropertie->format(" protected #dotNetPrimitiveType# _#name#;");
247 $lines[] = " #endregion";
248 $lines[] = " #region Constructors";
249 $lines[] = " public ".ucfirst($table)."() { }";
251 foreach ($tableProperties as $tablePropertie)
252 if (! $tablePropertie->isPK())
253 $temp[] = $tablePropertie->format("#dotNetPrimitiveType# #name#");
254 $lines[] = " public ".ucfirst($table)."(".implode(", ", $temp).")";
256 foreach ($tableProperties as $tablePropertie)
257 if (! $tablePropertie->isPK())
258 $lines[] = $tablePropertie->format(" this._#name#=#name#;");
260 $lines[] = " #endregion";
261 $lines[] = " #region Public Properties";
262 foreach ($tableProperties as $tablePropertie)
263 $lines[] = $tablePropertie->format(" public virtual #dotNetPrimitiveType# _#ucfirstName#\n {\n get {return _#name#;}\n set {_#name#=value;}\n }");
264 $lines[] = " #endregion";
266 $lines[] = " #endregion";
268 PMA_DBI_free_result($result);
270 return implode("\n", $lines);
273 function handleNHibernateXMLBody($db, $table, $crlf)
276 $lines[] = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
277 $lines[] = "<hibernate-mapping xmlns=\"urn:nhibernate-mapping-2.2\" namespace=\"".ucfirst($db)."\" assembly=\"".ucfirst($db)."\">";
278 $lines[] = " <class name=\"".ucfirst($table)."\" table=\"".$table."\">";
279 $result = PMA_DBI_query(sprintf("DESC %s.%s", PMA_backquote($db), PMA_backquote($table)));
282 $tableProperties = array();
283 while ($row = PMA_DBI_fetch_row($result))
284 $tableProperties[] = new TableProperty($row);
285 foreach ($tableProperties as $tablePropertie)
287 if ($tablePropertie->isPK())
288 $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>");
290 $lines[] = $tablePropertie->format(" <property name=\"#ucfirstName#\" type=\"#dotNetObjectType#\">\n <column name=\"#name#\" sql-type=\"#type#\" not-null=\"#notNull#\" #indexName#/>\n </property>");
292 PMA_DBI_free_result($result);
294 $lines[]=" </class>";
295 $lines[]="</hibernate-mapping>";
296 return implode("\n", $lines);
299 function cgGetOption($optionName)
302 return $GLOBALS[$what . "_" . $optionName];