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
8 if (! defined('PHPMYADMIN')) {
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
);
29 if (isset($plugin_list)) {
30 $plugin_list['codegen'] = array(
33 'mime_type' => 'text/cs',
35 array('type' => 'hidden', 'name' => 'data'),
36 array('type' => 'select', 'name' => 'format', 'text' => __('Format'), 'values' => $CG_FORMATS),
38 'options_text' => __('Options'),
43 * Set of functions used to build exports of tables
49 * @param string Text of comment
51 * @return bool Whether it suceeded
53 function PMA_exportComment($text)
59 * Outputs export footer
61 * @return bool Whether it suceeded
65 function PMA_exportFooter()
71 * Outputs export header
73 * @return bool Whether it suceeded
77 function PMA_exportHeader()
83 * Outputs database header
85 * @param string Database name
87 * @return bool Whether it suceeded
91 function PMA_exportDBHeader($db)
97 * Outputs database footer
99 * @param string Database name
101 * @return bool Whether it suceeded
105 function PMA_exportDBFooter($db)
111 * Outputs create database database
113 * @param string Database name
115 * @return bool Whether it suceeded
119 function PMA_exportDBCreate($db)
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
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);
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
157 public $defaultValue;
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
, "(");
172 return substr($this->type
, 0, $pos);
177 return $this->nullable
== "NO" ?
"true" : "false";
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";
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";
207 function getIndexName()
209 if (strlen($this->key
)>0)
210 return "index=\"" . $this->name
. "\"";
215 return $this->key
=="PRI";
217 function format($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);
232 function handleNHibernateCSBody($db, $table, $crlf)
235 $result=PMA_DBI_query(sprintf("DESC %s.%s", PMA_backquote($db), PMA_backquote($table)));
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);
247 $lines[] = " #region ".ucfirst($table);
248 $lines[] = " public class ".ucfirst($table);
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)."() { }";
257 foreach ($tableProperties as $tablePropertie)
258 if (! $tablePropertie->isPK())
259 $temp[] = $tablePropertie->format("#dotNetPrimitiveType# #name#");
260 $lines[] = " public ".ucfirst($table)."(".implode(", ", $temp).")";
262 foreach ($tableProperties as $tablePropertie)
263 if (! $tablePropertie->isPK())
264 $lines[] = $tablePropertie->format(" this._#name#=#name#;");
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";
272 $lines[] = " #endregion";
274 PMA_DBI_free_result($result);
276 return implode("\n", $lines);
279 function handleNHibernateXMLBody($db, $table, $crlf)
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)));
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>");
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)
308 return $GLOBALS[$what . "_" . $optionName];