Skip to content

Commit 6af80cb

Browse files
committed
optimize
1 parent c3d3620 commit 6af80cb

File tree

5 files changed

+130
-134
lines changed

5 files changed

+130
-134
lines changed

src/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const tool = [
5858
{'name': 'armConverter', 'cat': ['conversion']},
5959
{'name': 'bcrypt', 'cat': ['encryption','check']},
6060
{'name': 'ipcalc', 'cat': ['generate']},
61-
{'name': 'sqlFillParameter', 'cat': ['other']}
61+
{'name': 'sqlFillParameter', 'cat': ['generate']}
6262
]
6363

6464
// 工具类功能配置
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
2-
"invalid_param": "invalid parameter",
3-
'parameter': 'parameter',
4-
'parameter_too_little': 'no enough parameters ',
5-
'fill': 'fill',
6-
'clear': 'clear',
7-
'out': 'out'
2+
"invalid_param": "Invalid Parameter",
3+
'parameter': 'Parameter',
4+
'parameter_too_little': 'No Enough Parameters',
5+
'out': 'Out',
6+
'copy': 'Copy'
87
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
2-
"invalid_param": "无效的参数",
3-
'parameter': '参数',
4-
'parameter_too_little': '参数个数不足',
5-
'fill': '填充',
6-
'clear': '清空',
7-
'out': '输出'
2+
"invalid_param": "无效的参数",
3+
'parameter': '参数',
4+
'parameter_too_little': '参数个数不足',
5+
'out': '输出',
6+
'copy': '复制'
87
}

src/views/tool/binary.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<heightResize @resize="resize">
2+
<heightResize :reduce="5" @resize="resize">
33
<Row :gutter="10">
44
<Col span="8">
55
<input-block top="5px">
@@ -50,7 +50,7 @@ export default {
5050
methods: {
5151
resize(height) {
5252
this.inputHeight = height
53-
this.outputHeight = Math.ceil((height - 20) / 3)
53+
this.outputHeight = Math.floor((height - 20) / 3)
5454
},
5555
result(type) {
5656
if (this.current.input.trim() === "") {

src/views/tool/sqlFillParameter.vue

Lines changed: 117 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,26 @@
11
<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>
3219
</template>
3320

3421
<script>
22+
import heightResize from "./components/heightResize";
23+
import autoHeightTextarea from "./components/autoHeightTextarea";
3524
3625
// 1-String 2-NUMBER 3-Timestamp
3726
const TYPE_STR = ['String', 'Integer', 'Long', 'Timestamp']
@@ -40,100 +29,109 @@ const TYPE_STR = ['String', 'Integer', 'Long', 'Timestamp']
4029
* SQL 参数填充
4130
*/
4231
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+
}
6045
},
61-
clear() {
62-
this.current.sqlStr = ''
63-
this.current.paramStr = ''
64-
this.current.outStr = ''
46+
created() {
47+
this.$initToolData()
6548
},
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
102101
}
103-
// 字符拼接
104-
resultStr += tempParamStr
105-
paramIndex++
106-
} else { // 正常拼接
107-
resultStr += c
108-
}
109102
}
110-
this.current.outStr = resultStr;
111-
this.$saveToolData(this.current);
112-
} catch (error) {
113-
this.$Message.error(error.message)
114-
}
103+
115104
},
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+
}
132135
}
133-
}
134136
}
135137
</script>
136-
137-
<style scoped>
138-
139-
</style>

0 commit comments

Comments
 (0)