forked from status-20X/stylusdb-sql-base
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5d110ae
commit 03d9e48
Showing
3 changed files
with
24 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,13 @@ | ||
// src/index.js | ||
|
||
const parseQuery = require('./queryParser'); | ||
const readCSV = require('./csvReader'); | ||
|
||
async function executeSELECTQuery(query) { | ||
const { fields, table, whereClause } = parseQuery(query); | ||
const data = await readCSV(`${table}.csv`); | ||
|
||
// Filtering based on WHERE clause | ||
const filteredData = whereClause | ||
? data.filter(row => { | ||
const [field, value] = whereClause.split('=').map(s => s.trim()); | ||
return row[field] === value; | ||
}) | ||
: data; | ||
|
||
// Selecting the specified fields | ||
return filteredData.map(row => { | ||
const selectedRow = {}; | ||
fields.forEach(field => { | ||
selectedRow[field] = row[field]; | ||
}); | ||
return selectedRow; | ||
}); | ||
} | ||
|
||
module.exports = executeSELECTQuery; | ||
function evaluateCondition(row, clause) { | ||
const { field, operator, value } = clause; | ||
switch (operator) { | ||
case '=': return row[field] === value; | ||
case '!=': return row[field] !== value; | ||
case '>': return row[field] > value; | ||
case '<': return row[field] < value; | ||
case '>=': return row[field] >= value; | ||
case '<=': return row[field] <= value; | ||
default: throw new Error(`Unsupported operator: ${operator}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,12 @@ | ||
// src/queryParser.js | ||
|
||
function parseQuery(query) { | ||
const selectRegex = /SELECT (.+?) FROM (.+?)(?: WHERE (.*))?$/i; | ||
const match = query.match(selectRegex); | ||
|
||
if (match) { | ||
const [, fields, table, whereClause] = match; | ||
return { | ||
fields: fields.split(',').map(field => field.trim()), | ||
table: table.trim(), | ||
whereClause: whereClause ? whereClause.trim() : null | ||
}; | ||
} else { | ||
throw new Error('Invalid query format'); | ||
} | ||
} | ||
|
||
module.exports = parseQuery; | ||
function parseWhereClause(whereString) { | ||
const conditionRegex = /(.*?)(=|!=|>|<|>=|<=)(.*)/; | ||
return whereString.split(/ AND | OR /i).map(conditionString => { | ||
const match = conditionString.match(conditionRegex); | ||
if (match) { | ||
const [, field, operator, value] = match; | ||
return { field: field.trim(), operator, value: value.trim() }; | ||
} | ||
throw new Error('Invalid WHERE clause format'); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters