sql.vue 14.9 KB
Newer Older
K
khadgarmage 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
L
ligang 已提交
17 18 19
<template>
  <div class="sql-model">
    <m-list-box>
G
i18n  
gongzijian 已提交
20
      <div slot="text">{{$t('Datasource')}}</div>
L
ligang 已提交
21 22 23 24 25 26 27 28 29
      <div slot="content">
        <m-datasource
                ref="refDs"
                @on-dsData="_onDsData"
                :data="{ type:type,datasource:datasource }">
        </m-datasource>
      </div>
    </m-list-box>
    <m-list-box>
G
i18n  
gongzijian 已提交
30
      <div slot="text">{{$t('SQL Type')}}</div>
L
ligang 已提交
31 32 33 34 35 36 37
      <div slot="content">
        <div style="display: inline-block;">
          <m-sql-type
                  @on-sqlType="_onSqlType"
                  :sql-type="sqlType">
          </m-sql-type>
        </div>
38
        <div v-if="sqlType==0" style="display: inline-block;padding-left: 10px;margin-top: 2px;">
L
ligang 已提交
39
          <x-checkbox-group v-model="showType">
O
oceanos 已提交
40
            <x-checkbox :label="'TABLE'" :disabled="isDetails">{{$t('TableMode')}}</x-checkbox>
G
i18n  
gongzijian 已提交
41
            <x-checkbox :label="'ATTACHMENT'" :disabled="isDetails">{{$t('Attachment')}}</x-checkbox>
L
ligang 已提交
42 43 44 45
          </x-checkbox-group>
        </div>
      </div>
    </m-list-box>
46
    <template v-if="sqlType==0">
47
      <m-list-box>
48
        <div slot="text"><strong class='requiredIcon'>*</strong>{{$t('Title')}}</div>
49 50 51 52 53 54 55 56 57
        <div slot="content">
          <x-input
            type="input"
            v-model="title"
            :placeholder="$t('Please enter the title of email')"
            autocomplete="off">
          </x-input>
        </div>
      </m-list-box>
58
      <m-list-box>
59
        <div slot="text"><strong class='requiredIcon'>*</strong>{{$t('Recipient')}}</div>
60
        <div slot="content">
61
          <m-email ref="refEmail" v-model="receivers" :disabled="isDetails" :repeat-data="receiversCc"></m-email>
62 63 64
        </div>
      </m-list-box>
      <m-list-box>
65
        <div slot="text">{{$t('Cc')}}</div>
66
        <div slot="content">
67
          <m-email ref="refCc" v-model="receiversCc" :disabled="isDetails" :repeat-data="receivers"></m-email>
68 69 70
        </div>
      </m-list-box>
    </template>
L
ligang 已提交
71
    <m-list-box v-show="type === 'HIVE'">
G
i18n  
gongzijian 已提交
72
      <div slot="text">{{$t('SQL Parameter')}}</div>
L
ligang 已提交
73 74 75 76 77
      <div slot="content">
        <x-input
                :disabled="isDetails"
                type="input"
                v-model="connParams"
G
i18n  
gongzijian 已提交
78
                :placeholder="$t('Please enter format') + ' key1=value1;key2=value2...'"
L
ligang 已提交
79 80 81 82 83
                autocomplete="off">
        </x-input>
      </div>
    </m-list-box>
    <m-list-box>
G
i18n  
gongzijian 已提交
84
      <div slot="text">{{$t('SQL Statement')}}</div>
L
ligang 已提交
85 86 87 88 89 90 91 92 93 94 95
      <div slot="content">
        <div class="from-mirror">
          <textarea
                  id="code-sql-mirror"
                  name="code-sql-mirror"
                  style="opacity: 0;">
          </textarea>
        </div>
      </div>
    </m-list-box>
    <m-list-box v-if="type === 'HIVE'">
G
i18n  
gongzijian 已提交
96
      <div slot="text">{{$t('UDF Function')}}</div>
L
ligang 已提交
97 98 99 100 101 102 103 104 105 106
      <div slot="content">
        <m-udfs
                ref="refUdfs"
                @on-udfsData="_onUdfsData"
                :udfs="udfs"
                :type="type">
        </m-udfs>
      </div>
    </m-list-box>
    <m-list-box>
