|
| 1 | +<!-- |
| 2 | + Licensed to Cloudera, Inc. under one |
| 3 | + or more contributor license agreements. See the NOTICE file |
| 4 | + distributed with this work for additional information |
| 5 | + regarding copyright ownership. Cloudera, Inc. licenses this file |
| 6 | + to you under the Apache License, Version 2.0 (the |
| 7 | + "License"); you may not use this file except in compliance |
| 8 | + with the License. You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, software |
| 13 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + See the License for the specific language governing permissions and |
| 16 | + limitations under the License. |
| 17 | +--> |
| 18 | + |
| 19 | +<template> |
| 20 | + <div class="sql-scratchpad"> |
| 21 | + <HueIcons /> |
| 22 | + <Spinner v-if="loading" spin="true" /> |
| 23 | + <div v-if="!loading && executor" class="sql-scratchpad-container"> |
| 24 | + <div class="sql-scratchpad-editor"> |
| 25 | + <AceEditor |
| 26 | + :id="id" |
| 27 | + :ace-options="aceOptions" |
| 28 | + :executor="executor" |
| 29 | + :sql-parser-provider="sqlParserProvider" |
| 30 | + :sql-reference-provider="sqlReferenceProvider" |
| 31 | + @active-statement-changed="onActiveStatementChanged" |
| 32 | + /> |
| 33 | + </div> |
| 34 | + <div class="sql-scratchpad-progress"> |
| 35 | + <ExecutableProgressBar :executable="activeExecutable" /> |
| 36 | + </div> |
| 37 | + <div class="sql-scratchpad-actions"> |
| 38 | + <ExecuteButton :executable="activeExecutable" /> |
| 39 | + <ExecuteLimitInput :executable="activeExecutable" /> |
| 40 | + </div> |
| 41 | + <div class="sql-scratchpad-result"> |
| 42 | + <ResultTable :executable="activeExecutable" /> |
| 43 | + </div> |
| 44 | + </div> |
| 45 | + <div v-else-if="!loading && !executor"> |
| 46 | + Failed loading the SQL Scratchpad! |
| 47 | + </div> |
| 48 | + </div> |
| 49 | +</template> |
| 50 | + |
| 51 | +<script lang="ts"> |
| 52 | + import { defineComponent, onMounted, PropType, ref, toRefs } from 'vue'; |
| 53 | + import { Ace } from 'ext/ace'; |
| 54 | +
|
| 55 | + import genericAutocompleteParser from 'parse/sql/generic/genericAutocompleteParser'; |
| 56 | + import genericSyntaxParser from 'parse/sql/generic/genericSyntaxParser'; |
| 57 | + import { SqlParserProvider } from 'parse/types'; |
| 58 | + import { SqlReferenceProvider } from 'sql/reference/types'; |
| 59 | +
|
| 60 | + import './SqlScratchpad.scss'; |
| 61 | + import AceEditor from '../aceEditor/AceEditor.vue'; |
| 62 | + import { ActiveStatementChangedEventDetails } from '../aceEditor/types'; |
| 63 | + import ExecutableProgressBar from '../ExecutableProgressBar.vue'; |
| 64 | + import ExecuteButton from '../ExecuteButton.vue'; |
| 65 | + import ExecuteLimitInput from '../ExecuteLimitInput.vue'; |
| 66 | + import ResultTable from '../result/ResultTable.vue'; |
| 67 | + import Executor from '../../execution/executor'; |
| 68 | + import SqlExecutable from '../../execution/sqlExecutable'; |
| 69 | + import contextCatalog from 'catalog/contextCatalog'; |
| 70 | + import HueIcons from 'components/icons/HueIcons.vue'; |
| 71 | + import Spinner from 'components/Spinner.vue'; |
| 72 | + import { findEditorConnector, getConfig } from 'config/hueConfig'; |
| 73 | + import { Compute, Connector, Namespace } from 'config/types'; |
| 74 | + import { UUID } from 'utils/hueUtils'; |
| 75 | +
|
| 76 | + export default defineComponent({ |
| 77 | + name: 'SqlScratchpad', |
| 78 | + components: { |
| 79 | + Spinner, |
| 80 | + HueIcons, |
| 81 | + ResultTable, |
| 82 | + ExecuteLimitInput, |
| 83 | + ExecuteButton, |
| 84 | + ExecutableProgressBar, |
| 85 | + AceEditor |
| 86 | + }, |
| 87 | + props: { |
| 88 | + dialect: { |
| 89 | + type: String as PropType<string | null>, |
| 90 | + default: null |
| 91 | + } |
| 92 | + }, |
| 93 | + setup(props) { |
| 94 | + const { dialect } = toRefs(props); |
| 95 | + const activeExecutable = ref<SqlExecutable | null>(null); |
| 96 | + const executor = ref<Executor | null>(null); |
| 97 | + const loading = ref<boolean>(true); |
| 98 | + const id = UUID(); |
| 99 | +
|
| 100 | + const sqlParserProvider: SqlParserProvider = { |
| 101 | + getAutocompleteParser: () => Promise.resolve(genericAutocompleteParser), |
| 102 | + getSyntaxParser: () => Promise.resolve(genericSyntaxParser) |
| 103 | + }; |
| 104 | +
|
| 105 | + const sqlReferenceProvider: SqlReferenceProvider = { |
| 106 | + getReservedKeywords: () => Promise.resolve(new Set<string>()), |
| 107 | + getSetOptions: () => Promise.resolve({}), |
| 108 | + getUdfCategories: () => Promise.resolve([]), |
| 109 | + hasUdfCategories: () => false |
| 110 | + }; |
| 111 | +
|
| 112 | + const aceOptions: Ace.Options = { |
| 113 | + showLineNumbers: true, |
| 114 | + showGutter: true, |
| 115 | + maxLines: null, |
| 116 | + minLines: null |
| 117 | + }; |
| 118 | +
|
| 119 | + const initializeExecutor = async (): Promise<void> => { |
| 120 | + try { |
| 121 | + await getConfig(); |
| 122 | + } catch { |
| 123 | + console.warn('Failed loading Hue config!'); |
| 124 | + return; |
| 125 | + } |
| 126 | +
|
| 127 | + const connector = findEditorConnector( |
| 128 | + connector => !dialect.value || connector.dialect === dialect.value |
| 129 | + ); |
| 130 | + if (!connector) { |
| 131 | + console.warn('No connector found!'); |
| 132 | + return; |
| 133 | + } |
| 134 | +
|
| 135 | + try { |
| 136 | + const { namespaces } = await contextCatalog.getNamespaces({ connector }); |
| 137 | +
|
| 138 | + if (!namespaces.length || !namespaces[0].computes.length) { |
| 139 | + console.warn('No namespaces or computes found!'); |
| 140 | + return; |
| 141 | + } |
| 142 | +
|
| 143 | + const namespace = namespaces[0]; |
| 144 | + const compute = namespace.computes[0]; |
| 145 | +
|
| 146 | + executor.value = new Executor({ |
| 147 | + connector: (() => connector as Connector) as KnockoutObservable<Connector>, |
| 148 | + namespace: (() => namespace) as KnockoutObservable<Namespace>, |
| 149 | + compute: (() => compute) as KnockoutObservable<Compute>, |
| 150 | + database: (() => 'default') as KnockoutObservable<string> |
| 151 | + }); |
| 152 | + } catch {} |
| 153 | + }; |
| 154 | +
|
| 155 | + const onActiveStatementChanged = (event: ActiveStatementChangedEventDetails) => { |
| 156 | + if (executor.value) { |
| 157 | + executor.value.update(event, false); |
| 158 | + activeExecutable.value = executor.value.activeExecutable as SqlExecutable; |
| 159 | + } |
| 160 | + }; |
| 161 | +
|
| 162 | + onMounted(async () => { |
| 163 | + loading.value = true; |
| 164 | + try { |
| 165 | + await initializeExecutor(); |
| 166 | + } catch {} |
| 167 | + loading.value = false; |
| 168 | + }); |
| 169 | +
|
| 170 | + return { |
| 171 | + aceOptions, |
| 172 | + activeExecutable, |
| 173 | + executor, |
| 174 | + id, |
| 175 | + onActiveStatementChanged, |
| 176 | + sqlParserProvider, |
| 177 | + sqlReferenceProvider |
| 178 | + }; |
| 179 | + } |
| 180 | + }); |
| 181 | +</script> |
0 commit comments