-
Notifications
You must be signed in to change notification settings - Fork 0
/
display-results.js
298 lines (255 loc) · 8.21 KB
/
display-results.js
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
var RES_TEXT_LEN = 3 + 2;
var PAGE_REST = false;
var RESULT_SET = null;
var INFO = null;
var resultsTimeoutId = null;
var IDX = 0;
function txtLine(){
var result = client_settings.extra_info ? '+---+' : '+';
for (var i in RESULT_SET.header){
for (var j = 0; j < RESULT_SET.header[i].length; j++){
result += '-';
}
result += '+';
}
return result;
}
function txtValues(row, idx){
var result = '|';
if (client_settings.extra_info){
var info_txt = idx != -1 ? 'i' + idx : ' ';
result = '| ' + info_txt + ' |';
}
var len;
for (var i in RESULT_SET.header){
var value = row[i];
if (row[i] == null)
{
value = '<NULL>';
}
len = value.length;
len = len > client_settings.max_field_length - RES_TEXT_LEN - 2 ? client_settings.max_field_length - RES_TEXT_LEN - 2 : len;
text = ' ' + value.substr(0, len) + ' ';
for (var j = len + 2; j < RESULT_SET.header[i].length; j++){
text += ' ';
}
text += '|';
result += text;
}
return result;
}
function displayTxtHeader(){
var row = new Array();
var txt = '';
for (var i = 0; i < RESULT_SET.header.length; i++){
row.push(RESULT_SET.header[i].name);
}
var pref = txtLine() + '\n';
if (((client_settings.page_size && !client_settings.pagination) || PAGE_REST) && IDX > 0){
pref = '';
}
return pref + txtValues(row, -1, false) + '\n' + txtLine();
}
function displayTxtRows(){
var result = '';
var start = IDX;
var end = IDX + (client_settings.page_size > 0 ? client_settings.page_size : RESULT_SET.rows.length);
for (var i = start; i < end && i < RESULT_SET.rows.length; i++){
result += txtValues(RESULT_SET.rows[i], i) + '\n';
}
IDX = end;
result += txtLine();
return result;
}
function displayTxt(){
TERM.disable();
for (var i in RESULT_SET.header){
if (RESULT_SET.header[i].length > client_settings.max_field_length - RES_TEXT_LEN){
RESULT_SET.header[i].length = client_settings.max_field_length - RES_TEXT_LEN;
}
}
var result = '';
result = displayTxtHeader(RESULT_SET.header) + '\n';
result += displayTxtRows();
TERM.echo(escape_brackets(result));
if (client_settings.extra_info){
var html = $('.terminal-output').children().last().html();
html = html.replace(/<br>\| i([\d]+) \|/g, '<br>| <a href="#" onclick="rowDialog($1); return false;">i</a> |');
$('.terminal-output').children().last().html(html);
}
displayFooter();
$('html,body').animate({scrollTop: $('.prompt').offset().top + 10, scrollLeft: $('.prompt').offset().left - 10}, 'fast');
}
function rowDialog(idx){
var content = '<table class="table">';
for (var i in RESULT_SET.header){
content += '<tr>';
content += '<td class="left-col' + (i == 0 ? ' no-border' : '') + '">' + RESULT_SET.header[i].name + '</td>';
content += '<td class="right-col' + (i == 0 ? ' no-border' : '') + '"><pre>' + RESULT_SET.rows[idx][i].replace(/</g, '<') + '</pre></td>';
content += '</tr>';
}
content += '</table>';
//content = '<div style="overflow: auto">' + content + '</div>';
$('#dialog').html(content).dialog('open');
}
function exportHTML(){
if (typeof (RESULT_SET.rows) != 'undefined'){
var html = '<table border="1">';
html += '<tr>'
for (var i in RESULT_SET.header){
html += '<td style="padding: 5px;">' + RESULT_SET.header[i].name + '</td>';
}
html += '</tr>';
for (var i in RESULT_SET.rows){
html += '<tr>';
for (var j in RESULT_SET.rows[i]){
html += '<td>' + RESULT_SET.rows[i][j] + '</td>';
}
html += '</tr>';
}
html += '</table>';
var p = window.open('', 'resizable=1,scrollbars=1,menubar=1,toolbar=1,titlebar=1,hotkeys=1,status=1,dependent=0,location=1');
p.document.write('<html><head><title>Exported Rows</title>');
p.document.write(html);
p.document.write('</body></html>');
p.document.close();
}
}
function exportTxt(){
if (typeof(RESULT_SET.rows) != 'undefined'){
var txt = '';
for (var i in RESULT_SET.header){
txt += RESULT_SET.header[i].name + '\t';
}
for (var i in RESULT_SET.rows){
txt += '\n';
for (var j in RESULT_SET.rows[i]){
txt += RESULT_SET.rows[i][j].replace(/\n/g, '\\n').replace(/\r/g, '').replace(/\t/g, '\\t') + '\t';
}
}
var p = window.open('sd', 'name');
p.document.write('<html><head><title>Exported Rows</title>');
p.document.write("<pre>" + txt + "</pre>");
p.document.write('</body></html>');
p.document.close();
}
}
function exportSQLTable(table){
var col_idx = [];
var fields = '';
for (var i in RESULT_SET.header){
if (RESULT_SET.header[i].table == table){
fields += (fields == '' ? '' : ', ') + '`' + RESULT_SET.header[i].orgname + '`';
col_idx.push(i);
}
}
var base_sql = 'replace into `' + table + '`(' + fields + ')';
var txt = base_sql;
var values = '', _values;
var collection = [];
for (var i in RESULT_SET.rows){
_values = '';
for (var j in col_idx){
_values += (_values == '' ? '' : ', ') + "'" + RESULT_SET.rows[i][col_idx[j]].replace(/'/g, '\\\'') + "'";
}
if (collection.indexOf(_values) == -1){
collection.push(_values);
values += (values == '' ? '' : ',\n') + '(' + _values + ')';
}
if (values.length > 5000){
txt += ' values' + values + '; \n' + base_sql;
values = '';
}
}
if (values != ''){
txt += ' values' + values;
}
return txt;
}
function getTablesFromResultSet(){
var tables = [];
if (typeof(RESULT_SET.header) != 'undefined'){
for (var i in RESULT_SET.header){
if (tables.indexOf(RESULT_SET.header[i].table) == -1){
tables.push(RESULT_SET.header[i].table);
}
}
}
return tables;
}
function exportSQL(){
if (typeof(RESULT_SET.rows) != 'undefined'){
var txt = '';
var tables = getTablesFromResultSet();
for (var i in tables){
txt += exportSQLTable(tables[i]) + ';\n';
}
var p = window.open('sd', 'name');
p.document.write('<html><head><title>Exported Rows</title>');
p.document.write("<pre>" + txt + "</pre>");
p.document.write('</body></html>');
p.document.close();
}
}
function displayFooter(){
if (IDX < RESULT_SET.rows.length && (!client_settings.pagination || PAGE_REST)){
resultsTimeoutId = setTimeout('displayTxt()', 10);
}
else {
PAGE_REST = false;
resultsTimeoutId = null;
if (IDX < RESULT_SET.rows.length){
$('.terminal-output').append('<a href="#" onclick="getNextPage(); return false;">Next page</a>');
$('.terminal-output').append(' ');
$('.terminal-output').append('<a href="#" onclick="getRestPages(); return false;">Get all results</a>');
$('.terminal-output').append(' ');
}
$('.terminal-output').append('<a href="#" onclick="exportHTML(); return false;">Export HTML</a>');
$('.terminal-output').append(' ');
$('.terminal-output').append('<a href="#" onclick="exportTxt(); return false;">Export TXT</a>');
$('.terminal-output').append(' ');
$('.terminal-output').append('<a href="#" onclick="exportSQL(); return false;">Export SQL</a>');
displayInfo();
TERM.enable();
}
}
function displayInfo(){
var msg = 'Execution time: ' + INFO.time + ' seconds. ' + (RESULT_SET.length == 0 ? 'Affected rows: ' : 'Total rows: ') + INFO.affected_rows + '. \n' + INFO.text;
TERM.echo('[[;#0f0;]' + escape_brackets(msg) + ']');
$('html,body').animate({scrollTop: $('.prompt').offset().top + 10, scrollLeft: $('.prompt').offset().left - 10}, 'fast');
}
function getNextPage(){
removeAllLinks();
if (client_settings.results_format == 'txt'){
displayTxt();
}
}
function getRestPages(){
removeAllLinks();
PAGE_REST = true;
if (client_settings.results_format == 'txt'){
TERM.echo(txtLine() + '\n');
displayTxt();
}
}
function removeAllLinks(){
//$('.terminal-output').find('a').remove();
$('.terminal-output').find('a').each(function(idx){
//console.log($(this).text());
$(this).replaceWith($(this).text());
});
}
function displayResult(table, info){
RESULT_SET = table;
INFO = info;
IDX = 0;
removeAllLinks();
if (table.rows.length > 0){
if (client_settings.results_format == 'txt'){
displayTxt()
}
}
else {
displayInfo();
}
}