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?)
12 use PhpMyAdmin\Response
;
13 use PhpMyAdmin\Template
;
15 require_once './libraries/common.inc.php';
20 require './libraries/db_common.inc.php';
21 $url_params['goto'] = 'tbl_structure.php';
22 $url_params['back'] = 'view_create.php';
24 $response = Response
::getInstance();
26 $view_algorithm_options = array(
32 $view_with_options = array(
37 $view_security_options = array(
42 // View name is a compulsory field
43 if (isset($_POST['view']['name'])
44 && empty($_POST['view']['name'])
46 $message = PhpMyAdmin\Message
::error(__('View name can not be empty!'));
51 $response->setRequestStatus(false);
55 if (isset($_POST['createview']) ||
isset($_POST['alterview'])) {
61 if (isset($_POST['createview'])) {
62 $sql_query = 'CREATE';
63 if (isset($_POST['view']['or_replace'])) {
64 $sql_query .= ' OR REPLACE';
70 if (Core
::isValid($_POST['view']['algorithm'], $view_algorithm_options)) {
71 $sql_query .= $sep . ' ALGORITHM = ' . $_POST['view']['algorithm'];
74 if (! empty($_POST['view']['definer'])) {
75 if (strpos($_POST['view']['definer'], '@') === false) {
76 $sql_query .= $sep . 'DEFINER='
77 . PhpMyAdmin\Util
::backquote($_POST['view']['definer']);
79 $arr = explode('@', $_POST['view']['definer']);
80 $sql_query .= $sep . 'DEFINER=' . PhpMyAdmin\Util
::backquote($arr[0]);
81 $sql_query .= '@' . PhpMyAdmin\Util
::backquote($arr[1]) . ' ';
85 if (isset($_POST['view']['sql_security'])) {
86 if (in_array($_POST['view']['sql_security'], $view_security_options)) {
87 $sql_query .= $sep . ' SQL SECURITY '
88 . $_POST['view']['sql_security'];
92 $sql_query .= $sep . ' VIEW '
93 . PhpMyAdmin\Util
::backquote($_POST['view']['name']);
95 if (! empty($_POST['view']['column_names'])) {
96 $sql_query .= $sep . ' (' . $_POST['view']['column_names'] . ')';
99 $sql_query .= $sep . ' AS ' . $_POST['view']['as'];
101 if (isset($_POST['view']['with'])) {
102 if (in_array($_POST['view']['with'], $view_with_options)) {
103 $sql_query .= $sep . ' WITH ' . $_POST['view']['with']
108 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
109 if (! isset($_POST['ajax_dialog'])) {
110 $message = PhpMyAdmin\Message
::rawError($GLOBALS['dbi']->getError());
116 PhpMyAdmin\Message
::error(
117 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
118 . $GLOBALS['dbi']->getError()
121 $response->setRequestStatus(false);
125 // If different column names defined for VIEW
126 $view_columns = array();
127 if (isset($_POST['view']['column_names'])) {
128 $view_columns = explode(',', $_POST['view']['column_names']);
131 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
132 $_POST['view']['as'], $view_columns
135 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
136 $pma_transformation_data = $systemDb->getExistingTransformationData(
140 if ($pma_transformation_data !== false) {
142 // SQL for store new transformation details of VIEW
143 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
144 $pma_transformation_data, $column_map,
145 $_POST['view']['name'], $GLOBALS['db']
148 // Store new transformations
149 if ($new_transformations_sql != '') {
150 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
154 unset($pma_transformation_data);
156 if (! isset($_POST['ajax_dialog'])) {
157 $message = PhpMyAdmin\Message
::success();
158 include 'tbl_structure.php';
162 PhpMyAdmin\Util
::getMessage(
163 PhpMyAdmin\Message
::success(),
167 $response->setRequestStatus(true);
173 $sql_query = ! empty($_GET['sql_query']) ?
$_GET['sql_query'] : '';
175 // prefill values if not already filled from former submission
177 'operation' => 'create',
181 'sql_security' => '',
183 'column_names' => '',
188 if (Core
::isValid($_POST['view'], 'array')) {
189 $view = array_merge($view, $_POST['view']);
192 $url_params['db'] = $GLOBALS['db'];
193 $url_params['reload'] = 1;
195 echo Template
::get('view_create')->render([
196 'ajax_dialog' => isset($_POST['ajax_dialog']),
197 'text_dir' => $text_dir,
198 'url_params' => $url_params,
200 'view_algorithm_options' => $view_algorithm_options,
201 'view_with_options' => $view_with_options,
202 'view_security_options' => $view_security_options,