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
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' => 'begin_group', 'name' => 'general_opts'),
37 array('type' => 'hidden', 'name' => 'structure_or_data'),
38 array('type' => 'select', 'name' => 'format', 'text' => __('Format:'), 'values' => $CG_FORMATS),
39 array('type' => 'end_group')
41 'options_text' => __('Options'),
46 * Set of functions used to build exports of tables
50 * Outputs export footer
52 * @return bool Whether it suceeded
56 function PMA_exportFooter()
62 * Outputs export header
64 * @return bool Whether it suceeded
68 function PMA_exportHeader()
74 * Outputs database header
76 * @param string $db Database name
77 * @return bool Whether it suceeded
81 function PMA_exportDBHeader($db)
87 * Outputs database footer
89 * @param string $db Database name
90 * @return bool Whether it suceeded
94 function PMA_exportDBFooter($db)
100 * Outputs CREATE DATABASE statement
102 * @param string $db Database name
103 * @return bool Whether it suceeded
107 function PMA_exportDBCreate($db)
113 * Outputs the content of a table in NHibernate format
115 * @param string $db database name
116 * @param string $table table name
117 * @param string $crlf the end of line sequence
118 * @param string $error_url the url to go back in case of error
119 * @param string $sql_query SQL query for obtaining data
120 * @return bool Whether it suceeded
124 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
126 global $CG_FORMATS, $CG_HANDLERS, $what;
127 $format = $GLOBALS[$what . '_format'];
128 if (isset($CG_FORMATS[$format])) {
129 return PMA_exportOutputHandler($CG_HANDLERS[$format]($db, $table, $crlf));
131 return PMA_exportOutputHandler(sprintf("%s is not supported.", $format));
136 * @package phpMyAdmin-Export
137 * @subpackage Codegen
145 public $defaultValue;
147 function __construct($row)
149 $this->name
= trim($row[0]);
150 $this->type
= trim($row[1]);
151 $this->nullable
= trim($row[2]);
152 $this->key
= trim($row[3]);
153 $this->defaultValue
= trim($row[4]);
154 $this->ext
= trim($row[5]);
156 function getPureType()
158 $pos=strpos($this->type
, "(");
160 return substr($this->type
, 0, $pos);
165 return $this->nullable
== "NO" ?
"true" : "false";
169 return $this->key
== "PRI" ||
$this->key
== "UNI" ?
"true" : "false";
171 function getDotNetPrimitiveType()
173 if (strpos($this->type
, "int") === 0) return "int";
174 if (strpos($this->type
, "long") === 0) return "long";
175 if (strpos($this->type
, "char") === 0) return "string";
176 if (strpos($this->type
, "varchar") === 0) return "string";
177 if (strpos($this->type
, "text") === 0) return "string";
178 if (strpos($this->type
, "longtext") === 0) return "string";
179 if (strpos($this->type
, "tinyint") === 0) return "bool";
180 if (strpos($this->type
, "datetime") === 0) return "DateTime";
183 function getDotNetObjectType()
185 if (strpos($this->type
, "int") === 0) return "Int32";
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 "Boolean";
192 if (strpos($this->type
, "datetime") === 0) return "DateTime";
195 function getIndexName()
197 if (strlen($this->key
)>0)
198 return "index=\"" . htmlspecialchars($this->name
, ENT_COMPAT
, 'UTF-8') . "\"";
203 return $this->key
=="PRI";
205 function formatCs($text)
207 $text=str_replace("#name#", cgMakeIdentifier($this->name
, false), $text);
208 return $this->format($text);
210 function formatXml($text)
212 $text=str_replace("#name#", htmlspecialchars($this->name
, ENT_COMPAT
, 'UTF-8'), $text);
213 $text=str_replace("#indexName#", $this->getIndexName(), $text);
214 return $this->format($text);
216 function format($text)
218 $text=str_replace("#ucfirstName#", cgMakeIdentifier($this->name
), $text);
219 $text=str_replace("#dotNetPrimitiveType#", $this->getDotNetPrimitiveType(), $text);
220 $text=str_replace("#dotNetObjectType#", $this->getDotNetObjectType(), $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);
228 function cgMakeIdentifier($str, $ucfirst = true)
230 // remove unsafe characters
231 $str = preg_replace('/[^\p{L}\p{Nl}_]/u', '', $str);
232 // make sure first character is a letter or _
233 if (!preg_match('/^\pL/u', $str)) {
237 $str = ucfirst($str);
242 function handleNHibernateCSBody($db, $table, $crlf)
245 $result=PMA_DBI_query(sprintf('DESC %s.%s', PMA_backquote($db), PMA_backquote($table)));
247 $tableProperties=array();
248 while ($row = PMA_DBI_fetch_row($result)) {
249 $tableProperties[] = new TableProperty($row);
251 PMA_DBI_free_result($result);
252 $lines[] = 'using System;';
253 $lines[] = 'using System.Collections;';
254 $lines[] = 'using System.Collections.Generic;';
255 $lines[] = 'using System.Text;';
256 $lines[] = 'namespace ' . cgMakeIdentifier($db);
258 $lines[] = ' #region ' . cgMakeIdentifier($table);
259 $lines[] = ' public class ' . cgMakeIdentifier($table);
261 $lines[] = ' #region Member Variables';
262 foreach ($tableProperties as $tablePropertie) {
263 $lines[] = $tablePropertie->formatCs(' protected #dotNetPrimitiveType# _#name#;');
265 $lines[] = ' #endregion';
266 $lines[] = ' #region Constructors';
267 $lines[] = ' public ' . cgMakeIdentifier($table).'() { }';
269 foreach ($tableProperties as $tablePropertie) {
270 if (! $tablePropertie->isPK()) {
271 $temp[] = $tablePropertie->formatCs('#dotNetPrimitiveType# #name#');
274 $lines[] = ' public ' . cgMakeIdentifier($table) . '(' . implode(', ', $temp) . ')';
276 foreach ($tableProperties as $tablePropertie) {
277 if (! $tablePropertie->isPK()) {
278 $lines[] = $tablePropertie->formatCs(' this._#name#=#name#;');
282 $lines[] = ' #endregion';
283 $lines[] = ' #region Public Properties';
284 foreach ($tableProperties as $tablePropertie) {
285 $lines[] = $tablePropertie->formatCs(''
286 . ' public virtual #dotNetPrimitiveType# #ucfirstName#' . "\n"
288 . ' get {return _#name#;}' . "\n"
289 . ' set {_#name#=value;}' . "\n"
293 $lines[] = ' #endregion';
295 $lines[] = ' #endregion';
298 return implode("\n", $lines);
301 function handleNHibernateXMLBody($db, $table, $crlf)
304 $lines[] = '<?xml version="1.0" encoding="utf-8" ?' . '>';
305 $lines[] = '<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" '
306 . 'namespace="' . cgMakeIdentifier($db) . '" '
307 . 'assembly="' . cgMakeIdentifier($db) . '">';
308 $lines[] = ' <class '
309 . 'name="' . cgMakeIdentifier($table) . '" '
310 . 'table="' . cgMakeIdentifier($table) . '">';
311 $result = PMA_DBI_query(sprintf("DESC %s.%s", PMA_backquote($db), PMA_backquote($table)));
313 while ($row = PMA_DBI_fetch_row($result)) {
314 $tablePropertie = new TableProperty($row);
315 if ($tablePropertie->isPK())
316 $lines[] = $tablePropertie->formatXml(''
317 . ' <id name="#ucfirstName#" type="#dotNetObjectType#" unsaved-value="0">' . "\n"
318 . ' <column name="#name#" sql-type="#type#" not-null="#notNull#" unique="#unique#" index="PRIMARY"/>' . "\n"
319 . ' <generator class="native" />' . "\n"
322 $lines[] = $tablePropertie->formatXml(''
323 . ' <property name="#ucfirstName#" type="#dotNetObjectType#">' . "\n"
324 . ' <column name="#name#" sql-type="#type#" not-null="#notNull#" #indexName#/>' . "\n"
327 PMA_DBI_free_result($result);
329 $lines[] = ' </class>';
330 $lines[] = '</hibernate-mapping>';
331 return implode("\n", $lines);