| 
									
										
										
										
											2018-03-27 22:42:46 -04:00
										 |  |  | import libraryLoader from '../services/library_loader.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  | import server from '../services/server.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 21:29:35 -04:00
										 |  |  | import infoService from "../services/info.js"; | 
					
						
							| 
									
										
										
										
											2018-02-10 09:14:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | const $dialog = $("#sql-console-dialog"); | 
					
						
							|  |  |  | const $query = $('#sql-console-query'); | 
					
						
							|  |  |  | const $executeButton = $('#sql-console-execute'); | 
					
						
							|  |  |  | const $resultHead = $('#sql-console-results thead'); | 
					
						
							|  |  |  | const $resultBody = $('#sql-console-results tbody'); | 
					
						
							| 
									
										
										
										
											2019-02-10 10:38:18 +01:00
										 |  |  | const $tables = $("#sql-console-tables"); | 
					
						
							| 
									
										
										
										
											2018-02-10 09:14:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | let codeEditor; | 
					
						
							| 
									
										
										
										
											2018-02-10 09:14:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-06 17:47:40 +01:00
										 |  |  | $dialog.on("shown.bs.modal", e => initEditor()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-13 23:27:00 +01:00
										 |  |  | async function showDialog() { | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     glob.activeDialog = $dialog; | 
					
						
							| 
									
										
										
										
											2018-03-07 20:33:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-13 23:27:00 +01:00
										 |  |  |     await showTables(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-06 17:47:40 +01:00
										 |  |  |     $dialog.modal(); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-02-10 09:14:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | async function initEditor() { | 
					
						
							|  |  |  |     if (!codeEditor) { | 
					
						
							| 
									
										
										
										
											2018-03-27 22:42:46 -04:00
										 |  |  |         await libraryLoader.requireLibrary(libraryLoader.CODE_MIRROR); | 
					
						
							| 
									
										
										
										
											2018-02-19 22:02:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         CodeMirror.keyMap.default["Shift-Tab"] = "indentLess"; | 
					
						
							|  |  |  |         CodeMirror.keyMap.default["Tab"] = "indentMore"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // removing Escape binding so that Escape will propagate to the dialog (which will close on escape)
 | 
					
						
							|  |  |  |         delete CodeMirror.keyMap.basic["Esc"]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         CodeMirror.modeURL = 'libraries/codemirror/mode/%N/%N.js'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         codeEditor = CodeMirror($query[0], { | 
					
						
							|  |  |  |             value: "", | 
					
						
							|  |  |  |             viewportMargin: Infinity, | 
					
						
							|  |  |  |             indentUnit: 4, | 
					
						
							|  |  |  |             highlightSelectionMatches: {showToken: /\w/, annotateScrollbar: false} | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-02-19 22:02:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         codeEditor.setOption("mode", "text/x-sqlite"); | 
					
						
							|  |  |  |         CodeMirror.autoLoadMode(codeEditor, "sql"); | 
					
						
							| 
									
										
										
										
											2017-12-14 20:38:56 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     codeEditor.focus(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-07 20:46:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | async function execute(e) { | 
					
						
							|  |  |  |     // stop from propagating upwards (dangerous especially with ctrl+enter executable javascript notes)
 | 
					
						
							|  |  |  |     e.preventDefault(); | 
					
						
							|  |  |  |     e.stopPropagation(); | 
					
						
							| 
									
										
										
										
											2017-12-14 20:38:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-18 08:29:56 +01:00
										 |  |  |     // execute the selected text or the whole content if there's no selection
 | 
					
						
							|  |  |  |     let sqlQuery = codeEditor.getSelection(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!sqlQuery) { | 
					
						
							|  |  |  |         sqlQuery = codeEditor.getValue(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-14 20:38:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     const result = await server.post("sql/execute", { | 
					
						
							|  |  |  |         query: sqlQuery | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-12-19 22:33:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     if (!result.success) { | 
					
						
							| 
									
										
										
										
											2018-03-25 21:29:35 -04:00
										 |  |  |         infoService.showError(result.error); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2018-03-25 21:29:35 -04:00
										 |  |  |         infoService.showMessage("Query was executed successfully."); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-19 22:33:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     const rows = result.rows; | 
					
						
							| 
									
										
										
										
											2017-12-14 20:38:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     $resultHead.empty(); | 
					
						
							|  |  |  |     $resultBody.empty(); | 
					
						
							| 
									
										
										
										
											2017-12-14 20:38:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     if (rows.length > 0) { | 
					
						
							|  |  |  |         const result = rows[0]; | 
					
						
							|  |  |  |         const rowEl = $("<tr>"); | 
					
						
							| 
									
										
										
										
											2017-12-14 20:38:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         for (const key in result) { | 
					
						
							|  |  |  |             rowEl.append($("<th>").html(key)); | 
					
						
							| 
									
										
										
										
											2017-12-14 20:38:56 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         $resultHead.append(rowEl); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-14 20:38:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     for (const result of rows) { | 
					
						
							|  |  |  |         const rowEl = $("<tr>"); | 
					
						
							| 
									
										
										
										
											2017-12-14 20:38:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         for (const key in result) { | 
					
						
							|  |  |  |             rowEl.append($("<td>").html(result[key])); | 
					
						
							| 
									
										
										
										
											2017-12-14 20:38:56 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resultBody.append(rowEl); | 
					
						
							| 
									
										
										
										
											2017-12-14 20:38:56 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-14 20:38:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-13 23:27:00 +01:00
										 |  |  | async function showTables() { | 
					
						
							|  |  |  |     const tables = await server.get('sql/schema'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $tables.empty(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const table of tables) { | 
					
						
							|  |  |  |         const $tableLink = $('<button class="btn">').text(table.name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const $columns = $("<table>"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const column of table.columns) { | 
					
						
							|  |  |  |             $columns.append( | 
					
						
							|  |  |  |                 $("<tr>") | 
					
						
							|  |  |  |                     .append($("<td>").text(column.name)) | 
					
						
							|  |  |  |                     .append($("<td>").text(column.type)) | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $tables.append($tableLink).append(" "); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $tableLink | 
					
						
							|  |  |  |             .tooltip({html: true, title: $columns.html()}) | 
					
						
							|  |  |  |             .click(() => codeEditor.setValue("SELECT * FROM " + table.name + " LIMIT 100")); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | $query.bind('keydown', 'ctrl+return', execute); | 
					
						
							| 
									
										
										
										
											2017-12-20 22:30:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | $executeButton.click(execute); | 
					
						
							| 
									
										
										
										
											2017-12-14 20:38:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | export default { | 
					
						
							|  |  |  |     showDialog | 
					
						
							|  |  |  | }; |