diff --git a/libraries/plugins/ExportPlugin.class.php b/libraries/plugins/ExportPlugin.class.php index ecfde15f98..66af3e9bf2 100644 --- a/libraries/plugins/ExportPlugin.class.php +++ b/libraries/plugins/ExportPlugin.class.php @@ -52,11 +52,12 @@ abstract class ExportPlugin extends PluginObserver /** * Outputs database header * - * @param string $db Database name + * @param string $db Database name + * @param string $db_alias Aliases of db * * @return bool Whether it succeeded */ - abstract public function exportDBHeader ($db); + abstract public function exportDBHeader ($db, $db_alias = ''); /** * Outputs database footer @@ -70,11 +71,12 @@ abstract class ExportPlugin extends PluginObserver /** * Outputs CREATE DATABASE statement * - * @param string $db Database name + * @param string $db Database name + * @param string $db_alias Aliases of db * * @return bool Whether it succeeded */ - abstract public function exportDBCreate($db); + abstract public function exportDBCreate($db, $db_alias = ''); /** * Outputs the content of a table @@ -84,26 +86,28 @@ abstract class ExportPlugin extends PluginObserver * @param string $crlf the end of line sequence * @param string $error_url the url to go back in case of error * @param string $sql_query SQL query for obtaining data + * @param array $aliases Aliases of db/table/columns * * @return bool Whether it succeeded */ - abstract public function exportData ($db, $table, $crlf, $error_url, $sql_query); - + abstract public function exportData ( + $db, $table, $crlf, $error_url, $sql_query, $aliases = array() + ); /** * The following methods are used in export.php or in db_operations.php, * but they are not implemented by all export plugins */ - /** * Exports routines (procedures and functions) * - * @param string $db Database + * @param string $db Database + * @param array $aliases Aliases of db/table/columns * * @return bool Whether it succeeded */ - public function exportRoutines($db) + public function exportRoutines($db, $aliases = array()) { ; } @@ -126,6 +130,7 @@ abstract class ExportPlugin extends PluginObserver * types which use this parameter * @param bool $mime whether to include mime comments * @param bool $dates whether to include creation/update/check dates + * @param array $aliases Aliases of db/table/columns * * @return bool Whether it succeeded */ @@ -139,7 +144,8 @@ abstract class ExportPlugin extends PluginObserver $relation = false, $comments = false, $mime = false, - $dates = false + $dates = false, + $aliases = array() ) { ; } @@ -147,13 +153,14 @@ abstract class ExportPlugin extends PluginObserver /** * Returns a stand-in CREATE definition to resolve view dependencies * - * @param string $db the database name - * @param string $view the view name - * @param string $crlf the end of line sequence + * @param string $db the database name + * @param string $view the view name + * @param string $crlf the end of line sequence + * @param array $aliases Aliases of db/table/columns * * @return string resulting definition */ - public function getTableDefStandIn($db, $view, $crlf) + public function getTableDefStandIn($db, $view, $crlf, $aliases = array()) { ; } @@ -181,10 +188,8 @@ abstract class ExportPlugin extends PluginObserver ; } - /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ - /** * Gets the export specific format plugin properties * @@ -202,5 +207,122 @@ abstract class ExportPlugin extends PluginObserver * @return void */ abstract protected function setProperties(); + + /** + * The following methods are implemented here so that they + * can be used by all export plugin without overriding it. + * Note: If you are creating a export plugin then dont include + * below methods unless you want to override them. + */ + + /** + * Initialize aliases + * + * @param array $aliases Alias information for db/table/column + * @param string &$db the database + * @param string &$table the table + * + * @return nothing + */ + public function initAlias($aliases, &$db, &$table = null) + { + if (!empty($aliases[$db]['tables'][$table]['alias'])) { + $table = $aliases[$db]['tables'][$table]['alias']; + } + if (!empty($aliases[$db]['alias'])) { + $db = $aliases[$db]['alias']; + } + } + + /** + * Search for alias of a identifier. + * + * @param array $aliases Alias information for db/table/column + * @param string $id the identifier to be searched + * @param string $type db/tbl/col or any combination of them + * representing what to be searched + * @param string $db the database in which search is to be done + * @param string $tbl the table in which search is to be done + * + * @return string alias of the identifier if found or '' + */ + public function getAlias($aliases, $id, $type = 'dbtblcol', $db = '', $tbl = '') + { + if (!empty($db) && isset($aliases[$db])) { + $aliases = array( + $db => $aliases[$db] + ); + } + // search each database + foreach ($aliases as $db_key => $db) { + // check if id is database and has alias + if (stristr($type, 'db') !== false + && $db_key === $id && !empty($db['alias']) + ) { + return $db['alias']; + } + if (empty($db['tables'])) { + continue; + } + if (!empty($tbl) && isset($db['tables'][$tbl])) { + $db['tables'] = array( + $tbl => $db['tables'][$tbl] + ); + } + // search each of its tables + foreach ($db['tables'] as $table_key => $table) { + // check if id is table and has alias + if (stristr($type, 'tbl') !== false + && $table_key === $id && !empty($table['alias']) + ) { + return $table['alias']; + } + if (empty($table['columns'])) { + continue; + } + // search each of its columns + foreach ($table['columns'] as $col_key => $col) { + // check if id is column + if (stristr($type, 'col') !== false + && $col_key === $id && !empty($col) + ) { + return $col; + } + } + } + } + return ''; + } + + /** + * Gives the relation string and + * also substitutes with alias if required + * in this format: + * [Foreign Table] ([Foreign Field]) + * + * @param array $res_rel the foreigners array + * @param string $field_name the field name + * @param string $db the field name + * @param array $aliases Alias information for db/table/column + * + * @return string the Relation string + */ + public function getRelationString( + $res_rel, $field_name, $db, $aliases = array() + ) { + $relation = ''; + if (isset($res_rel[$field_name])) { + $ftable = $res_rel[$field_name]['foreign_table']; + $ffield = $res_rel[$field_name]['foreign_field']; + if (!empty($aliases[$db]['tables'][$ftable]['columns'][$ffield])) { + $ffield = $aliases[$db]['tables'][$ftable]['columns'][$ffield]; + } + if (!empty($aliases[$db]['tables'][$ftable]['alias'])) { + $ftable = $aliases[$db]['tables'][$ftable]['alias']; + } + $relation = $ftable . ' (' . $ffield . ')'; + } + return $relation; + } } -?> \ No newline at end of file +?> diff --git a/libraries/plugins/export/ExportCodegen.class.php b/libraries/plugins/export/ExportCodegen.class.php index 4495e7ee4d..b616144ca6 100644 --- a/libraries/plugins/export/ExportCodegen.class.php +++ b/libraries/plugins/export/ExportCodegen.class.php @@ -151,11 +151,12 @@ class ExportCodegen extends ExportPlugin /** * Outputs database header * - * @param string $db Database name + * @param string $db Database name + * @param string $db_alias Aliases of db * * @return bool Whether it succeeded */ - public function exportDBHeader ($db) + public function exportDBHeader ($db, $db_alias = '') { return true; } @@ -175,11 +176,12 @@ class ExportCodegen extends ExportPlugin /** * Outputs CREATE DATABASE statement * - * @param string $db Database name + * @param string $db Database name + * @param string $db_alias Aliases of db * * @return bool Whether it succeeded */ - public function exportDBCreate($db) + public function exportDBCreate($db, $db_alias = '') { return true; } @@ -192,18 +194,20 @@ class ExportCodegen extends ExportPlugin * @param string $crlf the end of line sequence * @param string $error_url the url to go back in case of error * @param string $sql_query SQL query for obtaining data + * @param array $aliases Aliases of db/table/columns * * @return bool Whether it succeeded */ - public function exportData($db, $table, $crlf, $error_url, $sql_query) - { + public function exportData( + $db, $table, $crlf, $error_url, $sql_query, $aliases = array() + ) { $CG_FORMATS = $this->_getCgFormats(); $CG_HANDLERS = $this->_getCgHandlers(); $format = $GLOBALS['codegen_format']; if (isset($CG_FORMATS[$format])) { return PMA_exportOutputHandler( - $this->$CG_HANDLERS[$format]($db, $table, $crlf) + $this->$CG_HANDLERS[$format]($db, $table, $crlf, $aliases) ); } return PMA_exportOutputHandler(sprintf("%s is not supported.", $format)); @@ -234,14 +238,18 @@ class ExportCodegen extends ExportPlugin /** * C# Handler * - * @param string $db database name - * @param string $table table name - * @param string $crlf line separator + * @param string $db database name + * @param string $table table name + * @param string $crlf line separator + * @param array $aliases Aliases of db/table/columns * * @return string containing C# code lines, separated by "\n" */ - private function _handleNHibernateCSBody($db, $table, $crlf) + private function _handleNHibernateCSBody($db, $table, $crlf, $aliases = array()) { + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); $lines = array(); $result = $GLOBALS['dbi']->query( @@ -253,6 +261,10 @@ class ExportCodegen extends ExportPlugin if ($result) { $tableProperties = array(); while ($row = $GLOBALS['dbi']->fetchRow($result)) { + $col_as = $this->getAlias($aliases, $row[0], 'col', $db, $table); + if (!empty($col_as)) { + $row[0] = $col_as; + } $tableProperties[] = new TableProperty($row); } $GLOBALS['dbi']->freeResult($result); @@ -260,10 +272,12 @@ class ExportCodegen extends ExportPlugin $lines[] = 'using System.Collections;'; $lines[] = 'using System.Collections.Generic;'; $lines[] = 'using System.Text;'; - $lines[] = 'namespace ' . ExportCodegen::cgMakeIdentifier($db); + $lines[] = 'namespace ' . ExportCodegen::cgMakeIdentifier($db_alias); $lines[] = '{'; - $lines[] = ' #region ' . ExportCodegen::cgMakeIdentifier($table); - $lines[] = ' public class ' . ExportCodegen::cgMakeIdentifier($table); + $lines[] = ' #region ' + . ExportCodegen::cgMakeIdentifier($table_alias); + $lines[] = ' public class ' + . ExportCodegen::cgMakeIdentifier($table_alias); $lines[] = ' {'; $lines[] = ' #region Member Variables'; foreach ($tableProperties as $tableProperty) { @@ -274,7 +288,7 @@ class ExportCodegen extends ExportPlugin $lines[] = ' #endregion'; $lines[] = ' #region Constructors'; $lines[] = ' public ' - . ExportCodegen::cgMakeIdentifier($table) . '() { }'; + . ExportCodegen::cgMakeIdentifier($table_alias) . '() { }'; $temp = array(); foreach ($tableProperties as $tableProperty) { if (! $tableProperty->isPK()) { @@ -284,7 +298,7 @@ class ExportCodegen extends ExportPlugin } } $lines[] = ' public ' - . ExportCodegen::cgMakeIdentifier($table) + . ExportCodegen::cgMakeIdentifier($table_alias) . '(' . implode(', ', $temp) . ')'; @@ -314,28 +328,33 @@ class ExportCodegen extends ExportPlugin $lines[] = ' #endregion'; $lines[] = '}'; } - return implode("\n", $lines); + return implode($crlf, $lines); } /** * XML Handler * - * @param string $db database name - * @param string $table table name - * @param string $crlf line separator + * @param string $db database name + * @param string $table table name + * @param string $crlf line separator + * @param array $aliases Aliases of db/table/columns * * @return string containing XML code lines, separated by "\n" */ - private function _handleNHibernateXMLBody($db, $table, $crlf) - { + private function _handleNHibernateXMLBody( + $db, $table, $crlf, $aliases = array() + ) { + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); $lines = array(); $lines[] = ''; $lines[] = ''; + . 'namespace="' . ExportCodegen::cgMakeIdentifier($db_alias) . '" ' + . 'assembly="' . ExportCodegen::cgMakeIdentifier($db_alias) . '">'; $lines[] = ' '; + . 'name="' . ExportCodegen::cgMakeIdentifier($table_alias) . '" ' + . 'table="' . ExportCodegen::cgMakeIdentifier($table_alias) . '">'; $result = $GLOBALS['dbi']->query( sprintf( "DESC %s.%s", PMA_Util::backquote($db), @@ -344,6 +363,10 @@ class ExportCodegen extends ExportPlugin ); if ($result) { while ($row = $GLOBALS['dbi']->fetchRow($result)) { + $col_as = $this->getAlias($aliases, $row[0], 'col', $db, $table); + if (!empty($col_as)) { + $row[0] = $col_as; + } $tableProperty = new TableProperty($row); if ($tableProperty->isPK()) { $lines[] = $tableProperty->formatXml( @@ -369,7 +392,7 @@ class ExportCodegen extends ExportPlugin } $lines[] = ' '; $lines[] = ''; - return implode("\n", $lines); + return implode($crlf, $lines); } diff --git a/libraries/plugins/export/ExportCsv.class.php b/libraries/plugins/export/ExportCsv.class.php index 22cbd63121..917f84aeee 100644 --- a/libraries/plugins/export/ExportCsv.class.php +++ b/libraries/plugins/export/ExportCsv.class.php @@ -170,11 +170,12 @@ class ExportCsv extends ExportPlugin /** * Outputs database header * - * @param string $db Database name + * @param string $db Database name + * @param string $db_alias Alias of db * * @return bool Whether it succeeded */ - public function exportDBHeader ($db) + public function exportDBHeader ($db, $db_alias = '') { return true; } @@ -194,11 +195,12 @@ class ExportCsv extends ExportPlugin /** * Outputs CREATE DATABASE statement * - * @param string $db Database name + * @param string $db Database name + * @param string $db_alias Alias of db * * @return bool Whether it succeeded */ - public function exportDBCreate($db) + public function exportDBCreate($db, $db_alias = '') { return true; } @@ -211,13 +213,19 @@ class ExportCsv extends ExportPlugin * @param string $crlf the end of line sequence * @param string $error_url the url to go back in case of error * @param string $sql_query SQL query for obtaining data + * @param array $aliases Aliases of db/table/columns * * @return bool Whether it succeeded */ - public function exportData($db, $table, $crlf, $error_url, $sql_query) - { + public function exportData( + $db, $table, $crlf, $error_url, $sql_query, $aliases = array() + ) { global $what, $csv_terminated, $csv_separator, $csv_enclosed, $csv_escaped; + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + // Gets the data from the database $result = $GLOBALS['dbi']->query( $sql_query, null, PMA_DatabaseInterface::QUERY_UNBUFFERED @@ -228,16 +236,19 @@ class ExportCsv extends ExportPlugin if (isset($GLOBALS['csv_columns'])) { $schema_insert = ''; for ($i = 0; $i < $fields_cnt; $i++) { + $col_as = $GLOBALS['dbi']->fieldName($result, $i); + if (!empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; + } + $col_as = stripslashes($col_as); if ($csv_enclosed == '') { - $schema_insert .= stripslashes( - $GLOBALS['dbi']->fieldName($result, $i) - ); + $schema_insert .= $col_as; } else { $schema_insert .= $csv_enclosed . str_replace( $csv_enclosed, $csv_escaped . $csv_enclosed, - stripslashes($GLOBALS['dbi']->fieldName($result, $i)) + $col_as ) . $csv_enclosed; } diff --git a/libraries/plugins/export/ExportHtmlword.class.php b/libraries/plugins/export/ExportHtmlword.class.php index edd24aa372..6effe0151f 100644 --- a/libraries/plugins/export/ExportHtmlword.class.php +++ b/libraries/plugins/export/ExportHtmlword.class.php @@ -148,14 +148,18 @@ class ExportHtmlword extends ExportPlugin /** * Outputs database header * - * @param string $db Database name + * @param string $db Database name + * @param string $db_alias Aliases of db * * @return bool Whether it succeeded */ - public function exportDBHeader ($db) + public function exportDBHeader ($db, $db_alias = '') { + if (empty($db_alias)) { + $db_alias = $db; + } return PMA_exportOutputHandler( - '

' . __('Database') . ' ' . htmlspecialchars($db) . '

' + '

' . __('Database') . ' ' . htmlspecialchars($db_alias) . '

' ); } @@ -174,11 +178,12 @@ class ExportHtmlword extends ExportPlugin /** * Outputs CREATE DATABASE statement * - * @param string $db Database name + * @param string $db Database name + * @param string $db_alias Aliases of db * * @return bool Whether it succeeded */ - public function exportDBCreate($db) + public function exportDBCreate($db, $db_alias = '') { return true; } @@ -191,16 +196,22 @@ class ExportHtmlword extends ExportPlugin * @param string $crlf the end of line sequence * @param string $error_url the url to go back in case of error * @param string $sql_query SQL query for obtaining data + * @param array $aliases Aliases of db/table/columns * * @return bool Whether it succeeded */ - public function exportData($db, $table, $crlf, $error_url, $sql_query) - { + public function exportData( + $db, $table, $crlf, $error_url, $sql_query, $aliases = array() + ) { global $what; + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + if (! PMA_exportOutputHandler( '

' - . __('Dumping data for table') . ' ' . htmlspecialchars($table) + . __('Dumping data for table') . ' ' . htmlspecialchars($table_alias) . '

' )) { return false; @@ -221,10 +232,13 @@ class ExportHtmlword extends ExportPlugin if (isset($GLOBALS['htmlword_columns'])) { $schema_insert = ''; for ($i = 0; $i < $fields_cnt; $i++) { + $col_as = $GLOBALS['dbi']->fieldName($result, $i); + if (!empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; + } + $col_as = stripslashes($col_as); $schema_insert .= '' - . htmlspecialchars( - stripslashes($GLOBALS['dbi']->fieldName($result, $i)) - ) + . htmlspecialchars($col_as) . ''; } // end for $schema_insert .= ''; @@ -264,13 +278,14 @@ class ExportHtmlword extends ExportPlugin /** * Returns a stand-in CREATE definition to resolve view dependencies * - * @param string $db the database name - * @param string $view the view name - * @param string $crlf the end of line sequence + * @param string $db the database name + * @param string $view the view name + * @param string $crlf the end of line sequence + * @param array $aliases Aliases of db/table/columns * * @return string resulting definition */ - public function getTableDefStandIn($db, $view, $crlf) + public function getTableDefStandIn($db, $view, $crlf, $aliases = array()) { $schema_insert = '' . '' @@ -301,9 +316,14 @@ class ExportHtmlword extends ExportPlugin $columns = $GLOBALS['dbi']->getColumns($db, $view); foreach ($columns as $column) { + $col_as = $column['Field']; + if (!empty($aliases[$db]['tables'][$view]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$view]['columns'][$col_as]; + } $schema_insert .= $this->formatOneColumnDefinition( $column, - $unique_keys + $unique_keys, + $col_as ); $schema_insert .= ''; } @@ -331,6 +351,7 @@ class ExportHtmlword extends ExportPlugin * @param bool $add_semicolon whether to add semicolon and end-of-line * at the end * @param bool $view whether we're handling a view + * @param array $aliases Aliases of db/table/columns * * @return string resulting schema */ @@ -344,7 +365,8 @@ class ExportHtmlword extends ExportPlugin $do_mime, $show_dates = false, $add_semicolon = true, - $view = false + $view = false, + $aliases = array() ) { // set $cfgRelation here, because there is a chance that it's modified // since the class initialization @@ -356,7 +378,7 @@ class ExportHtmlword extends ExportPlugin * Gets fields properties */ $GLOBALS['dbi']->selectDb($db); - + $res_rel = array(); // Check if we can use Relations if ($do_relation && ! empty($cfgRelation['relation'])) { // Find which tables are related with the current one and write it in @@ -432,21 +454,24 @@ class ExportHtmlword extends ExportPlugin } } foreach ($columns as $column) { + $col_as = $column['Field']; + if (!empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; + } $schema_insert .= $this->formatOneColumnDefinition( $column, - $unique_keys + $unique_keys, + $col_as ); $field_name = $column['Field']; - if ($do_relation && $have_rel) { $schema_insert .= ''; + ) + . ''; } if ($do_comments && $cfgRelation['commwork']) { $schema_insert .= ''; $extracted_columnspec @@ -625,7 +662,7 @@ class ExportHtmlword extends ExportPlugin $fmt_post = $fmt_post . ''; } $definition .= ''; + . htmlspecialchars($col_alias) . $fmt_post . ''; $definition .= ''; $definition .= '
' - . (isset($res_rel[$field_name]) - ? htmlspecialchars( - $res_rel[$field_name]['foreign_table'] - . ' (' . $res_rel[$field_name]['foreign_field'] - . ')' + . htmlspecialchars( + $this->getRelationString( + $res_rel, $field_name, $db, $aliases ) - : '') . '' @@ -530,6 +555,7 @@ class ExportHtmlword extends ExportPlugin * export types which use this parameter * @param bool $do_mime whether to include mime comments * @param bool $dates whether to include creation/update/check dates + * @param array $aliases Aliases of db/table/columns * * @return bool Whether it succeeded */ @@ -543,18 +569,24 @@ class ExportHtmlword extends ExportPlugin $do_relation = false, $do_comments = false, $do_mime = false, - $dates = false + $dates = false, + $aliases = array() ) { + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + $dump = ''; switch($export_mode) { case 'create_table': $dump .= '

' - . __('Table structure for table') . ' ' . htmlspecialchars($table) + . __('Table structure for table') . ' ' + . htmlspecialchars($table_alias) . '

'; $dump .= $this->getTableDef( $db, $table, $crlf, $error_url, $do_relation, $do_comments, $do_mime, - $dates + $dates, true, false, $aliases ); break; case 'triggers': @@ -562,26 +594,27 @@ class ExportHtmlword extends ExportPlugin $triggers = $GLOBALS['dbi']->getTriggers($db, $table); if ($triggers) { $dump .= '

' - . __('Triggers') . ' ' . htmlspecialchars($table) + . __('Triggers') . ' ' . htmlspecialchars($table_alias) . '

'; $dump .= $this->getTriggers($db, $table); } break; case 'create_view': $dump .= '

' - . __('Structure for view') . ' ' . htmlspecialchars($table) + . __('Structure for view') . ' ' . htmlspecialchars($table_alias) . '

'; $dump .= $this->getTableDef( $db, $table, $crlf, $error_url, $do_relation, $do_comments, $do_mime, - $dates, true, true + $dates, true, true, $aliases ); break; case 'stand_in': $dump .= '

' - . __('Stand-in structure for view') . ' ' . htmlspecialchars($table) + . __('Stand-in structure for view') . ' ' + . htmlspecialchars($table_alias) . '

'; // export a stand-in definition to resolve view dependencies - $dump .= $this->getTableDefStandIn($db, $table, $crlf); + $dump .= $this->getTableDefStandIn($db, $table, $crlf, $aliases); } // end switch return PMA_exportOutputHandler($dump); @@ -590,14 +623,18 @@ class ExportHtmlword extends ExportPlugin /** * Formats the definition for one column * - * @param array $column info about this column - * @param array $unique_keys unique keys of the table + * @param array $column info about this column + * @param array $unique_keys unique keys of the table + * @param string $col_alias Column Alias * * @return string Formatted column definition */ protected function formatOneColumnDefinition( - $column, $unique_keys + $column, $unique_keys, $col_alias = '' ) { + if (empty($col_alias)) { + $col_alias = $column['Field']; + } $definition = '