@@ -79,7 +79,7 @@ public function __invoke(ServerRequest $request): void
7979 $ view = $ request ->getParsedBodyParam ('view ' );
8080
8181 // View name is a compulsory field
82- if (empty ($ view ['name ' ])) {
82+ if (isset ($ view ['name ' ]) && $ view [ ' name ' ] === '' ) {
8383 $ GLOBALS ['message ' ] = Message::error (__ ('View name can not be empty! ' ));
8484 $ this ->response ->addJSON ('message ' , $ GLOBALS ['message ' ]);
8585 $ this ->response ->setRequestStatus (false );
@@ -91,59 +91,8 @@ public function __invoke(ServerRequest $request): void
9191 $ alterview = $ request ->hasBodyParam ('alterview ' );
9292 $ ajaxdialog = $ request ->hasBodyParam ('ajax_dialog ' );
9393
94- if ($ createview || $ alterview ) {
95- /**
96- * Creates the view
97- */
98- $ separator = "\r\n" ;
99-
100- if ($ createview ) {
101- $ GLOBALS ['sql_query ' ] = 'CREATE ' ;
102- if (isset ($ view ['or_replace ' ])) {
103- $ GLOBALS ['sql_query ' ] .= ' OR REPLACE ' ;
104- }
105- } else {
106- $ GLOBALS ['sql_query ' ] = 'ALTER ' ;
107- }
108-
109- if (
110- isset ($ view ['algorithm ' ])
111- && in_array ($ view ['algorithm ' ], self ::VIEW_ALGORITHM_OPTIONS )
112- ) {
113- $ GLOBALS ['sql_query ' ] .= $ separator . ' ALGORITHM = ' . $ view ['algorithm ' ];
114- }
115-
116- if (! empty ($ view ['definer ' ])) {
117- if (! str_contains ($ view ['definer ' ], '@ ' )) {
118- $ GLOBALS ['sql_query ' ] .= $ separator . 'DEFINER= '
119- . Util::backquote ($ view ['definer ' ]);
120- } else {
121- $ definerArray = explode ('@ ' , $ view ['definer ' ]);
122- $ GLOBALS ['sql_query ' ] .= $ separator . 'DEFINER= ' . Util::backquote ($ definerArray [0 ]);
123- $ GLOBALS ['sql_query ' ] .= '@ ' . Util::backquote ($ definerArray [1 ]) . ' ' ;
124- }
125- }
126-
127- if (
128- isset ($ view ['sql_security ' ])
129- && in_array ($ view ['sql_security ' ], self ::VIEW_SECURITY_OPTIONS )
130- ) {
131- $ GLOBALS ['sql_query ' ] .= $ separator . ' SQL SECURITY '
132- . $ view ['sql_security ' ];
133- }
134-
135- $ GLOBALS ['sql_query ' ] .= $ separator . ' VIEW '
136- . Util::backquote ($ view ['name ' ]);
137-
138- if (! empty ($ view ['column_names ' ])) {
139- $ GLOBALS ['sql_query ' ] .= $ separator . ' ( ' . $ view ['column_names ' ] . ') ' ;
140- }
141-
142- $ GLOBALS ['sql_query ' ] .= $ separator . ' AS ' . $ view ['as ' ];
143-
144- if (isset ($ view ['with ' ]) && in_array ($ view ['with ' ], self ::VIEW_WITH_OPTIONS )) {
145- $ GLOBALS ['sql_query ' ] .= $ separator . ' WITH ' . $ view ['with ' ] . ' CHECK OPTION ' ;
146- }
94+ if (($ createview || $ alterview ) && $ view !== null ) {
95+ $ GLOBALS ['sql_query ' ] = $ this ->getSqlQuery ($ createview , $ view );
14796
14897 if (! $ this ->dbi ->tryQuery ($ GLOBALS ['sql_query ' ])) {
14998 if (! $ ajaxdialog ) {
@@ -164,46 +113,7 @@ public function __invoke(ServerRequest $request): void
164113 return ;
165114 }
166115
167- // If different column names defined for VIEW
168- $ viewColumns = [];
169- if (isset ($ view ['column_names ' ])) {
170- $ viewColumns = explode (', ' , $ view ['column_names ' ]);
171- }
172-
173- $ systemDb = $ this ->dbi ->getSystemDatabase ();
174- $ pmaTransformationData = $ systemDb ->getExistingTransformationData ($ GLOBALS ['db ' ]);
175-
176- if ($ pmaTransformationData !== false ) {
177- $ columnMap = $ systemDb ->getColumnMapFromSql ($ view ['as ' ], $ viewColumns );
178- // SQL for store new transformation details of VIEW
179- $ newTransformationsSql = $ systemDb ->getNewTransformationDataSql (
180- $ pmaTransformationData ,
181- $ columnMap ,
182- $ view ['name ' ],
183- $ GLOBALS ['db ' ]
184- );
185-
186- // Store new transformations
187- if ($ newTransformationsSql !== '' ) {
188- $ this ->dbi ->tryQuery ($ newTransformationsSql );
189- }
190- }
191-
192- if ($ ajaxdialog ) {
193- $ GLOBALS ['message ' ] = Message::success ();
194- /** @var StructureController $controller */
195- $ controller = Core::getContainerBuilder ()->get (StructureController::class);
196- $ controller ($ request );
197- } else {
198- $ this ->response ->addJSON (
199- 'message ' ,
200- Generator::getMessage (
201- Message::success (),
202- $ GLOBALS ['sql_query ' ]
203- )
204- );
205- $ this ->response ->setRequestStatus (true );
206- }
116+ $ this ->setSuccessResponse ($ view , $ ajaxdialog , $ request );
207117
208118 return ;
209119 }
@@ -261,7 +171,9 @@ public function __invoke(ServerRequest $request): void
261171 }
262172 }
263173
264- $ viewData = array_merge ($ viewData , $ view );
174+ if ($ view !== null ) {
175+ $ viewData = array_merge ($ viewData , $ view );
176+ }
265177
266178 $ GLOBALS ['urlParams ' ]['db ' ] = $ GLOBALS ['db ' ];
267179 $ GLOBALS ['urlParams ' ]['reload ' ] = 1 ;
@@ -278,4 +190,106 @@ public function __invoke(ServerRequest $request): void
278190 'view_security_options ' => self ::VIEW_SECURITY_OPTIONS ,
279191 ]);
280192 }
193+
194+ private function setSuccessResponse (array $ view , bool $ ajaxdialog , ServerRequest $ request ): void
195+ {
196+ // If different column names defined for VIEW
197+ $ viewColumns = [];
198+ if (isset ($ view ['column_names ' ])) {
199+ $ viewColumns = explode (', ' , $ view ['column_names ' ]);
200+ }
201+
202+ $ systemDb = $ this ->dbi ->getSystemDatabase ();
203+ $ pmaTransformationData = $ systemDb ->getExistingTransformationData ($ GLOBALS ['db ' ]);
204+
205+ if ($ pmaTransformationData !== false ) {
206+ $ columnMap = $ systemDb ->getColumnMapFromSql ($ view ['as ' ], $ viewColumns );
207+ // SQL for store new transformation details of VIEW
208+ $ newTransformationsSql = $ systemDb ->getNewTransformationDataSql (
209+ $ pmaTransformationData ,
210+ $ columnMap ,
211+ $ view ['name ' ],
212+ $ GLOBALS ['db ' ]
213+ );
214+
215+ // Store new transformations
216+ if ($ newTransformationsSql !== '' ) {
217+ $ this ->dbi ->tryQuery ($ newTransformationsSql );
218+ }
219+ }
220+
221+ if ($ ajaxdialog ) {
222+ $ GLOBALS ['message ' ] = Message::success ();
223+ /** @var StructureController $controller */
224+ $ controller = Core::getContainerBuilder ()->get (StructureController::class);
225+ $ controller ($ request );
226+ } else {
227+ $ this ->response ->addJSON (
228+ 'message ' ,
229+ Generator::getMessage (
230+ Message::success (),
231+ $ GLOBALS ['sql_query ' ]
232+ )
233+ );
234+ $ this ->response ->setRequestStatus (true );
235+ }
236+ }
237+
238+ /**
239+ * Creates the view
240+ */
241+ private function getSqlQuery (bool $ createview , array $ view ): string
242+ {
243+ $ separator = "\r\n" ;
244+
245+ if ($ createview ) {
246+ $ sqlQuery = 'CREATE ' ;
247+ if (isset ($ view ['or_replace ' ])) {
248+ $ sqlQuery .= ' OR REPLACE ' ;
249+ }
250+ } else {
251+ $ sqlQuery = 'ALTER ' ;
252+ }
253+
254+ if (
255+ isset ($ view ['algorithm ' ])
256+ && in_array ($ view ['algorithm ' ], self ::VIEW_ALGORITHM_OPTIONS )
257+ ) {
258+ $ sqlQuery .= $ separator . ' ALGORITHM = ' . $ view ['algorithm ' ];
259+ }
260+
261+ if (! empty ($ view ['definer ' ])) {
262+ if (! str_contains ($ view ['definer ' ], '@ ' )) {
263+ $ sqlQuery .= $ separator . 'DEFINER= '
264+ . Util::backquote ($ view ['definer ' ]);
265+ } else {
266+ $ definerArray = explode ('@ ' , $ view ['definer ' ]);
267+ $ sqlQuery .= $ separator . 'DEFINER= ' . Util::backquote ($ definerArray [0 ]);
268+ $ sqlQuery .= '@ ' . Util::backquote ($ definerArray [1 ]) . ' ' ;
269+ }
270+ }
271+
272+ if (
273+ isset ($ view ['sql_security ' ])
274+ && in_array ($ view ['sql_security ' ], self ::VIEW_SECURITY_OPTIONS )
275+ ) {
276+ $ sqlQuery .= $ separator . ' SQL SECURITY '
277+ . $ view ['sql_security ' ];
278+ }
279+
280+ $ sqlQuery .= $ separator . ' VIEW '
281+ . Util::backquote ($ view ['name ' ]);
282+
283+ if (! empty ($ view ['column_names ' ])) {
284+ $ sqlQuery .= $ separator . ' ( ' . $ view ['column_names ' ] . ') ' ;
285+ }
286+
287+ $ sqlQuery .= $ separator . ' AS ' . $ view ['as ' ];
288+
289+ if (isset ($ view ['with ' ]) && in_array ($ view ['with ' ], self ::VIEW_WITH_OPTIONS )) {
290+ $ sqlQuery .= $ separator . ' WITH ' . $ view ['with ' ] . ' CHECK OPTION ' ;
291+ }
292+
293+ return $ sqlQuery ;
294+ }
281295}
0 commit comments