Merge remote-tracking branch 'origin/master'
[phpmyadmin.git] / libraries / SystemDatabase.class.php
blob54b7e8aa91a22541006505be667bfb4789487576
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * hold PMA\SystemDatabase class
6 * @package PMA
7 */
9 namespace PMA;
11 if (! defined('PHPMYADMIN')) {
12 exit;
15 require_once 'libraries/database_interface.inc.php';
17 /**
18 * Class SystemDatabase
20 * @package PMA
22 class SystemDatabase
24 /**
25 * @var \PMA_DatabaseInterface
27 protected $dbi;
29 /**
30 * Get instance of SystemDatabase
32 * @param \PMA_DatabaseInterface $dbi Database interface for the system database
34 * @return SystemDatabase
36 function __construct(\PMA_DatabaseInterface $dbi)
38 $this->dbi = $dbi;
41 /**
42 * Get existing data on transformations applied for
43 * columns in a particular table
45 * @param string $db Database name looking for
47 * @return \mysqli_result Result of executed SQL query
49 public function getExistingTransformationData($db)
51 $cfgRelation = \PMA_getRelationsParam();
53 // Get the existing transformation details of the same database
54 // from pma__column_info table
55 $pma_transformation_sql = sprintf(
56 "SELECT * FROM %s.%s WHERE `db_name` = '%s'",
57 \PMA_Util::backquote($cfgRelation['db']),
58 \PMA_Util::backquote($cfgRelation['column_info']),
59 \PMA_Util::sqlAddSlashes($db)
62 return $this->dbi->tryQuery($pma_transformation_sql);
65 /**
66 * Get SQL query for store new transformation details of a VIEW
68 * @param object $pma_transformation_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
73 * @return string $new_transformations_sql SQL query for new transformations
75 function getNewTransformationDataSql(
76 $pma_transformation_data, $column_map, $view_name, $db
77 ) {
78 $cfgRelation = \PMA_getRelationsParam();
80 // Need to store new transformation details for VIEW
81 $new_transformations_sql = sprintf(
82 "INSERT INTO %s.%s ("
83 . "`db_name`, `table_name`, `column_name`, "
84 . "`comment`, `mimetype`, `transformation`, "
85 . "`transformation_options`) VALUES",
86 \PMA_Util::backquote($cfgRelation['db']),
87 \PMA_Util::backquote($cfgRelation['column_info'])
90 $column_count = 0;
91 $add_comma = false;
93 while ($data_row = $this->dbi->fetchAssoc($pma_transformation_data)) {
95 foreach ($column_map as $column) {
97 if ($data_row['table_name'] != $column['table_name']
98 || $data_row['column_name'] != $column['refering_column']
99 ) {
100 continue;
103 $new_transformations_sql .= sprintf(
104 "%s ('%s', '%s', '%s', '%s', '%s', '%s', '%s')",
105 $add_comma ? ', ' : '',
106 $db,
107 $view_name,
108 isset($column['real_column'])
109 ? $column['real_column']
110 : $column['refering_column'],
111 $data_row['comment'],
112 $data_row['mimetype'],
113 $data_row['transformation'],
114 \PMA_Util::sqlAddSlashes(
115 $data_row['transformation_options']
119 $add_comma = true;
120 $column_count++;
121 break;
124 if ($column_count == count($column_map)) {
125 break;
129 return ($column_count > 0) ? $new_transformations_sql : '';