Skip to content

Commit

Permalink
feat step 6
Browse files Browse the repository at this point in the history
  • Loading branch information
prakritigarg18 committed May 10, 2024
1 parent 352a8da commit 900decf
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
19 changes: 10 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ const parseQuery = require('./queryParser');
const readCSV = require('./csvReader');

async function executeSELECTQuery(query) {
const { fields, table, whereClause } = parseQuery(query);
const { fields, table, whereClauses } = 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;
// Apply WHERE clause filtering
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;
}))
: data;

// Selecting the specified fields
return filteredData.map(row => {
Expand All @@ -23,4 +23,5 @@ async function executeSELECTQuery(query) {
});
}

module.exports = executeSELECTQuery;
module.exports = executeSELECTQuery;

32 changes: 29 additions & 3 deletions src/queryParser.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@


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

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

module.exports = parseQuery;
function parseWhereClause(whereString) {
if (typeof whereString !== 'string') {
throw new Error('Input must be a string.');
}

const conditions = whereString.split(/ AND | OR /i);

return conditions.map(condition => {
const parts = condition.split(/\s+/);
if (parts.length !== 3) {
throw new Error('Invalid condition format.');
}

const [field, operator, value] = parts;
if (!field || !operator || !value) {
throw new Error('Invalid condition format.');
}

return { field, operator, value };
});
}


module.exports = parseQuery;
15 changes: 15 additions & 0 deletions tests/step-06/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ test('Execute SQL Query', async () => {
expect(result[0]).toEqual({ id: '1', name: 'John' });
});

describe('Parse SQL WHERE Clause', () => {
test('Parse SQL Query with WHERE Clause', () => {
const query = 'SELECT id, name FROM sample WHERE age = 25';
const parsed = parseQuery(query);
Expand All @@ -42,6 +43,20 @@ test('Parse SQL Query with WHERE Clause', () => {
value: "25",
}],
});
});
test('Invalid WHERE Clause (Missing Value)', () => {
const query = 'SELECT id, name FROM sample WHERE age =';
expect(() => parseQuery(query)).toThrow('Invalid condition format.');
});

test('Invalid WHERE Clause (Invalid Format)', () => {
const query = 'SELECT id, name FROM sample WHERE age = 25 AND';


expect(() => parseQuery(query)).toThrow('Invalid condition format.');
});


});

test('Execute SQL Query with WHERE Clause', async () => {
Expand Down

0 comments on commit 900decf

Please sign in to comment.