display message when search returns zero rows
[phpmyadmin/dkf.git] / libraries / export / codegen.php
blob8e36f408f69f73f54a45defdf61ddccd71a01c25
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 * @package phpMyAdmin-Export-Codegen
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
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);
26 /**
29 if (isset($plugin_list)) {
30 $plugin_list['codegen'] = array(
31 'text' => 'CodeGen',
32 'extension' => 'cs',
33 'mime_type' => 'text/cs',
34 'options' => array(
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'),
42 } else {
44 /**
45 * Set of functions used to build exports of tables
48 /**
49 * Outputs comment
51 * @param string Text of comment
53 * @return bool Whether it suceeded
55 function PMA_exportComment($text)
57 return TRUE;
60 /**
61 * Outputs export footer
63 * @return bool Whether it suceeded
65 * @access public
67 function PMA_exportFooter()
69 return TRUE;
72 /**
73 * Outputs export header
75 * @return bool Whether it suceeded
77 * @access public
79 function PMA_exportHeader()
81 return TRUE;
84 /**
85 * Outputs database header
87 * @param string Database name
89 * @return bool Whether it suceeded
91 * @access public
93 function PMA_exportDBHeader($db)
95 return TRUE;
98 /**
99 * Outputs database footer
101 * @param string Database name
103 * @return bool Whether it suceeded
105 * @access public
107 function PMA_exportDBFooter($db)
109 return TRUE;
113 * Outputs create database database
115 * @param string Database name
117 * @return bool Whether it suceeded
119 * @access public
121 function PMA_exportDBCreate($db)
123 return TRUE;
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
137 * @access public
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);
144 if ($index >= 0)
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
153 class TableProperty
155 public $name;
156 public $type;
157 public $nullable;
158 public $key;
159 public $defaultValue;
160 public $ext;
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, "(");
173 if ($pos > 0)
174 return substr($this->type, 0, $pos);
175 return $this->type;
177 function isNotNull()
179 return $this->nullable == "NO" ? "true" : "false";
181 function isUnique()
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";
195 return "unknown";
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";
207 return "Unknown";
209 function getIndexName()
211 if (strlen($this->key)>0)
212 return "index=\"" . $this->name . "\"";
213 return "";
215 function isPK()
217 return $this->key=="PRI";
219 function format($pattern)
221 $text=$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);
230 return $text;
234 function handleNHibernateCSBody($db, $table, $crlf)
236 $lines=array();
237 $result=PMA_DBI_query(sprintf("DESC %s.%s", PMA_backquote($db), PMA_backquote($table)));
238 if ($result)
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);
248 $lines[] = "{";
249 $lines[] = " #region ".ucfirst($table);
250 $lines[] = " public class ".ucfirst($table);
251 $lines[] = " {";
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)."() { }";
258 $temp = array();
259 foreach ($tableProperties as $tablePropertie)
260 if (! $tablePropertie->isPK())
261 $temp[] = $tablePropertie->format("#dotNetPrimitiveType# #name#");
262 $lines[] = " public ".ucfirst($table)."(".implode(", ", $temp).")";
263 $lines[] = " {";
264 foreach ($tableProperties as $tablePropertie)
265 if (! $tablePropertie->isPK())
266 $lines[] = $tablePropertie->format(" this._#name#=#name#;");
267 $lines[] = " }";
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";
273 $lines[] = " }";
274 $lines[] = " #endregion";
275 $lines[] = "}";
276 PMA_DBI_free_result($result);
278 return implode("\n", $lines);
281 function handleNHibernateXMLBody($db, $table, $crlf)
283 $lines=array();
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)));
288 if ($result)
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>");
297 else
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)
309 global $what;
310 return $GLOBALS[$what . "_" . $optionName];