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' => 'begin_group', 'name' => 'general_opts'),
36 array('type' => 'hidden', 'name' => 'structure_or_data'),
37 array('type' => 'select', 'name' => 'format', 'text' => __('Format:'), 'values' => $CG_FORMATS),
38 array('type' => 'end_group')
40 'options_text' => __('Options'),
45 * Set of functions used to build exports of tables
51 * @param string Text of comment
53 * @return bool Whether it suceeded
55 function PMA_exportComment($text)
61 * Outputs export footer
63 * @return bool Whether it suceeded
67 function PMA_exportFooter()
73 * Outputs export header
75 * @return bool Whether it suceeded
79 function PMA_exportHeader()
85 * Outputs database header
87 * @param string Database name
89 * @return bool Whether it suceeded
93 function PMA_exportDBHeader($db)
99 * Outputs database footer
101 * @param string Database name
103 * @return bool Whether it suceeded
107 function PMA_exportDBFooter($db)
113 * Outputs create database database
115 * @param string Database name
117 * @return bool Whether it suceeded
121 function PMA_exportDBCreate($db)
127 * Outputs the content of a table in NHibernate format
129 * @param string the database name
130 * @param string the table name
131 * @param string the end of line sequence
132 * @param string the url to go back in case of error
133 * @param string SQL query for obtaining data
135 * @return bool Whether it suceeded
139 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
141 global $CG_FORMATS, $CG_HANDLERS;
142 $format = cgGetOption("format");
143 $index = array_search($format, $CG_FORMATS);
145 return PMA_exportOutputHandler($CG_HANDLERS[$index]($db, $table, $crlf));
146 return PMA_exportOutputHandler(sprintf("%s is not supported.", $format));
151 * @package phpMyAdmin-Export-Codegen
159 public $defaultValue;
161 function __construct($row)
163 $this->name
= trim($row[0]);
164 $this->type
= trim($row[1]);
165 $this->nullable
= trim($row[2]);
166 $this->key
= trim($row[3]);
167 $this->defaultValue
= trim($row[4]);
168 $this->ext
= trim($row[5]);
170 function getPureType()
172 $pos=strpos($this->type
, "(");
174 return substr($this->type
, 0, $pos);
179 return $this->nullable
== "NO" ?
"true" : "false";
183 return $this->key
== "PRI" ||
$this->key
== "UNI" ?
"true" : "false";
185 function getDotNetPrimitiveType()
187 if (strpos($this->type
, "int") === 0) return "int";
188 if (strpos($this->type
, "long") === 0) return "long";
189 if (strpos($this->type
, "char") === 0) return "string";
190 if (strpos($this->type
, "varchar") === 0) return "string";
191 if (strpos($this->type
, "text") === 0) return "string";
192 if (strpos($this->type
, "longtext") === 0) return "string";
193 if (strpos($this->type
, "tinyint") === 0) return "bool";
194 if (strpos($this->type
, "datetime") === 0) return "DateTime";
197 function getDotNetObjectType()
199 if (strpos($this->type
, "int") === 0) return "Int32";
200 if (strpos($this->type
, "long") === 0) return "Long";
201 if (strpos($this->type
, "char") === 0) return "String";
202 if (strpos($this->type
, "varchar") === 0) return "String";
203 if (strpos($this->type
, "text") === 0) return "String";
204 if (strpos($this->type
, "longtext") === 0) return "String";
205 if (strpos($this->type
, "tinyint") === 0) return "Boolean";
206 if (strpos($this->type
, "datetime") === 0) return "DateTime";
209 function getIndexName()
211 if (strlen($this->key
)>0)
212 return "index=\"" . $this->name
. "\"";
217 return $this->key
=="PRI";
219 function format($pattern)
222 $text=str_replace("#name#", $this->name
, $text);
223 $text=str_replace("#type#", $this->getPureType(), $text);
224 $text=str_replace("#notNull#", $this->isNotNull(), $text);
225 $text=str_replace("#unique#", $this->isUnique(), $text);
226 $text=str_replace("#ucfirstName#", ucfirst($this->name
), $text);
227 $text=str_replace("#dotNetPrimitiveType#", $this->getDotNetPrimitiveType(), $text);
228 $text=str_replace("#dotNetObjectType#", $this->getDotNetObjectType(), $text);
229 $text=str_replace("#indexName#", $this->getIndexName(), $text);
234 function handleNHibernateCSBody($db, $table, $crlf)
237 $result=PMA_DBI_query(sprintf("DESC %s.%s", PMA_backquote($db), PMA_backquote($table)));
240 $tableProperties=array();
241 while ($row = PMA_DBI_fetch_row($result))
242 $tableProperties[] = new TableProperty($row);
243 $lines[] = "using System;";
244 $lines[] = "using System.Collections;";
245 $lines[] = "using System.Collections.Generic;";
246 $lines[] = "using System.Text;";
247 $lines[] = "namespace ".ucfirst($db);
249 $lines[] = " #region ".ucfirst($table);
250 $lines[] = " public class ".ucfirst($table);
252 $lines[] = " #region Member Variables";
253 foreach ($tableProperties as $tablePropertie)
254 $lines[] = $tablePropertie->format(" protected #dotNetPrimitiveType# _#name#;");
255 $lines[] = " #endregion";
256 $lines[] = " #region Constructors";
257 $lines[] = " public ".ucfirst($table)."() { }";
259 foreach ($tableProperties as $tablePropertie)
260 if (! $tablePropertie->isPK())
261 $temp[] = $tablePropertie->format("#dotNetPrimitiveType# #name#");
262 $lines[] = " public ".ucfirst($table)."(".implode(", ", $temp).")";
264 foreach ($tableProperties as $tablePropertie)
265 if (! $tablePropertie->isPK())
266 $lines[] = $tablePropertie->format(" this._#name#=#name#;");
268 $lines[] = " #endregion";
269 $lines[] = " #region Public Properties";
270 foreach ($tableProperties as $tablePropertie)
271 $lines[] = $tablePropertie->format(" public virtual #dotNetPrimitiveType# _#ucfirstName#\n {\n get {return _#name#;}\n set {_#name#=value;}\n }");
272 $lines[] = " #endregion";
274 $lines[] = " #endregion";
276 PMA_DBI_free_result($result);
278 return implode("\n", $lines);
281 function handleNHibernateXMLBody($db, $table, $crlf)
284 $lines[] = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
285 $lines[] = "<hibernate-mapping xmlns=\"urn:nhibernate-mapping-2.2\" namespace=\"".ucfirst($db)."\" assembly=\"".ucfirst($db)."\">";
286 $lines[] = " <class name=\"".ucfirst($table)."\" table=\"".$table."\">";
287 $result = PMA_DBI_query(sprintf("DESC %s.%s", PMA_backquote($db), PMA_backquote($table)));
290 $tableProperties = array();
291 while ($row = PMA_DBI_fetch_row($result))
292 $tableProperties[] = new TableProperty($row);
293 foreach ($tableProperties as $tablePropertie)
295 if ($tablePropertie->isPK())
296 $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>");
298 $lines[] = $tablePropertie->format(" <property name=\"#ucfirstName#\" type=\"#dotNetObjectType#\">\n <column name=\"#name#\" sql-type=\"#type#\" not-null=\"#notNull#\" #indexName#/>\n </property>");
300 PMA_DBI_free_result($result);
302 $lines[]=" </class>";
303 $lines[]="</hibernate-mapping>";
304 return implode("\n", $lines);
307 function cgGetOption($optionName)
310 return $GLOBALS[$what . "_" . $optionName];