tbl_views.lib.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Set of functions related to applying transformations for VIEWs
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Get the column details of VIEW with its original references
  13. *
  14. * @param string $sql_query SQL for original resource
  15. * @param array $view_columns Columns of VIEW if defined new column names
  16. *
  17. * @return array $column_map Details of VIEW columns
  18. */
  19. function PMA_getColumnMap($sql_query, $view_columns)
  20. {
  21. $column_map = array();
  22. // Select query which give results for VIEW
  23. $real_source_result = $GLOBALS['dbi']->tryQuery($sql_query);
  24. if ($real_source_result !== false) {
  25. $real_source_fields_meta = $GLOBALS['dbi']->getFieldsMeta(
  26. $real_source_result
  27. );
  28. $nbColumns = count($view_columns);
  29. $nbFields = count($real_source_fields_meta);
  30. if ($nbFields > 0) {
  31. for ($i=0; $i < $nbFields; $i++) {
  32. $map = array();
  33. $map['table_name'] = $real_source_fields_meta[$i]->table;
  34. $map['refering_column'] = $real_source_fields_meta[$i]->name;
  35. if ($nbColumns > 1) {
  36. $map['real_column'] = $view_columns[$i];
  37. }
  38. $column_map[] = $map;
  39. }
  40. }
  41. }
  42. unset($real_source_result);
  43. return $column_map;
  44. }
  45. /**
  46. * Get existing data on tranformations applyed for
  47. * columns in a particular table
  48. *
  49. * @param string $db Database name looking for
  50. *
  51. * @return mysqli_result Result of executed SQL query
  52. */
  53. function PMA_getExistingTranformationData($db)
  54. {
  55. $cfgRelation = PMA_getRelationsParam();
  56. // Get the existing transformation details of the same database
  57. // from pma__column_info table
  58. $pma_transformation_sql = 'SELECT * FROM '
  59. . PMA_Util::backquote($cfgRelation['db']) . '.'
  60. . PMA_Util::backquote($cfgRelation['column_info'])
  61. . ' WHERE `db_name` = \''
  62. . PMA_Util::sqlAddSlashes($db) . '\'';
  63. return $GLOBALS['dbi']->tryQuery($pma_transformation_sql);
  64. }
  65. /**
  66. * Get SQL query for store new transformation details of a VIEW
  67. *
  68. * @param mysqli_result $pma_tranformation_data Result set of SQL execution
  69. * @param array $column_map Details of VIEW columns
  70. * @param string $view_name Name of the VIEW
  71. * @param string $db Database name of the VIEW
  72. *
  73. * @return string $new_transformations_sql SQL query for new tranformations
  74. */
  75. function PMA_getNewTransformationDataSql(
  76. $pma_tranformation_data, $column_map, $view_name, $db
  77. ) {
  78. $cfgRelation = PMA_getRelationsParam();
  79. // Need to store new transformation details for VIEW
  80. $new_transformations_sql = 'INSERT INTO '
  81. . PMA_Util::backquote($cfgRelation['db']) . '.'
  82. . PMA_Util::backquote($cfgRelation['column_info'])
  83. . ' (`db_name`, `table_name`, `column_name`, `comment`, '
  84. . '`mimetype`, `transformation`, `transformation_options`)'
  85. . ' VALUES ';
  86. $column_count = 0;
  87. $add_comma = false;
  88. while ($data_row = $GLOBALS['dbi']->fetchAssoc($pma_tranformation_data)) {
  89. foreach ($column_map as $column) {
  90. if ($data_row['table_name'] == $column['table_name']
  91. && $data_row['column_name'] == $column['refering_column']
  92. ) {
  93. $new_transformations_sql .= $add_comma ? ', ' : '';
  94. $new_transformations_sql .= '('
  95. . '\'' . $db . '\', '
  96. . '\'' . $view_name . '\', '
  97. . '\'';
  98. $new_transformations_sql .= (isset($column['real_column']))
  99. ? $column['real_column']
  100. : $column['refering_column'];
  101. $new_transformations_sql .= '\', '
  102. . '\'' . $data_row['comment'] . '\', '
  103. . '\'' . $data_row['mimetype'] . '\', '
  104. . '\'' . $data_row['transformation'] . '\', '
  105. . '\''
  106. . PMA_Util::sqlAddSlashes(
  107. $data_row['transformation_options']
  108. )
  109. . '\')';
  110. $add_comma = true;
  111. $column_count++;
  112. break;
  113. }
  114. }
  115. if ($column_count == count($column_map)) {
  116. break;
  117. }
  118. }
  119. return ($column_count > 0) ? $new_transformations_sql : '';
  120. }
  121. ?>