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);
13 use PhpMyAdmin\DatabaseInterface
;
14 use PhpMyAdmin\Message
;
15 use PhpMyAdmin\Response
;
16 use PhpMyAdmin\Template
;
19 if (! defined('ROOT_PATH')) {
20 define('ROOT_PATH', __DIR__
. DIRECTORY_SEPARATOR
);
25 require_once ROOT_PATH
. 'libraries/common.inc.php';
26 require ROOT_PATH
. 'libraries/db_common.inc.php';
28 /** @var Response $response */
29 $response = $containerBuilder->get(Response
::class);
31 /** @var DatabaseInterface $dbi */
32 $dbi = $containerBuilder->get(DatabaseInterface
::class);
34 $url_params['goto'] = 'tbl_structure.php';
35 $url_params['back'] = 'view_create.php';
37 /** @var Template $template */
38 $template = $containerBuilder->get('template');
40 $view_algorithm_options = [
46 $view_with_options = [
51 $view_security_options = [
56 // View name is a compulsory field
57 if (isset($_POST['view']['name'])
58 && empty($_POST['view']['name'])
60 $message = Message
::error(__('View name can not be empty!'));
65 $response->setRequestStatus(false);
69 if (isset($_POST['createview']) ||
isset($_POST['alterview'])) {
75 if (isset($_POST['createview'])) {
76 $sql_query = 'CREATE';
77 if (isset($_POST['view']['or_replace'])) {
78 $sql_query .= ' OR REPLACE';
84 if (Core
::isValid($_POST['view']['algorithm'], $view_algorithm_options)) {
85 $sql_query .= $sep . ' ALGORITHM = ' . $_POST['view']['algorithm'];
88 if (! empty($_POST['view']['definer'])) {
89 if (strpos($_POST['view']['definer'], '@') === false) {
90 $sql_query .= $sep . 'DEFINER='
91 . Util
::backquote($_POST['view']['definer']);
93 $arr = explode('@', $_POST['view']['definer']);
94 $sql_query .= $sep . 'DEFINER=' . Util
::backquote($arr[0]);
95 $sql_query .= '@' . Util
::backquote($arr[1]) . ' ';
99 if (isset($_POST['view']['sql_security']) && in_array($_POST['view']['sql_security'], $view_security_options)) {
100 $sql_query .= $sep . ' SQL SECURITY '
101 . $_POST['view']['sql_security'];
104 $sql_query .= $sep . ' VIEW '
105 . Util
::backquote($_POST['view']['name']);
107 if (! empty($_POST['view']['column_names'])) {
108 $sql_query .= $sep . ' (' . $_POST['view']['column_names'] . ')';
111 $sql_query .= $sep . ' AS ' . $_POST['view']['as'];
113 if (isset($_POST['view']['with'])) {
114 if (in_array($_POST['view']['with'], $view_with_options)) {
115 $sql_query .= $sep . ' WITH ' . $_POST['view']['with']
120 if (! $dbi->tryQuery($sql_query)) {
121 if (! isset($_POST['ajax_dialog'])) {
122 $message = Message
::rawError($dbi->getError());
129 "<i>" . htmlspecialchars($sql_query) . "</i><br><br>"
133 $response->setRequestStatus(false);
137 // If different column names defined for VIEW
139 if (isset($_POST['view']['column_names'])) {
140 $view_columns = explode(',', $_POST['view']['column_names']);
143 $column_map = $dbi->getColumnMapFromSql(
144 $_POST['view']['as'],
148 $systemDb = $dbi->getSystemDatabase();
149 $pma_transformation_data = $systemDb->getExistingTransformationData(
153 if ($pma_transformation_data !== false) {
154 // SQL for store new transformation details of VIEW
155 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
156 $pma_transformation_data,
158 $_POST['view']['name'],
162 // Store new transformations
163 if ($new_transformations_sql != '') {
164 $dbi->tryQuery($new_transformations_sql);
167 unset($pma_transformation_data);
169 if (! isset($_POST['ajax_dialog'])) {
170 $message = Message
::success();
171 include ROOT_PATH
. 'tbl_structure.php';
180 $response->setRequestStatus(true);
186 $sql_query = ! empty($_POST['sql_query']) ?
$_POST['sql_query'] : '';
188 // prefill values if not already filled from former submission
190 'operation' => 'create',
194 'sql_security' => '',
196 'column_names' => '',
201 // Used to prefill the fields when editing a view
202 if (isset($_GET['db']) && isset($_GET['table'])) {
203 $item = $dbi->fetchSingleRow(
205 "SELECT `VIEW_DEFINITION`, `CHECK_OPTION`, `DEFINER`,
207 FROM `INFORMATION_SCHEMA`.`VIEWS`
208 WHERE TABLE_SCHEMA='%s'
209 AND TABLE_NAME='%s';",
210 $dbi->escapeString($_GET['db']),
211 $dbi->escapeString($_GET['table'])
214 $createView = $dbi->getTable($_GET['db'], $_GET['table'])
217 // CREATE ALGORITHM=<ALGORITHM> DE...
218 $parts = explode(" ", substr($createView, 17));
219 $item['ALGORITHM'] = $parts[0];
221 $view['operation'] = 'alter';
222 $view['definer'] = $item['DEFINER'];
223 $view['sql_security'] = $item['SECURITY_TYPE'];
224 $view['name'] = $_GET['table'];
225 $view['as'] = $item['VIEW_DEFINITION'];
226 $view['with'] = $item['CHECK_OPTION'];
227 $view['algorithm'] = $item['ALGORITHM'];
230 if (Core
::isValid($_POST['view'], 'array')) {
231 $view = array_merge($view, $_POST['view']);
234 $url_params['db'] = $GLOBALS['db'];
235 $url_params['reload'] = 1;
237 echo $template->render('view_create', [
238 'ajax_dialog' => isset($_POST['ajax_dialog']),
239 'text_dir' => $text_dir,
240 'url_params' => $url_params,
242 'view_algorithm_options' => $view_algorithm_options,
243 'view_with_options' => $view_with_options,
244 'view_security_options' => $view_security_options,