Skip to content

Commit

Permalink
Merge pull request #1 from status-20X/main
Browse files Browse the repository at this point in the history
Feedback
  • Loading branch information
prkagrawal authored May 30, 2024
2 parents 2873e75 + 832251d commit 259e9de
Show file tree
Hide file tree
Showing 30 changed files with 2,770 additions and 609 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[![Open in Visual Studio Code](https://classroom.github.com/assets/open-in-vscode-718a45dd9cf7e7f842a935f5ebbe5719a5e09af4491e668f4dbf3b35d5cca122.svg)](https://classroom.github.com/online_ide?assignment_repo_id=14792961&assignment_repo_type=AssignmentRepo)
<h1 align="center">StylusDB SQL</h1>
<p align="center">
A SQL database engine written in JavaScript
Expand Down
7 changes: 7 additions & 0 deletions enrollment.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
student_id,course
1,Mathematics
1,Physics
2,Chemistry
3,Mathematics
5,Biology
5,Physics
4 changes: 3 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "stylusdb-sql",
"version": "0.1.6",
"name": "stylusdb-sql-prkagrawal",
"version": "0.2.0",
"description": "A minimal SQL based DB based on CSV files. For educational purposes only.",
"main": "./src/index.js",
"directories": {
"doc": "docs"
"doc": "docs",
"test": "tests"
},
"scripts": {
"test": "jest",
Expand Down Expand Up @@ -38,11 +39,11 @@
"author": "Chakshu Gautam",
"license": "ISC",
"devDependencies": {
"csv-parser": "^3.0.0",
"jest": "^29.7.0"
},
"dependencies": {
"csv-parser": "^3.0.0",
"json2csv": "^6.0.0-alpha.2",
"xterm": "^5.3.0"
}
}
}
52 changes: 52 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env node

const readline = require('readline');
const { executeSELECTQuery, executeINSERTQuery, executeDELETEQuery } = require('./queryExecutor');

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 {
// Execute the query - do your own implementation

switch (true) {
case line.toLowerCase().startsWith('select'):
executeSELECTQuery(line)
.then(result => console.log('Result:', result))
.catch(err => console.error(err));
break;
case line.toLowerCase().startsWith('insert into'):
executeINSERTQuery(line)
.then(result => console.log(result.message))
.catch(err => console.error(err));
break;
case line.toLowerCase().startsWith('delete from'):
executeDELETEQuery(line)
.then(result => console.log(result.message))
.catch(err => console.error(err));
break;
default:
console.log('Unsupported command');
}
} catch (error) {
console.error('Error:', error.message);
}

rl.prompt();
}).on('close', () => {
console.log('Exiting SQL CLI');
process.exit(0);
});
26 changes: 26 additions & 0 deletions src/csvReader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const fs = require('fs');
const csv = require('csv-parser');
const { parse } = require('json2csv');

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

return new Promise((resolve, reject) => {
fs.createReadStream(filePath)
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => {
resolve(results);
})
.on('error', (error) => {
reject(error);
});
});
}

async function writeCSV(filename, data) {
const csv = parse(data);
fs.writeFileSync(filename, csv);
}

module.exports = {readCSV,writeCSV};
Loading

0 comments on commit 259e9de

Please sign in to comment.