G
i18n  
gongzijian 已提交
107
      <div slot="text">{{$t('Custom Parameters')}}</div>
L
ligang 已提交
108 109 110 111 112 113 114 115
      <div slot="content">
        <m-local-params
                ref="refLocalParams"
                @on-udpData="_onUdpData"
                :udp-list="localParams">
        </m-local-params>
      </div>
    </m-list-box>
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    <m-list-box>
      <div slot="text">{{$t('Pre Statement')}}</div>
      <div slot="content">
        <m-statement-list
          ref="refPreStatements"
          @on-statement-list="_onPreStatements"
          :statement-list="preStatements">
        </m-statement-list>
      </div>
    </m-list-box>
    <m-list-box>
      <div slot="text">{{$t('Post Statement')}}</div>
      <div slot="content">
        <m-statement-list
          ref="refPostStatements"
          @on-statement-list="_onPostStatements"
          :statement-list="postStatements">
        </m-statement-list>
      </div>
    </m-list-box>
L
ligang 已提交
136 137 138 139 140 141 142 143 144 145
  </div>
</template>
<script>
  import _ from 'lodash'
  import i18n from '@/module/i18n'
  import mUdfs from './_source/udfs'
  import mListBox from './_source/listBox'
  import mSqlType from './_source/sqlType'
  import mDatasource from './_source/datasource'
  import mLocalParams from './_source/localParams'
146
  import mStatementList from './_source/statementList'
L
ligang 已提交
147
  import disabledState from '@/module/mixin/disabledState'
148
  import mEmail from '@/conf/home/pages/projects/pages/definition/pages/list/_source/email'
