UPDATE 4.4.0.0
[phpmyadmin.git] / libraries / plugins / export / ExportCodegen.class.php
blobaa02369b33ed643b55ed6247da9d82b469dc429d
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
7 * @subpackage CodeGen
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /* Get the export interface */
14 require_once 'libraries/plugins/ExportPlugin.class.php';
15 /* Get the table property class */
16 require_once 'libraries/plugins/export/TableProperty.class.php';
18 /**
19 * Handles the export for the CodeGen class
21 * @package PhpMyAdmin-Export
22 * @subpackage CodeGen
24 class ExportCodegen extends ExportPlugin
26 /**
27 * CodeGen Formats
29 * @var array
31 private $_cgFormats;
33 /**
34 * CodeGen Handlers
36 * @var array
38 private $_cgHandlers;
40 /**
41 * Constructor
43 public function __construct()
45 // initialize the specific export CodeGen variables
46 $this->initSpecificVariables();
47 $this->setProperties();
50 /**
51 * Initialize the local variables that are used for export CodeGen
53 * @return void
55 protected function initSpecificVariables()
57 $this->_setCgFormats(
58 array(
59 "NHibernate C# DO",
60 "NHibernate XML"
64 $this->_setCgHandlers(
65 array(
66 "_handleNHibernateCSBody",
67 "_handleNHibernateXMLBody"
72 /**
73 * Sets the export CodeGen properties
75 * @return void
77 protected function setProperties()
79 $props = 'libraries/properties/';
80 include_once "$props/plugins/ExportPluginProperties.class.php";
81 include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
82 include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
83 include_once "$props/options/items/HiddenPropertyItem.class.php";
84 include_once "$props/options/items/SelectPropertyItem.class.php";
86 $exportPluginProperties = new ExportPluginProperties();
87 $exportPluginProperties->setText('CodeGen');
88 $exportPluginProperties->setExtension('cs');
89 $exportPluginProperties->setMimeType('text/cs');
90 $exportPluginProperties->setOptionsText(__('Options'));
92 // create the root group that will be the options field for
93 // $exportPluginProperties
94 // this will be shown as "Format specific options"
95 $exportSpecificOptions = new OptionsPropertyRootGroup();
96 $exportSpecificOptions->setName("Format Specific Options");
98 // general options main group
99 $generalOptions = new OptionsPropertyMainGroup();
100 $generalOptions->setName("general_opts");
101 // create primary items and add them to the group
102 $leaf = new HiddenPropertyItem();
103 $leaf->setName("structure_or_data");
104 $generalOptions->addProperty($leaf);
105 $leaf = new SelectPropertyItem();
106 $leaf->setName("format");
107 $leaf->setText(__('Format:'));
108 $leaf->setValues($this->_getCgFormats());
109 $generalOptions->addProperty($leaf);
110 // add the main group to the root group
111 $exportSpecificOptions->addProperty($generalOptions);
113 // set the options for the export plugin property item
114 $exportPluginProperties->setOptions($exportSpecificOptions);
115 $this->properties = $exportPluginProperties;
119 * Outputs export header
121 * @return bool Whether it succeeded
123 public function exportHeader ()
125 return true;
129 * Outputs export footer
131 * @return bool Whether it succeeded
133 public function exportFooter ()
135 return true;
139 * Outputs database header
141 * @param string $db Database name
142 * @param string $db_alias Aliases of db
144 * @return bool Whether it succeeded
146 public function exportDBHeader ($db, $db_alias = '')
148 return true;
152 * Outputs database footer
154 * @param string $db Database name
156 * @return bool Whether it succeeded
158 public function exportDBFooter ($db)
160 return true;
164 * Outputs CREATE DATABASE statement
166 * @param string $db Database name
167 * @param string $db_alias Aliases of db
169 * @return bool Whether it succeeded
171 public function exportDBCreate($db, $db_alias = '')
173 return true;
177 * Outputs the content of a table in NHibernate format
179 * @param string $db database name
180 * @param string $table table name
181 * @param string $crlf the end of line sequence
182 * @param string $error_url the url to go back in case of error
183 * @param string $sql_query SQL query for obtaining data
184 * @param array $aliases Aliases of db/table/columns
186 * @return bool Whether it succeeded
188 public function exportData(
189 $db, $table, $crlf, $error_url, $sql_query, $aliases = array()
191 $CG_FORMATS = $this->_getCgFormats();
192 $CG_HANDLERS = $this->_getCgHandlers();
194 $format = $GLOBALS['codegen_format'];
195 if (isset($CG_FORMATS[$format])) {
196 $method = $CG_HANDLERS[$format];
197 return PMA_exportOutputHandler(
198 $this->$method($db, $table, $crlf, $aliases)
201 return PMA_exportOutputHandler(sprintf("%s is not supported.", $format));
205 * Used to make identifiers (from table or database names)
207 * @param string $str name to be converted
208 * @param bool $ucfirst whether to make the first character uppercase
210 * @return string identifier
212 public static function cgMakeIdentifier($str, $ucfirst = true)
214 // remove unsafe characters
215 $str = preg_replace('/[^\p{L}\p{Nl}_]/u', '', $str);
216 // make sure first character is a letter or _
217 if (! preg_match('/^\pL/u', $str)) {
218 $str = '_' . $str;
220 if ($ucfirst) {
221 $str = ucfirst($str);
223 return $str;
227 * C# Handler
229 * @param string $db database name
230 * @param string $table table name
231 * @param string $crlf line separator
232 * @param array $aliases Aliases of db/table/columns
234 * @return string containing C# code lines, separated by "\n"
236 private function _handleNHibernateCSBody($db, $table, $crlf, $aliases = array())
238 $db_alias = $db;
239 $table_alias = $table;
240 $this->initAlias($aliases, $db_alias, $table_alias);
241 $lines = array();
243 $result = $GLOBALS['dbi']->query(
244 sprintf(
245 'DESC %s.%s', PMA_Util::backquote($db),
246 PMA_Util::backquote($table)
249 if ($result) {
250 $tableProperties = array();
251 while ($row = $GLOBALS['dbi']->fetchRow($result)) {
252 $col_as = $this->getAlias($aliases, $row[0], 'col', $db, $table);
253 if (!empty($col_as)) {
254 $row[0] = $col_as;
256 $tableProperties[] = new TableProperty($row);
258 $GLOBALS['dbi']->freeResult($result);
259 $lines[] = 'using System;';
260 $lines[] = 'using System.Collections;';
261 $lines[] = 'using System.Collections.Generic;';
262 $lines[] = 'using System.Text;';
263 $lines[] = 'namespace ' . ExportCodegen::cgMakeIdentifier($db_alias);
264 $lines[] = '{';
265 $lines[] = ' #region '
266 . ExportCodegen::cgMakeIdentifier($table_alias);
267 $lines[] = ' public class '
268 . ExportCodegen::cgMakeIdentifier($table_alias);
269 $lines[] = ' {';
270 $lines[] = ' #region Member Variables';
271 foreach ($tableProperties as $tableProperty) {
272 $lines[] = $tableProperty->formatCs(
273 ' protected #dotNetPrimitiveType# _#name#;'
276 $lines[] = ' #endregion';
277 $lines[] = ' #region Constructors';
278 $lines[] = ' public '
279 . ExportCodegen::cgMakeIdentifier($table_alias) . '() { }';
280 $temp = array();
281 foreach ($tableProperties as $tableProperty) {
282 if (! $tableProperty->isPK()) {
283 $temp[] = $tableProperty->formatCs(
284 '#dotNetPrimitiveType# #name#'
288 $lines[] = ' public '
289 . ExportCodegen::cgMakeIdentifier($table_alias)
290 . '('
291 . implode(', ', $temp)
292 . ')';
293 $lines[] = ' {';
294 foreach ($tableProperties as $tableProperty) {
295 if (! $tableProperty->isPK()) {
296 $lines[] = $tableProperty->formatCs(
297 ' this._#name#=#name#;'
301 $lines[] = ' }';
302 $lines[] = ' #endregion';
303 $lines[] = ' #region Public Properties';
304 foreach ($tableProperties as $tableProperty) {
305 $lines[] = $tableProperty->formatCs(
306 ' public virtual #dotNetPrimitiveType# #ucfirstName#'
307 . "\n"
308 . ' {' . "\n"
309 . ' get {return _#name#;}' . "\n"
310 . ' set {_#name#=value;}' . "\n"
311 . ' }'
314 $lines[] = ' #endregion';
315 $lines[] = ' }';
316 $lines[] = ' #endregion';
317 $lines[] = '}';
319 return implode($crlf, $lines);
323 * XML Handler
325 * @param string $db database name
326 * @param string $table table name
327 * @param string $crlf line separator
328 * @param array $aliases Aliases of db/table/columns
330 * @return string containing XML code lines, separated by "\n"
332 private function _handleNHibernateXMLBody(
333 $db, $table, $crlf, $aliases = array()
335 $db_alias = $db;
336 $table_alias = $table;
337 $this->initAlias($aliases, $db_alias, $table_alias);
338 $lines = array();
339 $lines[] = '<?xml version="1.0" encoding="utf-8" ?' . '>';
340 $lines[] = '<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" '
341 . 'namespace="' . ExportCodegen::cgMakeIdentifier($db_alias) . '" '
342 . 'assembly="' . ExportCodegen::cgMakeIdentifier($db_alias) . '">';
343 $lines[] = ' <class '
344 . 'name="' . ExportCodegen::cgMakeIdentifier($table_alias) . '" '
345 . 'table="' . ExportCodegen::cgMakeIdentifier($table_alias) . '">';
346 $result = $GLOBALS['dbi']->query(
347 sprintf(
348 "DESC %s.%s", PMA_Util::backquote($db),
349 PMA_Util::backquote($table)
352 if ($result) {
353 while ($row = $GLOBALS['dbi']->fetchRow($result)) {
354 $col_as = $this->getAlias($aliases, $row[0], 'col', $db, $table);
355 if (!empty($col_as)) {
356 $row[0] = $col_as;
358 $tableProperty = new TableProperty($row);
359 if ($tableProperty->isPK()) {
360 $lines[] = $tableProperty->formatXml(
361 ' <id name="#ucfirstName#" type="#dotNetObjectType#"'
362 . ' unsaved-value="0">' . "\n"
363 . ' <column name="#name#" sql-type="#type#"'
364 . ' not-null="#notNull#" unique="#unique#"'
365 . ' index="PRIMARY"/>' . "\n"
366 . ' <generator class="native" />' . "\n"
367 . ' </id>'
369 } else {
370 $lines[] = $tableProperty->formatXml(
371 ' <property name="#ucfirstName#"'
372 . ' type="#dotNetObjectType#">' . "\n"
373 . ' <column name="#name#" sql-type="#type#"'
374 . ' not-null="#notNull#" #indexName#/>' . "\n"
375 . ' </property>'
379 $GLOBALS['dbi']->freeResult($result);
381 $lines[] = ' </class>';
382 $lines[] = '</hibernate-mapping>';
383 return implode($crlf, $lines);
387 /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
391 * Getter for CodeGen formats
393 * @return array
395 private function _getCgFormats()
397 return $this->_cgFormats;
401 * Setter for CodeGen formats
403 * @param array $CG_FORMATS contains CodeGen Formats
405 * @return void
407 private function _setCgFormats($CG_FORMATS)
409 $this->_cgFormats = $CG_FORMATS;
413 * Getter for CodeGen handlers
415 * @return array
417 private function _getCgHandlers()
419 return $this->_cgHandlers;
423 * Setter for CodeGen handlers
425 * @param array $CG_HANDLERS contains CodeGen handler methods
427 * @return void
429 private function _setCgHandlers($CG_HANDLERS)
431 $this->_cgHandlers = $CG_HANDLERS;