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\SqlParser\Parser
;
17 use PhpMyAdmin\SqlParser\Statements\CreateStatement
;
18 use PhpMyAdmin\SqlParser\TokensList
;
19 use PhpMyAdmin\Template
;
22 if (! defined('ROOT_PATH')) {
23 define('ROOT_PATH', __DIR__
. DIRECTORY_SEPARATOR
);
28 require_once ROOT_PATH
. 'libraries/common.inc.php';
29 require ROOT_PATH
. 'libraries/db_common.inc.php';
31 /** @var Response $response */
32 $response = $containerBuilder->get(Response
::class);
34 /** @var DatabaseInterface $dbi */
35 $dbi = $containerBuilder->get(DatabaseInterface
::class);
37 $url_params['goto'] = 'tbl_structure.php';
38 $url_params['back'] = 'view_create.php';
40 /** @var Template $template */
41 $template = $containerBuilder->get('template');
43 $view_algorithm_options = [
49 $view_with_options = [
54 $view_security_options = [
59 // View name is a compulsory field
60 if (isset($_POST['view']['name'])
61 && empty($_POST['view']['name'])
63 $message = Message
::error(__('View name can not be empty!'));
68 $response->setRequestStatus(false);
72 if (isset($_POST['createview']) ||
isset($_POST['alterview'])) {
78 if (isset($_POST['createview'])) {
79 $sql_query = 'CREATE';
80 if (isset($_POST['view']['or_replace'])) {
81 $sql_query .= ' OR REPLACE';
87 if (Core
::isValid($_POST['view']['algorithm'], $view_algorithm_options)) {
88 $sql_query .= $sep . ' ALGORITHM = ' . $_POST['view']['algorithm'];
91 if (! empty($_POST['view']['definer'])) {
92 if (strpos($_POST['view']['definer'], '@') === false) {
93 $sql_query .= $sep . 'DEFINER='
94 . Util
::backquote($_POST['view']['definer']);
96 $arr = explode('@', $_POST['view']['definer']);
97 $sql_query .= $sep . 'DEFINER=' . Util
::backquote($arr[0]);
98 $sql_query .= '@' . Util
::backquote($arr[1]) . ' ';
102 if (isset($_POST['view']['sql_security']) && in_array($_POST['view']['sql_security'], $view_security_options)) {
103 $sql_query .= $sep . ' SQL SECURITY '
104 . $_POST['view']['sql_security'];
107 $sql_query .= $sep . ' VIEW '
108 . Util
::backquote($_POST['view']['name']);
110 if (! empty($_POST['view']['column_names'])) {
111 $sql_query .= $sep . ' (' . $_POST['view']['column_names'] . ')';
114 $sql_query .= $sep . ' AS ' . $_POST['view']['as'];
116 if (isset($_POST['view']['with'])) {
117 if (in_array($_POST['view']['with'], $view_with_options)) {
118 $sql_query .= $sep . ' WITH ' . $_POST['view']['with']
123 if (! $dbi->tryQuery($sql_query)) {
124 if (! isset($_POST['ajax_dialog'])) {
125 $message = Message
::rawError($dbi->getError());
132 "<i>" . htmlspecialchars($sql_query) . "</i><br><br>"
136 $response->setRequestStatus(false);
140 // If different column names defined for VIEW
142 if (isset($_POST['view']['column_names'])) {
143 $view_columns = explode(',', $_POST['view']['column_names']);
146 $column_map = $dbi->getColumnMapFromSql(
147 $_POST['view']['as'],
151 $systemDb = $dbi->getSystemDatabase();
152 $pma_transformation_data = $systemDb->getExistingTransformationData(
156 if ($pma_transformation_data !== false) {
157 // SQL for store new transformation details of VIEW
158 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
159 $pma_transformation_data,
161 $_POST['view']['name'],
165 // Store new transformations
166 if ($new_transformations_sql != '') {
167 $dbi->tryQuery($new_transformations_sql);
170 unset($pma_transformation_data);
172 if (! isset($_POST['ajax_dialog'])) {
173 $message = Message
::success();
174 include ROOT_PATH
. 'tbl_structure.php';
183 $response->setRequestStatus(true);
189 $sql_query = ! empty($_POST['sql_query']) ?
$_POST['sql_query'] : '';
191 // prefill values if not already filled from former submission
193 'operation' => 'create',
197 'sql_security' => '',
199 'column_names' => '',
204 // Used to prefill the fields when editing a view
205 if (isset($_GET['db']) && isset($_GET['table'])) {
206 $item = $dbi->fetchSingleRow(
208 "SELECT `VIEW_DEFINITION`, `CHECK_OPTION`, `DEFINER`,
210 FROM `INFORMATION_SCHEMA`.`VIEWS`
211 WHERE TABLE_SCHEMA='%s'
212 AND TABLE_NAME='%s';",
213 $dbi->escapeString($_GET['db']),
214 $dbi->escapeString($_GET['table'])
217 $createView = $dbi->getTable($_GET['db'], $_GET['table'])
220 // CREATE ALGORITHM=<ALGORITHM> DE...
221 $parts = explode(" ", substr($createView, 17));
222 $item['ALGORITHM'] = $parts[0];
224 $view['operation'] = 'alter';
225 $view['definer'] = $item['DEFINER'];
226 $view['sql_security'] = $item['SECURITY_TYPE'];
227 $view['name'] = $_GET['table'];
228 $view['as'] = $item['VIEW_DEFINITION'];
229 $view['with'] = $item['CHECK_OPTION'];
230 $view['algorithm'] = $item['ALGORITHM'];
232 if (empty($view['as']) && is_string($createView)) {
233 $parser = new Parser($createView);
235 * @var CreateStatement $stmt
237 $stmt = $parser->statements
[0];
238 $view['as'] = isset($stmt->body
) ? TokensList
::build($stmt->body
) : $view['as'];
242 if (Core
::isValid($_POST['view'], 'array')) {
243 $view = array_merge($view, $_POST['view']);
246 $url_params['db'] = $GLOBALS['db'];
247 $url_params['reload'] = 1;
249 echo $template->render('view_create', [
250 'ajax_dialog' => isset($_POST['ajax_dialog']),
251 'text_dir' => $text_dir,
252 'url_params' => $url_params,
254 'view_algorithm_options' => $view_algorithm_options,
255 'view_with_options' => $view_with_options,
256 'view_security_options' => $view_security_options,