| 
									
										
										
										
											2017-12-14 20:38:56 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const sqlConsole = (function() { | 
					
						
							|  |  |  |     const dialogEl = $("#sql-console-dialog"); | 
					
						
							|  |  |  |     const queryEl = $('#sql-console-query'); | 
					
						
							|  |  |  |     const executeButton = $('#sql-console-execute'); | 
					
						
							|  |  |  |     const resultHeadEl = $('#sql-console-results thead'); | 
					
						
							|  |  |  |     const resultBodyEl = $('#sql-console-results tbody'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function showDialog() { | 
					
						
							|  |  |  |         glob.activeDialog = dialogEl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dialogEl.dialog({ | 
					
						
							|  |  |  |             modal: true, | 
					
						
							|  |  |  |             width: $(window).width(), | 
					
						
							|  |  |  |             height: $(window).height() | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function execute() { | 
					
						
							|  |  |  |         const sqlQuery = queryEl.val(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 22:33:44 -05:00
										 |  |  |         const result = await server.post("sql/execute", { | 
					
						
							| 
									
										
										
										
											2017-12-14 20:38:56 -05:00
										 |  |  |             query: sqlQuery | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 22:33:44 -05:00
										 |  |  |         if (!result.success) { | 
					
						
							|  |  |  |             showError(result.error); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-26 10:21:33 -05:00
										 |  |  |         else { | 
					
						
							|  |  |  |             showMessage("Query was executed successfully."); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-19 22:33:44 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const rows = result.rows; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 20:38:56 -05:00
										 |  |  |         resultHeadEl.empty(); | 
					
						
							|  |  |  |         resultBodyEl.empty(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 22:33:44 -05:00
										 |  |  |         if (rows.length > 0) { | 
					
						
							|  |  |  |             const result = rows[0]; | 
					
						
							| 
									
										
										
										
											2017-12-14 20:38:56 -05:00
										 |  |  |             const rowEl = $("<tr>"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for (const key in result) { | 
					
						
							|  |  |  |                 rowEl.append($("<th>").html(key)); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             resultHeadEl.append(rowEl); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 22:33:44 -05:00
										 |  |  |         for (const result of rows) { | 
					
						
							| 
									
										
										
										
											2017-12-14 20:38:56 -05:00
										 |  |  |             const rowEl = $("<tr>"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for (const key in result) { | 
					
						
							|  |  |  |                 rowEl.append($("<td>").html(result[key])); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             resultBodyEl.append(rowEl); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $(document).bind('keydown', 'alt+o', showDialog); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-20 22:30:44 -05:00
										 |  |  |     queryEl.bind('keydown', 'ctrl+return', execute); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 20:38:56 -05:00
										 |  |  |     executeButton.click(execute); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         showDialog | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | })(); |