L
ligang 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  import codemirror from '@/conf/home/pages/resource/pages/file/pages/_source/codemirror'

  let editor

  export default {
    name: 'sql',
    data () {
      return {
        // Data source type
        type: '',
        // data source
        datasource: '',
        // Return to the selected data source
        rtDatasource: '',
        // Sql statement
        sql: '',
        // Custom parameter
        localParams: [],
        // UDF function
        udfs: '',
        // Sql type
170
        sqlType: '0',
171 172
        // Email title
        title: '',
L
ligang 已提交
173 174 175
        // Form/attachment
        showType: ['TABLE'],
        // Sql parameter
176 177 178 179
        connParams: '',
        // Pre statements
        preStatements: [],
        // Post statements
B
baoliang 已提交
180
        postStatements: [],
181 182 183 184
        // recipients
        receivers: [],
        // copy to
        receiversCc: []
L
ligang 已提交
185 186 187 188
      }
    },
    mixins: [disabledState],
    props: {
189 190
      backfillItem: Object,
      createNodeId: Number
L
ligang 已提交
191 192 193 194 195 196 197
    },
    methods: {
      /**
       * return sqlType
       */
      _onSqlType (a) {
        this.sqlType = a
B
break60 已提交
198 199 200
        if(a==0) {
          this.showType = ['TABLE']
        }
L
ligang 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
      },
      /**
       * return udfs
       */
      _onUdfsData (a) {
        this.udfs = a
      },
      /**
       * return Custom parameter
       */
      _onUdpData (a) {
        this.localParams = a
      },
      /**
       * return data source
       */
      _onDsData (o) {
        this.type = o.type
        this.rtDatasource = o.datasource
      },
221 222 223 224 225 226 227 228 229 230 231 232
      /**
       * return pre statements
       */
      _onPreStatements (a) {
        this.preStatements = a
      },
      /**
       * return post statements
       */
      _onPostStatements (a) {
        this.postStatements = a
      },
L
ligang 已提交
233 234 235 236 237
      /**
       * verification
       */
      _verification () {
        if (!editor.getValue()) {
G
i18n  
gongzijian 已提交
238
          this.$message.warning(`${i18n.$t('Please enter a SQL Statement(required)')}`)
L
ligang 已提交
239 240 241 242 243 244 245
          return false
        }

        // datasource Subcomponent verification
        if (!this.$refs.refDs._verifDatasource()) {
          return false
        }
B
break60 已提交
246 247 248 249 250 251 252 253 254 255 256 257
        if (this.sqlType==0 && !this.showType.length) {
          this.$message.warning(`${i18n.$t('One form or attachment must be selected')}`)
          return false
        }
        if (this.sqlType==0 && !this.title) {
          this.$message.warning(`${i18n.$t('Mail subject required')}`)
          return false
        }
        if (this.sqlType==0 && !this.receivers.length) {
          this.$message.warning(`${i18n.$t('Recipient required')}`)
          return false
        }
258
        // receivers Subcomponent verification
259
        if (this.sqlType==0 && !this.$refs.refEmail._manualEmail()) {
260 261 262
          return false
        }
        // receiversCc Subcomponent verification
263
        if (this.sqlType==0 && !this.$refs.refCc._manualEmail()) {
264 265
          return false
        }
L
ligang 已提交
266 267 268 269 270 271 272 273 274 275 276 277
        // udfs Subcomponent verification Verification only if the data type is HIVE
        if (this.type === 'HIVE') {
          if (!this.$refs.refUdfs._verifUdfs()) {
            return false
          }
        }

        // localParams Subcomponent verification
        if (!this.$refs.refLocalParams._verifProp()) {
          return false
        }

278 279 280 281 282 283 284 285 286 287
        // preStatements Subcomponent verification
        if (!this.$refs.refPreStatements._verifProp()) {
          return false
        }

        // postStatements Subcomponent verification
        if (!this.$refs.refPostStatements._verifProp()) {
          return false
        }

L
ligang 已提交
288 289 290 291 292 293 294
        // storage
        this.$emit('on-params', {
          type: this.type,
          datasource: this.rtDatasource,
          sql: editor.getValue(),
          udfs: this.udfs,
          sqlType: this.sqlType,
295
          title: this.title,
296 297
          receivers: this.receivers.join(','),
          receiversCc: this.receiversCc.join(','),
L
ligang 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310
          showType: (() => {
            /**
             * Special processing return order TABLE,ATTACHMENT
             * Handling checkout sequence
             */
            let showType = this.showType
            if (showType.length === 2 && showType[0] === 'ATTACHMENT') {
              return [showType[1], showType[0]].join(',')
            } else {
              return showType.join(',')
            }
          })(),
          localParams: this.localParams,
311 312 313
          connParams: this.connParams,
          preStatements: this.preStatements,
          postStatements: this.postStatements
L
ligang 已提交
314 315 316 317 318 319 320
        })
        return true
      },
      /**
       * Processing code highlighting
       */
      _handlerEditor () {
321 322
        this._destroyEditor()

L
ligang 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336
        // editor
        editor = codemirror('code-sql-mirror', {
          mode: 'sql',
          readOnly: this.isDetails
        })

        this.keypress = () => {
          if (!editor.getOption('readOnly')) {
            editor.showHint({
              completeSingle: false
            })
          }
        }

337 338 339 340
        this.changes = () => {
          this._cacheParams()
        }

L
ligang 已提交
341 342 343
        // Monitor keyboard
        editor.on('keypress', this.keypress)

344 345
        editor.on('changes', this.changes)

L
ligang 已提交
346 347 348
        editor.setValue(this.sql)

        return editor
349 350
      },
      _getReceiver () {
351 352 353 354 355 356 357 358
        let param = {}
        let current = this.router.history.current
        if (current.name === 'projects-definition-details') {
          param.processDefinitionId = current.params.id
        } else {
          param.processInstanceId = current.params.id
        }
        this.store.dispatch('dag/getReceiver', param).then(res => {
359 360 361
          this.receivers = res.receivers && res.receivers.split(',') || []
          this.receiversCc = res.receiversCc && res.receiversCc.split(',') || []
        })
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
      },
      _cacheParams () {
        this.$emit('on-cache-params', {
          type: this.type,
          datasource: this.rtDatasource,
          sql: editor ? editor.getValue() : '',
          udfs: this.udfs,
          sqlType: this.sqlType,
          title: this.title,
          receivers: this.receivers.join(','),
          receiversCc: this.receiversCc.join(','),
          showType: (() => {

            let showType = this.showType
            if (showType.length === 2 && showType[0] === 'ATTACHMENT') {
              return [showType[1], showType[0]].join(',')
            } else {
              return showType.join(',')
            }
          })(),
          localParams: this.localParams,
          connParams: this.connParams,
          preStatements: this.preStatements,
          postStatements: this.postStatements
        });
      },
      _destroyEditor () {
         if (editor) {
          editor.toTextArea() // Uninstall
          editor.off($('.code-sql-mirror'), 'keypress', this.keypress)
          editor.off($('.code-sql-mirror'), 'changes', this.changes)
        }
L
ligang 已提交
394 395 396 397 398
      }
    },
    watch: {
      // Listening to sqlType
      sqlType (val) {
399
        if (val==0) {
L
ligang 已提交
400 401
          this.showType = []
        }
402
        if (val != 0) {
403
          this.title = ''
404 405 406
          this.receivers = []
          this.receiversCc = []
        }
L
ligang 已提交
407 408 409 410 411 412
      },
      // Listening data source
      type (val) {
        if (val !== 'HIVE') {
          this.connParams = ''
        }
413
      },
414 415
      //Watch the cacheParams
      cacheParams (val) {
416
        this._cacheParams()
417
      }
L
ligang 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431
    },
    created () {
      let o = this.backfillItem

      // Non-null objects represent backfill
      if (!_.isEmpty(o)) {
        // backfill
        this.type = o.params.type || ''
        this.datasource = o.params.datasource || ''
        this.sql = o.params.sql || ''
        this.udfs = o.params.udfs || ''
        this.sqlType = o.params.sqlType
        this.connParams = o.params.connParams || ''
        this.localParams = o.params.localParams || []
432 433 434 435 436
        if(o.params.showType == '') {
          this.showType = []
        } else {
          this.showType = o.params.showType.split(',') || []
        }
437 438
        this.preStatements = o.params.preStatements || []
        this.postStatements = o.params.postStatements || []
439
        this.title = o.params.title || ''
440 441
        this.receivers = o.params.receivers && o.params.receivers.split(',') || []
        this.receiversCc = o.params.receiversCc && o.params.receiversCc.split(',') || []
L
ligang 已提交
442
      }
443 444
      // read tasks from cache
      if (!_.some(this.store.state.dag.cacheTasks, { id: this.createNodeId }) &&
445
        this.router.history.current.name !== 'definition-create') {
446
        this._getReceiver()
L
ligang 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460
      }
    },
    mounted () {
      setTimeout(() => {
        this._handlerEditor()
      }, 200)
    },
    destroyed () {
      /**
       * Destroy the editor instance
       */
      if (editor) {
        editor.toTextArea() // Uninstall
        editor.off($('.code-sql-mirror'), 'keypress', this.keypress)
461
        editor.off($('.code-sql-mirror'), 'changes', this.changes)
L
ligang 已提交
462 463
      }
    },
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
    computed: {
      cacheParams () {
        return {
          type: this.type,
          datasource: this.rtDatasource,
          udfs: this.udfs,
          sqlType: this.sqlType,
          title: this.title,
          receivers: this.receivers.join(','),
          receiversCc: this.receiversCc.join(','),
          showType: (() => {

            let showType = this.showType
            if (showType.length === 2 && showType[0] === 'ATTACHMENT') {
              return [showType[1], showType[0]].join(',')
            } else {
              return showType.join(',')
            }
          })(),
          localParams: this.localParams,
          connParams: this.connParams,
          preStatements: this.preStatements,
          postStatements: this.postStatements
        }
      }
    },
B
baoliang 已提交
490
    components: { mListBox, mDatasource, mLocalParams, mUdfs, mSqlType, mStatementList, mEmail }
L
ligang 已提交
491 492
  }
</script>
B
break60 已提交
493 494 495 496 497 498 499
<style lang="scss" rel="stylesheet/scss">
  .requiredIcon {
    color: #ff0000;
    padding-right: 4px;
  }
</style>