diff --git a/.github/.keep b/.github/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/.github/workflows/classroom.yml b/.github/workflows/classroom.yml index dca83b024..8c4fa1b7e 100644 --- a/.github/workflows/classroom.yml +++ b/.github/workflows/classroom.yml @@ -1,19 +1,220 @@ -name: GitHub Classroom Workflow - -on: - - push - - workflow_dispatch - +name: Autograding Tests +'on': +- push +- workflow_dispatch +- repository_dispatch permissions: checks: write actions: read contents: read - jobs: - build: - name: Autograding + run-autograding-tests: runs-on: ubuntu-latest if: github.actor != 'github-classroom[bot]' steps: - - uses: actions/checkout@v4 - - uses: education/autograding@v1 + - name: Checkout code + uses: actions/checkout@v4 + - name: Step-1 Test + id: step-1-test + uses: education/autograding-command-grader@v1 + with: + test-name: Step-1 Test + setup-command: npm install + command: npm run test:1 + timeout: 10 + max-score: 10 + - name: Step-2 Test + id: step-2-test + uses: education/autograding-command-grader@v1 + with: + test-name: Step-2 Test + setup-command: npm install + command: npm run test:2 + timeout: 10 + max-score: 10 + - name: Step-3 Test + id: step-3-test + uses: education/autograding-command-grader@v1 + with: + test-name: Step-3 Test + setup-command: npm install + command: npm run test:3 + timeout: 10 + max-score: 10 + - name: Step-4 Test + id: step-4-test + uses: education/autograding-command-grader@v1 + with: + test-name: Step-4 Test + setup-command: npm install + command: npm run test:4 + timeout: 10 + - name: Step-5 Test + id: step-5-test + uses: education/autograding-command-grader@v1 + with: + test-name: Step-5 Test + setup-command: npm install + command: npm run test:5 + timeout: 10 + max-score: 10 + - name: Step-6 Test + id: step-6-test + uses: education/autograding-command-grader@v1 + with: + test-name: Step-6 Test + setup-command: npm install + command: npm run test:6 + timeout: 10 + max-score: 10 + - name: Step-7 Test + id: step-7-test + uses: education/autograding-command-grader@v1 + with: + test-name: Step-7 Test + setup-command: npm install + command: npm run test:7 + timeout: 10 + max-score: 10 + - name: Step-8 Test + id: step-8-test + uses: education/autograding-command-grader@v1 + with: + test-name: Step-8 Test + setup-command: npm install + command: npm run test:8 + timeout: 10 + max-score: 10 + - name: Step-9 Test + id: step-9-test + uses: education/autograding-command-grader@v1 + with: + test-name: Step-9 Test + setup-command: npm install + command: npm run test:9 + timeout: 10 + max-score: 10 + - name: Step-10 Test + id: step-10-test + uses: education/autograding-command-grader@v1 + with: + test-name: Step-10 Test + setup-command: npm install + command: npm run test:10 + timeout: 10 + max-score: 10 + - name: Step-11 Test + id: step-11-test + uses: education/autograding-command-grader@v1 + with: + test-name: Step-11 Test + setup-command: npm install + command: npm run test:11 + timeout: 10 + max-score: 10 + - name: Step-12 Test + id: step-12-test + uses: education/autograding-command-grader@v1 + with: + test-name: Step-12 Test + setup-command: npm install + command: npm run test:12 + timeout: 10 + max-score: 10 + - name: Step-13 Test + id: step-13-test + uses: education/autograding-command-grader@v1 + with: + test-name: Step-13 Test + setup-command: npm install + command: npm run test:13 + timeout: 10 + max-score: 10 + - name: Step-14 Test + id: step-14-test + uses: education/autograding-command-grader@v1 + with: + test-name: Step-14 Test + setup-command: npm install + command: npm run test:14 + timeout: 10 + max-score: 10 + - name: Step-15 Test + id: step-15-test + uses: education/autograding-command-grader@v1 + with: + test-name: Step-15 Test + setup-command: npm install + command: npm run test:15 + timeout: 10 + max-score: 10 + - name: Step-16 Test + id: step-16-test + uses: education/autograding-command-grader@v1 + with: + test-name: Step-16 Test + setup-command: npm install + command: npm run test:16 + timeout: 10 + max-score: 10 + - name: Step-17 Test + id: step-17-test + uses: education/autograding-command-grader@v1 + with: + test-name: Step-17 Test + setup-command: npm install + command: npm run test:17 + timeout: 10 + max-score: 10 + - name: Step-18 Test + id: step-18-test + uses: education/autograding-command-grader@v1 + with: + test-name: Step-18 Test + setup-command: npm install + command: npm run test:18 + timeout: 10 + max-score: 10 + - name: Step-19 Test + id: step-19-test + uses: education/autograding-command-grader@v1 + with: + test-name: Step-19 Test + setup-command: npm install + command: npm run test:19 + timeout: 10 + max-score: 10 + - name: Step-20 Test + id: step-20-test + uses: education/autograding-command-grader@v1 + with: + test-name: Step-20 Test + setup-command: npm install + command: npm run test:20 + timeout: 10 + max-score: 10 + - name: Autograding Reporter + uses: education/autograding-grading-reporter@v1 + env: + STEP-1-TEST_RESULTS: "${{steps.step-1-test.outputs.result}}" + STEP-2-TEST_RESULTS: "${{steps.step-2-test.outputs.result}}" + STEP-3-TEST_RESULTS: "${{steps.step-3-test.outputs.result}}" + STEP-4-TEST_RESULTS: "${{steps.step-4-test.outputs.result}}" + STEP-5-TEST_RESULTS: "${{steps.step-5-test.outputs.result}}" + STEP-6-TEST_RESULTS: "${{steps.step-6-test.outputs.result}}" + STEP-7-TEST_RESULTS: "${{steps.step-7-test.outputs.result}}" + STEP-8-TEST_RESULTS: "${{steps.step-8-test.outputs.result}}" + STEP-9-TEST_RESULTS: "${{steps.step-9-test.outputs.result}}" + STEP-10-TEST_RESULTS: "${{steps.step-10-test.outputs.result}}" + STEP-11-TEST_RESULTS: "${{steps.step-11-test.outputs.result}}" + STEP-12-TEST_RESULTS: "${{steps.step-12-test.outputs.result}}" + STEP-13-TEST_RESULTS: "${{steps.step-13-test.outputs.result}}" + STEP-14-TEST_RESULTS: "${{steps.step-14-test.outputs.result}}" + STEP-15-TEST_RESULTS: "${{steps.step-15-test.outputs.result}}" + STEP-16-TEST_RESULTS: "${{steps.step-16-test.outputs.result}}" + STEP-17-TEST_RESULTS: "${{steps.step-17-test.outputs.result}}" + STEP-18-TEST_RESULTS: "${{steps.step-18-test.outputs.result}}" + STEP-19-TEST_RESULTS: "${{steps.step-19-test.outputs.result}}" + STEP-20-TEST_RESULTS: "${{steps.step-20-test.outputs.result}}" + with: + runners: step-1-test,step-2-test,step-3-test,step-4-test,step-5-test,step-6-test,step-7-test,step-8-test,step-9-test,step-10-test,step-11-test,step-12-test,step-13-test,step-14-test,step-15-test,step-16-test,step-17-test,step-18-test,step-19-test,step-20-test diff --git a/README.md b/README.md index eadfc715a..dea6de209 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![Open in Visual Studio Code](https://classroom.github.com/assets/open-in-vscode-718a45dd9cf7e7f842a935f5ebbe5719a5e09af4491e668f4dbf3b35d5cca122.svg)](https://classroom.github.com/online_ide?assignment_repo_id=14546071&assignment_repo_type=AssignmentRepo)

StylusDB SQL

A SQL database engine written in JavaScript diff --git a/enrollment.csv b/enrollment.csv new file mode 100644 index 000000000..e80af8d93 --- /dev/null +++ b/enrollment.csv @@ -0,0 +1,6 @@ +student_id,course +1,Mathematics +1,Physics +2,Chemistry +3,Mathematics +5,Biology \ No newline at end of file diff --git a/sample.csv b/sample.csv new file mode 100644 index 000000000..9e7a9fa25 --- /dev/null +++ b/sample.csv @@ -0,0 +1,4 @@ +id,name,age +1,John,30 +2,Jane,25 +3,Bob,22 \ No newline at end of file diff --git a/src/cli.js b/src/cli.js new file mode 100644 index 000000000..0bf6dcac7 --- /dev/null +++ b/src/cli.js @@ -0,0 +1,51 @@ +const readline = require('readline'); +const { executeSELECTQuery, executeINSERTQuery, executeDELETEQuery } = require('./queryExecutor.js'); + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +rl.setPrompt('SQL> '); +console.log('SQL Query Engine CLI. Enter your SQL commands, or type "exit" to quit.'); + +rl.prompt(); + +rl.on('line', async (line) => { + if (line.toLowerCase() === 'exit') { + rl.close(); + return; + } + + try { + // Parsing the first word of the command to determine the query type + const queryType = line.trim().split(' ')[0].toUpperCase(); + + switch (queryType) { + case 'SELECT': + // Execute SELECT query + const selectResult = await executeSELECTQuery(line); + console.log(selectResult); + break; + case 'INSERT': + // Execute INSERT query + const insertResult = await executeINSERTQuery(line); + console.log(insertResult); + break; + case 'DELETE': + // Execute DELETE query + const deleteResult = await executeDELETEQuery(line); + console.log(deleteResult); + break; + default: + console.error('Unsupported query type:', queryType); + } + } catch (error) { + console.error('Error:', error.message); + } + + rl.prompt(); +}).on('close', () => { + console.log('Exiting SQL CLI'); + process.exit(0); +}); diff --git a/src/csvReader.js b/src/csvReader.js index e69de29bb..b03e9fa6c 100644 --- a/src/csvReader.js +++ b/src/csvReader.js @@ -0,0 +1,30 @@ +const csv = require('csv-parser'); +const fs = require('fs'); +const { parse } = require("json2csv"); + +async function readCSV(filePath) { + const results = []; + + try { + const stream = fs.createReadStream(filePath) + .on('error', error => { throw error; }); + + await new Promise((resolve, reject) => { + stream.pipe(csv()) + .on('data', (data) => results.push(data)) + .on('end', () => resolve()) + .on('error', (error) => reject(error)); + }); + + return results; + } catch (error) { + throw error; + } +} + +async function writeCSV(filename, data) { + const csv = parse(data); + fs.writeFileSync(filename, csv); + } + +module.exports = {readCSV,writeCSV}; diff --git a/src/queryExecutor.js b/src/queryExecutor.js new file mode 100644 index 000000000..b84c9c76d --- /dev/null +++ b/src/queryExecutor.js @@ -0,0 +1,531 @@ +const { parseSelectQuery,parseINSERTQuery,parseDELETEQuery } = require("./queryParser"); +const {readCSV,writeCSV} = require("./csvReader"); + +function performInnerJoin(data, joinData, joinCondition, fields, table) { + return data.flatMap((mainRow) => { + return joinData + .filter((joinRow) => { + const mainValue = mainRow[joinCondition.left.split(".")[1]]; + const joinValue = joinRow[joinCondition.right.split(".")[1]]; + return mainValue === joinValue; + }) + .map((joinRow) => { + return fields.reduce((acc, field) => { + const [tableName, fieldName] = field.split("."); + acc[field] = + tableName === table ? mainRow[fieldName] : joinRow[fieldName]; + return acc; + }, {}); + }); + }); +} + +function performLeftJoin(data, joinData, joinCondition, fields, table) { + return data.flatMap((mainRow) => { + const matchingJoinRows = joinData.filter((joinRow) => { + const mainValue = getValueFromRow(mainRow, joinCondition.left); + const joinValue = getValueFromRow(joinRow, joinCondition.right); + return mainValue === joinValue; + }); + + if (matchingJoinRows.length === 0) { + return [createResultRow(mainRow, null, fields, table, true)]; + } + + return matchingJoinRows.map((joinRow) => + createResultRow(mainRow, joinRow, fields, table, true), + ); + }); +} + +function getValueFromRow(row, compoundFieldName) { + const [tableName, fieldName] = compoundFieldName.split("."); + return row[`${tableName}.${fieldName}`] || row[fieldName]; +} + +function performRightJoin(data, joinData, joinCondition, fields, table) { + // Cache the structure of a main table row (keys only) + const mainTableRowStructure = + data.length > 0 + ? Object.keys(data[0]).reduce((acc, key) => { + acc[key] = null; // Set all values to null initially + return acc; + }, {}) + : {}; + + return joinData.map((joinRow) => { + const mainRowMatch = data.find((mainRow) => { + const mainValue = getValueFromRow(mainRow, joinCondition.left); + const joinValue = getValueFromRow(joinRow, joinCondition.right); + return mainValue === joinValue; + }); + + // Use the cached structure if no match is found + const mainRowToUse = mainRowMatch || mainTableRowStructure; + + // Include all necessary fields from the 'student' table + return createResultRow(mainRowToUse, joinRow, fields, table, true); + }); +} + +function createResultRow( + mainRow, + joinRow, + fields, + table, + includeAllMainFields, +) { + const resultRow = {}; + + if (includeAllMainFields) { + // Include all fields from the main table + Object.keys(mainRow || {}).forEach((key) => { + const prefixedKey = `${table}.${key}`; + resultRow[prefixedKey] = mainRow ? mainRow[key] : null; + }); + } + + // Now, add or overwrite with the fields specified in the query + fields.forEach((field) => { + const [tableName, fieldName] = field.includes(".") + ? field.split(".") + : [table, field]; + resultRow[field] = + tableName === table && mainRow + ? mainRow[fieldName] + : joinRow + ? joinRow[fieldName] + : null; + }); + + return resultRow; +} + +function applyGroupBy(data, groupByFields, fields, hasAggregateWithoutGroupBy) { + const groupedData = {}; + + for (const row of data) { + const groupKey = groupByFields + ? groupByFields.map((field) => row[field]).join("|") + : hasAggregateWithoutGroupBy + ? "AGGREGATE" + : ""; + + if (!groupedData[groupKey]) { + groupedData[groupKey] = { + count: 0, + sum: {}, + avg: {}, + min: {}, + max: {}, + rows: [], + }; + } + + groupedData[groupKey].count++; + groupedData[groupKey].rows.push(row); + + for (const field of fields) { + const [, functionName, fieldName] = field.match( + /^(COUNT|SUM|AVG|MIN|MAX)\((.+)\)$/, + ) || [null, null, field]; + + if ( + functionName === "SUM" || + functionName === "AVG" || + functionName === "MIN" || + functionName === "MAX" + ) { + const value = parseFloat(row[fieldName]); + if (!isNaN(value)) { + if (!groupedData[groupKey].sum[field]) { + groupedData[groupKey].sum[field] = 0; + groupedData[groupKey].min[field] = Infinity; + groupedData[groupKey].max[field] = -Infinity; + } + + groupedData[groupKey].sum[field] += value; + groupedData[groupKey].min[field] = Math.min( + groupedData[groupKey].min[field], + value, + ); + groupedData[groupKey].max[field] = Math.max( + groupedData[groupKey].max[field], + value, + ); + } + } + } + } + + const result = Object.values(groupedData).map((group) => { + const aggregatedRow = {}; + for (const field of fields) { + const [, functionName, fieldName] = field.match( + /(\w+)\((\*|\w+)\)/, + ) || [null, null, field]; + if (functionName === "COUNT") { + if (fieldName === "*") { + aggregatedRow[field] = hasAggregateWithoutGroupBy + ? group.count + : group.rows.length; + } else { + aggregatedRow[field] = group.rows.filter( + (row) => row[fieldName] !== null, + ).length; + } + } else if (functionName === "SUM") { + aggregatedRow[field] = group.sum[field] || 0; + } else if (functionName === "AVG") { + aggregatedRow[field] = + group.sum[field] !== undefined + ? group.sum[field] / + group.rows.filter((row) => row[fieldName] !== null).length + : 0; + } else if (functionName === "MIN") { + aggregatedRow[field] = + group.min[field] !== Infinity ? group.min[field] : null; + } else if (functionName === "MAX") { + aggregatedRow[field] = + group.max[field] !== -Infinity ? group.max[field] : null; + } else { + aggregatedRow[field] = group.rows[0][field]; + } + } + return aggregatedRow; + }); + + return result; +} + +function evaluateCondition(row, clause) { + const { field, operator, value } = clause; + const cleanedValue = parseValue(value); // Remove single quotes and handle quoted values + + const fieldValue = parseValue(row[field]); + + if (operator === 'LIKE') { + // Transform SQL LIKE pattern to JavaScript RegExp pattern + const regexPattern = '^' + value.replace(/%/g, '.*').replace(/_/g, '.') + '$'; + const regex = new RegExp(regexPattern, 'i'); // 'i' for case-insensitive matching + console.log(regex) + return regex.test(row[field]); +} + + switch (operator) { + case "=": + return fieldValue === cleanedValue; + case ">": + return fieldValue > cleanedValue; + case "<": + return fieldValue < cleanedValue; + case ">=": + return fieldValue >= cleanedValue; + case "<=": + return fieldValue <= cleanedValue; + case "!=": + return fieldValue !== cleanedValue; + default: + throw new Error(`Unsupported operator: ${operator}`); + } +} + +// Helper function to parse value based on its apparent type +function parseValue(value) { + // Return null or undefined as is + if (value === null || value === undefined) { + return value; + } + + // If the value is a string enclosed in single or double quotes, remove them + if ( + typeof value === "string" && + ((value.startsWith("'") && value.endsWith("'")) || + (value.startsWith('"') && value.endsWith('"'))) + ) { + value = value.substring(1, value.length - 1); + } + + // Check if value is a number + if (!isNaN(value) && value.trim() !== "") { + return Number(value); + } + // Assume value is a string if not a number + return value; +} + +async function executeSELECTQuery(query) { + try { + const { + fields, + table, + whereClauses, + joinType, + joinTable, + joinCondition, + groupByFields, + hasAggregateWithoutGroupBy, + orderByFields, + limit, + isDistinct + } = parseSelectQuery(query); + + // console.log("Parsed Query:", parseSelectQuery(query)); // Logging the parsed query for debugging + + let data = await readCSV(`${table}.csv`); + // console.log("Initial data:", data); + + // Perform INNER JOIN if specified + if (joinTable && joinCondition) { + const joinData = await readCSV(`${joinTable}.csv`); + switch (joinType.toUpperCase()) { + case "INNER": + // console.log("Performing INNER JOIN..."); + data = performInnerJoin(data, joinData, joinCondition, fields, table); + break; + case "LEFT": + // console.log("Performing LEFT JOIN..."); + data = performLeftJoin(data, joinData, joinCondition, fields, table); + break; + case "RIGHT": + // console.log("Performing RIGHT JOIN..."); + data = performRightJoin(data, joinData, joinCondition, fields, table); + break; + default: + throw new Error(`Unsupported JOIN type: ${joinType}`); + } + } + + // Apply WHERE clause filtering after JOIN (or on the original data if no join) + let filteredData = + whereClauses.length > 0 + ? data.filter((row) => + whereClauses.every((clause) => evaluateCondition(row, clause)), + ) + : data; + + let groupResults = filteredData; + // console.log("Has Aggregate Without Group By:", hasAggregateWithoutGroupBy); + + if (hasAggregateWithoutGroupBy) { + // console.log("Handling Aggregate Without Group By..."); + // Special handling for queries like 'SELECT COUNT(*) FROM table' + const result = {}; + + // console.log("Filtered Data:", filteredData); + + fields.forEach((field) => { + const match = /(\w+)\((\*|\w+)\)/.exec(field); + if (match) { + const [, aggFunc, aggField] = match; + switch (aggFunc.toUpperCase()) { + case "COUNT": + result[field] = filteredData.length; + break; + case "SUM": + result[field] = filteredData.reduce( + (acc, row) => acc + parseFloat(row[aggField]), + 0, + ); + break; + case "AVG": + result[field] = + filteredData.reduce( + (acc, row) => acc + parseFloat(row[aggField]), + 0, + ) / filteredData.length; + break; + case "MIN": + result[field] = Math.min( + ...filteredData.map((row) => parseFloat(row[aggField])), + ); + break; + case "MAX": + result[field] = Math.max( + ...filteredData.map((row) => parseFloat(row[aggField])), + ); + break; + // Additional aggregate functions can be handled here + } + } + }); + + if (isDistinct) { + // console.log("Handling DISTINCT..."); + result = [...new Map(result.map(item => [fields.map(field => item[field]).join('|'), item])).values()]; + } + + if (orderByFields) { + // console.log("Sorting results..."); + const orderByGroup = (a, b) => { + for (let { fieldName, order } of orderByFields) { + if (a[fieldName] < b[fieldName]) return order === "ASC" ? -1 : 1; + if (a[fieldName] > b[fieldName]) return order === "ASC" ? 1 : -1; + } + return 0; + }; + // Sort the result + return [result].sort(orderByGroup); + } else { + return [result]; + } + // Add more cases here if needed for other aggregates + } else if (groupByFields) { + // console.log("Applying Group By..."); + groupResults = applyGroupBy(filteredData, groupByFields, fields); + + // Order them by the specified fields + let orderedResults = groupResults; + if (orderByFields) { + // console.log("Sorting results..."); + orderedResults = groupResults.sort((a, b) => { + for (let { fieldName, order } of orderByFields) { + if (a[fieldName] < b[fieldName]) return order === "ASC" ? -1 : 1; + if (a[fieldName] > b[fieldName]) return order === "ASC" ? 1 : -1; + } + return 0; + }); + } + + if (isDistinct) { + // console.log("Handling DISTINCT... elseif"); + orderedResults = [...new Map(orderedResults.map(item => [fields.map(field => item[field]).join('|'), item])).values()]; + } + + if (limit !== null) { + // console.log("Applying Limit..."); + orderedResults = orderedResults.slice(0, limit); + } + + return orderedResults; + } else { + // Order them by the specified fields + let orderedResults = groupResults; + if (orderByFields) { + // console.log("Sorting results..."); + orderedResults = groupResults.sort((a, b) => { + for (let { fieldName, order } of orderByFields) { + if (a[fieldName] < b[fieldName]) return order === "ASC" ? -1 : 1; + if (a[fieldName] > b[fieldName]) return order === "ASC" ? 1 : -1; + } + return 0; + }); + } + + if (isDistinct) { + // console.log("Handling DISTINCT...else"); + orderedResults = [...new Map(orderedResults.map(item => [fields.map(field => item[field]).join('|'), item])).values()]; + } + + if (limit !== null) { + // console.log("Applying Limit..."); + orderedResults = orderedResults.slice(0, limit); + } + + // Select the specified fields + return orderedResults.map((row) => { + const selectedRow = {}; + fields.forEach((field) => { + // Assuming 'field' is just the column name without table prefix + selectedRow[field] = row[field]; + }); + return selectedRow; + }); + } + + + } + catch (error) { + // Log error and provide user-friendly message + console.error("Error executing query:", error); + throw new Error(`Error executing query: ${error.message}`); + } +} + +async function executeINSERTQuery(query) { + try { + const { + type, + table, + columns, + values, + } = parseINSERTQuery(query); + + // Validate if the provided columns and values arrays have the same length + if (columns.length !== values.length) { + throw new Error(`Number of columns (${columns.length}) does not match the number of values (${values.length}).`); + } + + // Create an object with key-value pairs of column names and their respective values + const rowData = {}; + for (let i = 0; i < columns.length; i++) { + rowData[columns[i]] = values[i]; + } + + // Prepare data for writing to CSV + const dataToWrite = [rowData]; + + // Define the file path where the CSV will be written + const filePath = `${table}.csv`; + + // Write data to CSV + await writeCSV(filePath, dataToWrite); + + return dataToWrite.length; // Return the number of rows inserted + } catch (error) { + console.error("Error executing INSERT query:", error); + throw new Error(`Error executing INSERT query: ${error.message}`); + } +} + + +async function executeDELETEQuery(query) { + try { + // Parsing the DELETE query + const { table, whereClauses } = parseDELETEQuery(query); + + // Reading data from CSV + let data = await readCSV(`${table}.csv`); + + if (whereClauses.length > 0) { + // Filter out the rows that meet the where clause conditions + data = data.filter(row => { + for (const { column, operator, value } of whereClauses) { + if (evaluateCondition(row, { field: column, operator, value })) { + return false; + } + } + return true; + }); + } else { + // If no where clause, clear the entire table + data = []; + } + + // Save the updated data back to the CSV file + await writeCSV(`${table}.csv`, data); + + return { message: "Rows deleted successfully." }; + } catch (error) { + console.error("Error executing DELETE query:", error); + throw new Error(`Error executing DELETE query: ${error.message}`); + } +} + + + + + + +// (async () => { +// try { +// const data = await executeDELETEQuery( +// "DELETE FROM courses WHERE course_id = '2'", +// ); + // console.log("Result:", data); +// } catch (error) { +// console.error("Error:", error); +// } +// })(); + +module.exports = {executeSELECTQuery,executeINSERTQuery,executeDELETEQuery}; diff --git a/src/queryParser.js b/src/queryParser.js new file mode 100644 index 000000000..18d961ab5 --- /dev/null +++ b/src/queryParser.js @@ -0,0 +1,140 @@ +function hasAggregateWithoutGroupBy_(fields) { + const aggregateRegex = /(\bCOUNT\b|\bAVG\b|\bSUM\b|\bMIN\b|\bMAX\b)\s*\(\s*(\*|\w+)\s*\)/i + return fields.some(field => aggregateRegex.test(field)); +} + + +function parseJoinCondition(joinCondition) { + const [left, right] = joinCondition.split('=').map(part => part.trim()); + return { left, right }; +} + +function parseWhereClause(whereClause) { + const conditions = whereClause.split(" AND ").map((condition) => { + if (condition.includes(" LIKE ")) { + + const [field, pattern] = condition.split(/\sLIKE\s/i); + return { field: field.trim(), operator: "LIKE", value: pattern.trim(). replace(/['"]/g, '') }; + } else { + const [field, operator, value] = condition.trim().split(/\s+/); + return { field, operator, value }; + } + }); + return conditions; +} + +function parseJoinClause(query) { + const joinRegex = /\s(INNER|LEFT|RIGHT) JOIN\s(.+?)\sON\s([\w.]+)\s*=\s*([\w.]+)/i; + const joinMatch = query.match(joinRegex); + + if (joinMatch) { + return { + joinType: joinMatch[1].trim(), + joinTable: joinMatch[2].trim(), + joinCondition: { + left: joinMatch[3].trim(), + right: joinMatch[4].trim() + } + }; + } + + return { + joinType: null, + joinTable: null, + joinCondition: null + }; +} + +function parseSelectQuery(query) { + try { + let isDistinct = false; + + if (query.toUpperCase().includes('SELECT DISTINCT')) { + isDistinct = true; + query = query.replace('SELECT DISTINCT', 'SELECT'); + } + + const selectRegex = /^SELECT\s(.*?)\sFROM\s(.*?)(?:\s(INNER|LEFT|RIGHT)\sJOIN\s(.*?)\sON\s(.*?))?(?:\sWHERE\s(.*?))?(?:\sGROUP\sBY\s(.*?))?(?:\sORDER\sBY\s(.*?))?(?:\sLIMIT\s(\d+))?$/i; + + const matches = query.match(selectRegex); + + if (!matches) { + throw new Error(`Invalid SELECT format.`); + } + + + + const fields = matches[1].split(',').map(field => field.trim()); + const table = matches[2].trim(); + const { joinType, joinTable, joinCondition } = parseJoinClause(query); + const whereClause = matches[6] ? matches[6].trim() : null; + const whereClauses = whereClause ? parseWhereClause(whereClause) : []; + const groupByFields = matches[7] ? matches[7].split(',').map(field => field.trim()) : null; + const orderByFields = matches[8] ? matches[8].split(',').map(field => { + const [fieldName, order] = field.trim().split(/\s+/); + return { fieldName, order: order ? order.toUpperCase() : 'ASC' }; + }) : null; + const hasAggregateWithoutGroupBy = hasAggregateWithoutGroupBy_(fields) && !groupByFields; + const limit = matches[9] ? parseInt(matches[9]) : null; + + return { fields, table, whereClauses, groupByFields, orderByFields, joinType, joinTable, joinCondition, hasAggregateWithoutGroupBy, limit ,isDistinct}; + } catch (error) { + throw new Error(`Query parsing error: ${error.message}`); + } +} + +function parseINSERTQuery(query) { + const insertRegex = /^INSERT INTO\s+(\w+)\s+\((.*?)\)\s+VALUES\s+\((.*?)\)$/i; + + const match = query.match(insertRegex); + if (!match) { + throw new Error("Invalid INSERT query format."); + } + + const table = match[1]; + const columns = match[2].split(",").map((column) => column.trim().replace(/['"]/g, '')); + const values = match[3].split(",").map((value) => value.trim().replace(/['"]/g, '')); + + return { + type: "INSERT", + table, + columns, + values, + }; +} + +function parseDELETEQuery(query) { + try { + const deleteRegex = /^DELETE\sFROM\s(\w+)\s(?:WHERE\s(.+))?$/i; + const matches = query.match(deleteRegex); + + if (!matches) { + throw new Error(`Invalid DELETE statement format.`); + } + + const table = matches[1].trim(); + const whereClause = matches[2] ? matches[2].trim().replace(/['"]/g, '') : null; + const whereClauses = []; + + if (whereClause) { + // Splitting the where clause into individual conditions + const conditions = whereClause.split(/\s+AND\s+/); + conditions.forEach(condition => { + const [column, operator, value] = condition.split(/\s+/); + whereClauses.push({ column, operator, value }); + }); + } + + return { type: 'DELETE', table, whereClauses }; + } catch (error) { + throw new Error(`Query parsing error: ${error.message}`); + } +} + + + +// const parsedQuery = parseDELETEQuery("DELETE FROM courses WHERE course_id = '2'"); +// console.log(parsedQuery); + +// Exporting both parseQuery and parseJoinClause +module.exports = { parseSelectQuery,parseJoinClause,parseINSERTQuery,parseDELETEQuery }; \ No newline at end of file diff --git a/src/server.js b/src/server.js new file mode 100644 index 000000000..26d31e9f6 --- /dev/null +++ b/src/server.js @@ -0,0 +1 @@ +console.log('server') \ No newline at end of file diff --git a/student.csv b/student.csv new file mode 100644 index 000000000..e9c960121 --- /dev/null +++ b/student.csv @@ -0,0 +1,5 @@ +id,name,age +1,John,30 +2,Jane,25 +3,Bob,22 +4,Alice,24 \ No newline at end of file diff --git a/tests/step-02/index.test.js b/tests/step-02/index.test.js index a5467ee48..e25c5763a 100644 --- a/tests/step-02/index.test.js +++ b/tests/step-02/index.test.js @@ -1,4 +1,5 @@ -const readCSV = require('../../src/csvReader'); +const {readCSV} = require('../../src/csvReader'); + test('Read CSV File', async () => { const data = await readCSV('./sample.csv'); diff --git a/tests/step-03/index.test.js b/tests/step-03/index.test.js index 9145ad3e4..ddaec817e 100644 --- a/tests/step-03/index.test.js +++ b/tests/step-03/index.test.js @@ -1,5 +1,5 @@ -const readCSV = require('../../src/csvReader'); -const parseQuery = require('../../src/queryParser'); +const {readCSV} = require('../../src/csvReader'); +const {parseSelectQuery} = require('../../src/queryParser'); test('Read CSV File', async () => { const data = await readCSV('./sample.csv'); @@ -10,10 +10,18 @@ test('Read CSV File', async () => { }); test('Parse SQL Query', () => { - const query = 'SELECT id, name FROM sample'; - const parsed = parseQuery(query); + const query = 'SELECT id, name FROM student'; + const parsed = parseSelectQuery +(query); expect(parsed).toEqual({ fields: ['id', 'name'], - table: 'sample' + table: 'student', + whereClauses: [], + joinCondition: null, + joinTable: null, + joinType: null, + groupByFields: null, + hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); \ No newline at end of file diff --git a/tests/step-04/index.test.js b/tests/step-04/index.test.js index bc353dd3d..2f92564b5 100644 --- a/tests/step-04/index.test.js +++ b/tests/step-04/index.test.js @@ -1,26 +1,33 @@ -const readCSV = require('../../src/csvReader'); -const parseQuery = require('../../src/queryParser'); -const executeSELECTQuery = require('../../src/index'); +const {readCSV} = require('../../src/csvReader'); +const {parseSelectQuery} = require('../../src/queryParser'); +const {executeSELECTQuery} = require('../../src/queryExecutor'); test('Read CSV File', async () => { - const data = await readCSV('./sample.csv'); + const data = await readCSV('./student.csv'); expect(data.length).toBeGreaterThan(0); - expect(data.length).toBe(3); + expect(data.length).toBe(4); expect(data[0].name).toBe('John'); expect(data[0].age).toBe('30'); //ignore the string type here, we will fix this later }); test('Parse SQL Query', () => { - const query = 'SELECT id, name FROM sample'; - const parsed = parseQuery(query); + const query = 'SELECT id, name FROM student'; + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], - table: 'sample' + table: 'student', + whereClauses: [], + joinCondition: null, + joinTable: null, + joinType: null, + groupByFields: null, + hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Execute SQL Query', async () => { - const query = 'SELECT id, name FROM sample'; + const query = 'SELECT id, name FROM student'; const result = await executeSELECTQuery(query); expect(result.length).toBeGreaterThan(0); expect(result[0]).toHaveProperty('id'); diff --git a/tests/step-05/index.test.js b/tests/step-05/index.test.js index 66a77c061..362e55269 100644 --- a/tests/step-05/index.test.js +++ b/tests/step-05/index.test.js @@ -1,27 +1,33 @@ -const readCSV = require('../../src/csvReader'); -const parseQuery = require('../../src/queryParser'); -const executeSELECTQuery = require('../../src/index'); +const {readCSV} = require('../../src/csvReader'); +const {parseSelectQuery} = require('../../src/queryParser'); +const {executeSELECTQuery} = require('../../src/queryExecutor'); test('Read CSV File', async () => { - const data = await readCSV('./sample.csv'); + const data = await readCSV('./student.csv'); expect(data.length).toBeGreaterThan(0); - expect(data.length).toBe(3); + expect(data.length).toBe(4); expect(data[0].name).toBe('John'); expect(data[0].age).toBe('30'); //ignore the string type here, we will fix this later }); test('Parse SQL Query', () => { - const query = 'SELECT id, name FROM sample'; - const parsed = parseQuery(query); + const query = 'SELECT id, name FROM student'; + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], - table: 'sample', - whereClause: null + table: 'student', + whereClauses: [], + joinCondition: null, + joinTable: null, + joinType: null, + groupByFields: null, + hasAggregateWithoutGroupBy: false, + orderByFields: null,"limit": null,isDistinct: false, }); }); test('Execute SQL Query', async () => { - const query = 'SELECT id, name FROM sample'; + const query = 'SELECT id, name FROM student'; const result = await executeSELECTQuery(query); expect(result.length).toBeGreaterThan(0); expect(result[0]).toHaveProperty('id'); @@ -31,17 +37,27 @@ test('Execute SQL Query', async () => { }); test('Parse SQL Query with WHERE Clause', () => { - const query = 'SELECT id, name FROM sample WHERE age = 25'; - const parsed = parseQuery(query); + const query = 'SELECT id, name FROM student WHERE age = 25'; + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], - table: 'sample', - whereClause: 'age = 25' + table: 'student', + whereClauses: [{ + "field": "age", + "operator": "=", + "value": "25", + }], + joinCondition: null, + joinTable: null, + joinType: null, + groupByFields: null, + hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Execute SQL Query with WHERE Clause', async () => { - const query = 'SELECT id, name FROM sample WHERE age = 25'; + const query = 'SELECT id, name FROM student WHERE age = 25'; const result = await executeSELECTQuery(query); expect(result.length).toBe(1); expect(result[0]).toHaveProperty('id'); diff --git a/tests/step-06/index.test.js b/tests/step-06/index.test.js index 2e2ef6416..1ed547708 100644 --- a/tests/step-06/index.test.js +++ b/tests/step-06/index.test.js @@ -1,27 +1,33 @@ -const readCSV = require('../../src/csvReader'); -const parseQuery = require('../../src/queryParser'); -const executeSELECTQuery = require('../../src/index'); +const {readCSV} = require('../../src/csvReader'); +const {parseSelectQuery} = require('../../src/queryParser'); +const {executeSELECTQuery} = require('../../src/queryExecutor'); test('Read CSV File', async () => { - const data = await readCSV('./sample.csv'); + const data = await readCSV('./student.csv'); expect(data.length).toBeGreaterThan(0); - expect(data.length).toBe(3); + expect(data.length).toBe(4); expect(data[0].name).toBe('John'); expect(data[0].age).toBe('30'); //ignore the string type here, we will fix this later }); test('Parse SQL Query', () => { - const query = 'SELECT id, name FROM sample'; - const parsed = parseQuery(query); + const query = 'SELECT id, name FROM student'; + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], - table: 'sample', - whereClauses: [] + table: 'student', + whereClauses: [], + joinCondition: null, + joinTable: null, + joinType: null, + groupByFields: null, + hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Execute SQL Query', async () => { - const query = 'SELECT id, name FROM sample'; + const query = 'SELECT id, name FROM student'; const result = await executeSELECTQuery(query); expect(result.length).toBeGreaterThan(0); expect(result[0]).toHaveProperty('id'); @@ -31,21 +37,27 @@ test('Execute SQL Query', async () => { }); test('Parse SQL Query with WHERE Clause', () => { - const query = 'SELECT id, name FROM sample WHERE age = 25'; - const parsed = parseQuery(query); + const query = 'SELECT id, name FROM student WHERE age = 25'; + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], - table: 'sample', + table: 'student', whereClauses: [{ - field: "age", - operator: "=", - value: "25", + "field": "age", + "operator": "=", + "value": "25", }], + joinCondition: null, + joinTable: null, + joinType: null, + groupByFields: null, + hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Execute SQL Query with WHERE Clause', async () => { - const query = 'SELECT id, name FROM sample WHERE age = 25'; + const query = 'SELECT id, name FROM student WHERE age = 25'; const result = await executeSELECTQuery(query); expect(result.length).toBe(1); expect(result[0]).toHaveProperty('id'); @@ -53,12 +65,13 @@ test('Execute SQL Query with WHERE Clause', async () => { expect(result[0].id).toBe('2'); }); + test('Parse SQL Query with Multiple WHERE Clauses', () => { - const query = 'SELECT id, name FROM sample WHERE age = 30 AND name = John'; - const parsed = parseQuery(query); + const query = 'SELECT id, name FROM student WHERE age = 30 AND name = John'; + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], - table: 'sample', + table: 'student', whereClauses: [{ "field": "age", "operator": "=", @@ -67,12 +80,18 @@ test('Parse SQL Query with Multiple WHERE Clauses', () => { "field": "name", "operator": "=", "value": "John", - }] + }], + joinCondition: null, + joinTable: null, + joinType: null, + groupByFields: null, + hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Execute SQL Query with Multiple WHERE Clause', async () => { - const query = 'SELECT id, name FROM sample WHERE age = 30 AND name = John'; + const query = 'SELECT id, name FROM student WHERE age = 30 AND name = John'; const result = await executeSELECTQuery(query); expect(result.length).toBe(1); expect(result[0]).toEqual({ id: '1', name: 'John' }); diff --git a/tests/step-07/index.test.js b/tests/step-07/index.test.js index ee0ebed5e..3c504574c 100644 --- a/tests/step-07/index.test.js +++ b/tests/step-07/index.test.js @@ -1,27 +1,33 @@ -const readCSV = require('../../src/csvReader'); -const parseQuery = require('../../src/queryParser'); -const executeSELECTQuery = require('../../src/index'); +const {readCSV} = require('../../src/csvReader'); +const {parseSelectQuery} = require('../../src/queryParser'); +const {executeSELECTQuery} = require('../../src/queryExecutor'); test('Read CSV File', async () => { - const data = await readCSV('./sample.csv'); + const data = await readCSV('./student.csv'); expect(data.length).toBeGreaterThan(0); - expect(data.length).toBe(3); + expect(data.length).toBe(4); expect(data[0].name).toBe('John'); expect(data[0].age).toBe('30'); //ignore the string type here, we will fix this later }); test('Parse SQL Query', () => { - const query = 'SELECT id, name FROM sample'; - const parsed = parseQuery(query); + const query = 'SELECT id, name FROM student'; + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], - table: 'sample', - whereClauses: [] + table: 'student', + whereClauses: [], + joinCondition: null, + joinTable: null, + joinType: null, + groupByFields: null, + hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Execute SQL Query', async () => { - const query = 'SELECT id, name FROM sample'; + const query = 'SELECT id, name FROM student'; const result = await executeSELECTQuery(query); expect(result.length).toBeGreaterThan(0); expect(result[0]).toHaveProperty('id'); @@ -31,21 +37,27 @@ test('Execute SQL Query', async () => { }); test('Parse SQL Query with WHERE Clause', () => { - const query = 'SELECT id, name FROM sample WHERE age = 25'; - const parsed = parseQuery(query); + const query = 'SELECT id, name FROM student WHERE age = 25'; + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], - table: 'sample', + table: 'student', whereClauses: [{ - field: "age", - operator: "=", - value: "25", + "field": "age", + "operator": "=", + "value": "25", }], + joinCondition: null, + joinTable: null, + joinType: null, + groupByFields: null, + hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Execute SQL Query with WHERE Clause', async () => { - const query = 'SELECT id, name FROM sample WHERE age = 25'; + const query = 'SELECT id, name FROM student WHERE age = 25'; const result = await executeSELECTQuery(query); expect(result.length).toBe(1); expect(result[0]).toHaveProperty('id'); @@ -53,12 +65,13 @@ test('Execute SQL Query with WHERE Clause', async () => { expect(result[0].id).toBe('2'); }); + test('Parse SQL Query with Multiple WHERE Clauses', () => { - const query = 'SELECT id, name FROM sample WHERE age = 30 AND name = John'; - const parsed = parseQuery(query); + const query = 'SELECT id, name FROM student WHERE age = 30 AND name = John'; + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], - table: 'sample', + table: 'student', whereClauses: [{ "field": "age", "operator": "=", @@ -67,27 +80,32 @@ test('Parse SQL Query with Multiple WHERE Clauses', () => { "field": "name", "operator": "=", "value": "John", - }] + }], + joinCondition: null, + joinTable: null, + joinType: null, + groupByFields: null, + hasAggregateWithoutGroupBy: false, "orderByFields": null,"limit": null,isDistinct: false, }); }); -test('Execute SQL Query with Multiple WHERE Clause', async () => { - const query = 'SELECT id, name FROM sample WHERE age = 30 AND name = John'; +test('Execute SQL Query with Complex WHERE Clause', async () => { + const query = 'SELECT id, name FROM student WHERE age = 30 AND name = John'; const result = await executeSELECTQuery(query); expect(result.length).toBe(1); expect(result[0]).toEqual({ id: '1', name: 'John' }); }); test('Execute SQL Query with Greater Than', async () => { - const queryWithGT = 'SELECT id FROM sample WHERE age > 22'; + const queryWithGT = 'SELECT id FROM student WHERE age > 22'; const result = await executeSELECTQuery(queryWithGT); - expect(result.length).toEqual(2); + expect(result.length).toEqual(3); expect(result[0]).toHaveProperty('id'); }); test('Execute SQL Query with Not Equal to', async () => { - const queryWithGT = 'SELECT name FROM sample WHERE age != 25'; + const queryWithGT = 'SELECT name FROM student WHERE age != 25'; const result = await executeSELECTQuery(queryWithGT); - expect(result.length).toEqual(2); + expect(result.length).toEqual(3); expect(result[0]).toHaveProperty('name'); }); \ No newline at end of file diff --git a/tests/step-08/index.test.js b/tests/step-08/index.test.js index aab1467e6..b7bf486ae 100644 --- a/tests/step-08/index.test.js +++ b/tests/step-08/index.test.js @@ -1,24 +1,28 @@ -const readCSV = require('../../src/csvReader'); -const parseQuery = require('../../src/queryParser'); -const executeSELECTQuery = require('../../src/index'); +const {readCSV} = require('../../src/csvReader'); +const {parseSelectQuery} = require('../../src/queryParser'); +const {executeSELECTQuery} = require('../../src/queryExecutor'); test('Read CSV File', async () => { const data = await readCSV('./student.csv'); expect(data.length).toBeGreaterThan(0); - expect(data.length).toBe(3); + expect(data.length).toBe(4); expect(data[0].name).toBe('John'); expect(data[0].age).toBe('30'); //ignore the string type here, we will fix this later }); test('Parse SQL Query', () => { const query = 'SELECT id, name FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', whereClauses: [], joinCondition: null, - joinTable: null + joinTable: null, + joinType: null, + groupByFields: null, + hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); @@ -34,7 +38,7 @@ test('Execute SQL Query', async () => { test('Parse SQL Query with WHERE Clause', () => { const query = 'SELECT id, name FROM student WHERE age = 25'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -44,7 +48,11 @@ test('Parse SQL Query with WHERE Clause', () => { "value": "25", }], joinCondition: null, - joinTable: null + joinTable: null, + joinType: null, + groupByFields: null, + hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); @@ -57,9 +65,10 @@ test('Execute SQL Query with WHERE Clause', async () => { expect(result[0].id).toBe('2'); }); + test('Parse SQL Query with Multiple WHERE Clauses', () => { const query = 'SELECT id, name FROM student WHERE age = 30 AND name = John'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -73,7 +82,11 @@ test('Parse SQL Query with Multiple WHERE Clauses', () => { "value": "John", }], joinCondition: null, - joinTable: null + joinTable: null, + joinType: null, + groupByFields: null, + hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); @@ -87,38 +100,46 @@ test('Execute SQL Query with Complex WHERE Clause', async () => { test('Execute SQL Query with Greater Than', async () => { const queryWithGT = 'SELECT id FROM student WHERE age > 22'; const result = await executeSELECTQuery(queryWithGT); - expect(result.length).toEqual(2); + expect(result.length).toEqual(3); expect(result[0]).toHaveProperty('id'); }); test('Execute SQL Query with Not Equal to', async () => { const queryWithGT = 'SELECT name FROM student WHERE age != 25'; const result = await executeSELECTQuery(queryWithGT); - expect(result.length).toEqual(2); + expect(result.length).toEqual(3); expect(result[0]).toHaveProperty('name'); }); test('Parse SQL Query with INNER JOIN', async () => { const query = 'SELECT student.name, enrollment.course FROM student INNER JOIN enrollment ON student.id=enrollment.student_id'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', whereClauses: [], joinTable: 'enrollment', - joinCondition: { left: 'student.id', right: 'enrollment.student_id' } + joinType: "INNER", + joinCondition: { left: 'student.id', right: 'enrollment.student_id' }, + groupByFields: null, + hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }) }); test('Parse SQL Query with INNER JOIN and WHERE Clause', async () => { const query = 'SELECT student.name, enrollment.course FROM student INNER JOIN enrollment ON student.id = enrollment.student_id WHERE student.age > 20'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', whereClauses: [{ field: 'student.age', operator: '>', value: '20' }], joinTable: 'enrollment', - joinCondition: { left: 'student.id', right: 'enrollment.student_id' } + joinType: "INNER", + joinCondition: { left: 'student.id', right: 'enrollment.student_id' }, + groupByFields: null, + hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }) }); diff --git a/tests/step-09/index.test.js b/tests/step-09/index.test.js index aaf711f5a..587f08ffd 100644 --- a/tests/step-09/index.test.js +++ b/tests/step-09/index.test.js @@ -1,6 +1,6 @@ -const readCSV = require('../../src/csvReader'); -const {parseQuery} = require('../../src/queryParser'); -const executeSELECTQuery = require('../../src/index'); +const {readCSV} = require('../../src/csvReader'); +const {parseSelectQuery} = require('../../src/queryParser'); +const {executeSELECTQuery} = require('../../src/queryExecutor'); test('Read CSV File', async () => { const data = await readCSV('./student.csv'); @@ -12,14 +12,17 @@ test('Read CSV File', async () => { test('Parse SQL Query', () => { const query = 'SELECT id, name FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', whereClauses: [], joinCondition: null, joinTable: null, - joinType: null + joinType: null, + groupByFields: null, + hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); @@ -35,7 +38,7 @@ test('Execute SQL Query', async () => { test('Parse SQL Query with WHERE Clause', () => { const query = 'SELECT id, name FROM student WHERE age = 25'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -46,7 +49,10 @@ test('Parse SQL Query with WHERE Clause', () => { }], joinCondition: null, joinTable: null, - joinType: null + joinType: null, + groupByFields: null, + hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); @@ -59,9 +65,10 @@ test('Execute SQL Query with WHERE Clause', async () => { expect(result[0].id).toBe('2'); }); + test('Parse SQL Query with Multiple WHERE Clauses', () => { const query = 'SELECT id, name FROM student WHERE age = 30 AND name = John'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -76,7 +83,10 @@ test('Parse SQL Query with Multiple WHERE Clauses', () => { }], joinCondition: null, joinTable: null, - joinType: null + joinType: null, + groupByFields: null, + hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); @@ -103,27 +113,33 @@ test('Execute SQL Query with Not Equal to', async () => { test('Parse SQL Query with INNER JOIN', async () => { const query = 'SELECT student.name, enrollment.course FROM student INNER JOIN enrollment ON student.id=enrollment.student_id'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', whereClauses: [], joinTable: 'enrollment', + joinType: "INNER", joinCondition: { left: 'student.id', right: 'enrollment.student_id' }, - joinType: 'INNER' + groupByFields: null, + hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }) }); test('Parse SQL Query with INNER JOIN and WHERE Clause', async () => { const query = 'SELECT student.name, enrollment.course FROM student INNER JOIN enrollment ON student.id = enrollment.student_id WHERE student.age > 20'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', whereClauses: [{ field: 'student.age', operator: '>', value: '20' }], joinTable: 'enrollment', + joinType: "INNER", joinCondition: { left: 'student.id', right: 'enrollment.student_id' }, - joinType: 'INNER' + groupByFields: null, + hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }) }); diff --git a/tests/step-10/index.test.js b/tests/step-10/index.test.js index 5e118eda5..23a09e264 100644 --- a/tests/step-10/index.test.js +++ b/tests/step-10/index.test.js @@ -1,6 +1,6 @@ -const readCSV = require('../../src/csvReader'); -const {parseQuery, parseJoinClause} = require('../../src/queryParser'); -const executeSELECTQuery = require('../../src/index'); +const {readCSV} = require('../../src/csvReader'); +const {parseSelectQuery, parseJoinClause} = require('../../src/queryParser'); +const {executeSELECTQuery} = require('../../src/queryExecutor'); test('Read CSV File', async () => { const data = await readCSV('./student.csv'); @@ -258,7 +258,7 @@ test('Average age of students above a certain age', async () => { test('Parse SQL Query', () => { const query = 'SELECT id, name FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -268,12 +268,13 @@ test('Parse SQL Query', () => { joinType: null, groupByFields: null, hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse SQL Query with WHERE Clause', () => { const query = 'SELECT id, name FROM student WHERE age = 25'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -287,12 +288,13 @@ test('Parse SQL Query with WHERE Clause', () => { joinType: null, groupByFields: null, hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse SQL Query with Multiple WHERE Clauses', () => { const query = 'SELECT id, name FROM student WHERE age = 30 AND name = John'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -310,12 +312,13 @@ test('Parse SQL Query with Multiple WHERE Clauses', () => { joinType: null, groupByFields: null, hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse SQL Query with INNER JOIN', async () => { const query = 'SELECT student.name, enrollment.course FROM student INNER JOIN enrollment ON student.id=enrollment.student_id'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -325,12 +328,13 @@ test('Parse SQL Query with INNER JOIN', async () => { joinCondition: { left: 'student.id', right: 'enrollment.student_id' }, groupByFields: null, hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }) }); test('Parse SQL Query with INNER JOIN and WHERE Clause', async () => { const query = 'SELECT student.name, enrollment.course FROM student INNER JOIN enrollment ON student.id = enrollment.student_id WHERE student.age > 20'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -340,6 +344,7 @@ test('Parse SQL Query with INNER JOIN and WHERE Clause', async () => { joinCondition: { left: 'student.id', right: 'enrollment.student_id' }, groupByFields: null, hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }) }); @@ -387,7 +392,7 @@ test('Returns null for queries without JOIN', () => { test('Parse LEFT Join Query Completely', () => { const query = 'SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id'; - const result = parseQuery(query); + const result = parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -397,12 +402,13 @@ test('Parse LEFT Join Query Completely', () => { joinCondition: { left: 'student.id', right: 'enrollment.student_id' }, groupByFields: null, hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }) }) test('Parse LEFT Join Query Completely', () => { const query = 'SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id'; - const result = parseQuery(query); + const result = parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -412,12 +418,13 @@ test('Parse LEFT Join Query Completely', () => { joinCondition: { left: 'student.id', right: 'enrollment.student_id' }, groupByFields: null, hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }) }) test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the main table', async () => { const query = 'SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id WHERE student.age > 22'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -427,12 +434,13 @@ test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the main tabl "whereClauses": [{ "field": "student.age", "operator": ">", "value": "22" }], groupByFields: null, hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the join table', async () => { const query = `SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id WHERE enrollment.course = 'Physics'`; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -442,12 +450,13 @@ test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the join tabl "whereClauses": [{ "field": "enrollment.course", "operator": "=", "value": "'Physics'" }], groupByFields: null, hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the main table', async () => { const query = 'SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id WHERE student.age < 25'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -457,12 +466,13 @@ test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the main tab "whereClauses": [{ "field": "student.age", "operator": "<", "value": "25" }], groupByFields: null, hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the join table', async () => { const query = `SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id WHERE enrollment.course = 'Chemistry'`; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -472,13 +482,14 @@ test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the join tab "whereClauses": [{ "field": "enrollment.course", "operator": "=", "value": "'Chemistry'" }], groupByFields: null, hasAggregateWithoutGroupBy: false, + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse COUNT Aggregate Query', () => { const query = 'SELECT COUNT(*) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['COUNT(*)'], table: 'student', @@ -488,13 +499,14 @@ test('Parse COUNT Aggregate Query', () => { "joinCondition": null, "joinTable": null, "joinType": null, + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse SUM Aggregate Query', () => { const query = 'SELECT SUM(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['SUM(age)'], table: 'student', @@ -504,12 +516,13 @@ test('Parse SUM Aggregate Query', () => { "joinCondition": null, "joinTable": null, "joinType": null, + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse AVG Aggregate Query', () => { const query = 'SELECT AVG(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['AVG(age)'], table: 'student', @@ -519,12 +532,13 @@ test('Parse AVG Aggregate Query', () => { "joinCondition": null, "joinTable": null, "joinType": null, + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse MIN Aggregate Query', () => { const query = 'SELECT MIN(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['MIN(age)'], table: 'student', @@ -534,12 +548,13 @@ test('Parse MIN Aggregate Query', () => { "joinCondition": null, "joinTable": null, "joinType": null, + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse MAX Aggregate Query', () => { const query = 'SELECT MAX(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['MAX(age)'], table: 'student', @@ -549,12 +564,13 @@ test('Parse MAX Aggregate Query', () => { "joinCondition": null, "joinTable": null, "joinType": null, + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse basic GROUP BY query', () => { const query = 'SELECT age, COUNT(*) FROM student GROUP BY age'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['age', 'COUNT(*)'], table: 'student', @@ -563,13 +579,14 @@ test('Parse basic GROUP BY query', () => { joinType: null, joinTable: null, joinCondition: null, - hasAggregateWithoutGroupBy: false + hasAggregateWithoutGroupBy: false, + orderByFields: null,"limit": null,isDistinct: false, }); }); test('Parse GROUP BY query with WHERE clause', () => { const query = 'SELECT age, COUNT(*) FROM student WHERE age > 22 GROUP BY age'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['age', 'COUNT(*)'], table: 'student', @@ -578,13 +595,14 @@ test('Parse GROUP BY query with WHERE clause', () => { joinType: null, joinTable: null, joinCondition: null, - hasAggregateWithoutGroupBy: false + hasAggregateWithoutGroupBy: false, + orderByFields: null,"limit": null,isDistinct: false, }); }); test('Parse GROUP BY query with multiple fields', () => { const query = 'SELECT student_id, course, COUNT(*) FROM enrollment GROUP BY student_id, course'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['student_id', 'course', 'COUNT(*)'], table: 'enrollment', @@ -593,13 +611,14 @@ test('Parse GROUP BY query with multiple fields', () => { joinType: null, joinTable: null, joinCondition: null, - hasAggregateWithoutGroupBy: false + hasAggregateWithoutGroupBy: false, + orderByFields: null,"limit": null,isDistinct: false, }); }); test('Parse GROUP BY query with JOIN and WHERE clauses', () => { const query = 'SELECT student.name, COUNT(*) FROM student INNER JOIN enrollment ON student.id = enrollment.student_id WHERE enrollment.course = "Mathematics" GROUP BY student.name'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['student.name', 'COUNT(*)'], table: 'student', @@ -611,6 +630,7 @@ test('Parse GROUP BY query with JOIN and WHERE clauses', () => { left: 'student.id', right: 'enrollment.student_id' }, - hasAggregateWithoutGroupBy: false + hasAggregateWithoutGroupBy: false, + orderByFields: null,"limit": null,isDistinct: false, }); }); \ No newline at end of file diff --git a/tests/step-11/index.test.js b/tests/step-11/index.test.js index 1cf5f2def..3d68176b4 100644 --- a/tests/step-11/index.test.js +++ b/tests/step-11/index.test.js @@ -1,6 +1,6 @@ -const readCSV = require('../../src/csvReader'); -const {parseQuery, parseJoinClause} = require('../../src/queryParser'); -const executeSELECTQuery = require('../../src/index'); +const {readCSV} = require('../../src/csvReader'); +const {parseSelectQuery, parseJoinClause} = require('../../src/queryParser'); +const {executeSELECTQuery} = require('../../src/queryExecutor'); test('Read CSV File', async () => { const data = await readCSV('./student.csv'); @@ -258,7 +258,7 @@ test('Average age of students above a certain age', async () => { test('Parse SQL Query', () => { const query = 'SELECT id, name FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -268,13 +268,14 @@ test('Parse SQL Query', () => { joinType: null, groupByFields: null, hasAggregateWithoutGroupBy: false, - "orderByFields": null + "orderByFields": null, + "limit": null,isDistinct: false, }); }); test('Parse SQL Query with WHERE Clause', () => { const query = 'SELECT id, name FROM student WHERE age = 25'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -288,13 +289,14 @@ test('Parse SQL Query with WHERE Clause', () => { joinType: null, groupByFields: null, hasAggregateWithoutGroupBy: false, - "orderByFields": null + "orderByFields": null, + "limit": null,isDistinct: false, }); }); test('Parse SQL Query with Multiple WHERE Clauses', () => { const query = 'SELECT id, name FROM student WHERE age = 30 AND name = John'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -312,13 +314,14 @@ test('Parse SQL Query with Multiple WHERE Clauses', () => { joinType: null, groupByFields: null, hasAggregateWithoutGroupBy: false, - "orderByFields": null + "orderByFields": null, + "limit": null,isDistinct: false, }); }); test('Parse SQL Query with INNER JOIN', async () => { const query = 'SELECT student.name, enrollment.course FROM student INNER JOIN enrollment ON student.id=enrollment.student_id'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -328,13 +331,14 @@ test('Parse SQL Query with INNER JOIN', async () => { joinCondition: { left: 'student.id', right: 'enrollment.student_id' }, groupByFields: null, hasAggregateWithoutGroupBy: false, - "orderByFields": null + "orderByFields": null, + "limit": null,isDistinct: false, }) }); test('Parse SQL Query with INNER JOIN and WHERE Clause', async () => { const query = 'SELECT student.name, enrollment.course FROM student INNER JOIN enrollment ON student.id = enrollment.student_id WHERE student.age > 20'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -344,7 +348,8 @@ test('Parse SQL Query with INNER JOIN and WHERE Clause', async () => { joinCondition: { left: 'student.id', right: 'enrollment.student_id' }, groupByFields: null, hasAggregateWithoutGroupBy: false, - "orderByFields": null + "orderByFields": null, + "limit": null,isDistinct: false, }) }); @@ -392,7 +397,7 @@ test('Returns null for queries without JOIN', () => { test('Parse LEFT Join Query Completely', () => { const query = 'SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id'; - const result = parseQuery(query); + const result = parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -402,13 +407,14 @@ test('Parse LEFT Join Query Completely', () => { joinCondition: { left: 'student.id', right: 'enrollment.student_id' }, groupByFields: null, hasAggregateWithoutGroupBy: false, - "orderByFields": null + "orderByFields": null, + "limit": null,isDistinct: false, }) }) test('Parse LEFT Join Query Completely', () => { const query = 'SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id'; - const result = parseQuery(query); + const result = parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -418,13 +424,13 @@ test('Parse LEFT Join Query Completely', () => { joinCondition: { left: 'student.id', right: 'enrollment.student_id' }, groupByFields: null, hasAggregateWithoutGroupBy: false, - "orderByFields": null + "orderByFields": null,"limit": null,isDistinct: false, }) }) test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the main table', async () => { const query = 'SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id WHERE student.age > 22'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -434,13 +440,13 @@ test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the main tabl "whereClauses": [{ "field": "student.age", "operator": ">", "value": "22" }], groupByFields: null, hasAggregateWithoutGroupBy: false, - "orderByFields": null + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the join table', async () => { const query = `SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id WHERE enrollment.course = 'Physics'`; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -450,13 +456,13 @@ test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the join tabl "whereClauses": [{ "field": "enrollment.course", "operator": "=", "value": "'Physics'" }], groupByFields: null, hasAggregateWithoutGroupBy: false, - "orderByFields": null + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the main table', async () => { const query = 'SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id WHERE student.age < 25'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -466,13 +472,13 @@ test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the main tab "whereClauses": [{ "field": "student.age", "operator": "<", "value": "25" }], groupByFields: null, hasAggregateWithoutGroupBy: false, - "orderByFields": null + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the join table', async () => { const query = `SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id WHERE enrollment.course = 'Chemistry'`; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -482,14 +488,14 @@ test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the join tab "whereClauses": [{ "field": "enrollment.course", "operator": "=", "value": "'Chemistry'" }], groupByFields: null, hasAggregateWithoutGroupBy: false, - "orderByFields": null + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse COUNT Aggregate Query', () => { const query = 'SELECT COUNT(*) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['COUNT(*)'], table: 'student', @@ -499,14 +505,14 @@ test('Parse COUNT Aggregate Query', () => { "joinCondition": null, "joinTable": null, "joinType": null, - "orderByFields": null + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse SUM Aggregate Query', () => { const query = 'SELECT SUM(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['SUM(age)'], table: 'student', @@ -516,13 +522,13 @@ test('Parse SUM Aggregate Query', () => { "joinCondition": null, "joinTable": null, "joinType": null, - "orderByFields": null + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse AVG Aggregate Query', () => { const query = 'SELECT AVG(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['AVG(age)'], table: 'student', @@ -532,13 +538,13 @@ test('Parse AVG Aggregate Query', () => { "joinCondition": null, "joinTable": null, "joinType": null, - "orderByFields": null + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse MIN Aggregate Query', () => { const query = 'SELECT MIN(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['MIN(age)'], table: 'student', @@ -548,13 +554,13 @@ test('Parse MIN Aggregate Query', () => { "joinCondition": null, "joinTable": null, "joinType": null, - "orderByFields": null + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse MAX Aggregate Query', () => { const query = 'SELECT MAX(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['MAX(age)'], table: 'student', @@ -564,13 +570,13 @@ test('Parse MAX Aggregate Query', () => { "joinCondition": null, "joinTable": null, "joinType": null, - "orderByFields": null + "orderByFields": null,"limit": null,isDistinct: false, }); }); test('Parse basic GROUP BY query', () => { const query = 'SELECT age, COUNT(*) FROM student GROUP BY age'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['age', 'COUNT(*)'], table: 'student', @@ -580,13 +586,13 @@ test('Parse basic GROUP BY query', () => { joinTable: null, joinCondition: null, hasAggregateWithoutGroupBy: false, - orderByFields: null + orderByFields: null,"limit": null,isDistinct: false, }); }); test('Parse GROUP BY query with WHERE clause', () => { const query = 'SELECT age, COUNT(*) FROM student WHERE age > 22 GROUP BY age'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['age', 'COUNT(*)'], table: 'student', @@ -596,13 +602,13 @@ test('Parse GROUP BY query with WHERE clause', () => { joinTable: null, joinCondition: null, hasAggregateWithoutGroupBy: false, - orderByFields: null + orderByFields: null,"limit": null,isDistinct: false, }); }); test('Parse GROUP BY query with multiple fields', () => { const query = 'SELECT student_id, course, COUNT(*) FROM enrollment GROUP BY student_id, course'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['student_id', 'course', 'COUNT(*)'], table: 'enrollment', @@ -612,13 +618,13 @@ test('Parse GROUP BY query with multiple fields', () => { joinTable: null, joinCondition: null, hasAggregateWithoutGroupBy: false, - orderByFields: null + orderByFields: null,"limit": null,isDistinct: false, }); }); test('Parse GROUP BY query with JOIN and WHERE clauses', () => { const query = 'SELECT student.name, COUNT(*) FROM student INNER JOIN enrollment ON student.id = enrollment.student_id WHERE enrollment.course = "Mathematics" GROUP BY student.name'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['student.name', 'COUNT(*)'], table: 'student', @@ -631,7 +637,7 @@ test('Parse GROUP BY query with JOIN and WHERE clauses', () => { right: 'enrollment.student_id' }, hasAggregateWithoutGroupBy: false, - orderByFields: null + orderByFields: null,"limit": null,isDistinct: false, }); }); diff --git a/tests/step-12/index.test.js b/tests/step-12/index.test.js index d15c77ef5..7956c15a2 100644 --- a/tests/step-12/index.test.js +++ b/tests/step-12/index.test.js @@ -1,6 +1,6 @@ -const readCSV = require('../../src/csvReader'); -const {parseQuery, parseJoinClause} = require('../../src/queryParser'); -const executeSELECTQuery = require('../../src/index'); +const {readCSV} = require('../../src/csvReader'); +const {parseSelectQuery, parseJoinClause} = require('../../src/queryParser'); +const {executeSELECTQuery} = require('../../src/queryExecutor'); test('Read CSV File', async () => { const data = await readCSV('./student.csv'); @@ -258,7 +258,7 @@ test('Average age of students above a certain age', async () => { test('Parse SQL Query', () => { const query = 'SELECT id, name FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -269,13 +269,13 @@ test('Parse SQL Query', () => { groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse SQL Query with WHERE Clause', () => { const query = 'SELECT id, name FROM student WHERE age = 25'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -290,13 +290,13 @@ test('Parse SQL Query with WHERE Clause', () => { groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse SQL Query with Multiple WHERE Clauses', () => { const query = 'SELECT id, name FROM student WHERE age = 30 AND name = John'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -315,13 +315,13 @@ test('Parse SQL Query with Multiple WHERE Clauses', () => { groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse SQL Query with INNER JOIN', async () => { const query = 'SELECT student.name, enrollment.course FROM student INNER JOIN enrollment ON student.id=enrollment.student_id'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -332,13 +332,13 @@ test('Parse SQL Query with INNER JOIN', async () => { groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }) }); test('Parse SQL Query with INNER JOIN and WHERE Clause', async () => { const query = 'SELECT student.name, enrollment.course FROM student INNER JOIN enrollment ON student.id = enrollment.student_id WHERE student.age > 20'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -349,7 +349,7 @@ test('Parse SQL Query with INNER JOIN and WHERE Clause', async () => { groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }) }); @@ -397,7 +397,7 @@ test('Returns null for queries without JOIN', () => { test('Parse LEFT Join Query Completely', () => { const query = 'SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id'; - const result = parseQuery(query); + const result = parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -408,13 +408,13 @@ test('Parse LEFT Join Query Completely', () => { groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }) }) test('Parse LEFT Join Query Completely', () => { const query = 'SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id'; - const result = parseQuery(query); + const result = parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -425,13 +425,13 @@ test('Parse LEFT Join Query Completely', () => { groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }) }) test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the main table', async () => { const query = 'SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id WHERE student.age > 22'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -442,13 +442,13 @@ test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the main tabl groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the join table', async () => { const query = `SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id WHERE enrollment.course = 'Physics'`; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -459,13 +459,13 @@ test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the join tabl groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the main table', async () => { const query = 'SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id WHERE student.age < 25'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -476,13 +476,13 @@ test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the main tab groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the join table', async () => { const query = `SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id WHERE enrollment.course = 'Chemistry'`; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -493,14 +493,14 @@ test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the join tab groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse COUNT Aggregate Query', () => { const query = 'SELECT COUNT(*) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['COUNT(*)'], table: 'student', @@ -511,14 +511,14 @@ test('Parse COUNT Aggregate Query', () => { "joinTable": null, "joinType": null, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse SUM Aggregate Query', () => { const query = 'SELECT SUM(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['SUM(age)'], table: 'student', @@ -529,13 +529,13 @@ test('Parse SUM Aggregate Query', () => { "joinTable": null, "joinType": null, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse AVG Aggregate Query', () => { const query = 'SELECT AVG(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['AVG(age)'], table: 'student', @@ -546,13 +546,13 @@ test('Parse AVG Aggregate Query', () => { "joinTable": null, "joinType": null, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse MIN Aggregate Query', () => { const query = 'SELECT MIN(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['MIN(age)'], table: 'student', @@ -563,13 +563,13 @@ test('Parse MIN Aggregate Query', () => { "joinTable": null, "joinType": null, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse MAX Aggregate Query', () => { const query = 'SELECT MAX(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['MAX(age)'], table: 'student', @@ -580,13 +580,13 @@ test('Parse MAX Aggregate Query', () => { "joinTable": null, "joinType": null, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse basic GROUP BY query', () => { const query = 'SELECT age, COUNT(*) FROM student GROUP BY age'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['age', 'COUNT(*)'], table: 'student', @@ -597,13 +597,13 @@ test('Parse basic GROUP BY query', () => { joinCondition: null, hasAggregateWithoutGroupBy: false, orderByFields: null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse GROUP BY query with WHERE clause', () => { const query = 'SELECT age, COUNT(*) FROM student WHERE age > 22 GROUP BY age'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['age', 'COUNT(*)'], table: 'student', @@ -614,13 +614,13 @@ test('Parse GROUP BY query with WHERE clause', () => { joinCondition: null, hasAggregateWithoutGroupBy: false, orderByFields: null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse GROUP BY query with multiple fields', () => { const query = 'SELECT student_id, course, COUNT(*) FROM enrollment GROUP BY student_id, course'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['student_id', 'course', 'COUNT(*)'], table: 'enrollment', @@ -631,13 +631,13 @@ test('Parse GROUP BY query with multiple fields', () => { joinCondition: null, hasAggregateWithoutGroupBy: false, orderByFields: null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse GROUP BY query with JOIN and WHERE clauses', () => { const query = 'SELECT student.name, COUNT(*) FROM student INNER JOIN enrollment ON student.id = enrollment.student_id WHERE enrollment.course = "Mathematics" GROUP BY student.name'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['student.name', 'COUNT(*)'], table: 'student', @@ -651,7 +651,7 @@ test('Parse GROUP BY query with JOIN and WHERE clauses', () => { }, hasAggregateWithoutGroupBy: false, orderByFields: null, - "limit": null, + "limit": null,isDistinct: false, }); }); diff --git a/tests/step-13/index.test.js b/tests/step-13/index.test.js index 0797faaba..5aa323540 100644 --- a/tests/step-13/index.test.js +++ b/tests/step-13/index.test.js @@ -1,6 +1,6 @@ -const readCSV = require('../../src/csvReader'); -const {parseQuery, parseJoinClause} = require('../../src/queryParser'); -const executeSELECTQuery = require('../../src/index'); +const {readCSV} = require('../../src/csvReader'); +const {parseSelectQuery, parseJoinClause} = require('../../src/queryParser'); +const {executeSELECTQuery} = require('../../src/queryExecutor'); test('Read CSV File', async () => { const data = await readCSV('./student.csv'); @@ -258,7 +258,7 @@ test('Average age of students above a certain age', async () => { test('Parse SQL Query', () => { const query = 'SELECT id, name FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -269,13 +269,13 @@ test('Parse SQL Query', () => { groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse SQL Query with WHERE Clause', () => { const query = 'SELECT id, name FROM student WHERE age = 25'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -290,13 +290,13 @@ test('Parse SQL Query with WHERE Clause', () => { groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse SQL Query with Multiple WHERE Clauses', () => { const query = 'SELECT id, name FROM student WHERE age = 30 AND name = John'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -315,13 +315,13 @@ test('Parse SQL Query with Multiple WHERE Clauses', () => { groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse SQL Query with INNER JOIN', async () => { const query = 'SELECT student.name, enrollment.course FROM student INNER JOIN enrollment ON student.id=enrollment.student_id'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -332,13 +332,13 @@ test('Parse SQL Query with INNER JOIN', async () => { groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }) }); test('Parse SQL Query with INNER JOIN and WHERE Clause', async () => { const query = 'SELECT student.name, enrollment.course FROM student INNER JOIN enrollment ON student.id = enrollment.student_id WHERE student.age > 20'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -349,7 +349,7 @@ test('Parse SQL Query with INNER JOIN and WHERE Clause', async () => { groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }) }); @@ -397,7 +397,7 @@ test('Returns null for queries without JOIN', () => { test('Parse LEFT Join Query Completely', () => { const query = 'SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id'; - const result = parseQuery(query); + const result = parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -408,13 +408,13 @@ test('Parse LEFT Join Query Completely', () => { groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }) }) test('Parse LEFT Join Query Completely', () => { const query = 'SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id'; - const result = parseQuery(query); + const result = parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -425,13 +425,13 @@ test('Parse LEFT Join Query Completely', () => { groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }) }) test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the main table', async () => { const query = 'SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id WHERE student.age > 22'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -442,13 +442,13 @@ test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the main tabl groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the join table', async () => { const query = `SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id WHERE enrollment.course = 'Physics'`; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -459,13 +459,13 @@ test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the join tabl groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the main table', async () => { const query = 'SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id WHERE student.age < 25'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -476,13 +476,13 @@ test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the main tab groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the join table', async () => { const query = `SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id WHERE enrollment.course = 'Chemistry'`; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -493,14 +493,14 @@ test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the join tab groupByFields: null, hasAggregateWithoutGroupBy: false, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse COUNT Aggregate Query', () => { const query = 'SELECT COUNT(*) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['COUNT(*)'], table: 'student', @@ -511,14 +511,14 @@ test('Parse COUNT Aggregate Query', () => { "joinTable": null, "joinType": null, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse SUM Aggregate Query', () => { const query = 'SELECT SUM(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['SUM(age)'], table: 'student', @@ -529,13 +529,13 @@ test('Parse SUM Aggregate Query', () => { "joinTable": null, "joinType": null, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse AVG Aggregate Query', () => { const query = 'SELECT AVG(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['AVG(age)'], table: 'student', @@ -546,13 +546,13 @@ test('Parse AVG Aggregate Query', () => { "joinTable": null, "joinType": null, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse MIN Aggregate Query', () => { const query = 'SELECT MIN(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['MIN(age)'], table: 'student', @@ -563,13 +563,13 @@ test('Parse MIN Aggregate Query', () => { "joinTable": null, "joinType": null, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse MAX Aggregate Query', () => { const query = 'SELECT MAX(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['MAX(age)'], table: 'student', @@ -580,13 +580,13 @@ test('Parse MAX Aggregate Query', () => { "joinTable": null, "joinType": null, "orderByFields": null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse basic GROUP BY query', () => { const query = 'SELECT age, COUNT(*) FROM student GROUP BY age'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['age', 'COUNT(*)'], table: 'student', @@ -597,13 +597,13 @@ test('Parse basic GROUP BY query', () => { joinCondition: null, hasAggregateWithoutGroupBy: false, orderByFields: null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse GROUP BY query with WHERE clause', () => { const query = 'SELECT age, COUNT(*) FROM student WHERE age > 22 GROUP BY age'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['age', 'COUNT(*)'], table: 'student', @@ -614,13 +614,13 @@ test('Parse GROUP BY query with WHERE clause', () => { joinCondition: null, hasAggregateWithoutGroupBy: false, orderByFields: null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse GROUP BY query with multiple fields', () => { const query = 'SELECT student_id, course, COUNT(*) FROM enrollment GROUP BY student_id, course'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['student_id', 'course', 'COUNT(*)'], table: 'enrollment', @@ -631,13 +631,13 @@ test('Parse GROUP BY query with multiple fields', () => { joinCondition: null, hasAggregateWithoutGroupBy: false, orderByFields: null, - "limit": null + "limit": null,isDistinct: false, }); }); test('Parse GROUP BY query with JOIN and WHERE clauses', () => { const query = 'SELECT student.name, COUNT(*) FROM student INNER JOIN enrollment ON student.id = enrollment.student_id WHERE enrollment.course = "Mathematics" GROUP BY student.name'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['student.name', 'COUNT(*)'], table: 'student', @@ -651,7 +651,7 @@ test('Parse GROUP BY query with JOIN and WHERE clauses', () => { }, hasAggregateWithoutGroupBy: false, orderByFields: null, - "limit": null, + "limit": null,isDistinct: false, }); }); diff --git a/tests/step-14/index.test.js b/tests/step-14/index.test.js index 502411fa7..218f60f31 100644 --- a/tests/step-14/index.test.js +++ b/tests/step-14/index.test.js @@ -1,6 +1,6 @@ -const readCSV = require('../../src/csvReader'); -const {parseQuery, parseJoinClause} = require('../../src/queryParser'); -const executeSELECTQuery = require('../../src/index'); +const {readCSV} = require('../../src/csvReader'); +const {parseSelectQuery, parseJoinClause} = require('../../src/queryParser'); +const {executeSELECTQuery} = require('../../src/queryExecutor'); test('Read CSV File', async () => { const data = await readCSV('./student.csv'); @@ -258,7 +258,7 @@ test('Average age of students above a certain age', async () => { test('Parse SQL Query', () => { const query = 'SELECT id, name FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -276,7 +276,7 @@ test('Parse SQL Query', () => { test('Parse SQL Query with WHERE Clause', () => { const query = 'SELECT id, name FROM student WHERE age = 25'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -298,7 +298,7 @@ test('Parse SQL Query with WHERE Clause', () => { test('Parse SQL Query with Multiple WHERE Clauses', () => { const query = 'SELECT id, name FROM student WHERE age = 30 AND name = John'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -324,7 +324,7 @@ test('Parse SQL Query with Multiple WHERE Clauses', () => { test('Parse SQL Query with INNER JOIN', async () => { const query = 'SELECT student.name, enrollment.course FROM student INNER JOIN enrollment ON student.id=enrollment.student_id'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -342,7 +342,7 @@ test('Parse SQL Query with INNER JOIN', async () => { test('Parse SQL Query with INNER JOIN and WHERE Clause', async () => { const query = 'SELECT student.name, enrollment.course FROM student INNER JOIN enrollment ON student.id = enrollment.student_id WHERE student.age > 20'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -402,7 +402,7 @@ test('Returns null for queries without JOIN', () => { test('Parse LEFT Join Query Completely', () => { const query = 'SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id'; - const result = parseQuery(query); + const result = parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -420,7 +420,7 @@ test('Parse LEFT Join Query Completely', () => { test('Parse LEFT Join Query Completely', () => { const query = 'SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id'; - const result = parseQuery(query); + const result = parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -438,7 +438,7 @@ test('Parse LEFT Join Query Completely', () => { test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the main table', async () => { const query = 'SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id WHERE student.age > 22'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -456,7 +456,7 @@ test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the main tabl test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the join table', async () => { const query = `SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id WHERE enrollment.course = 'Physics'`; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -474,7 +474,7 @@ test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the join tabl test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the main table', async () => { const query = 'SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id WHERE student.age < 25'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -492,7 +492,7 @@ test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the main tab test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the join table', async () => { const query = `SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id WHERE enrollment.course = 'Chemistry'`; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -511,7 +511,7 @@ test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the join tab test('Parse COUNT Aggregate Query', () => { const query = 'SELECT COUNT(*) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['COUNT(*)'], table: 'student', @@ -530,7 +530,7 @@ test('Parse COUNT Aggregate Query', () => { test('Parse SUM Aggregate Query', () => { const query = 'SELECT SUM(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['SUM(age)'], table: 'student', @@ -548,7 +548,7 @@ test('Parse SUM Aggregate Query', () => { test('Parse AVG Aggregate Query', () => { const query = 'SELECT AVG(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['AVG(age)'], table: 'student', @@ -566,7 +566,7 @@ test('Parse AVG Aggregate Query', () => { test('Parse MIN Aggregate Query', () => { const query = 'SELECT MIN(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['MIN(age)'], table: 'student', @@ -584,7 +584,7 @@ test('Parse MIN Aggregate Query', () => { test('Parse MAX Aggregate Query', () => { const query = 'SELECT MAX(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['MAX(age)'], table: 'student', @@ -602,7 +602,7 @@ test('Parse MAX Aggregate Query', () => { test('Parse basic GROUP BY query', () => { const query = 'SELECT age, COUNT(*) FROM student GROUP BY age'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['age', 'COUNT(*)'], table: 'student', @@ -620,7 +620,7 @@ test('Parse basic GROUP BY query', () => { test('Parse GROUP BY query with WHERE clause', () => { const query = 'SELECT age, COUNT(*) FROM student WHERE age > 22 GROUP BY age'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['age', 'COUNT(*)'], table: 'student', @@ -638,7 +638,7 @@ test('Parse GROUP BY query with WHERE clause', () => { test('Parse GROUP BY query with multiple fields', () => { const query = 'SELECT student_id, course, COUNT(*) FROM enrollment GROUP BY student_id, course'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['student_id', 'course', 'COUNT(*)'], table: 'enrollment', @@ -656,7 +656,7 @@ test('Parse GROUP BY query with multiple fields', () => { test('Parse GROUP BY query with JOIN and WHERE clauses', () => { const query = 'SELECT student.name, COUNT(*) FROM student INNER JOIN enrollment ON student.id = enrollment.student_id WHERE enrollment.course = "Mathematics" GROUP BY student.name'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['student.name', 'COUNT(*)'], table: 'student', diff --git a/tests/step-15/index.test.js b/tests/step-15/index.test.js index a2aa4daee..ae985223f 100644 --- a/tests/step-15/index.test.js +++ b/tests/step-15/index.test.js @@ -1,6 +1,6 @@ -const readCSV = require('../../src/csvReader'); -const {parseQuery, parseJoinClause} = require('../../src/queryParser'); -const executeSELECTQuery = require('../../src/index'); +const {readCSV} = require('../../src/csvReader'); +const {parseSelectQuery, parseJoinClause} = require('../../src/queryParser'); +const {executeSELECTQuery} = require('../../src/queryExecutor'); test('Read CSV File', async () => { const data = await readCSV('./student.csv'); @@ -258,7 +258,7 @@ test('Average age of students above a certain age', async () => { test('Parse SQL Query', () => { const query = 'SELECT id, name FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -276,7 +276,7 @@ test('Parse SQL Query', () => { test('Parse SQL Query with WHERE Clause', () => { const query = 'SELECT id, name FROM student WHERE age = 25'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -298,7 +298,7 @@ test('Parse SQL Query with WHERE Clause', () => { test('Parse SQL Query with Multiple WHERE Clauses', () => { const query = 'SELECT id, name FROM student WHERE age = 30 AND name = John'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -324,7 +324,7 @@ test('Parse SQL Query with Multiple WHERE Clauses', () => { test('Parse SQL Query with INNER JOIN', async () => { const query = 'SELECT student.name, enrollment.course FROM student INNER JOIN enrollment ON student.id=enrollment.student_id'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -342,7 +342,7 @@ test('Parse SQL Query with INNER JOIN', async () => { test('Parse SQL Query with INNER JOIN and WHERE Clause', async () => { const query = 'SELECT student.name, enrollment.course FROM student INNER JOIN enrollment ON student.id = enrollment.student_id WHERE student.age > 20'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -402,7 +402,7 @@ test('Returns null for queries without JOIN', () => { test('Parse LEFT Join Query Completely', () => { const query = 'SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id'; - const result = parseQuery(query); + const result = parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -420,7 +420,7 @@ test('Parse LEFT Join Query Completely', () => { test('Parse LEFT Join Query Completely', () => { const query = 'SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id'; - const result = parseQuery(query); + const result = parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -438,7 +438,7 @@ test('Parse LEFT Join Query Completely', () => { test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the main table', async () => { const query = 'SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id WHERE student.age > 22'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -456,7 +456,7 @@ test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the main tabl test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the join table', async () => { const query = `SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id WHERE enrollment.course = 'Physics'`; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -474,7 +474,7 @@ test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the join tabl test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the main table', async () => { const query = 'SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id WHERE student.age < 25'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -492,7 +492,7 @@ test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the main tab test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the join table', async () => { const query = `SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id WHERE enrollment.course = 'Chemistry'`; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -511,7 +511,7 @@ test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the join tab test('Parse COUNT Aggregate Query', () => { const query = 'SELECT COUNT(*) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['COUNT(*)'], table: 'student', @@ -530,7 +530,7 @@ test('Parse COUNT Aggregate Query', () => { test('Parse SUM Aggregate Query', () => { const query = 'SELECT SUM(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['SUM(age)'], table: 'student', @@ -548,7 +548,7 @@ test('Parse SUM Aggregate Query', () => { test('Parse AVG Aggregate Query', () => { const query = 'SELECT AVG(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['AVG(age)'], table: 'student', @@ -566,7 +566,7 @@ test('Parse AVG Aggregate Query', () => { test('Parse MIN Aggregate Query', () => { const query = 'SELECT MIN(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['MIN(age)'], table: 'student', @@ -584,7 +584,7 @@ test('Parse MIN Aggregate Query', () => { test('Parse MAX Aggregate Query', () => { const query = 'SELECT MAX(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['MAX(age)'], table: 'student', @@ -602,7 +602,7 @@ test('Parse MAX Aggregate Query', () => { test('Parse basic GROUP BY query', () => { const query = 'SELECT age, COUNT(*) FROM student GROUP BY age'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['age', 'COUNT(*)'], table: 'student', @@ -620,7 +620,7 @@ test('Parse basic GROUP BY query', () => { test('Parse GROUP BY query with WHERE clause', () => { const query = 'SELECT age, COUNT(*) FROM student WHERE age > 22 GROUP BY age'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['age', 'COUNT(*)'], table: 'student', @@ -638,7 +638,7 @@ test('Parse GROUP BY query with WHERE clause', () => { test('Parse GROUP BY query with multiple fields', () => { const query = 'SELECT student_id, course, COUNT(*) FROM enrollment GROUP BY student_id, course'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['student_id', 'course', 'COUNT(*)'], table: 'enrollment', @@ -656,7 +656,7 @@ test('Parse GROUP BY query with multiple fields', () => { test('Parse GROUP BY query with JOIN and WHERE clauses', () => { const query = 'SELECT student.name, COUNT(*) FROM student INNER JOIN enrollment ON student.id = enrollment.student_id WHERE enrollment.course = "Mathematics" GROUP BY student.name'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['student.name', 'COUNT(*)'], table: 'student', diff --git a/tests/step-16/index.test.js b/tests/step-16/index.test.js index a2aa4daee..ae985223f 100644 --- a/tests/step-16/index.test.js +++ b/tests/step-16/index.test.js @@ -1,6 +1,6 @@ -const readCSV = require('../../src/csvReader'); -const {parseQuery, parseJoinClause} = require('../../src/queryParser'); -const executeSELECTQuery = require('../../src/index'); +const {readCSV} = require('../../src/csvReader'); +const {parseSelectQuery, parseJoinClause} = require('../../src/queryParser'); +const {executeSELECTQuery} = require('../../src/queryExecutor'); test('Read CSV File', async () => { const data = await readCSV('./student.csv'); @@ -258,7 +258,7 @@ test('Average age of students above a certain age', async () => { test('Parse SQL Query', () => { const query = 'SELECT id, name FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -276,7 +276,7 @@ test('Parse SQL Query', () => { test('Parse SQL Query with WHERE Clause', () => { const query = 'SELECT id, name FROM student WHERE age = 25'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -298,7 +298,7 @@ test('Parse SQL Query with WHERE Clause', () => { test('Parse SQL Query with Multiple WHERE Clauses', () => { const query = 'SELECT id, name FROM student WHERE age = 30 AND name = John'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['id', 'name'], table: 'student', @@ -324,7 +324,7 @@ test('Parse SQL Query with Multiple WHERE Clauses', () => { test('Parse SQL Query with INNER JOIN', async () => { const query = 'SELECT student.name, enrollment.course FROM student INNER JOIN enrollment ON student.id=enrollment.student_id'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -342,7 +342,7 @@ test('Parse SQL Query with INNER JOIN', async () => { test('Parse SQL Query with INNER JOIN and WHERE Clause', async () => { const query = 'SELECT student.name, enrollment.course FROM student INNER JOIN enrollment ON student.id = enrollment.student_id WHERE student.age > 20'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -402,7 +402,7 @@ test('Returns null for queries without JOIN', () => { test('Parse LEFT Join Query Completely', () => { const query = 'SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id'; - const result = parseQuery(query); + const result = parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -420,7 +420,7 @@ test('Parse LEFT Join Query Completely', () => { test('Parse LEFT Join Query Completely', () => { const query = 'SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id'; - const result = parseQuery(query); + const result = parseSelectQuery(query); expect(result).toEqual({ fields: ['student.name', 'enrollment.course'], table: 'student', @@ -438,7 +438,7 @@ test('Parse LEFT Join Query Completely', () => { test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the main table', async () => { const query = 'SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id WHERE student.age > 22'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -456,7 +456,7 @@ test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the main tabl test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the join table', async () => { const query = `SELECT student.name, enrollment.course FROM student LEFT JOIN enrollment ON student.id=enrollment.student_id WHERE enrollment.course = 'Physics'`; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -474,7 +474,7 @@ test('Parse SQL Query with LEFT JOIN with a WHERE clause filtering the join tabl test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the main table', async () => { const query = 'SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id WHERE student.age < 25'; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -492,7 +492,7 @@ test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the main tab test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the join table', async () => { const query = `SELECT student.name, enrollment.course FROM student RIGHT JOIN enrollment ON student.id=enrollment.student_id WHERE enrollment.course = 'Chemistry'`; - const result = await parseQuery(query); + const result = await parseSelectQuery(query); expect(result).toEqual({ "fields": ["student.name", "enrollment.course"], "joinCondition": { "left": "student.id", "right": "enrollment.student_id" }, @@ -511,7 +511,7 @@ test('Parse SQL Query with RIGHT JOIN with a WHERE clause filtering the join tab test('Parse COUNT Aggregate Query', () => { const query = 'SELECT COUNT(*) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['COUNT(*)'], table: 'student', @@ -530,7 +530,7 @@ test('Parse COUNT Aggregate Query', () => { test('Parse SUM Aggregate Query', () => { const query = 'SELECT SUM(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['SUM(age)'], table: 'student', @@ -548,7 +548,7 @@ test('Parse SUM Aggregate Query', () => { test('Parse AVG Aggregate Query', () => { const query = 'SELECT AVG(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['AVG(age)'], table: 'student', @@ -566,7 +566,7 @@ test('Parse AVG Aggregate Query', () => { test('Parse MIN Aggregate Query', () => { const query = 'SELECT MIN(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['MIN(age)'], table: 'student', @@ -584,7 +584,7 @@ test('Parse MIN Aggregate Query', () => { test('Parse MAX Aggregate Query', () => { const query = 'SELECT MAX(age) FROM student'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['MAX(age)'], table: 'student', @@ -602,7 +602,7 @@ test('Parse MAX Aggregate Query', () => { test('Parse basic GROUP BY query', () => { const query = 'SELECT age, COUNT(*) FROM student GROUP BY age'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['age', 'COUNT(*)'], table: 'student', @@ -620,7 +620,7 @@ test('Parse basic GROUP BY query', () => { test('Parse GROUP BY query with WHERE clause', () => { const query = 'SELECT age, COUNT(*) FROM student WHERE age > 22 GROUP BY age'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['age', 'COUNT(*)'], table: 'student', @@ -638,7 +638,7 @@ test('Parse GROUP BY query with WHERE clause', () => { test('Parse GROUP BY query with multiple fields', () => { const query = 'SELECT student_id, course, COUNT(*) FROM enrollment GROUP BY student_id, course'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['student_id', 'course', 'COUNT(*)'], table: 'enrollment', @@ -656,7 +656,7 @@ test('Parse GROUP BY query with multiple fields', () => { test('Parse GROUP BY query with JOIN and WHERE clauses', () => { const query = 'SELECT student.name, COUNT(*) FROM student INNER JOIN enrollment ON student.id = enrollment.student_id WHERE enrollment.course = "Mathematics" GROUP BY student.name'; - const parsed = parseQuery(query); + const parsed = parseSelectQuery(query); expect(parsed).toEqual({ fields: ['student.name', 'COUNT(*)'], table: 'student', diff --git a/tests/step-17/index.test.js b/tests/step-17/index.test.js index c99d01fbb..dc1fa19ae 100644 --- a/tests/step-17/index.test.js +++ b/tests/step-17/index.test.js @@ -1,5 +1,5 @@ const {readCSV} = require('../../src/csvReader'); -const {executeSELECTQuery } = require('../../src/index'); +const {executeSELECTQuery } = require('../../src/queryExecutor'); const { parseJoinClause, parseSelectQuery } = require('../../src/queryParser'); test('Read CSV File', async () => { diff --git a/tests/step-17/insertExecute.test.js b/tests/step-17/insertExecute.test.js new file mode 100644 index 000000000..9672c9fa7 --- /dev/null +++ b/tests/step-17/insertExecute.test.js @@ -0,0 +1,35 @@ +const { executeINSERTQuery } = require('../../src/queryExecutor'); +const { readCSV, writeCSV } = require('../../src/csvReader'); +const fs = require('fs'); + +// Helper function to create grades.csv with initial data +async function createGradesCSV() { + const initialData = [ + { student_id: '1', course: 'Mathematics', grade: 'A' }, + { student_id: '2', course: 'Chemistry', grade: 'B' }, + { student_id: '3', course: 'Mathematics', grade: 'C' } + ]; + await writeCSV('grades.csv', initialData); +} + +// Test to INSERT a new grade and verify +test('Execute INSERT INTO Query for grades.csv', async () => { + // Create grades.csv with initial data + await createGradesCSV(); + + // Execute INSERT statement + const insertQuery = "INSERT INTO grades (student_id, course, grade) VALUES ('4', 'Physics', 'A')"; + await executeINSERTQuery(insertQuery); + + // Verify the new entry + const updatedData = await readCSV('grades.csv'); + console.log(updatedData) + const newEntry = updatedData.find(row => row.student_id === '4' && row.course === 'Physics'); + console.log(updatedData) + expect(newEntry).toBeDefined(); + console.log("----",newEntry.grade) + expect(newEntry.grade).toEqual('A'); + + // Cleanup: Delete grades.csv + fs.unlinkSync('grades.csv'); +}); \ No newline at end of file diff --git a/tests/step-18/deleteExecutor.test.js b/tests/step-18/deleteExecutor.test.js index 11ae617b7..452d39df1 100644 --- a/tests/step-18/deleteExecutor.test.js +++ b/tests/step-18/deleteExecutor.test.js @@ -1,4 +1,4 @@ -const { executeDELETEQuery } = require('../../src/index'); +const { executeDELETEQuery } = require('../../src/queryExecutor'); const { readCSV, writeCSV } = require('../../src/csvReader'); const fs = require('fs'); @@ -21,8 +21,10 @@ test('Execute DELETE FROM Query for courses.csv', async () => { const deleteQuery = "DELETE FROM courses WHERE course_id = '2'"; await executeDELETEQuery(deleteQuery); + // Verify the course was removed const updatedData = await readCSV('courses.csv'); + console.log(updatedData); const deletedCourse = updatedData.find(course => course.course_id === '2'); expect(deletedCourse).toBeUndefined(); diff --git a/tests/step-18/index.test.js b/tests/step-18/index.test.js index c99d01fbb..dc1fa19ae 100644 --- a/tests/step-18/index.test.js +++ b/tests/step-18/index.test.js @@ -1,5 +1,5 @@ const {readCSV} = require('../../src/csvReader'); -const {executeSELECTQuery } = require('../../src/index'); +const {executeSELECTQuery } = require('../../src/queryExecutor'); const { parseJoinClause, parseSelectQuery } = require('../../src/queryParser'); test('Read CSV File', async () => { diff --git a/tests/step-18/insertExecuter.test.js b/tests/step-18/insertExecuter.test.js index 8c405f727..eb511f757 100644 --- a/tests/step-18/insertExecuter.test.js +++ b/tests/step-18/insertExecuter.test.js @@ -1,4 +1,4 @@ -const { executeINSERTQuery } = require('../../src/index'); +const { executeINSERTQuery } = require('../../src/queryExecutor'); const { readCSV, writeCSV } = require('../../src/csvReader'); const fs = require('fs'); @@ -24,7 +24,7 @@ test('Execute INSERT INTO Query for grades.csv', async () => { // Verify the new entry const updatedData = await readCSV('grades.csv'); const newEntry = updatedData.find(row => row.student_id === '4' && row.course === 'Physics'); - console.log(updatedData) + console.log(newEntry) expect(newEntry).toBeDefined(); expect(newEntry.grade).toEqual('A'); diff --git a/tests/step-19/deleteExecutor.test.js b/tests/step-19/deleteExecutor.test.js index 11ae617b7..636403858 100644 --- a/tests/step-19/deleteExecutor.test.js +++ b/tests/step-19/deleteExecutor.test.js @@ -1,4 +1,4 @@ -const { executeDELETEQuery } = require('../../src/index'); +const { executeDELETEQuery } = require('../../src/queryExecutor'); const { readCSV, writeCSV } = require('../../src/csvReader'); const fs = require('fs'); diff --git a/tests/step-19/index.test.js b/tests/step-19/index.test.js index c99d01fbb..dc1fa19ae 100644 --- a/tests/step-19/index.test.js +++ b/tests/step-19/index.test.js @@ -1,5 +1,5 @@ const {readCSV} = require('../../src/csvReader'); -const {executeSELECTQuery } = require('../../src/index'); +const {executeSELECTQuery } = require('../../src/queryExecutor'); const { parseJoinClause, parseSelectQuery } = require('../../src/queryParser'); test('Read CSV File', async () => { diff --git a/tests/step-19/insertExecuter.test.js b/tests/step-19/insertExecuter.test.js index 8c405f727..581d17f73 100644 --- a/tests/step-19/insertExecuter.test.js +++ b/tests/step-19/insertExecuter.test.js @@ -1,4 +1,4 @@ -const { executeINSERTQuery } = require('../../src/index'); +const { executeINSERTQuery } = require('../../src/queryExecutor'); const { readCSV, writeCSV } = require('../../src/csvReader'); const fs = require('fs'); diff --git a/tests/step-20/deleteExecutor.test.js b/tests/step-20/deleteExecutor.test.js index 636403858..0e53a921e 100644 --- a/tests/step-20/deleteExecutor.test.js +++ b/tests/step-20/deleteExecutor.test.js @@ -1,31 +1,31 @@ -const { executeDELETEQuery } = require('../../src/queryExecutor'); -const { readCSV, writeCSV } = require('../../src/csvReader'); -const fs = require('fs'); +const { executeDELETEQuery } = require("../../src/queryExecutor"); +const { readCSV, writeCSV } = require("../../src/csvReader"); +const fs = require("fs"); // Helper function to create courses.csv with initial data async function createCoursesCSV() { - const initialData = [ - { course_id: '1', course_name: 'Mathematics', instructor: 'Dr. Smith' }, - { course_id: '2', course_name: 'Chemistry', instructor: 'Dr. Jones' }, - { course_id: '3', course_name: 'Physics', instructor: 'Dr. Taylor' } - ]; - await writeCSV('courses.csv', initialData); + const initialData = [ + { course_id: "1", course_name: "Mathematics", instructor: "Dr. Smith" }, + { course_id: "2", course_name: "Chemistry", instructor: "Dr. Jones" }, + { course_id: "3", course_name: "Physics", instructor: "Dr. Taylor" }, + ]; + await writeCSV("courses.csv", initialData); } // Test to DELETE a course and verify -test('Execute DELETE FROM Query for courses.csv', async () => { - // Create courses.csv with initial data - await createCoursesCSV(); +test("Execute DELETE FROM Query for courses.csv", async () => { + // Create courses.csv with initial data + await createCoursesCSV(); - // Execute DELETE statement - const deleteQuery = "DELETE FROM courses WHERE course_id = '2'"; - await executeDELETEQuery(deleteQuery); + // Execute DELETE statement + const deleteQuery = "DELETE FROM courses WHERE course_id = '2'"; + await executeDELETEQuery(deleteQuery); - // Verify the course was removed - const updatedData = await readCSV('courses.csv'); - const deletedCourse = updatedData.find(course => course.course_id === '2'); - expect(deletedCourse).toBeUndefined(); + // Verify the course was removed + const updatedData = await readCSV("courses.csv"); + const deletedCourse = updatedData.find((course) => course.course_id === "2"); + expect(deletedCourse).toBeUndefined(); - // Cleanup: Delete courses.csv - fs.unlinkSync('courses.csv'); -}); \ No newline at end of file + // Cleanup: Delete courses.csv + fs.unlinkSync("courses.csv"); +});