Repository navigation
Expand file tree
/
Copy pathsql_query.html
More file actions
665 lines (622 loc) · 26.4 KB
/
Copy pathsql_query.html
File metadata and controls
665 lines (622 loc) · 26.4 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
{% extends 'base.html' %}
{% load staticfiles %}
{% block link_css %}
<style>
.CodeMirror {
border: 1px solid #eee;
height: 450px;
}
.tab .nav-tabs {
padding-left: 15px;
}
.tab .nav-tabs li a {
padding: 5px 10px;
margin-right: 5px;
position: relative;
}
/* 修复bootstrap-table中显示数据时,若原数据包含了多个空格显示为一个空格的bug */
.bootstrap-table .table:not(.table-condensed) > tbody > tr > td {
white-space: pre !important;
}
</style>
{% endblock %}
{% block right_content %}
<div class="row">
<div class="col-md-12">
<div class="box box-info">
<div class="box-header box-border">
<div class="mailbox-read-info">
<h3 class="box-title"><i class="fa fa-cog"></i> MySQL查询</h3>
</div>
</div>
<div class="row">
<div class="box-body box-border">
<div class="col-md-3" style="padding-right: 2px">
<div style="height: 610px;overflow: scroll;border:2px solid #eee;border-left-width: 0; border-right-width: 4px">
<div id="container"></div>
</div>
</div>
<div class="col-md-9" style="padding-left: 2px">
<div style="margin: 3px">
<button type="button" onclick="beautifySQL()" class="btn bg-info btn-sm"><i
class="fa fa-magic"></i> 格式化SQL
</button>
<button onclick="commitSqlQueryForm()" class="btn bg-info btn-sm"><i
class="fa fa-flash"></i> 执行
</button>
<button class="btn bg-info btn-sm" onclick="getHistorySql()"><i
class="fa fa-history"></i> 我的SQL
</button>
<select onchange="selectTheme()" id=select title="选择主题..." class="pull-right">
<option selected>default</option>
<option>material</option>
<option>darcula</option>
<option>monokai</option>
</select>
</div>
<div style="margin: 2px">
<textarea title="" id="s_sql"></textarea>
</div>
<div style="margin: 2px">
<textarea title="" id="query_log"></textarea>
</div>
</div>
</div>
</div>
<div class="box-body with-border" id="typediv1" style="visibility: hidden">
<div class="tab">
<ul class="nav nav-tabs" id="li_append"></ul>
<div class="tab-content" id="table_append"></div>
</div>
</div>
</div>
</div>
</div>
<!-- SQL历史模态框 -->
<div class="modal fade" id="modal_history_sql" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel"><i class="fa fa-history"></i> SQL历史
</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="box-body">
<div class="input-group">
<input title="" id='search_contents' type="text" placeholder="输入关键字、搜索更多SQL记录"
class="form-control">
<span class="input-group-btn">
<button type="button" onclick="getFilterHistorySql()" class="btn btn-info btn-flat">搜索</button>
</span>
</div>
</div>
<div class="box-body">
<p>默认显示最近的1000条记录</p>
<textarea title="" id="preview_history_sql"></textarea>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
</div>
</div>
</div>
</div>
<!-- 表结构模态框 -->
<div class="modal fade modal-wide" id="modal_table_stru">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title"><i class="fa fa-table"></i> 表结构信息</h4>
</div>
<div class="modal-body">
<div class="box-body">
<div>
<pre><code class="sql" id="table_stru"></code></pre>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">关闭</button>
</div>
</div>
</div>
</div>
<!-- 表基本信息模态框 -->
<div class="modal fade modal-wide" id="modal_table_base">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title"><i class="fa fa-table"></i> 表基本信息</h4>
</div>
<div class="modal-body">
<div class="box-body">
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>属性名</th>
<th>属性值</th>
</tr>
</thead>
<tbody id="table_base"></tbody>
</table>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">关闭</button>
</div>
</div>
</div>
</div>
<!-- 表索引模态框 -->
<div class="modal fade modal-wide" id="modal_table_index">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title"><i class="fa fa-table"></i> 表索引信息</h4>
</div>
<div class="modal-body">
<div class="box-body">
<div>
<table class="table table-hover" id="table_index"></table>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">关闭</button>
</div>
</div>
</div>
</div>
{% endblock %}
{% block link_javascripts %}
<script>
let myTextarea = document.getElementById('s_sql');
let myCodeMirror = CodeMirror.fromTextArea(myTextarea, {
lineNumbers: true,
mode: "text/x-mysql",
autoRefresh: true,
smartIndent: true,
indentWithTabs: true,
styleActiveLine: true,
autofocus: true,
keyMap: "sublime",
autoCloseBrackets: true,
matchBrackets: true,
lineWrapping: true,
showCursorWhenSelecting: true,
hint: CodeMirror.hint.sql
});
let myQueryLog = CodeMirror.fromTextArea(document.getElementById('query_log'), {
mode: "text/x-mysql",
autoRefresh: true,
readOnly: true,
autofocus: false,
lineWrapping: true,
theme: "material",
placeholder: '消息 ...'
});
myQueryLog.setSize('height', '120px');
myCodeMirror.on("keyup", function (cm, event) {
if (!cm.state.completionActive && /*Enables keyboard navigation in autocomplete list*/
event.keyCode !== 13 && /*delete不提示*/
event.keyCode !== 186 && /*分号;不提示*/
event.keyCode !== 32 && /*按空格不提示*/
event.keyCode !== 9 && /*按tab不提示*/
event.keyCode !== 8) { /*Enter - do not open autocomplete list just after item has been selected in it*/
CodeMirror.commands.autocomplete(cm, null, {completeSingle: false});
}
});
/**
* 获取授权的库表字段信息
*/
$('#container').jstree({
'core': {
"animation": 0,
"themes": {
"responsive": true,
},
'data': {
'dataType': 'json',
'async': true,
'cache': true,
'timeout': 20000,
'method': 'GET',
'url': '{% url 'p_get_grant_schema' %}',
'data': function (node) {
return {'id': node.id, 'text': node.text}
},
},
},
"types": {
"default": {
"icon": "fa fa-database text-green"
},
},
"contextmenu": {
select_node: true,
show_at_node: true,
items: {
"show_struc": {
"label": "查看表结构",
"action": function (data) {
let inst = $.jstree.reference(data.reference);
let node = inst.get_node(data.reference, false);
let d = node.id;
if (d.split('__')[2].split('.').length > 1) {
showTableStrucModal(d);
} else {
displayPNotify(2, '请选择表进行查看')
}
}
},
"show_index": {
"label": "查看表索引",
"action": function (data) {
let inst = $.jstree.reference(data.reference);
let node = inst.get_node(data.reference, false);
let d = node.id;
if (d.split('__')[2].split('.').length > 1) {
showTableIndexModal(d);
} else {
displayPNotify(2, '请选择表进行查看')
}
}
},
"show_baseinfo": {
"label": "查看表基本信息",
"action": function (data) {
let inst = $.jstree.reference(data.reference);
let node = inst.get_node(data.reference, false);
let d = node.id;
if (d.split('__')[2].split('.').length > 1) {
showTableBaseModal(d);
} else {
displayPNotify(2, '请选择表进行查看')
}
}
}
}
},
"plugins": ["types", "sort", "themes", "state", "contextmenu"]
});
jstree_click();
let query_schema = '';
let tab_schema = '';
let $table_index = $('#table_index');
function jstree_click() {
let $container = $('#container');
// 单击jstree时触发
$container.on("changed.jstree", function (e, data) {
query_schema = data.selected.toString();
});
$container.on("after_open.jstree", function (e, data) {
// tab补全
// 172.17.101.40-3306-test
tab_schema = data.node.id.split('___').join(',');
if (data.node.id.split('___')[2].split('.').length === 1) {
if (tab_schema) {
get_table_info(tab_schema);
}
}
});
}
<!-- 查看表结构 -->
function showTableStrucModal(data) {
$('#table_stru').empty();
$.ajax({
url: "{% url 'p_get_table_stru' %}",
type: 'GET',
dataType: 'json',
data: {'schema': data},
timeout: 30000,
cache: true,
success: function (result) {
if (result.status === 0) {
let data = result.data;
$('#modal_table_stru').modal('show');
$('#table_stru').append(data.stru);
highlightSQL()
}
}
});
}
<!-- 查看表索引 -->
function showTableIndexModal(data) {
$.ajax({
url: "{% url 'p_get_table_index' %}",
type: 'GET',
dataType: 'json',
data: {'schema': data},
timeout: 30000,
cache: true,
success: function (result) {
if (result.status === 0) {
let data = result.data;
$('#modal_table_index').modal('show');
$table_index.bootstrapTable('destroy').bootstrapTable({
columns: data.index.columnDefinition,
data: data.index.data,
pageNumber: 1,
pageSize: 20,
pageList: [20, 50],
locale: 'zh-CN',
sidePagination: "client",
pagination: true,
singleSelect: true,
minimumCountColumns: 2,
matchBrackets: true,
lineWrapping: true,
classes: 'table table-hover',
iconSize: 'sm',
rowStyle: function rowStyle(row, index) {
return {
classes: 'text-nowrap another-class',
css: {"font-size": "12px"}
};
},
});
}
}
});
}
<!-- 查看表基本信息 -->
function showTableBaseModal(data) {
$('#table_base').empty();
$.ajax({
url: "{% url 'p_get_table_base' %}",
type: 'GET',
dataType: 'json',
data: {'schema': data},
timeout: 30000,
cache: true,
success: function (result) {
if (result.status === 0) {
let data = result.data;
$('#modal_table_base').modal('show');
$('#table_base').append(data)
}
}
});
}
<!-- 语法高亮 -->
function highlightSQL() {
{# 模态框加载后,执行,否则顺序不对,无法高亮SQL语法 #}
$('pre code').each(function (i, block) {
hljs.highlightBlock(block);
hljs.configure({
tabReplace: 4
})
});
}
// 获取查询日志
let protocol = 'ws://';
if (window.location.protocol === 'https:') {
protocol = 'wss://';
}
let socket = new WebSocket(protocol + window.location.host + '/ws/');
socket.onmessage = function (message) {
let result = JSON.parse(message.data);
let msg = result.msg;
let currentDate = new Date();
let mytime = currentDate.toLocaleString();
if (result.type === 1) {
let query_log_html = '';
if (msg instanceof Array) {
query_log_html = '时间:' + mytime + '\n' + msg.join('\n') + '\n';
} else {
query_log_html = '时间:' + mytime + '\n' + msg + '\n\n'
}
myQueryLog.replaceRange(query_log_html, CodeMirror.Pos(myQueryLog.lastLine()));
myQueryLog.setCursor(myQueryLog.lineCount(), 0);
}
};
/**
* 执行MySQL查询语句
*/
function commitSqlQueryForm() {
// 清空输出的结果
let csrftoken = $.cookie('csrftoken');
$('#li_append').empty();
$('#table_append').empty();
// 获取选中的内容,否则为全部内容
let contents = '';
if (myCodeMirror.somethingSelected()) {
contents = myCodeMirror.getSelection()
} else {
contents = myCodeMirror.getValue();
}
// 判断输入的内容是否为空
if (contents.length < 1) {
displayPNotify(2, '内容不能为空');
return false;
}
if (!query_schema) {
displayPNotify(2, '请点击选中左侧库或表,在执行查询');
return false;
}
$.ajax({
data: {
'contents': contents,
'schema': query_schema,
'csrfmiddlewaretoken': csrftoken
},
dataType: 'json',
url: '{% url 'p_exec_sql_query' %}',
method: 'POST',
timeout: 700000, // 700秒
beforeSubmit: showLoadingScreen($('body'), '数据查询中,请稍后...'),
success: function (result) {
hideLoadingScreen($('body'));
if (result.status === 0) {
let data = result.data;
document.getElementById('typediv1').style.visibility = "visible";
let li_html = '';
let table_html = '';
for (let i in data) {
li_html += "<li><a href=\"#tab_" + i + "\" data-toggle=\"tab\">" + "结果集" + i + "</a></li>";
table_html += "<div class=\"tab-pane\" id=\"tab_" + i + "\">\n" +
"<table id=\"table" + i + "\"></table>\n" +
"</div>"
}
$('#table_append').append(table_html);
$('#li_append').append(li_html);
$('.nav-tabs>li>a').first().trigger('click');
for (let key in data) {
let d = data[key];
let $table = $("#table" + key);
$table.bootstrapTable('destroy').bootstrapTable({
columns: d.columnDefinition,
data: d.data,
search: true,
showRefresh: true,
showExport: true,
showToggle: true,
showColumns: true,
minimumCountColumns: 2,
exportDataType: 'all',
exportTypes: ['csv', 'txt', 'excel', 'sql'],
pageNumber: 1,
pageSize: 20,
locale: 'zh-CN',
height: 800,
sidePagination: "client",
pagination: true,
singleSelect: true,
matchBrackets: true,
lineWrapping: true,
{#fixedColumns: true,#}
fixedNumber: +1,
rowspan: 5,
iconSize: 'sm',
rowStyle: function rowStyle(row, index) {
return {
classes: 'text-nowrap another-class',
css: {"font-size": "12px"}
};
},
classes: 'table table-hover'
});
}
}
else {
document.getElementById('typediv1').style.visibility = "hidden";
displayPNotify(result.status, result.msg)
}
},
error: function (jqXHR) {
if (jqXHR.status === 403) {
displayPNotify(jqXHR.status);
hideLoadingScreen();
}
}
})
}
$(function () {
// 保存用户输入
let user_input = localStorage.getItem("user_input_history");
let $textarea = $("textarea");
if (user_input) {
myCodeMirror.setValue(user_input);
}
$textarea.bind('input propertychange', function () {
let contents = myCodeMirror.getValue();
localStorage.setItem("user_input_history", contents);
});
$textarea.on('paste', function () {
let contents = myCodeMirror.getValue();
localStorage.setItem("user_input_history", contents);
});
$textarea.on('keyup', function (event) {
if (8 <= event.keyCode <= 46) {
let contents = myCodeMirror.getValue();
localStorage.setItem("user_input_history", contents);
}
});
$("button").on('click', function () {
let contents = myCodeMirror.getValue();
localStorage.setItem("user_input_history", contents);
});
});
// 设置主题
let input = document.getElementById("select");
function selectTheme() {
let theme = input.options[input.selectedIndex].textContent;
myCodeMirror.setOption("theme", theme);
location.hash = "#" + theme;
}
let choice = (location.hash && location.hash.slice(1)) ||
(document.location.search &&
decodeURIComponent(document.location.search.slice(1)));
if (choice) {
input.value = choice;
myCodeMirror.setOption("theme", choice);
}
CodeMirror.on(window, "hashchange", function () {
let theme = location.hash.slice(1);
if (theme) {
input.value = theme;
selectTheme();
}
});
// 获取当前用户执行的SQL记录
let hSTextarea = document.getElementById('preview_history_sql');
let hSCodeMirror = CodeMirror.fromTextArea(hSTextarea, {
mode: "text/x-mysql",
readOnly: true,
theme: 'material',
lineNumbers: true,
autoRefresh: true,
smartIndent: true,
indentWithTabs: true,
styleActiveLine: true,
autofocus: true,
keyMap: "sublime",
autoCloseBrackets: true,
matchBrackets: true,
lineWrapping: true,
});
function getHistorySql() {
$('#preview_history_sql').empty();
$.ajax({
url: '{% url 'p_get_history_sql' %}',
type: 'GET',
dataType: 'json',
timeout: 10000,
cache: false,
success: function (result) {
let data = result.data.join(';\n\n');
hSCodeMirror.setValue(data);
$('#modal_history_sql').modal('show')
}
})
}
function getFilterHistorySql() {
let contents = $('#search_contents').val();
let csrftoken = $.cookie('csrftoken');
$('#preview_history_sql').empty();
$.ajax({
url: '{% url 'p_get_history_sql' %}',
type: 'POST',
data: {'contents': contents, 'csrfmiddlewaretoken': csrftoken},
dataType: 'json',
timeout: 10000,
cache: false,
success: function (result) {
let data = result.data.join(';\n\n');
hSCodeMirror.setValue(data)
}
})
}
</script>
{% endblock %}