2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * handles creation of VIEWs
6 * @todo js error when view name is empty (strFormEmpty)
7 * @todo (also validate if js is disabled, after form submission?)
10 declare(strict_types
=1);
14 use PhpMyAdmin\Response
;
15 use PhpMyAdmin\Template
;
17 require_once './libraries/common.inc.php';
22 require './libraries/db_common.inc.php';
23 $url_params['goto'] = 'tbl_structure.php';
24 $url_params['back'] = 'view_create.php';
26 $response = Response
::getInstance();
28 $view_algorithm_options = array(
34 $view_with_options = array(
39 $view_security_options = array(
44 if (empty($sql_query)) {
48 // View name is a compulsory field
49 if (isset($_REQUEST['view']['name'])
50 && empty($_REQUEST['view']['name'])
52 $message = PhpMyAdmin\Message
::error(__('View name can not be empty!'));
57 $response->setRequestStatus(false);
61 if (isset($_REQUEST['createview']) ||
isset($_REQUEST['alterview'])) {
67 if (isset($_REQUEST['createview'])) {
68 $sql_query = 'CREATE';
69 if (isset($_REQUEST['view']['or_replace'])) {
70 $sql_query .= ' OR REPLACE';
76 if (Core
::isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
77 $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
80 if (! empty($_REQUEST['view']['definer'])) {
81 if (strpos($_REQUEST['view']['definer'], '@') === false) {
82 $sql_query .= $sep . 'DEFINER='
83 . PhpMyAdmin\Util
::backquote($_REQUEST['view']['definer']);
85 $arr = explode('@', $_REQUEST['view']['definer']);
86 $sql_query .= $sep . 'DEFINER=' . PhpMyAdmin\Util
::backquote($arr[0]);
87 $sql_query .= '@' . PhpMyAdmin\Util
::backquote($arr[1]) . ' ';
91 if (isset($_REQUEST['view']['sql_security'])) {
92 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
93 $sql_query .= $sep . ' SQL SECURITY '
94 . $_REQUEST['view']['sql_security'];
98 $sql_query .= $sep . ' VIEW '
99 . PhpMyAdmin\Util
::backquote($_REQUEST['view']['name']);
101 if (! empty($_REQUEST['view']['column_names'])) {
102 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
105 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
107 if (isset($_REQUEST['view']['with'])) {
108 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
109 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
114 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
115 if (! isset($_REQUEST['ajax_dialog'])) {
116 $message = PhpMyAdmin\Message
::rawError($GLOBALS['dbi']->getError());
122 PhpMyAdmin\Message
::error(
123 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
124 . $GLOBALS['dbi']->getError()
127 $response->setRequestStatus(false);
131 // If different column names defined for VIEW
132 $view_columns = array();
133 if (isset($_REQUEST['view']['column_names'])) {
134 $view_columns = explode(',', $_REQUEST['view']['column_names']);
137 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
138 $_REQUEST['view']['as'], $view_columns
141 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
142 $pma_transformation_data = $systemDb->getExistingTransformationData(
146 if ($pma_transformation_data !== false) {
148 // SQL for store new transformation details of VIEW
149 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
150 $pma_transformation_data, $column_map,
151 $_REQUEST['view']['name'], $GLOBALS['db']
154 // Store new transformations
155 if ($new_transformations_sql != '') {
156 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
160 unset($pma_transformation_data);
162 if (! isset($_REQUEST['ajax_dialog'])) {
163 $message = PhpMyAdmin\Message
::success();
164 include 'tbl_structure.php';
168 PhpMyAdmin\Util
::getMessage(
169 PhpMyAdmin\Message
::success(),
173 $response->setRequestStatus(true);
179 // prefill values if not already filled from former submission
181 'operation' => 'create',
185 'sql_security' => '',
187 'column_names' => '',
192 if (Core
::isValid($_REQUEST['view'], 'array')) {
193 $view = array_merge($view, $_REQUEST['view']);
196 $url_params['db'] = $GLOBALS['db'];
197 $url_params['reload'] = 1;
199 echo Template
::get('view_create')->render([
200 'ajax_dialog' => isset($_REQUEST['ajax_dialog']),
201 'text_dir' => $text_dir,
202 'url_params' => $url_params,