Install docker image:
docker pull public.ecr.aws/q2l1y6g6/antdb:latest
Example database commands:
docker run --rm -v "$(pwd):/data" public.ecr.aws/q2l1y6g6/antdb /data/sample.db .dbinfo docker run --rm -v "$(pwd):/data" public.ecr.aws/q2l1y6g6/antdb /data/sample.db "SELECT count(*) FROM oranges" docker run --rm -v "$(pwd):/data" public.ecr.aws/q2l1y6g6/antdb /data/sample.db "SELECT name FROM apples" docker run --rm -v "$(pwd):/data" public.ecr.aws/q2l1y6g6/antdb /data/superheroes.db "SELECT name, first_appearance FROM superheroes WHERE hair_color = 'Brown Hair'" docker run --rm -v "$(pwd):/data" public.ecr.aws/q2l1y6g6/antdb /data/superheroes.db "SELECT count(*) FROM superheroes WHERE eye_color = 'Blue Eyes'" docker run --rm -v "$(pwd):/data" public.ecr.aws/q2l1y6g6/antdb /data/companies.db "SELECT id, name, country FROM companies WHERE country = 'nepal'"Flowchart:
+--------------------------+
| Start Query Evaluation |
+--------------------------+
|
v
+--------------------------+
| Parse SQL Statement |
+--------------------------+
|
v
+----------------------------+
| Is Statement Create Table? |
+----------------------------+
| |
Yes No
| |
v v
+--------------------------+ +----------------------------+
| Throw SQLException: | | Is Statement Create Index? |
| Table Creation Not | +----------------------------+
| Supported | | |
+--------------------------+ Yes No
| |
v v
+--------------------------+
| Throw SQLException: |
| Index Creation Not |
| Supported |
+--------------------------+
|
v
+--------------------------+
| Is Statement Select? |
+--------------------------+
| |
Yes No
| |
v v
+--------------------------+
| Get Table from Database |
+--------------------------+
|
v
+--------------------------+
| Is Filter Present? |
+--------------------------+
| |
Yes No
| |
v v
+----------------------------+
| Get Rows with Filter |
+----------------------------+
|
v
+----------------------------+
| Retrieve All Rows from |
| Table |
+----------------------------+
|
v
+----------------------------+
| Evaluate Columns on Rows |
+----------------------------+
|
v
+----------------------------+
| Return Results |
+----------------------------+
|
v
+---------------+
| End Query |
| Evaluation |
+---------------+
-
Start Query Evaluation:
- This is the initial step where the process begins when a user submits an SQL query for evaluation.
-
Parse SQL Statement:
- The SQL statement is parsed using the
Parserclass. This step converts the raw SQL text into an Abstract Syntax Tree (AST), which represents the structure of the query.
- The SQL statement is parsed using the
-
Check for Create Table Statement:
- The flowchart checks if the parsed statement is a
CREATE TABLEstatement. - Yes: If it is a
CREATE TABLE, an exception is thrown indicating that table creation is not supported. This reflects a limitation in your current implementation. - No: If it is not a
CREATE TABLE, the flow proceeds to check for aCREATE INDEXstatement.
- The flowchart checks if the parsed statement is a
-
Check for Create Index Statement:
- Similar to the previous step, this checks if the statement is a
CREATE INDEX. - Yes: If it is, an exception is thrown indicating that index creation is not supported.
- No: If it is neither, the flow moves on to check if the statement is a
SELECTstatement.
- Similar to the previous step, this checks if the statement is a
-
Check for Select Statement:
- The flow checks if the statement is a
SELECT. - Yes: If it is a
SELECT, the engine retrieves the corresponding table from the database based on the table name specified in the query. - No: If it is not a recognized statement type, an exception can be thrown indicating that it's an unsupported statement.
- The flow checks if the statement is a
-
Check for Filter Presence:
- After retrieving the table, the flow checks whether there is a filter condition (e.g., a WHERE clause) present in the SELECT statement.
- Yes: If there is a filter, it calls a method to get rows that match this filter condition.
- No: If no filter exists, it retrieves all rows from the specified table.
-
Get Rows with Filter:
- This step involves retrieving rows from the table based on the specified filter conditions. It may involve using indices if available to optimize retrieval.
-
Retrieve All Rows from Table:
- If no filter was specified, all rows are retrieved from the table directly.
-
Evaluate Columns on Rows:
- Regardless of whether rows were filtered or retrieved in bulk, this step evaluates the specified columns in the SELECT statement against each row retrieved.
- This involves extracting values based on column names and potentially applying any aggregation functions (like COUNT).
-
Return Results:
- After evaluating all relevant columns and rows, this step returns the results back to the caller.
- The results may be formatted as a list of rows or another suitable structure based on how data needs to be presented.
-
End Query Evaluation:
- The process concludes after returning results, marking the end of query evaluation.
+---------------------+
| Start SQL Query |
+---------------------+
|
v
+---------------------+
| Initialize Scanner |
+---------------------+
|
v
+---------------------+
| Read Next Token |
+---------------------+
|
v
+---------------------+
| Is Token EOF? |
+---------------------+
| |
Yes No
| |
v v
+---------------------+ +---------------------+
| Throw SQLException |<---->| Parse Expression |
+---------------------+ +---------------------+
|
v
+---------------------+
| Is Token a Keyword? |
+---------------------+
| |
Yes No
| |
v v
+---------------------+ +---------------------+
| Handle Keyword |<---->| Handle Identifier |
+---------------------+ +---------------------+
|
v
+---------------------+
| Build AST Node |
+---------------------+
|
v
+------------------------+
| Is Statement Complete? |
+------------------------+
| |
Yes No
| |
v v
+-----------------------+
| Return AST Statement |
+-----------------------+
|
v
+-----------------------+
| Execute AST Statement |
+-----------------------+
|
v
+-----------------------+
| Retrieve Data |
+-----------------------+
|
v
+-----------------------+
| Apply Filters(WHERE)|
+-----------------------+
|
v
+-----------------------+
| Return Results |
+-----------------------+
|
v
+----------------------+
| End |
+----------------------+
- Start SQL Query - The process begins when a user inputs an SQL query.
- Initialize Scanner - A
Scannerinstance is created to tokenize the input string. - Read Next Token - The scanner reads the next token from the input.
- Is Token EOF? - Check if the end of the input has been reached.
- If yes, Throw SQLException.
- If no, go to Parse Expression.
- Parse Expression - The parser processes the token to determine its type.
- Is Token a Keyword? - Check if the token is a recognized SQL keyword.
- If yes, go to Handle Keyword.
- If no, go to Handle Identifier.
- Handle Keyword/Identifier - Handle the token accordingly and build an AST node.
- Build AST Node - Construct an appropriate node in the Abstract Syntax Tree (AST).
- Is Statement Complete? - Check if the entire statement has been parsed.
- If yes, go to Return AST Statement.
- If no, loop back to Read Next Token.
- Return AST Statement - Return the constructed AST statement.
- Execute AST Statement - Execute the statement represented by the AST.
- Retrieve Data - Access the relevant data from storage based on the executed statement.
- Apply Filters (WHERE) - Apply any filtering conditions specified in the query.
- Return Results - Return the results of the query execution to the user.
- End - The process concludes after returning results.
+--------------------------+
| Start Process |
+--------------------------+
|
v
+--------------------------+
| Receive SQL Query |
| (e.g., SELECT, INSERT) |
+--------------------------+
|
v
+--------------------------+
| Parse Query |
| (Syntax & Semantic Check)|
+--------------------------+
|
v
+--------------------------+
| Generate Abstract Syntax|
| Tree (AST) |
+--------------------------+
|
v
+--------------------------+
| Optimize Query Plan |
| (Choose Execution Plan) |
+--------------------------+
|
v
+--------------------------+
| Execute Query |
| |
| +------------------+ |
| | CRUD Ops | |
| +------------------+ |
| | | |
| | Create Data |<--|
| | (INSERT INTO) | |
| +------------------+ |
| | | |
| | Read Data |<--|
| | (SELECT FROM) | |
| +------------------+ |
| | | |
| | Update Data |<--|
| | (UPDATE SET) | |
| +------------------+ |
| | | |
| | Delete Data |<--|
| | (DELETE FROM) | |
| +------------------+ |
+--------------------------+
|
v
+--------------------------+
| Save Changes |
| (Commit Transaction) |
+--------------------------+
|
v
+--------------------------+
| End Process |
+--------------------------+