-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_tmp.html
338 lines (283 loc) · 13.7 KB
/
sql_tmp.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<!DOCTYPE html>
<html lang="zh-cn"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="case_when.ico">
<title>SQL 模板生成</title>
<link rel="stylesheet" href="bootstrap.min.css">
<link href="footer.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="d-flex flex-column flex-md-row align-items-center p-3 px-md-4 mb-3 bg-white border-bottom box-shadow">
<h5 class="my-0 mr-md-auto font-weight-normal"><b>Tools for SQL</b></h5>
<nav class="my-2 my-md-0 mr-md-3 nav">
<a class="nav-link p-2 text-dark" href="index.html">首页</a>
<a class="nav-link p-2 active" href="sql_tmp.html" data-toggle="tooltip" title="快速生成sql模板" >SQL 模板生成</a>
<a class="nav-link p-2 text-dark" href="case_when.html" data-toggle="tooltip" title="不想写case when的时候">快速 CASE WHEN</a>
<a class="nav-link p-2 text-dark" href="avoid_lose.html" data-toggle="tooltip" title="一次使用N张表" >疯狂JOIN</a>
<li><a class="nav-link" href="lab_join.html" data-toggle="tooltip" title="多表相似关联" >多表相似关联</a></li>
<li><a class="nav-link" href="beauty_sql.html" data-toggle="tooltip" title="SQL美化">SQL美化</a></li>
<a class="nav-link p-2 text-dark" href="lab.html" data-toggle="tooltip" title="sql实验室" >实验室</a>
<a class="nav-link p-2 text-dark" href="https://regex101.com/" data-toggle="tooltip" title="正则表达实验室" target="_blank">REGEX101</a>
</nav>
</div>
<div class="container">
<div class="py-5 text-center">
<h2>SQL 模板自动生成小工具</h2>
<p class="lead">输入相应的表名,选择合适的关联方式以及其他参数,自动生成SQL模板</p>
</div>
<div class="row">
<div class="col-md-7 order-md-1">
<form class="needs-validation" novalidate="">
<div class="row">
<div class="col-md-8 mb-3">
<label for="table1">主表名称</label>
<input type="text" class="form-control" id="table1" placeholder="" value="">
</div>
<div class="col-md-4 order-md-1">
<label for="outputName">是否加参数</label>
<select class="custom-select" id="jc">
<option value=1>是</option>
<option value=2>否</option>
</select>
</div>
</div>
<hr class="mb-3">
<div class="row">
<div class="col-md-4 mb-3">
<label for="table2">第二张表名</label>
<input type="text" class="form-control" id="table2" placeholder="选填" value="">
</div>
<div class="col-md-2 mb-3">
<label for="outputName">关联方式</label>
<select class="custom-select" id="jw2">
<option value=1>LEFT</option>
<option value=2>INNER</option>
<option value=3>RIGHT</option>
</select>
</div>
<div class="col-md-2 mb-3">
<label for="outputName">关联表序</label>
<select class="custom-select" id="on2">
<option value=1>1</option>
<option value=2 disabled="disabled" >2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
</select>
</div>
<div class="col-md-4 mb-3">
<label for="outputName">关联字段</label>
<input type="text" class="form-control" id="kw2" placeholder="多字段用空格分隔" value="">
</div>
</div>
<hr class="mb-3">
<div class="row">
<div class="col-md-4 mb-3">
<label for="table3">第三张表名</label>
<input type="text" class="form-control" id="table3" placeholder="选填" value="">
</div>
<div class="col-md-2 mb-3">
<label for="outputName">关联方式</label>
<select class="custom-select" id="jw3">
<option value=1>LEFT</option>
<option value=2>INNER</option>
<option value=3>RIGHT</option>
</select>
</div>
<div class="col-md-2 mb-3">
<label for="outputName">关联表序</label>
<select class="custom-select" id="on3">
<option value=1>1</option>
<option value=2>2</option>
<option value=3 disabled="disabled">3</option>
<option value=4>4</option>
<option value=5>5</option>
</select>
</div>
<div class="col-md-4 mb-3">
<label for="outputName">关联字段</label>
<input type="text" class="form-control" id="kw3" placeholder="多字段用空格分隔" value="">
</div>
</div>
<hr class="mb-3">
<div class="row">
<div class="col-md-4 mb-3">
<label for="table4th">第四张表名</label>
<input type="text" class="form-control" id="table4" placeholder="选填" value="">
</div>
<div class="col-md-2 mb-3">
<label for="outputName">关联方式</label>
<select class="custom-select" id="jw4">
<option value=1>LEFT</option>
<option value=2>INNER</option>
<option value=3>RIGHT</option>
</select>
</div>
<div class="col-md-2 mb-3">
<label for="outputName">关联表序</label>
<select class="custom-select" id="on4">
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4 disabled="disabled">4</option>
<option value=5>5</option>
</select>
</div>
<div class="col-md-4 mb-3">
<label for="outputName">关联字段</label>
<input type="text" class="form-control" id="kw4" placeholder="多字段用空格分隔" value="">
</div>
</div>
<hr class="mb-3">
<div class="row">
<div class="col-md-4 mb-3">
<label for="table5th">第五张表名</label>
<input type="text" class="form-control" id="table5" placeholder="选填" value="">
</div>
<div class="col-md-2 mb-3">
<label for="outputName">关联方式</label>
<select class="custom-select" id="jw5">
<option value=1>LEFT</option>
<option value=2>INNER</option>
<option value=3>RIGHT</option>
</select>
</div>
<div class="col-md-2 mb-3">
<label for="outputName">关联表序</label>
<select class="custom-select" id="on5">
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5 disabled="disabled">5</option>
</select>
</div>
<div class="col-md-4 mb-3">
<label for="outputName">关联字段</label>
<input type="text" class="form-control" id="kw5" placeholder="多字段用空格分隔" value="">
</div>
</div>
<hr class="mb-3">
<button class="btn btn-primary btn-lg btn-block" id="button1" type="submit" onclick="generate();return false;">一键生成</button>
<br>
</form>
</div>
<div class="col-md-5 order-md-2 mb-4">
<textarea class="form-control" id="textarea" rows="26" placeholder="目前只支持最多五张表相关联!"></textarea>
</div>
</div>
</div>
<footer class="text-muted text-center text-small footer">
<p class="mb-1">© 2018-2019 Clark Yu · <span><a class= 'text-muted text-dark' href="update_log.html" data-toggle="tooltip" title="又有新的更新啦!" target="_blank" style='text-decoration:none;'>更新日志</a></span> · <span><a class= 'text-muted text-dark' href="https://zhuanlan.zhihu.com/p/44502560" data-toggle="tooltip" title="提个意见!" target="_blank" style='text-decoration:none;'>给我留言</a></span></p>
<ul class="list-inline">
</ul>
</footer>
<script src="jquery.slim.min.js"></script>
<script src="popper.min.js"></script>
<script src="bootstrap.min.js"></script>
<script>
function generate() {
function check_kuming(table_name){
var re = /^(ods|dwb|dwb|dws|dim|rods|adm|sdm|rpt|app|tmp|hds]{3,4})_/;
var re_result = re.exec(table_name);
if(!re_result){
return table_name;
}else{
return re_result[1] + '.' + table_name;
}
}
function getNowFormatDate() {
var date = new Date(new Date()-24*60*60*1000);
var seperator1 = "-";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = year + seperator1 + month + seperator1 + strDate;
return currentdate;
}
var yesterday = getNowFormatDate();
function getRadioValue(radioName)
{
var radios = document.getElementsByName(radioName);
if(!radios)
return '';
for (var i = 0; i < radios.length; i++)
{
if (radios[i].checked)
return radios[i].value;
}
return '';
}
var table1 = document.getElementById("table1").value;
table1 = check_kuming(table1);
var first_text = `(\n\tSELECT\n\n\tFROM ${table1}\n\tWHERE pt = '${yesterday}'\n) t1`;
total_table_num = 5;
var text = '';
for (var i = 2; i <= total_table_num; i++){
var n = `${i}`;
var table = document.getElementById(`table${n}`).value;
table = check_kuming(table);
var join_way = $(`#jw${n} option:selected`).text() + ' JOIN';
var join_rk = $(`#on${n} option:selected`).val();
var on_list = document.getElementById(`kw${n}`).value.split(" ");
function getOntext(on_list,join_rk,n)
{
var ontext = '\nON ';
var lg = on_list.length;
if(lg>0){
for (var i = 0; i < lg; i++){
var kw = on_list[i];
ontext += `t${join_rk}.${kw} = t${n}.${kw}`
if(i!=lg-1){
ontext += '\nAND '
}
}
}else{
ontext = '';
}
return ontext;
}
var on_text = getOntext(on_list,join_rk,n);
if (table.length > 0){
text += `\n${join_way}` + `\n(\n\tSELECT\n\n\tFROM ${table}\n\tWHERE pt = '${yesterday}'\n)t${n}` + on_text;
}else{
text += '';
}
}
var tObj = document.getElementById('textarea');
var if_jc = $(`#jc option:selected`).val();
if(if_jc == 1){
var jc = 'SET hive.execution.engine=mr;\nSET hive.exec.parallel=true;\nSET hive.exec.parallel.thread.number=132;\n\n';
}else{
var jc = '';
}
var only_one = 0;
for (var i = 2; i <= total_table_num; i++){
if(document.getElementById(`table${i}`).value.length > 0){
only_one = 1
break;
}
}
if(document.getElementById(`table1`).value.length > 0){
if(only_one == 0){
tObj.value = jc + `SELECT\n\nFROM ${table1}\nWHERE pt = '${yesterday}'` + text + '\n;';
}else{
tObj.value = jc + 'SELECT\n\nFROM\n' + first_text + text + '\n;';
}
}else{
tObj.value = '请输入表名!';
}
}
$(function () {
$('[data-toggle="tooltip"]').tooltip()
})
</script>
</body></html>