diff --git a/src/index.js b/src/index.js index 5eb73c3f9..70b09509e 100644 --- a/src/index.js +++ b/src/index.js @@ -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; \ No newline at end of file +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}`); + } +} \ No newline at end of file diff --git a/src/queryParser.js b/src/queryParser.js index a7a27174d..195388dba 100644 --- a/src/queryParser.js +++ b/src/queryParser.js @@ -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; \ No newline at end of file +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'); + }); +} \ No newline at end of file diff --git a/tests/step-07/index.test.js b/tests/step-07/index.test.js index ee0ebed5e..e8b1868d2 100644 --- a/tests/step-07/index.test.js +++ b/tests/step-07/index.test.js @@ -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'); -}); \ No newline at end of file +});