grammar MysqlQuery;

 @header{package com.antlr.mysql.query;}  

AS                              : A S;
SELECT : S E L E C T;
FROM : F R O M;
TABLE : T A B L E;
MAX : M A X;
SUM : S U M;
AVG : A V G;
MIN : M I N;
COUNT : C O U N T;
ALL : A L L;
DISTINCT : D I S T I N C T;
WHERE : W H E R E;
GROUP : G R O U P;
BY : B Y ;
ORDER : O R D E R;
HAVING : H A V I N G;
NOT : N O T;
IS : I S ;
TRUE : T R U E;
FALSE : F A L S E;
UNKNOWN : U N K N O W N;
BETWEEN : B E T W E E N;
AND : A N D;
IN : I N;
NULL : N U L L;
OR : O R ;
ASC : A S C;
DESC : D E S C;
LIMIT : L I M I T ;
OFFSET : O F F S E T; fragment A : [aA];
fragment B : [bB];
fragment C : [cC];
fragment D : [dD];
fragment E : [eE];
fragment F : [fF];
fragment G : [gG];
fragment H : [hH];
fragment I : [iI];
fragment J : [jJ];
fragment K : [kK];
fragment L : [lL];
fragment M : [mM];
fragment N : [nN];
fragment O : [oO];
fragment P : [pP];
fragment Q : [qQ];
fragment R : [rR];
fragment S : [sS];
fragment T : [tT];
fragment U : [uU];
fragment V : [vV];
fragment W : [wW];
fragment X : [xX];
fragment Y : [yY];
fragment Z : [zZ];
fragment HEX_DIGIT: [0-9A-F];
fragment DEC_DIGIT: [0-9];
fragment LETTER: [a-zA-Z]; ID: ( 'A'..'Z' | 'a'..'z' | '_' | '$') ( 'A'..'Z' | 'a'..'z' | '_' | '$' | '0'..'9' )*;
TEXT_STRING : ( '\'' ( ('\\' '\\') | ('\'' '\'') | ('\\' '\'') | ~('\'') )* '\'' );
ID_LITERAL: '*'|('@'|'_'|LETTER)(LETTER|DEC_DIGIT|'_')*;
REVERSE_QUOTE_ID : '`' ~'`'+ '`';
DECIMAL_LITERAL: DEC_DIGIT+; tableName : tmpName=ID;
column_name :ID;
function_name : tmpName=ID ; selectStatement:
SELECT
selectElements
(
FROM tableSources
( whereClause )?
( groupByCaluse )?
( havingCaluse )?
) ?
( orderByClause )?
( limitClause )?
; selectElements
: (star='*' | selectElement ) (',' selectElement)*
; tableSources
: tableName (',' tableName)*
; whereClause
: WHERE logicExpression
; logicExpression
: logicExpression logicalOperator logicExpression
| fullColumnName comparisonOperator value
| fullColumnName BETWEEN value AND value
| fullColumnName NOT? IN '(' value (',' value)* ')'
| '(' logicExpression ')'
; groupByCaluse
: GROUP BY groupByItem (',' groupByItem)*
;
havingCaluse
: HAVING logicExpression
; orderByClause
: ORDER BY orderByExpression (',' orderByExpression)*
; limitClause
: LIMIT
(
(offset=decimalLiteral ',')? limit=decimalLiteral
| limit=decimalLiteral OFFSET offset=decimalLiteral
)
; orderByExpression
: fullColumnName order=(ASC | DESC)?
; groupByItem
: fullColumnName order=(ASC | DESC)?
; logicalOperator
: AND | '&' '&' | OR | '|' '|'
; comparisonOperator
: '=' | '>' | '<' | '<' '=' | '>' '='
| '<' '>' | '!' '=' | '<' '=' '>'
; value
: uid
| textLiteral
| decimalLiteral
; decimalLiteral
: DECIMAL_LITERAL
;
textLiteral
: TEXT_STRING
; selectElement
: fullColumnName (AS? uid)? #selectColumnElement
| functionCall (AS? uid)? #selectFunctionElement
; fullColumnName
: column_name
; functionCall
: aggregateWindowedFunction #aggregateFunctionCall
; aggregateWindowedFunction
: (AVG | MAX | MIN | SUM) '(' functionArg ')'
| COUNT '(' (starArg='*' | functionArg?) ')'
| COUNT '(' aggregator=DISTINCT functionArgs ')'
; functionArg
: column_name
; functionArgs
: column_name (',' column_name)*
; uid
: ID
; WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines

Antlr4 SQL Query 解析实例的更多相关文章

  1. SQL Server的实例恢复解析

    同Oracle一样,SQL Server在非一致性关闭的时候也会进行实例恢复(Instance Recovery),本文根据stack overflow的文章介绍一些SQL Server实例恢复的知识 ...

  2. NHibernate系列文章二十六:NHibernate查询之SQL Query查询(附程序下载)

    摘要 NHibernate在很早的版本就提供了SQL Query(原生SQL查询),对于很复杂的查询,如果使用其他的查询方式实现比较困难的时候,一般使用SQL Query.使用SQL Query是基于 ...

  3. SQL 软解析和硬解析详解

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt329 当客户端进程,将SQL语句通过监听器发送到Oracle时, 会触发一个 ...

  4. tp5 r3 一个简单的SQL语句调试实例

    tp5 r3 一个简单的SQL语句调试实例先看效果核心代码 public function index() { if (IS_AJAX && session("uid&quo ...

  5. SQL EXPLAIN解析

    本文转载自MySQL性能优化最佳实践 - 08 SQL EXPLAIN解析 什么是归并排序? 如果需要排序的数据超过了sort_buffer_size的大小,说明无法在内存中完成排序,就需要写到临时文 ...

  6. SQL Server 多实例下的复制

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 搭建步骤(Procedure) 注意事项(Attention) 二.背景(Contexts) ...

  7. jstl中的sql:query标签获取的结果如何格式化输出

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  8. .net连接DB2的异常SQL0666 - SQL query exceeds specified time limit or storage limit.错误处理

    SQL0666 - SQL query exceeds specified time limit or storage limit. 原因:查询超时 解决办法: set the DbCommand.C ...

  9. JSON解析实例——使用Json-lib

    JSON解析实例——使用Json-lib Json-lib下载及使用 本文介绍用一个类库进行JSON解析. 工具下载地址:http://sourceforge.net/projects/json-li ...

随机推荐

  1. Failed to start LSB: Bring up/down networking.

    由于我的虚拟机是从别的机器拷贝过来的,导入新机器后,没有问题,第二天就网络连接不上了,就出现下面的错误 [root@centos ~]# /etc/init.d/network restart Res ...

  2. bootstrap中点击左边展开

    <div id="sideNav" href=""><i class="material-icons dp48 glyphicon- ...

  3. poj2406 Power Strings 【KMP】

    Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&quo ...

  4. POJ 3580 - SuperMemo - [伸展树splay]

    题目链接:http://poj.org/problem?id=3580 Your friend, Jackson is invited to a TV show called SuperMemo in ...

  5. 江南大学第三届程序设计竞赛K题 - Cun Tou Gaming - [贪心+堆]

    描述 CTG(Cun Tou Gaming) 是我校的一支 LOL 战队,他们参加比赛总是可以拿到冠军,因为每次都只有他们一支队伍参赛,所以只需要去签个到就可以直接夺冠并领取奖金.现在有  n 场比赛 ...

  6. Fata7y Ya Warda! SPOJ - DRUIDEOI 单调栈

    题意:1e5个数围成一个环.现在要输出每个数左右第一个大于它的数的下标.若没有,则输出-1. 题解:单调栈板题.只是要把数据压入栈压两遍来模仿环.    具体分析:考虑一个递减的数列.要找左边最大的 ...

  7. iOS-深入理解(转载)

    RunLoop 是 iOS 和 OS X 开发中非常基础的一个概念,这篇文章将从 CFRunLoop 的源码入手,介绍 RunLoop 的概念以及底层实现原理.之后会介绍一下在 iOS 中,苹果是如何 ...

  8. 设置elasticsearch一次最大数量查询

    PUT my_index/_settings?preserve_existing=true{ "max_result_window": "2000000000" ...

  9. LeetCode-104.Maxinum Depth of Binary Tree

    Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longe ...

  10. android studio 布局

    1) 可见(visible)XML文件:Android:visibility="visible"Java代码:view.setVisibility(View.VISIBLE); 2 ...