Skip to content

sonhmai/sql-parser-simplified

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SQL Parsing

Getting Started

cargo test

Structure

src // lib source code with unit tests
tests // integration test

SQLString --Lexer/Tokenizer--> Tokens --Parser--> Statement (AbstractSyntaxTree - AST)

SQL Statement (AST)

  • A statement represents the level that is to be communicated with the relational database.
  • AST is a data structure that represents a SQL statement (INSERT, SELECT, UPDATE, CREATE, BEGIN, etc.).

Expression

  • every node in AST is an expression
  • expression evaluates to value
  • there is only a single set of Expression types for all dialects.

Identifier

  • names of database objects (schema, table, column, etc.)
-- the whole str is a SQL statement (an AST)
SELECT --> SELECT expression
    col1, col2 --> column identifier
FROM --> From expression
    table1 --> Table identifier

Insert statement example

INSERT INTO
    customer
VALUES (1, 2)

-- tokenizer -> raw_tokens
Word(INSERT), Whitespace, Word(INFO), 
Whitespace, Word(customer), 
Whitespace, Word(VALUES),
Whitespace, LParen, Number(1), Comma, Whitespace, Number(2), RParen
EOF
-- removed whitespaces -> tokens
Word(INSERT), Word(INFO), Word(customer), 
Word(VALUES), LParen, Number(1), Comma, Number(2), RParen
EOF
-- parser -> Statement: INSERT
Statement:Insert
    into = true,
    table_name: ObjectName = customer,
    columns: Vec<Ident> = vec![],
    source: Option<Query> = Some(
        Query { 
            with: None, 
            body: Values(Values { 
                explicit_row: false, 
                rows: [
                    [Value(Number("1", false)), 
                    Value(Number("2", false)), 
                    Value(Number("3", false))]] 
        }
    )

Select statement example


Refs

About

A toy and simplified implementation of a SQL Parser

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages