tbl_views.lib.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 = PMA_DBI_try_query($sql_query);
  24. if ($real_source_result !== false) {
  25. $real_source_fields_meta = PMA_DBI_get_fields_meta($real_source_result);
  26. if (count($real_source_fields_meta) > 0) {
  27. for ($i=0; $i<count($real_source_fields_meta); $i++) {
  28. $map = array();
  29. $map['table_name'] = $real_source_fields_meta[$i]->table;
  30. $map['refering_column'] = $real_source_fields_meta[$i]->name;
  31. if (count($view_columns) > 1) {
  32. $map['real_column'] = $view_columns[$i];
  33. }
  34. $column_map[] = $map;
  35. }
  36. }
  37. }
  38. unset($real_source_result);
  39. return $column_map;
  40. }
  41. /**
  42. * Get existing data on tranformations applyed for
  43. * columns in a particular table
  44. *
  45. * @param string $db Database name looking for
  46. *
  47. * @return mysqli_result Result of executed SQL query
  48. */
  49. function PMA_getExistingTranformationData($db)
  50. {
  51. $cfgRelation = PMA_getRelationsParam();
  52. // Get the existing transformation details of the same database
  53. // from pma__column_info table
  54. $pma_transformation_sql = 'SELECT * FROM '
  55. . PMA_Util::backquote($cfgRelation['db']) . '.'
  56. . PMA_Util::backquote($cfgRelation['column_info'])
  57. . ' WHERE `db_name` = \''
  58. . PMA_Util::sqlAddSlashes($db) . '\'';
  59. return PMA_DBI_try_query($pma_transformation_sql);
  60. }
  61. /**
  62. * Get SQL query for store new transformation details of a VIEW
  63. *
  64. * @param mysqli_result $pma_tranformation_data Result set of SQL execution
  65. * @param array $column_map Details of VIEW columns
  66. * @param string $view_name Name of the VIEW
  67. * @param string $db Database name of the VIEW
  68. *
  69. * @return string $new_transformations_sql SQL query for new tranformations
  70. */
  71. function PMA_getNewTransformationDataSql(
  72. $pma_tranformation_data, $column_map, $view_name, $db
  73. ) {
  74. $cfgRelation = PMA_getRelationsParam();
  75. // Need to store new transformation details for VIEW
  76. $new_transformations_sql = 'INSERT INTO '
  77. . PMA_Util::backquote($cfgRelation['db']) . '.'
  78. . PMA_Util::backquote($cfgRelation['column_info'])
  79. . ' (`db_name`, `table_name`, `column_name`, `comment`, '
  80. . '`mimetype`, `transformation`, `transformation_options`)'
  81. . ' VALUES ';
  82. $column_count = 0;
  83. $add_comma = false;
  84. while ($data_row = PMA_DBI_fetch_assoc($pma_tranformation_data)) {
  85. foreach ($column_map as $column) {
  86. if ($data_row['table_name'] == $column['table_name']
  87. && $data_row['column_name'] == $column['refering_column']
  88. ) {
  89. $new_transformations_sql .= $add_comma ? ', ' : '';
  90. $new_transformations_sql .= '('
  91. . '\'' . $db . '\', '
  92. . '\'' . $view_name . '\', '
  93. . '\'';
  94. $new_transformations_sql .= (isset($column['real_column']))
  95. ? $column['real_column']
  96. : $column['refering_column'];
  97. $new_transformations_sql .= '\', '
  98. . '\'' . $data_row['comment'] . '\', '
  99. . '\'' . $data_row['mimetype'] . '\', '
  100. . '\'' . $data_row['transformation'] . '\', '
  101. . '\''
  102. . PMA_Util::sqlAddSlashes(
  103. $data_row['transformation_options']
  104. )
  105. . '\')';
  106. $add_comma = true;
  107. $column_count++;
  108. break;
  109. }
  110. }
  111. if ($column_count == count($column_map)) {
  112. break;
  113. }
  114. }
  115. return ($column_count > 0) ? $new_transformations_sql : '';
  116. }
  117. ?>