Skip to content

Commit

Permalink
step5
Browse files Browse the repository at this point in the history
  • Loading branch information
kesharibhai84 committed Apr 10, 2024
1 parent 750fb85 commit 5d110ae
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
20 changes: 14 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@ const parseQuery = require('./queryParser');
const readCSV = require('./csvReader');

async function executeSELECTQuery(query) {
const { fields, table } = parseQuery(query);
const { fields, table, whereClause } = parseQuery(query);
const data = await readCSV(`${table}.csv`);

// Filter the fields based on the query
return data.map(row => {
const filteredRow = {};
// Filtering based on WHERE clause
const filteredData = whereClause
? data.filter(row => {
const [field, value] = whereClause.split('=').map(s => s.trim());
return row[field] === value;
})
: data;

// Selecting the specified fields
return filteredData.map(row => {
const selectedRow = {};
fields.forEach(field => {
filteredRow[field] = row[field];
selectedRow[field] = row[field];
});
return filteredRow;
return selectedRow;
});
}

Expand Down
7 changes: 4 additions & 3 deletions src/queryParser.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// src/queryParser.js

function parseQuery(query) {
const selectRegex = /SELECT (.+) FROM (.+)/i;
const selectRegex = /SELECT (.+?) FROM (.+?)(?: WHERE (.*))?$/i;
const match = query.match(selectRegex);

if (match) {
const [, fields, table] = match;
const [, fields, table, whereClause] = match;
return {
fields: fields.split(',').map(field => field.trim()),
table: table.trim()
table: table.trim(),
whereClause: whereClause ? whereClause.trim() : null
};
} else {
throw new Error('Invalid query format');
Expand Down

0 comments on commit 5d110ae

Please sign in to comment.