ExportCodegen.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Set of functions used to build NHibernate dumps of tables
  5. *
  6. * @package PhpMyAdmin-Export
  7. * @subpackage CodeGen
  8. */
  9. if (! defined('PHPMYADMIN')) {
  10. exit;
  11. }
  12. /* Get the export interface */
  13. require_once 'libraries/plugins/ExportPlugin.class.php';
  14. /* Get the table property class */
  15. require_once 'libraries/plugins/export/TableProperty.class.php';
  16. /**
  17. * Handles the export for the CodeGen class
  18. *
  19. * @package PhpMyAdmin-Export
  20. * @subpackage CodeGen
  21. */
  22. class ExportCodegen extends ExportPlugin
  23. {
  24. /**
  25. * CodeGen Formats
  26. *
  27. * @var array
  28. */
  29. private $_cgFormats;
  30. /**
  31. * CodeGen Handlers
  32. *
  33. * @var array
  34. */
  35. private $_cgHandlers;
  36. /**
  37. * Constructor
  38. */
  39. public function __construct()
  40. {
  41. // initialize the specific export CodeGen variables
  42. $this->initSpecificVariables();
  43. $this->setProperties();
  44. }
  45. /**
  46. * Initialize the local variables that are used for export CodeGen
  47. *
  48. * @return void
  49. */
  50. protected function initSpecificVariables()
  51. {
  52. $this->_setCgFormats(
  53. array(
  54. "NHibernate C# DO",
  55. "NHibernate XML"
  56. )
  57. );
  58. $this->_setCgHandlers(
  59. array(
  60. "_handleNHibernateCSBody",
  61. "_handleNHibernateXMLBody"
  62. )
  63. );
  64. }
  65. /**
  66. * Sets the export CodeGen properties
  67. *
  68. * @return void
  69. */
  70. protected function setProperties()
  71. {
  72. $props = 'libraries/properties/';
  73. include_once "$props/plugins/ExportPluginProperties.class.php";
  74. include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
  75. include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
  76. include_once "$props/options/items/HiddenPropertyItem.class.php";
  77. include_once "$props/options/items/SelectPropertyItem.class.php";
  78. $exportPluginProperties = new ExportPluginProperties();
  79. $exportPluginProperties->setText('CodeGen');
  80. $exportPluginProperties->setExtension('cs');
  81. $exportPluginProperties->setMimeType('text/cs');
  82. $exportPluginProperties->setOptionsText(__('Options'));
  83. // create the root group that will be the options field for
  84. // $exportPluginProperties
  85. // this will be shown as "Format specific options"
  86. $exportSpecificOptions = new OptionsPropertyRootGroup();
  87. $exportSpecificOptions->setName("Format Specific Options");
  88. // general options main group
  89. $generalOptions = new OptionsPropertyMainGroup();
  90. $generalOptions->setName("general_opts");
  91. // create primary items and add them to the group
  92. $leaf = new HiddenPropertyItem();
  93. $leaf->setName("structure_or_data");
  94. $generalOptions->addProperty($leaf);
  95. $leaf = new SelectPropertyItem();
  96. $leaf->setName("format");
  97. $leaf->setText(__('Format:'));
  98. $leaf->setValues($this->_getCgFormats());
  99. $generalOptions->addProperty($leaf);
  100. // add the main group to the root group
  101. $exportSpecificOptions->addProperty($generalOptions);
  102. // set the options for the export plugin property item
  103. $exportPluginProperties->setOptions($exportSpecificOptions);
  104. $this->properties = $exportPluginProperties;
  105. }
  106. /**
  107. * This method is called when any PluginManager to which the observer
  108. * is attached calls PluginManager::notify()
  109. *
  110. * @param SplSubject $subject The PluginManager notifying the observer
  111. * of an update.
  112. *
  113. * @return void
  114. */
  115. public function update (SplSubject $subject)
  116. {
  117. }
  118. /**
  119. * Outputs export header
  120. *
  121. * @return bool Whether it succeeded
  122. */
  123. public function exportHeader ()
  124. {
  125. return true;
  126. }
  127. /**
  128. * Outputs export footer
  129. *
  130. * @return bool Whether it succeeded
  131. */
  132. public function exportFooter ()
  133. {
  134. return true;
  135. }
  136. /**
  137. * Outputs database header
  138. *
  139. * @param string $db Database name
  140. *
  141. * @return bool Whether it succeeded
  142. */
  143. public function exportDBHeader ($db)
  144. {
  145. return true;
  146. }
  147. /**
  148. * Outputs database footer
  149. *
  150. * @param string $db Database name
  151. *
  152. * @return bool Whether it succeeded
  153. */
  154. public function exportDBFooter ($db)
  155. {
  156. return true;
  157. }
  158. /**
  159. * Outputs CREATE DATABASE statement
  160. *
  161. * @param string $db Database name
  162. *
  163. * @return bool Whether it succeeded
  164. */
  165. public function exportDBCreate($db)
  166. {
  167. return true;
  168. }
  169. /**
  170. * Outputs the content of a table in NHibernate format
  171. *
  172. * @param string $db database name
  173. * @param string $table table name
  174. * @param string $crlf the end of line sequence
  175. * @param string $error_url the url to go back in case of error
  176. * @param string $sql_query SQL query for obtaining data
  177. *
  178. * @return bool Whether it succeeded
  179. */
  180. public function exportData($db, $table, $crlf, $error_url, $sql_query)
  181. {
  182. $CG_FORMATS = $this->_getCgFormats();
  183. $CG_HANDLERS = $this->_getCgHandlers();
  184. $format = $GLOBALS['codegen_format'];
  185. if (isset($CG_FORMATS[$format])) {
  186. return PMA_exportOutputHandler(
  187. $this->$CG_HANDLERS[$format]($db, $table, $crlf)
  188. );
  189. }
  190. return PMA_exportOutputHandler(sprintf("%s is not supported.", $format));
  191. }
  192. /**
  193. * Used to make identifiers (from table or database names)
  194. *
  195. * @param string $str name to be converted
  196. * @param bool $ucfirst whether to make the first character uppercase
  197. *
  198. * @return string identifier
  199. */
  200. public static function cgMakeIdentifier($str, $ucfirst = true)
  201. {
  202. // remove unsafe characters
  203. $str = preg_replace('/[^\p{L}\p{Nl}_]/u', '', $str);
  204. // make sure first character is a letter or _
  205. if (! preg_match('/^\pL/u', $str)) {
  206. $str = '_' . $str;
  207. }
  208. if ($ucfirst) {
  209. $str = ucfirst($str);
  210. }
  211. return $str;
  212. }
  213. /**
  214. * C# Handler
  215. *
  216. * @param string $db database name
  217. * @param string $table table name
  218. * @param string $crlf line separator
  219. *
  220. * @return string containing C# code lines, separated by "\n"
  221. */
  222. private function _handleNHibernateCSBody($db, $table, $crlf)
  223. {
  224. $lines = array();
  225. $result = PMA_DBI_query(
  226. sprintf(
  227. 'DESC %s.%s', PMA_Util::backquote($db),
  228. PMA_Util::backquote($table)
  229. )
  230. );
  231. if ($result) {
  232. $tableProperties = array();
  233. while ($row = PMA_DBI_fetch_row($result)) {
  234. $tableProperties[] = new TableProperty($row);
  235. }
  236. PMA_DBI_free_result($result);
  237. $lines[] = 'using System;';
  238. $lines[] = 'using System.Collections;';
  239. $lines[] = 'using System.Collections.Generic;';
  240. $lines[] = 'using System.Text;';
  241. $lines[] = 'namespace ' . ExportCodegen::cgMakeIdentifier($db);
  242. $lines[] = '{';
  243. $lines[] = ' #region ' . ExportCodegen::cgMakeIdentifier($table);
  244. $lines[] = ' public class ' . ExportCodegen::cgMakeIdentifier($table);
  245. $lines[] = ' {';
  246. $lines[] = ' #region Member Variables';
  247. foreach ($tableProperties as $tableProperty) {
  248. $lines[] = $tableProperty->formatCs(
  249. ' protected #dotNetPrimitiveType# _#name#;'
  250. );
  251. }
  252. $lines[] = ' #endregion';
  253. $lines[] = ' #region Constructors';
  254. $lines[] = ' public '
  255. . ExportCodegen::cgMakeIdentifier($table) . '() { }';
  256. $temp = array();
  257. foreach ($tableProperties as $tableProperty) {
  258. if (! $tableProperty->isPK()) {
  259. $temp[] = $tableProperty->formatCs(
  260. '#dotNetPrimitiveType# #name#'
  261. );
  262. }
  263. }
  264. $lines[] = ' public '
  265. . ExportCodegen::cgMakeIdentifier($table)
  266. . '('
  267. . implode(', ', $temp)
  268. . ')';
  269. $lines[] = ' {';
  270. foreach ($tableProperties as $tableProperty) {
  271. if (! $tableProperty->isPK()) {
  272. $lines[] = $tableProperty->formatCs(
  273. ' this._#name#=#name#;'
  274. );
  275. }
  276. }
  277. $lines[] = ' }';
  278. $lines[] = ' #endregion';
  279. $lines[] = ' #region Public Properties';
  280. foreach ($tableProperties as $tableProperty) {
  281. $lines[] = $tableProperty->formatCs(
  282. ' public virtual #dotNetPrimitiveType# #ucfirstName#'
  283. . "\n"
  284. . ' {' . "\n"
  285. . ' get {return _#name#;}' . "\n"
  286. . ' set {_#name#=value;}' . "\n"
  287. . ' }'
  288. );
  289. }
  290. $lines[] = ' #endregion';
  291. $lines[] = ' }';
  292. $lines[] = ' #endregion';
  293. $lines[] = '}';
  294. }
  295. return implode("\n", $lines);
  296. }
  297. /**
  298. * XML Handler
  299. *
  300. * @param string $db database name
  301. * @param string $table table name
  302. * @param string $crlf line separator
  303. *
  304. * @return string containing XML code lines, separated by "\n"
  305. */
  306. private function _handleNHibernateXMLBody($db, $table, $crlf)
  307. {
  308. $lines = array();
  309. $lines[] = '<?xml version="1.0" encoding="utf-8" ?' . '>';
  310. $lines[] = '<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" '
  311. . 'namespace="' . ExportCodegen::cgMakeIdentifier($db) . '" '
  312. . 'assembly="' . ExportCodegen::cgMakeIdentifier($db) . '">';
  313. $lines[] = ' <class '
  314. . 'name="' . ExportCodegen::cgMakeIdentifier($table) . '" '
  315. . 'table="' . ExportCodegen::cgMakeIdentifier($table) . '">';
  316. $result = PMA_DBI_query(
  317. sprintf(
  318. "DESC %s.%s", PMA_Util::backquote($db),
  319. PMA_Util::backquote($table)
  320. )
  321. );
  322. if ($result) {
  323. while ($row = PMA_DBI_fetch_row($result)) {
  324. $tableProperty = new TableProperty($row);
  325. if ($tableProperty->isPK()) {
  326. $lines[] = $tableProperty->formatXml(
  327. ' <id name="#ucfirstName#" type="#dotNetObjectType#"'
  328. . ' unsaved-value="0">' . "\n"
  329. . ' <column name="#name#" sql-type="#type#"'
  330. . ' not-null="#notNull#" unique="#unique#"'
  331. . ' index="PRIMARY"/>' . "\n"
  332. . ' <generator class="native" />' . "\n"
  333. . ' </id>'
  334. );
  335. } else {
  336. $lines[] = $tableProperty->formatXml(
  337. ' <property name="#ucfirstName#"'
  338. . ' type="#dotNetObjectType#">' . "\n"
  339. . ' <column name="#name#" sql-type="#type#"'
  340. . ' not-null="#notNull#" #indexName#/>' . "\n"
  341. . ' </property>'
  342. );
  343. }
  344. }
  345. PMA_DBI_free_result($result);
  346. }
  347. $lines[] = ' </class>';
  348. $lines[] = '</hibernate-mapping>';
  349. return implode("\n", $lines);
  350. }
  351. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  352. /**
  353. * Getter for CodeGen formats
  354. *
  355. * @return array
  356. */
  357. private function _getCgFormats()
  358. {
  359. return $this->_cgFormats;
  360. }
  361. /**
  362. * Setter for CodeGen formats
  363. *
  364. * @param array $CG_FORMATS contains CodeGen Formats
  365. *
  366. * @return void
  367. */
  368. private function _setCgFormats($CG_FORMATS)
  369. {
  370. $this->_cgFormats = $CG_FORMATS;
  371. }
  372. /**
  373. * Getter for CodeGen handlers
  374. *
  375. * @return array
  376. */
  377. private function _getCgHandlers()
  378. {
  379. return $this->_cgHandlers;
  380. }
  381. /**
  382. * Setter for CodeGen handlers
  383. *
  384. * @param array $CG_HANDLERS contains CodeGen handler methods
  385. *
  386. * @return void
  387. */
  388. private function _setCgHandlers($CG_HANDLERS)
  389. {
  390. $this->_cgHandlers = $CG_HANDLERS;
  391. }
  392. }
  393. ?>