@@ -6,6 +6,8 @@ const dbFileElm = document.getElementById('dbfile');
6
6
const savedbElm = document . getElementById ( 'savedb' ) ;
7
7
const querySection = document . getElementById ( 'query' ) ;
8
8
const uploadSection = document . getElementById ( 'dropzone' ) ;
9
+ const promptArea = document . getElementById ( 'prompt-area' ) ;
10
+ const artificialLimit = 5000 ;
9
11
10
12
// Start the worker in which sql.js will run
11
13
const worker = new Worker ( "worker.sql-wasm.js" ) ;
@@ -31,24 +33,59 @@ function noerror() {
31
33
errorElm . classList . remove ( 'alert' , 'alert-danger' ) ;
32
34
}
33
35
36
+ function showResults ( results , maxRows ) {
37
+ tic ( ) ;
38
+ outputElm . innerHTML = "" ;
39
+ for ( let i = 0 ; i < results . length ; i ++ ) {
40
+ let table = tableCreate ( results [ i ] . columns , results [ i ] . values , maxRows ) ;
41
+ outputElm . appendChild ( table ) ;
42
+ }
43
+ toc ( "Displaying results" ) ;
44
+ }
45
+
46
+ function askUserIfToShowEverything ( results ) {
47
+ promptArea . classList . remove ( 'visually-hidden' ) ;
48
+ promptArea . innerHTML = `
49
+ <div class="group-question">
50
+ <p>השאילתה מחזירה הרבה תוצאות. להציג את כולן?</p>
51
+ <div class="grouped-buttons">
52
+ <button class="btn btn-success">לא</button>
53
+ <button data-get-all="true" class="btn btn-danger">כן</button>
54
+ </div>
55
+ </div>
56
+ ` ;
57
+ const buttons = Array . from ( promptArea . querySelectorAll ( 'button' ) )
58
+ buttons . forEach ( ( b ) => {
59
+ b . addEventListener ( 'click' , ( ) => {
60
+ const getAll = ( b . dataset . getAll !== undefined ) ;
61
+ const maxToShow = getAll ? Infinity : artificialLimit ;
62
+ if ( getAll ) {
63
+ promptArea . innerHTML = "<p>זה יקח קצת זמן...</p>" ;
64
+ }
65
+ showResults ( results , maxToShow ) ;
66
+ promptArea . classList . add ( 'visually-hidden' ) ;
67
+ } ) ;
68
+ } ) ;
69
+ }
70
+
34
71
// Run a command in the database
35
72
function execute ( commands ) {
36
73
tic ( ) ;
37
74
worker . onmessage = function ( event ) {
38
75
var results = event . data . results ;
39
76
77
+
40
78
toc ( "Executing SQL" ) ;
41
79
if ( ! results ) {
42
80
error ( { message : event . data . error } ) ;
43
81
return ;
44
82
}
45
83
46
- tic ( ) ;
47
- outputElm . innerHTML = "" ;
48
- for ( var i = 0 ; i < results . length ; i ++ ) {
49
- outputElm . appendChild ( tableCreate ( results [ i ] . columns , results [ i ] . values ) ) ;
84
+ if ( results [ 0 ] . values . length > artificialLimit ) {
85
+ askUserIfToShowEverything ( results ) ;
86
+ } else {
87
+ showResults ( results , Infinity ) ;
50
88
}
51
- toc ( "Displaying results" ) ;
52
89
}
53
90
worker . postMessage ( { action : 'exec' , sql : commands } ) ;
54
91
outputElm . textContent = "Fetching results..." ;
@@ -61,7 +98,9 @@ var tableCreate = function () {
61
98
var open = '<' + tagName + '>' , close = '</' + tagName + '>' ;
62
99
return open + vals . join ( close + open ) + close ;
63
100
}
64
- return function ( columns , values ) {
101
+ return function ( columns , values , maxRows ) {
102
+ maxRows = Math . min ( maxRows , values . length ) ;
103
+ values = values . slice ( 0 , maxRows ) ;
65
104
var tbl = document . createElement ( 'table' ) ;
66
105
tbl . classList . add ( 'table' , 'table-hover' )
67
106
var html = '<thead>' + valconcat ( columns , 'th' ) + '</thead>' ;
0 commit comments