Skip to content

Commit

Permalink
step6
Browse files Browse the repository at this point in the history
  • Loading branch information
kesharibhai84 committed Apr 11, 2024
1 parent 5d110ae commit 03d9e48
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 46 deletions.
39 changes: 12 additions & 27 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
// src/index.js

const parseQuery = require('./queryParser');
const readCSV = require('./csvReader');

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

// 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 => {
selectedRow[field] = row[field];
});
return selectedRow;
});
}

module.exports = executeSELECTQuery;
function evaluateCondition(row, clause) {
const { field, operator, value } = clause;
switch (operator) {
case '=': return row[field] === value;
case '!=': return row[field] !== value;
case '>': return row[field] > value;
case '<': return row[field] < value;
case '>=': return row[field] >= value;
case '<=': return row[field] <= value;
default: throw new Error(`Unsupported operator: ${operator}`);
}
}
29 changes: 11 additions & 18 deletions src/queryParser.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
// src/queryParser.js

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

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

module.exports = parseQuery;
function parseWhereClause(whereString) {
const conditionRegex = /(.*?)(=|!=|>|<|>=|<=)(.*)/;
return whereString.split(/ AND | OR /i).map(conditionString => {
const match = conditionString.match(conditionRegex);
if (match) {
const [, field, operator, value] = match;
return { field: field.trim(), operator, value: value.trim() };
}
throw new Error('Invalid WHERE clause format');
});
}
2 changes: 1 addition & 1 deletion tests/step-07/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ test('Execute SQL Query with Not Equal to', async () => {
const result = await executeSELECTQuery(queryWithGT);
expect(result.length).toEqual(2);
expect(result[0]).toHaveProperty('name');
});
});

0 comments on commit 03d9e48

Please sign in to comment.