Skip to content

Commit

Permalink
feat: steps 2 and 3
Browse files Browse the repository at this point in the history
  • Loading branch information
PayalKumari10 committed Apr 23, 2024
1 parent 8ccd37d commit 157da93
Show file tree
Hide file tree
Showing 31 changed files with 2,126 additions and 1,199 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"githubPullRequests.ignoredPullRequestBranches": [
"main"
]
}
6 changes: 6 additions & 0 deletions enrollment.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
student_id,course
1,Mathematics
1,Physics
2,Chemistry
3,Mathematics
5,Biology
2 changes: 1 addition & 1 deletion sample.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
id,name,age
1,John,30
2,Jane,25
3,Bob,22
3,Bob,22
46 changes: 46 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env node

const readline = require("readline");
const { executeSELECTQuery, executeINSERTQuery,executeDELETEQuery,
} = require("./index");

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

rl.setPrompt("SQL> ");
console.log(
'SQL Query Engine CLI. Enter your SQL commands, or type "exit" to quit.'
);

rl.prompt();

rl.on("line", async (line) => {
if (line.toLowerCase() === "exit") {
rl.close();
return;
}

try {
if (line.toLowerCase().startsWith("select")) {
const result = await executeSELECTQuery(line);
console.log("Result:", result);
} else if (line.toLowerCase().startsWith("insert into")) {
const result = await executeINSERTQuery(line);
console.log(result.message);
} else if (line.toLowerCase().startsWith("delete from")) {
const result = await executeDELETEQuery(line);
console.log(result.message);
} else {
console.log("Unsupported command");
}
} catch (error) {
console.error("Error:", error.message);
}

rl.prompt();
}).on("close", () => {
console.log("Exiting SQL CLI");
process.exit(0);
});
25 changes: 19 additions & 6 deletions src/csvReader.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@

const fs = require("fs");
const csv = require("csv-parser");
const { parse } = require("json2csv");

function readCSV(filePath) {
const results = [];

async function readCSV(filePath) {
return new Promise((resolve, reject) => {
const data = [];
fs.createReadStream(filePath)
.pipe(csv())
.on('data', (data) => results.push(data))
.on('data', (row) => {
data.push(row);
})
.on('end', () => {
resolve(results);
resolve(data);
})
.on('error', (error) => {
reject(error);
});
});
}
async function writeCSV(filename, data) {
try {
const csvData = parse(data);
fs.writeFileSync(filename, csvData);
} catch (error) {
throw error;
}
}
module.exports = { readCSV, writeCSV };



module.exports = readCSV;
Loading

0 comments on commit 157da93

Please sign in to comment.