Skip to content

Commit

Permalink
step 7 passed
Browse files Browse the repository at this point in the history
  • Loading branch information
Sudarshan-21 committed May 15, 2024
1 parent 11c5d9f commit b1bce52
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
25 changes: 21 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@ const fs = require("fs");
const parseQuery = require("./queryParser");
const readCSV = require("./csvReader");

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}`);
}
}

async function executeSELECTQuery(query) {
if (!query.trim().toUpperCase().startsWith("SELECT")) {
throw new Error("Only SELECT queries are supported");
Expand All @@ -22,10 +42,7 @@ async function executeSELECTQuery(query) {
const filteredData =
whereClauses.length > 0
? data.filter((row) =>
whereClauses.every((clause) => {
// You can expand this to handle different operators
return row[clause.field] === clause.value;
})
whereClauses.every((clause) => evaluateCondition(row, clause))
)
: data;

Expand Down
12 changes: 8 additions & 4 deletions src/queryParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ function parseQuery(query) {
}

function parseWhereClause(whereString) {
const conditions = whereString.split(/ AND | OR /i);
return conditions.map((condition) => {
const [field, operator, value] = condition.split(/\s+/);
return { field, operator, value };
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");
});
}

Expand Down

0 comments on commit b1bce52

Please sign in to comment.