1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Set of functions used to build NHibernate dumps of tables
*
* @package phpMyAdmin-Export-Codegen
* @version $Id$
*/
if (! defined('PHPMYADMIN')) {
exit;
}
/**
* This gets executed twice so avoid a notice
*/
if (! defined('CG_FORMAT_NHIBERNATE_CS')) {
define("CG_FORMAT_NHIBERNATE_CS", "NHibernate C# DO");
define("CG_FORMAT_NHIBERNATE_XML", "NHibernate XML");
define("CG_HANDLER_NHIBERNATE_CS_BODY", "handleNHibernateCSBody");
define("CG_HANDLER_NHIBERNATE_XML_BODY", "handleNHibernateXMLBody");
}
$CG_FORMATS = array(CG_FORMAT_NHIBERNATE_CS, CG_FORMAT_NHIBERNATE_XML);
$CG_HANDLERS = array(CG_HANDLER_NHIBERNATE_CS_BODY, CG_HANDLER_NHIBERNATE_XML_BODY);
/**
*
*/
if (isset($plugin_list)) {
$plugin_list['codegen'] = array(
'text' => 'CodeGen',
'extension' => 'cs',
'mime_type' => 'text/cs',
'options' => array(
array('type' => 'hidden', 'name' => 'data'),
array('type' => 'select', 'name' => 'format', 'text' => 'strFormat', 'values' => $CG_FORMATS),
),
'options_text' => 'strOptions',
);
} else {
/**
* Set of functions used to build exports of tables
*/
/**
* Outputs comment
*
* @param string Text of comment
*
* @return bool Whether it suceeded
*/
function PMA_exportComment($text)
{
return TRUE;
}
/**
* Outputs export footer
*
* @return bool Whether it suceeded
*
* @access public
*/
function PMA_exportFooter()
{
return TRUE;
}
/**
* Outputs export header
*
* @return bool Whether it suceeded
*
* @access public
*/
function PMA_exportHeader()
{
return TRUE;
}
/**
* Outputs database header
*
* @param string Database name
*
* @return bool Whether it suceeded
*
* @access public
*/
function PMA_exportDBHeader($db)
{
return TRUE;
}
/**
* Outputs database footer
*
* @param string Database name
*
* @return bool Whether it suceeded
*
* @access public
*/
function PMA_exportDBFooter($db)
{
return TRUE;
}
/**
* Outputs create database database
*
* @param string Database name
*
* @return bool Whether it suceeded
*
* @access public
*/
function PMA_exportDBCreate($db)
{
return TRUE;
}
/**
* Outputs the content of a table in NHibernate format
*
* @param string the database name
* @param string the table name
* @param string the end of line sequence
* @param string the url to go back in case of error
* @param string SQL query for obtaining data
*
* @return bool Whether it suceeded
*
* @access public
*/
function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
{
global $CG_FORMATS, $CG_HANDLERS;
$format = cgGetOption("format");
$index = array_search($format, $CG_FORMATS);
if ($index >= 0)
return PMA_exportOutputHandler($CG_HANDLERS[$index]($db, $table, $crlf));
return PMA_exportOutputHandler(sprintf("%s is not supported.", $format));
}
/**
*
* @package phpMyAdmin-Export-Codegen
*/
class TableProperty
{
public $name;
public $type;
public $nullable;
public $key;
public $defaultValue;
public $ext;
function __construct($row)
{
$this->name = trim($row[0]);
$this->type = trim($row[1]);
$this->nullable = trim($row[2]);
$this->key = trim($row[3]);
$this->defaultValue = trim($row[4]);
$this->ext = trim($row[5]);
}
function getPureType()
{
$pos=strpos($this->type, "(");
if ($pos > 0)
return substr($this->type, 0, $pos);
return $this->type;
}
function isNotNull()
{
return $this->nullable == "NO" ? "true" : "false";
}
function isUnique()
{
return $this->key == "PRI" || $this->key == "UNI" ? "true" : "false";
}
function getDotNetPrimitiveType()
{
if (strpos($this->type, "int") === 0) return "int";
if (strpos($this->type, "long") === 0) return "long";
if (strpos($this->type, "char") === 0) return "string";
if (strpos($this->type, "varchar") === 0) return "string";
if (strpos($this->type, "text") === 0) return "string";
if (strpos($this->type, "longtext") === 0) return "string";
if (strpos($this->type, "tinyint") === 0) return "bool";
if (strpos($this->type, "datetime") === 0) return "DateTime";
return "unknown";
}
function getDotNetObjectType()
{
if (strpos($this->type, "int") === 0) return "Int32";
if (strpos($this->type, "long") === 0) return "Long";
if (strpos($this->type, "char") === 0) return "String";
if (strpos($this->type, "varchar") === 0) return "String";
if (strpos($this->type, "text") === 0) return "String";
if (strpos($this->type, "longtext") === 0) return "String";
if (strpos($this->type, "tinyint") === 0) return "Boolean";
if (strpos($this->type, "datetime") === 0) return "DateTime";
return "Unknown";
}
function getIndexName()
{
if (strlen($this->key)>0)
return "index=\"" . $this->name . "\"";
return "";
}
function isPK()
{
return $this->key=="PRI";
}
function format($pattern)
{
$text=$pattern;
$text=str_replace("#name#", $this->name, $text);
$text=str_replace("#type#", $this->getPureType(), $text);
$text=str_replace("#notNull#", $this->isNotNull(), $text);
$text=str_replace("#unique#", $this->isUnique(), $text);
$text=str_replace("#ucfirstName#", ucfirst($this->name), $text);
$text=str_replace("#dotNetPrimitiveType#", $this->getDotNetPrimitiveType(), $text);
$text=str_replace("#dotNetObjectType#", $this->getDotNetObjectType(), $text);
$text=str_replace("#indexName#", $this->getIndexName(), $text);
return $text;
}
}
function handleNHibernateCSBody($db, $table, $crlf)
{
$lines=array();
$result=PMA_DBI_query(sprintf("DESC %s.%s", PMA_backquote($db), PMA_backquote($table)));
if ($result)
{
$tableProperties=array();
while ($row = PMA_DBI_fetch_row($result))
$tableProperties[] = new TableProperty($row);
$lines[] = "using System;";
$lines[] = "using System.Collections;";
$lines[] = "using System.Collections.Generic;";
$lines[] = "using System.Text;";
$lines[] = "namespace ".ucfirst($db);
$lines[] = "{";
$lines[] = " #region ".ucfirst($table);
$lines[] = " public class ".ucfirst($table);
$lines[] = " {";
$lines[] = " #region Member Variables";
foreach ($tableProperties as $tablePropertie)
$lines[] = $tablePropertie->format(" protected #dotNetPrimitiveType# _#name#;");
$lines[] = " #endregion";
$lines[] = " #region Constructors";
$lines[] = " public ".ucfirst($table)."() { }";
$temp = array();
foreach ($tableProperties as $tablePropertie)
if (! $tablePropertie->isPK())
$temp[] = $tablePropertie->format("#dotNetPrimitiveType# #name#");
$lines[] = " public ".ucfirst($table)."(".implode(", ", $temp).")";
$lines[] = " {";
foreach ($tableProperties as $tablePropertie)
if (! $tablePropertie->isPK())
$lines[] = $tablePropertie->format(" this._#name#=#name#;");
$lines[] = " }";
$lines[] = " #endregion";
$lines[] = " #region Public Properties";
foreach ($tableProperties as $tablePropertie)
$lines[] = $tablePropertie->format(" public virtual #dotNetPrimitiveType# _#ucfirstName#\n {\n get {return _#name#;}\n set {_#name#=value;}\n }");
$lines[] = " #endregion";
$lines[] = " }";
$lines[] = " #endregion";
$lines[] = "}";
PMA_DBI_free_result($result);
}
return implode("\n", $lines);
}
function handleNHibernateXMLBody($db, $table, $crlf)
{
$lines=array();
$lines[] = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
$lines[] = "<hibernate-mapping xmlns=\"urn:nhibernate-mapping-2.2\" namespace=\"".ucfirst($db)."\" assembly=\"".ucfirst($db)."\">";
$lines[] = " <class name=\"".ucfirst($table)."\" table=\"".$table."\">";
$result = PMA_DBI_query(sprintf("DESC %s.%s", PMA_backquote($db), PMA_backquote($table)));
if ($result)
{
$tableProperties = array();
while ($row = PMA_DBI_fetch_row($result))
$tableProperties[] = new TableProperty($row);
foreach ($tableProperties as $tablePropertie)
{
if ($tablePropertie->isPK())
$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>");
else
$lines[] = $tablePropertie->format(" <property name=\"#ucfirstName#\" type=\"#dotNetObjectType#\">\n <column name=\"#name#\" sql-type=\"#type#\" not-null=\"#notNull#\" #indexName#/>\n </property>");
}
PMA_DBI_free_result($result);
}
$lines[]=" </class>";
$lines[]="</hibernate-mapping>";
return implode("\n", $lines);
}
function cgGetOption($optionName)
{
global $what;
return $GLOBALS[$what . "_" . $optionName];
}
}
?>
|