Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / plugins / export / ExportCodegen.class.php
blobd0a2216a4c89502e07a357148c1daedb19e29150
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 * This method is called when any PluginManager to which the observer
120 * is attached calls PluginManager::notify()
122 * @param SplSubject $subject The PluginManager notifying the observer
123 * of an update.
125 * @return void
127 public function update (SplSubject $subject)
132 * Outputs export header
134 * @return bool Whether it succeeded
136 public function exportHeader ()
138 return true;
142 * Outputs export footer
144 * @return bool Whether it succeeded
146 public function exportFooter ()
148 return true;
152 * Outputs database header
154 * @param string $db Database name
156 * @return bool Whether it succeeded
158 public function exportDBHeader ($db)
160 return true;
164 * Outputs database footer
166 * @param string $db Database name
168 * @return bool Whether it succeeded
170 public function exportDBFooter ($db)
172 return true;
176 * Outputs CREATE DATABASE statement
178 * @param string $db Database name
180 * @return bool Whether it succeeded
182 public function exportDBCreate($db)
184 return true;
188 * Outputs the content of a table in NHibernate format
190 * @param string $db database name
191 * @param string $table table name
192 * @param string $crlf the end of line sequence
193 * @param string $error_url the url to go back in case of error
194 * @param string $sql_query SQL query for obtaining data
196 * @return bool Whether it succeeded
198 public function exportData($db, $table, $crlf, $error_url, $sql_query)
200 $CG_FORMATS = $this->_getCgFormats();
201 $CG_HANDLERS = $this->_getCgHandlers();
203 $format = $GLOBALS['codegen_format'];
204 if (isset($CG_FORMATS[$format])) {
205 return PMA_exportOutputHandler(
206 $this->$CG_HANDLERS[$format]($db, $table, $crlf)
209 return PMA_exportOutputHandler(sprintf("%s is not supported.", $format));
213 * Used to make identifiers (from table or database names)
215 * @param string $str name to be converted
216 * @param bool $ucfirst whether to make the first character uppercase
218 * @return string identifier
220 public static function cgMakeIdentifier($str, $ucfirst = true)
222 // remove unsafe characters
223 $str = preg_replace('/[^\p{L}\p{Nl}_]/u', '', $str);
224 // make sure first character is a letter or _
225 if (! preg_match('/^\pL/u', $str)) {
226 $str = '_' . $str;
228 if ($ucfirst) {
229 $str = ucfirst($str);
231 return $str;
235 * C# Handler
237 * @param string $db database name
238 * @param string $table table name
239 * @param string $crlf line separator
241 * @return string containing C# code lines, separated by "\n"
243 private function _handleNHibernateCSBody($db, $table, $crlf)
245 $lines = array();
247 $result = PMA_DBI_query(
248 sprintf(
249 'DESC %s.%s', PMA_Util::backquote($db),
250 PMA_Util::backquote($table)
253 if ($result) {
254 $tableProperties = array();
255 while ($row = PMA_DBI_fetch_row($result)) {
256 $tableProperties[] = new TableProperty($row);
258 PMA_DBI_free_result($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);
264 $lines[] = '{';
265 $lines[] = ' #region ' . ExportCodegen::cgMakeIdentifier($table);
266 $lines[] = ' public class ' . ExportCodegen::cgMakeIdentifier($table);
267 $lines[] = ' {';
268 $lines[] = ' #region Member Variables';
269 foreach ($tableProperties as $tableProperty) {
270 $lines[] = $tableProperty->formatCs(
271 ' protected #dotNetPrimitiveType# _#name#;'
274 $lines[] = ' #endregion';
275 $lines[] = ' #region Constructors';
276 $lines[] = ' public '
277 . ExportCodegen::cgMakeIdentifier($table) . '() { }';
278 $temp = array();
279 foreach ($tableProperties as $tableProperty) {
280 if (! $tableProperty->isPK()) {
281 $temp[] = $tableProperty->formatCs(
282 '#dotNetPrimitiveType# #name#'
286 $lines[] = ' public '
287 . ExportCodegen::cgMakeIdentifier($table)
288 . '('
289 . implode(', ', $temp)
290 . ')';
291 $lines[] = ' {';
292 foreach ($tableProperties as $tableProperty) {
293 if (! $tableProperty->isPK()) {
294 $lines[] = $tableProperty->formatCs(
295 ' this._#name#=#name#;'
299 $lines[] = ' }';
300 $lines[] = ' #endregion';
301 $lines[] = ' #region Public Properties';
302 foreach ($tableProperties as $tableProperty) {
303 $lines[] = $tableProperty->formatCs(
304 ' public virtual #dotNetPrimitiveType# #ucfirstName#'
305 . "\n"
306 . ' {' . "\n"
307 . ' get {return _#name#;}' . "\n"
308 . ' set {_#name#=value;}' . "\n"
309 . ' }'
312 $lines[] = ' #endregion';
313 $lines[] = ' }';
314 $lines[] = ' #endregion';
315 $lines[] = '}';
317 return implode("\n", $lines);
321 * XML Handler
323 * @param string $db database name
324 * @param string $table table name
325 * @param string $crlf line separator
327 * @return string containing XML code lines, separated by "\n"
329 private function _handleNHibernateXMLBody($db, $table, $crlf)
331 $lines = array();
332 $lines[] = '<?xml version="1.0" encoding="utf-8" ?' . '>';
333 $lines[] = '<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" '
334 . 'namespace="' . ExportCodegen::cgMakeIdentifier($db) . '" '
335 . 'assembly="' . ExportCodegen::cgMakeIdentifier($db) . '">';
336 $lines[] = ' <class '
337 . 'name="' . ExportCodegen::cgMakeIdentifier($table) . '" '
338 . 'table="' . ExportCodegen::cgMakeIdentifier($table) . '">';
339 $result = PMA_DBI_query(
340 sprintf(
341 "DESC %s.%s", PMA_Util::backquote($db),
342 PMA_Util::backquote($table)
345 if ($result) {
346 while ($row = PMA_DBI_fetch_row($result)) {
347 $tableProperty = new TableProperty($row);
348 if ($tableProperty->isPK()) {
349 $lines[] = $tableProperty->formatXml(
350 ' <id name="#ucfirstName#" type="#dotNetObjectType#"'
351 . ' unsaved-value="0">' . "\n"
352 . ' <column name="#name#" sql-type="#type#"'
353 . ' not-null="#notNull#" unique="#unique#"'
354 . ' index="PRIMARY"/>' . "\n"
355 . ' <generator class="native" />' . "\n"
356 . ' </id>'
358 } else {
359 $lines[] = $tableProperty->formatXml(
360 ' <property name="#ucfirstName#"'
361 . ' type="#dotNetObjectType#">' . "\n"
362 . ' <column name="#name#" sql-type="#type#"'
363 . ' not-null="#notNull#" #indexName#/>' . "\n"
364 . ' </property>'
368 PMA_DBI_free_result($result);
370 $lines[] = ' </class>';
371 $lines[] = '</hibernate-mapping>';
372 return implode("\n", $lines);
376 /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
380 * Getter for CodeGen formats
382 * @return array
384 private function _getCgFormats()
386 return $this->_cgFormats;
390 * Setter for CodeGen formats
392 * @param array $CG_FORMATS contains CodeGen Formats
394 * @return void
396 private function _setCgFormats($CG_FORMATS)
398 $this->_cgFormats = $CG_FORMATS;
402 * Getter for CodeGen handlers
404 * @return array
406 private function _getCgHandlers()
408 return $this->_cgHandlers;
412 * Setter for CodeGen handlers
414 * @param array $CG_HANDLERS contains CodeGen handler methods
416 * @return void
418 private function _setCgHandlers($CG_HANDLERS)
420 $this->_cgHandlers = $CG_HANDLERS;