BLOB streaming documentation
[phpmyadmin/crack.git] / libraries / export / codegen.php
blob7a487396046d34c68c5315fe93d89a8a6c9a3bdd
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build NHibernate dumps of tables
6 * @version $Id$
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
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);
24 /**
27 if (isset($plugin_list)) {
28 $plugin_list['codegen'] = array(
29 'text' => 'CodeGen',
30 'extension' => 'cs',
31 'mime_type' => 'text/cs',
32 'options' => array(
33 array('type' => 'hidden', 'name' => 'data'),
34 array('type' => 'select', 'name' => 'format', 'text' => 'strFormat', 'values' => $CG_FORMATS),
36 'options_text' => 'strOptions',
38 } else {
40 /**
41 * Set of functions used to build exports of tables
44 /**
45 * Outputs comment
47 * @param string Text of comment
49 * @return bool Whether it suceeded
51 function PMA_exportComment($text)
53 return TRUE;
56 /**
57 * Outputs export footer
59 * @return bool Whether it suceeded
61 * @access public
63 function PMA_exportFooter()
65 return TRUE;
68 /**
69 * Outputs export header
71 * @return bool Whether it suceeded
73 * @access public
75 function PMA_exportHeader()
77 return TRUE;
80 /**
81 * Outputs database header
83 * @param string Database name
85 * @return bool Whether it suceeded
87 * @access public
89 function PMA_exportDBHeader($db)
91 return TRUE;
94 /**
95 * Outputs database footer
97 * @param string Database name
99 * @return bool Whether it suceeded
101 * @access public
103 function PMA_exportDBFooter($db)
105 return TRUE;
109 * Outputs create database database
111 * @param string Database name
113 * @return bool Whether it suceeded
115 * @access public
117 function PMA_exportDBCreate($db)
119 return TRUE;
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
133 * @access public
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);
140 if ($index >= 0)
141 return PMA_exportOutputHandler($CG_HANDLERS[$index]($db, $table, $crlf));
142 return PMA_exportOutputHandler(sprintf("%s is not supported.", $format));
145 class TableProperty
147 public $name;
148 public $type;
149 public $nullable;
150 public $key;
151 public $defaultValue;
152 public $ext;
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, "(");
165 if ($pos > 0)
166 return substr($this->type, 0, $pos);
167 return $this->type;
169 function isNotNull()
171 return $this->nullable == "NO" ? "true" : "false";
173 function isUnique()
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";
187 return "unknown";
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";
199 return "Unknown";
201 function getIndexName()
203 if (strlen($this->key)>0)
204 return "index=\"" . $this->name . "\"";
205 return "";
207 function isPK()
209 return $this->key=="PRI";
211 function format($pattern)
213 $text=$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);
222 return $text;
226 function handleNHibernateCSBody($db, $table, $crlf)
228 $lines=array();
229 $result=PMA_DBI_query(sprintf("DESC %s.%s", PMA_backquote($db), PMA_backquote($table)));
230 if ($result)
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);
240 $lines[] = "{";
241 $lines[] = " #region ".ucfirst($table);
242 $lines[] = " public class ".ucfirst($table);
243 $lines[] = " {";
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)."() { }";
250 $temp = array();
251 foreach ($tableProperties as $tablePropertie)
252 if (! $tablePropertie->isPK())
253 $temp[] = $tablePropertie->format("#dotNetPrimitiveType# #name#");
254 $lines[] = " public ".ucfirst($table)."(".implode(", ", $temp).")";
255 $lines[] = " {";
256 foreach ($tableProperties as $tablePropertie)
257 if (! $tablePropertie->isPK())
258 $lines[] = $tablePropertie->format(" this._#name#=#name#;");
259 $lines[] = " }";
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";
265 $lines[] = " }";
266 $lines[] = " #endregion";
267 $lines[] = "}";
268 PMA_DBI_free_result($result);
270 return implode("\n", $lines);
273 function handleNHibernateXMLBody($db, $table, $crlf)
275 $lines=array();
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)));
280 if ($result)
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>");
289 else
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)
301 global $what;
302 return $GLOBALS[$what . "_" . $optionName];