Skip to content

Commit ddae56f

Browse files
authored
Merge pull request #4 from CNAG-Biomedical-Informatics/codex/add-query-builder-form-with-dropdown
Add query builder
2 parents e1b600e + 589db65 commit ddae56f

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
`sql.js-httpvfs` is a fork of and wrapper around sql.js to provide a read-only HTTP-Range-request based virtual file system for SQLite. It allows hosting an SQLite database on a static file hoster and querying that database from the browser without fully downloading it.
44

5-
Provide the URL of any SQLite database file and edit the default SQL Query.
5+
Provide the URL of any SQLite database file and edit the default SQL query.
6+
You can also use the query builder form to choose a column and value to quickly
7+
generate a filter.
68

79
[Read more](https://github.com/phiresky/sql.js-httpvfs)

src/App.svelte

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
{ value: "16384", name: "16384" },
3232
{ value: "32768", name: "32768" },
3333
];
34-
let sqlQuery = `SELECT * FROM tcga_table LIMIT 20';`;
34+
let sqlQuery = `SELECT * FROM tcga_table LIMIT 20;`;
35+
let activeTable = "";
36+
let selectedColumn = "";
37+
let searchValue = "";
3538
// let dbUrl = "https://nishad.github.io/sql.js-httpvfs-playground/db/imdb-titles-100000_1024_indexed.db";
3639
// let dbUrl =
3740
// "https://cnag-biomedical-informatics.github.io/sql.js-httpvfs-playground/db/tcga.db";
@@ -114,12 +117,15 @@
114117
);
115118
if (tablesData.result.length) {
116119
const table = tablesData.result[0].name;
120+
activeTable = table;
117121
console.log("Selected table: ", table);
118122
119123
// Override if this table has a special filter
120124
if (Object.keys(exampleQueries).includes(table)) {
121125
console.log("Using example query for table:", table);
122126
sqlQuery = `SELECT * FROM "${table}" WHERE ${exampleQueries[table]};`;
127+
} else {
128+
sqlQuery = `SELECT * FROM "${table}" LIMIT 20;`;
123129
}
124130
125131
await runQuery(dbUrl, sqlQuery);
@@ -167,6 +173,13 @@
167173
let totalBytes;
168174
let totalRequests;
169175
176+
function runBuilderQuery() {
177+
if (!activeTable || !selectedColumn || !searchValue) return;
178+
const escaped = searchValue.replace(/'/g, "''");
179+
sqlQuery = `SELECT * FROM "${activeTable}" WHERE "${selectedColumn}" LIKE '%${escaped}%' LIMIT 20;`;
180+
runQuery(dbUrl, sqlQuery);
181+
}
182+
170183
async function runQuery(url = dbUrl, query = sqlQuery) {
171184
result = null;
172185
querying = true;
@@ -252,20 +265,41 @@
252265
<Select class="mt-2" items={pageSizes} bind:value={pageSize} />
253266
</Label>
254267
</div> -->
255-
<div class="p-6">
256-
<Label class="space-y-2">
257-
<span>Edit SQL Query</span>
258-
<CodeJar bind:value={sqlQuery} syntax="sql" {highlight} />
259-
</Label>
260-
</div>
261-
<div class="p-6">
262-
<Button on:click={() => runQuery()}>
263-
{#if querying}
264-
<Spinner class="mr-3" size="4" color="white" /> Querying ...
265-
{:else}
266-
Run Query
267-
{/if}
268-
</Button>
268+
<div class="p-6 space-y-4">
269+
<div class="space-y-2">
270+
<Label class="space-y-2">
271+
<span>Column</span>
272+
<input list="columns" class="border rounded w-full p-2" bind:value={selectedColumn} />
273+
<datalist id="columns">
274+
{#each ptInstructs as col}
275+
<option value={col.key} />
276+
{/each}
277+
</datalist>
278+
</Label>
279+
<Label class="space-y-2">
280+
<span>Search value</span>
281+
<Input type="text" bind:value={searchValue} />
282+
</Label>
283+
<Button size="sm" on:click={runBuilderQuery}>Search</Button>
284+
</div>
285+
<details>
286+
<summary class="cursor-pointer underline">Expand to show and modify the SQL query</summary>
287+
<div class="mt-4">
288+
<Label class="space-y-2">
289+
<span>Edit SQL Query</span>
290+
<CodeJar bind:value={sqlQuery} syntax="sql" {highlight} />
291+
</Label>
292+
<div class="mt-2">
293+
<Button on:click={() => runQuery()}>
294+
{#if querying}
295+
<Spinner class="mr-3" size="4" color="white" /> Querying ...
296+
{:else}
297+
Run Query
298+
{/if}
299+
</Button>
300+
</div>
301+
</div>
302+
</details>
269303
</div>
270304
271305
{#if result}

0 commit comments

Comments
 (0)