Skip to content

Commit

Permalink
Fix use of sql on loaded files
Browse files Browse the repository at this point in the history
Don't create input.txt for sql queries if we're not using it.
Let all languages use any file loaded
  • Loading branch information
mwenge committed Apr 6, 2024
1 parent d567703 commit 05fd269
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,32 +279,36 @@ async function evaluateSQL() {
let input = await runningPipe.input();
let buffInput = enc.encode(input);

// Drop the table if it already exists.
let tableName = "input.txt";
await sql.asyncRunSQL("DROP TABLE \"" + tableName + "\";");

// Write the standard input to a TSV table first.
updateProgress("Loading input as a table..");
await localforage.setItem(tableName, enc.encode(buffInput).buffer);
let res = await sql.asyncCreateTable(buffInput, tableName);
if (res.error) {
await runningPipe.updateOutput("Error creating input.txt table: " + res.error);
throw new Error("=> Error occurred while running SQL.");
let program = preprocessedProgram(runningPipe);
if (program.includes("input.txt")) {
// Drop the table if it already exists.
let tableName = "input.txt";
await sql.asyncRunSQL("DROP TABLE \"" + tableName + "\";");

// Write the standard input to a TSV table first.
updateProgress("Loading input as a table..");
await localforage.setItem(tableName, enc.encode(buffInput).buffer);
let res = await sql.asyncCreateTable(buffInput, tableName);
if (res.error) {
await runningPipe.updateOutput("Error creating input.txt table: " + res.error);
throw new Error("=> Error occurred while running SQL.");
}
}

// Load the files associated with the pipe to tables.
let files = runningPipe.files();
let files = await ps.pipeline.getFilesForCurrentPipe();
console.log("files",files);
await Promise.all(files.map(async (f) => {
await sql.asyncRunSQL("DROP TABLE \"" + f + "\";");
}));
await Promise.all(files.map(async (f) => {
updateProgress("Loading " + f + " as a table..");
console.log("Loading " + f + " as a table..");
let data = await localforage.getItem(f);
await sql.asyncCreateTable(new Uint8Array(data), f);
}));

// Now run the query against it.
let program = preprocessedProgram(runningPipe);
updateProgress("Running Query..");
const { results, error } = await sql.asyncRunSQL(program);

Expand Down Expand Up @@ -340,7 +344,7 @@ async function evaluateR() {
let program = preprocessedProgram(runningPipe);

// Get any files and add the input to 'input.txt'.
let files = runningPipe.files();
let files = await ps.pipeline.getFilesForCurrentPipe();
await localforage.setItem("input.txt", enc.encode(input).buffer);

const { results, error, output } = await R.asyncRunR(program, input, files);
Expand Down Expand Up @@ -407,7 +411,7 @@ async function evaluatePython() {
async function evaluateJS() {
let input = await runningPipe.input();
let program = preprocessedProgram(runningPipe);
let files = runningPipe.files();
let files = await ps.pipeline.getFilesForCurrentPipe();
const { results, error, output } = await asyncRunJS(input, program);
let stdout = '';
if (error) {
Expand All @@ -430,7 +434,7 @@ async function evaluateJS() {
async function evaluateLua() {
let input = await runningPipe.input();
let program = preprocessedProgram(runningPipe);
let files = runningPipe.files();
let files = await ps.pipeline.getFilesForCurrentPipe();
const { results, error, output } = await asyncRunLua(input, program);
let stdout = '';
if (error) {
Expand All @@ -453,7 +457,7 @@ async function evaluateLua() {
async function evaluateAwk() {
let input = await runningPipe.input();
let program = preprocessedProgram(runningPipe);
let files = runningPipe.files();
let files = await ps.pipeline.getFilesForCurrentPipe();
const { results, error, output } = await asyncRunAwk(input, program);
let stdout = '';
if (error) {
Expand All @@ -476,7 +480,7 @@ async function evaluateAwk() {
async function evaluateLisp() {
let input = await runningPipe.input();
let program = preprocessedProgram(runningPipe);
let files = runningPipe.files();
let files = await ps.pipeline.getFilesForCurrentPipe();
const { results, error, output } = await asyncRunLisp(input, program);
let stdout = '';
if (error) {
Expand Down

0 comments on commit 05fd269

Please sign in to comment.