1
1
<template >
2
- <div >
3
- <Form >
4
- <FormItem label =" SQL" >
5
- <Input type =" textarea"
6
- v-model =" current.sqlStr"
7
- :rows =" 3"
8
- placeholder =" SELECT * FROM T WHERE id=? AND name = ?" ></Input >
9
- </FormItem >
10
- <FormItem :label =" $t('sqlFillParameter_parameter')" >
11
- <Input type =" textarea"
12
- v-model =" current.paramStr"
13
- :rows =" 3"
14
- placeholder =" 1(Integer),zhangshan(String)" ></Input >
15
- </FormItem >
16
- <option-block class =" page-option-block" >
17
- <FormItem >
18
- <ButtonGroup >
19
- <Button type =" primary" @click =" fill" >{{ $t('sqlFillParameter_fill') }}</Button >
20
- <Button type =" primary" @click =" clear" >{{ $t('sqlFillParameter_clear') }}</Button >
21
- </ButtonGroup >
22
- </FormItem >
23
- </option-block >
24
- <FormItem :label =" $t('sqlFillParameter_out')" >
25
- <Input type =" textarea"
26
- v-model =" current.outStr"
27
- :rows =" 3"
28
- placeholder =" SELECT * FROM T WHERE id=1 AND name='zhangshan'" ></Input >
29
- </FormItem >
30
- </Form >
31
- </div >
2
+ <heightResize :reduce =" 5" @resize =" resize" >
3
+ <Row :gutter =" 10" >
4
+ <Col span =" 12" >
5
+ <autoHeightTextarea v-model =" current.sqlStr" :height =" inputHeight"
6
+ placeholder =" SQL:SELECT * FROM T WHERE id=? AND name = ?" />
7
+ <autoHeightTextarea v-model =" current.paramStr" :height =" inputHeight" style =" margin-top : 10px "
8
+ :placeholder =" `${$t('sqlFillParameter_parameter')}:1(Integer),zhangshan(String)`" />
9
+ </Col >
10
+ <Col span =" 12" >
11
+ <input-block :text =" `${outStr ? $t('sqlFillParameter_copy') : ''}`"
12
+ @on-default-right-bottom-click =" ()=>$copy(outStr)" >
13
+ <autoHeightTextarea v-model =" outStr" :height =" outputHeight"
14
+ :placeholder =" `${$t('sqlFillParameter_out')}:SELECT * FROM T WHERE id=1 AND name='zhangshan'`" />
15
+ </input-block >
16
+ </Col >
17
+ </Row >
18
+ </heightResize >
32
19
</template >
33
20
34
21
<script >
22
+ import heightResize from " ./components/heightResize" ;
23
+ import autoHeightTextarea from " ./components/autoHeightTextarea" ;
35
24
36
25
// 1-String 2-NUMBER 3-Timestamp
37
26
const TYPE_STR = [' String' , ' Integer' , ' Long' , ' Timestamp' ]
@@ -40,100 +29,109 @@ const TYPE_STR = ['String', 'Integer', 'Long', 'Timestamp']
40
29
* SQL 参数填充
41
30
*/
42
31
export default {
43
- data () {
44
- return {
45
-
46
- current: {
47
- sqlStr: " " , // SQL字符串
48
- paramStr: " " , // 参数字符串
49
- outStr: ' ' ,// 输出结果
50
- },
51
- }
52
- },
53
- created () {
54
- this .$initToolData ()
55
- },
56
- methods: {
57
- resize (height ) {
58
- this .inputHeight = Math .min (Math .ceil (height / 2 ), 160 );
59
- this .outputHeight = height - this .inputHeight ;
32
+ components: {
33
+ heightResize,
34
+ autoHeightTextarea
35
+ },
36
+ data () {
37
+ return {
38
+ inputHeight: 100 ,
39
+ outputHeight: 100 ,
40
+ current: {
41
+ sqlStr: " " ,
42
+ paramStr: " " ,
43
+ },
44
+ }
60
45
},
61
- clear () {
62
- this .current .sqlStr = ' '
63
- this .current .paramStr = ' '
64
- this .current .outStr = ' '
46
+ created () {
47
+ this .$initToolData ()
65
48
},
66
- fill () {
67
- try {
68
- // 解析参数
69
- let paramList = this .convertParameter ()
70
- // 按字读取SQL,将?做替换
71
- let tempSqlStr = this .current .sqlStr
72
- let resultStr = ' ' // 替换后的字符串
73
- let paramIndex = 0 // 参数访问索引
74
- let tempParamStr = ' ' // 用于存放参数字符串
75
- for (let i = 0 ; i < tempSqlStr .length ; i++ ) {
76
- // 检查到?就进行参数替换,不考虑SQL本身的合法性,不做SQL的语法和词法分析
77
- let c = tempSqlStr .charAt (i)
78
- if (c === ' ?' ) {
79
- // 需要检查参数列表的越界
80
- if (paramList .length <= paramIndex) {
81
- throw new Error (this .$t (' sqlFillParameter_parameter_too_little' ))
82
- }
83
- let param = paramList[paramIndex]
84
- switch (param .type ) {
85
- // String
86
- case TYPE_STR [0 ]:
87
- tempParamStr = ' \' ' + param .value + ' \' '
88
- break
89
- // Integer
90
- // Long
91
- case TYPE_STR [1 ]:
92
- case TYPE_STR [2 ]:
93
- tempParamStr = param .value
94
- break
95
- // Timestamp
96
- case TYPE_STR [3 ]:
97
- tempParamStr = ' timestamp' + param .value
98
- break
99
- // 其他类型直接拼接原始字符
100
- default :
101
- tempParamStr = param .value
49
+ computed: {
50
+ outStr () {
51
+ try {
52
+ if (! this .current .sqlStr || ! this .current .paramStr ) {
53
+ return " " ;
54
+ }
55
+ // 解析参数
56
+ let paramList = this .convertParameter ()
57
+ // 按字读取SQL,将?做替换
58
+ let tempSqlStr = this .current .sqlStr
59
+ let resultStr = ' ' // 替换后的字符串
60
+ let paramIndex = 0 // 参数访问索引
61
+ let tempParamStr = ' ' // 用于存放参数字符串
62
+ for (let i = 0 ; i < tempSqlStr .length ; i++ ) {
63
+ // 检查到?就进行参数替换,不考虑SQL本身的合法性,不做SQL的语法和词法分析
64
+ let c = tempSqlStr .charAt (i)
65
+ if (c === ' ?' ) {
66
+ // 需要检查参数列表的越界
67
+ if (paramList .length <= paramIndex) {
68
+ throw new Error (this .$t (' sqlFillParameter_parameter_too_little' ))
69
+ }
70
+ let param = paramList[paramIndex]
71
+ switch (param .type ) {
72
+ // String
73
+ case TYPE_STR [0 ]:
74
+ tempParamStr = ' \' ' + param .value + ' \' '
75
+ break
76
+ // Integer
77
+ // Long
78
+ case TYPE_STR [1 ]:
79
+ case TYPE_STR [2 ]:
80
+ tempParamStr = param .value
81
+ break
82
+ // Timestamp
83
+ case TYPE_STR [3 ]:
84
+ tempParamStr = ' timestamp' + param .value
85
+ break
86
+ // 其他类型直接拼接原始字符
87
+ default :
88
+ tempParamStr = param .value
89
+ }
90
+ // 字符拼接
91
+ resultStr += tempParamStr
92
+ paramIndex++
93
+ } else { // 正常拼接
94
+ resultStr += c
95
+ }
96
+ }
97
+ this .$saveToolData (this .current );
98
+ return resultStr;
99
+ } catch (e) {
100
+ return e .message
102
101
}
103
- // 字符拼接
104
- resultStr += tempParamStr
105
- paramIndex++
106
- } else { // 正常拼接
107
- resultStr += c
108
- }
109
102
}
110
- this .current .outStr = resultStr;
111
- this .$saveToolData (this .current );
112
- } catch (error) {
113
- this .$Message .error (error .message )
114
- }
103
+
115
104
},
116
- convertParameter : function () {
117
- if (this .current .paramStr ) {
118
- let paramStrList = this .current .paramStr .split (' ,' , - 1 )
119
- return paramStrList .map (x => {
120
- let valueEndIndex = x .lastIndexOf (' (' )
121
- if (valueEndIndex < 0 ) {
122
- throw new Error (this .$t (' sqlFillParameter_invalid_param' ) + x)
123
- }
124
- let value = x .substring (0 , valueEndIndex)
125
- let len = x .length
126
- let type = x .substring (valueEndIndex + 1 , len - 1 )
127
- return {value, type}
128
- })
129
- } else {
130
- return []
131
- }
105
+ methods: {
106
+ resize (height ) {
107
+ this .outputHeight = height
108
+ this .inputHeight = Math .floor ((height - 10 ) / 2 )
109
+ },
110
+ clear () {
111
+ this .current .sqlStr = ' '
112
+ this .current .paramStr = ' '
113
+ this .current .outStr = ' '
114
+ },
115
+ fill () {
116
+
117
+ },
118
+ convertParameter : function () {
119
+ if (this .current .paramStr ) {
120
+ let paramStrList = this .current .paramStr .split (' ,' , - 1 )
121
+ return paramStrList .map (x => {
122
+ let valueEndIndex = x .lastIndexOf (' (' )
123
+ if (valueEndIndex < 0 ) {
124
+ throw new Error (this .$t (' sqlFillParameter_invalid_param' ) + x)
125
+ }
126
+ let value = x .substring (0 , valueEndIndex)
127
+ let len = x .length
128
+ let type = x .substring (valueEndIndex + 1 , len - 1 )
129
+ return {value, type}
130
+ })
131
+ } else {
132
+ return []
133
+ }
134
+ }
132
135
}
133
- }
134
136
}
135
137
</script >
136
-
137
- <style scoped>
138
-
139
- </style >
0 commit comments