• Stars
    star
    124
  • Rank 288,207 (Top 6 %)
  • Language PLpgSQL
  • License
    MIT License
  • Created over 10 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An ANTLR4 grammar for SQLite statements.

SQLite parser

NOTE: this grammar/repo is not actively maintained. Consider looking at a more recent version of this grammar in the ANTLR grammars repository.

An ANTLR4 grammar for SQLite 3.8.x based on the official specs.

Install

To install this library, do the following:

git clone https://github.com/bkiers/sqlite-parser
cd sqlite-parser
mvn clean install -DskipTests=true

Test

The generated parser has been tested by approximately 30000 SQLite statements scraped from the test suite of the SQLite repository. Running these tests, which can take quite a few minutes, can be done as follows:

mvn clean test

If running the tests takes too long for your liking, try increasing the max heap space as follows:

export JAVA_TOOL_OPTIONS="-Xmx4096m" && mvn clean test

Example

Let's say you would like to record all the names of functions used in an select-statement:

SELECT log AS x FROM t1
GROUP BY x
HAVING count(*) >= 4
ORDER BY max(n) + 0

This can be done by attaching a listener to the parse tree that listens when the parse tree enters an SQL expression, and the function name inside this expression is not null:

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) throws Exception {

        // The list that will hold our function names.
        final List<String> functionNames = new ArrayList<String>();

        // The select-statement to be parsed.
        String sql = "SELECT log AS x FROM t1 \n" +
                "GROUP BY x                   \n" +
                "HAVING count(*) >= 4         \n" +
                "ORDER BY max(n) + 0          \n";

        // Create a lexer and parser for the input.
        SQLiteLexer lexer = new SQLiteLexer(new ANTLRInputStream(sql));
        SQLiteParser parser = new SQLiteParser(new CommonTokenStream(lexer));

        // Invoke the `select_stmt` production.
        ParseTree tree = parser.select_stmt();

        // Walk the `select_stmt` production and listen when the parser
        // enters the `expr` production.
        ParseTreeWalker.DEFAULT.walk(new SQLiteBaseListener(){

            @Override
            public void enterExpr(@NotNull SQLiteParser.ExprContext ctx) {
                // Check if the expression is a function call.
                if (ctx.function_name() != null) {
                    // Yes, it was a function call: add the name of the function
                    // to out list.
                    functionNames.add(ctx.function_name().getText());
                }
            }
        }, tree);

        // Print the parsed functions.
        System.out.println("functionNames=" + functionNames);
    }
}

which will print:

functionNames=[count, max]

More Repositories

1

tiny-language-antlr4

Tiny Language with ANTLR 4
Java
201
star
2

retrofit-oauth

A Retrofit + Google OAuth demo.
Java
200
star
3

Liqp

An ANTLR based 'Liquid Template' parser and rendering engine.
Java
165
star
4

Mu

A small demonstration how to use ANTLR 4's visitor feature.
Java
113
star
5

rrd-antlr4

Railroad Diagrams for ANTLR 4 grammar rules.
ANTLR
101
star
6

python3-parser

An ANTLR4 grammar for Python 3
Python
40
star
7

GrahamScan

A Java implementation of the Graham Scan algorithm to find the convex hull of a set of points.
Java
37
star
8

pcre-parser

An ANTLR 4 grammar for PCRE
Java
30
star
9

CompGeom

A computational geometry library using arbitrary-precision arithmetic where possible, written in Java.
Java
26
star
10

RotatingCalipers

A Java implementation of the Rotating Calipers algorithm to find the minimum bounding rectangle of a set of points.
Java
24
star
11

PCREParser

An ANTLR 3 grammar for PCRE
Java
19
star
12

Curta

A small, customizable expression evaluator.
Java
16
star
13

ecmascript-parser

An ANTLR4 grammar for ECMAScript 5.1
Java
15
star
14

lua-parser

A Lua 5.3 parser and AST walker using ANTLR 3
GAP
14
star
15

atlas

Atlas, an offline reverse Geo-coding library written in Java.
Java
13
star
16

antlr4-csv-demo

ANTLR4-CSV-Demo
Java
10
star
17

PGN-parser

A Portable Game Notation (PGN) ANTLR 4 grammar and parser.
ANTLR
7
star
18

antlr-tree-rewriter-java

Java
4
star
19

antlr4android

An Android app with an ANTLR4 expression evaluator.
Java
4
star
20

ICalParser

An iCalendar (RFC 5545) parser backed up by an ANTLR v4 grammar.
Java
3
star
21

iri-parser

An ANTLR 4 grammar and parser for IRI (Internationalized Resource Identifiers).
ANTLR
3
star
22

uri-parser

An ANTLR4 grammar for URIs
ANTLR
2
star
23

antlr-tree-rewriter-cs

C#
1
star
24

ah-bonus-mailer

Albert Heijn bonus mailer
Python
1
star
25

xi

TypeScript
1
star
26

ada2012-parser

Ada
1
star
27

BigO.SPARQLParser

A C# SPARQL parser with rewrite possibilities
C#
1
star