Skip to content

Commit 6c21b6c

Browse files
committed
feat: Implement artificial limit.
1 parent d5e7850 commit 6c21b6c

File tree

2 files changed

+80
-7
lines changed

2 files changed

+80
-7
lines changed

gui.js

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const dbFileElm = document.getElementById('dbfile');
66
const savedbElm = document.getElementById('savedb');
77
const querySection = document.getElementById('query');
88
const uploadSection = document.getElementById('dropzone');
9+
const promptArea = document.getElementById('prompt-area');
10+
const artificialLimit = 5000;
911

1012
// Start the worker in which sql.js will run
1113
const worker = new Worker("worker.sql-wasm.js");
@@ -31,24 +33,59 @@ function noerror() {
3133
errorElm.classList.remove('alert', 'alert-danger');
3234
}
3335

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+
3471
// Run a command in the database
3572
function execute(commands) {
3673
tic();
3774
worker.onmessage = function (event) {
3875
var results = event.data.results;
3976

77+
4078
toc("Executing SQL");
4179
if (!results) {
4280
error({message: event.data.error});
4381
return;
4482
}
4583

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);
5088
}
51-
toc("Displaying results");
5289
}
5390
worker.postMessage({ action: 'exec', sql: commands });
5491
outputElm.textContent = "Fetching results...";
@@ -61,7 +98,9 @@ var tableCreate = function () {
6198
var open = '<' + tagName + '>', close = '</' + tagName + '>';
6299
return open + vals.join(close + open) + close;
63100
}
64-
return function (columns, values) {
101+
return function (columns, values, maxRows) {
102+
maxRows = Math.min(maxRows, values.length);
103+
values = values.slice(0, maxRows);
65104
var tbl = document.createElement('table');
66105
tbl.classList.add('table', 'table-hover')
67106
var html = '<thead>' + valconcat(columns, 'th') + '</thead>';

index.html

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,39 @@
9393
#results {
9494
margin-bottom: 3rem;
9595
}
96+
97+
#prompt-area {
98+
display: flex;
99+
width: 50%;
100+
margin-left: auto;
101+
margin-right: auto;
102+
display: flex;
103+
104+
}
105+
106+
.group-question {
107+
padding: 0.5em;
108+
border: 1px solid;
109+
justify-content: center;
110+
justify-items: center;
111+
flex-direction: column;
112+
width: 100%;
113+
display: flex;
114+
}
115+
116+
.grouped-buttons {
117+
display: flex;
118+
justify-items: center;
119+
align-items: center;
120+
justify-content: center;
121+
}
122+
123+
.group-question button,
124+
.group-question > p {
125+
margin: 0.5em;
126+
align-self: center;
127+
display: flex;
128+
}
96129
</style>
97130
<link rel="stylesheet" href="dropzone.min.css">
98131
</head>
@@ -113,7 +146,7 @@ <h1>הריצו את ה־SQL שלכם</h1>
113146
<br>
114147

115148
<div id="code-section">
116-
<textarea id="commands">SELECT * FROM movies;</textarea>
149+
<textarea id="commands"></textarea>
117150
</div>
118151

119152

@@ -125,6 +158,7 @@ <h1>הריצו את ה־SQL שלכם</h1>
125158

126159
<div id="results">
127160
<div id="error" class="error" role="alert"></div>
161+
<div id="prompt-area"></div>
128162
<pre id="output"></pre>
129163
</div>
130164
</main>

0 commit comments

Comments
 (0)