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?)
13 use PhpMyAdmin\Response
;
14 use PhpMyAdmin\Template
;
16 require_once './libraries/common.inc.php';
21 require './libraries/db_common.inc.php';
22 $url_params['goto'] = 'tbl_structure.php';
23 $url_params['back'] = 'view_create.php';
25 $response = Response
::getInstance();
27 $view_algorithm_options = array(
33 $view_with_options = array(
38 $view_security_options = array(
43 if (empty($sql_query)) {
47 // View name is a compulsory field
48 if (isset($_REQUEST['view']['name'])
49 && empty($_REQUEST['view']['name'])
51 $message = PhpMyAdmin\Message
::error(__('View name can not be empty!'));
56 $response->setRequestStatus(false);
60 if (isset($_REQUEST['createview']) ||
isset($_REQUEST['alterview'])) {
66 if (isset($_REQUEST['createview'])) {
67 $sql_query = 'CREATE';
68 if (isset($_REQUEST['view']['or_replace'])) {
69 $sql_query .= ' OR REPLACE';
75 if (Core
::isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
76 $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
79 if (! empty($_REQUEST['view']['definer'])) {
80 if (strpos($_REQUEST['view']['definer'], '@') === FALSE) {
81 $sql_query .= $sep . 'DEFINER='
82 . PhpMyAdmin\Util
::backquote($_REQUEST['view']['definer']);
84 $arr = explode('@', $_REQUEST['view']['definer']);
85 $sql_query .= $sep . 'DEFINER=' . PhpMyAdmin\Util
::backquote($arr[0]);
86 $sql_query .= '@' . PhpMyAdmin\Util
::backquote($arr[1]) . ' ';
90 if (isset($_REQUEST['view']['sql_security'])) {
91 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
92 $sql_query .= $sep . ' SQL SECURITY '
93 . $_REQUEST['view']['sql_security'];
97 $sql_query .= $sep . ' VIEW '
98 . PhpMyAdmin\Util
::backquote($_REQUEST['view']['name']);
100 if (! empty($_REQUEST['view']['column_names'])) {
101 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
104 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
106 if (isset($_REQUEST['view']['with'])) {
107 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
108 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
113 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
114 if (! isset($_REQUEST['ajax_dialog'])) {
115 $message = PhpMyAdmin\Message
::rawError($GLOBALS['dbi']->getError());
121 PhpMyAdmin\Message
::error(
122 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
123 . $GLOBALS['dbi']->getError()
126 $response->setRequestStatus(false);
130 // If different column names defined for VIEW
131 $view_columns = array();
132 if (isset($_REQUEST['view']['column_names'])) {
133 $view_columns = explode(',', $_REQUEST['view']['column_names']);
136 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
137 $_REQUEST['view']['as'], $view_columns
140 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
141 $pma_transformation_data = $systemDb->getExistingTransformationData(
145 if ($pma_transformation_data !== false) {
147 // SQL for store new transformation details of VIEW
148 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
149 $pma_transformation_data, $column_map,
150 $_REQUEST['view']['name'], $GLOBALS['db']
153 // Store new transformations
154 if ($new_transformations_sql != '') {
155 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
159 unset($pma_transformation_data);
161 if (! isset($_REQUEST['ajax_dialog'])) {
162 $message = PhpMyAdmin\Message
::success();
163 include 'tbl_structure.php';
167 PhpMyAdmin\Util
::getMessage(
168 PhpMyAdmin\Message
::success(),
172 $response->setRequestStatus(true);
178 // prefill values if not already filled from former submission
180 'operation' => 'create',
184 'sql_security' => '',
186 'column_names' => '',
191 if (Core
::isValid($_REQUEST['view'], 'array')) {
192 $view = array_merge($view, $_REQUEST['view']);
195 $url_params['db'] = $GLOBALS['db'];
196 $url_params['reload'] = 1;
198 echo Template
::get('view_create')->render([
199 'ajax_dialog' => isset($_REQUEST['ajax_dialog']),
200 'text_dir' => $text_dir,
201 'url_params' => $url_params,