@@ -122,6 +122,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
122122| <K_ALGORITHM: "ALGORITHM">
123123| <K_ALL:"ALL">
124124| <K_ALTER:"ALTER">
125+ | <K_ANALYZE:"ANALYZE">
125126| <K_AND:"AND">
126127| <K_AND_OPERATOR:"&&">
127128| <K_ANY:"ANY">
@@ -136,6 +137,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
136137| <K_BOTH:"BOTH">
137138| <K_BY:"BY">
138139| <K_CACHE: "CACHE">
140+ | <K_BUFFERS: "BUFFERS">
139141| <K_BYTE: "BYTE">
140142| <K_CALL : "CALL">
141143| <K_CASCADE: "CASCADE">
@@ -153,6 +155,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
153155| <K_COMMENT:"COMMENT">
154156| <K_CONNECT:"CONNECT">
155157| <K_CONSTRAINT:"CONSTRAINT">
158+ | <K_COSTS: "COSTS">
156159| <K_CREATE:"CREATE">
157160| <K_CROSS:"CROSS">
158161| <K_CURRENT: "CURRENT">
@@ -192,6 +195,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
192195| <K_FOR:"FOR">
193196| <K_FORCE : "FORCE">
194197| <K_FOREIGN:"FOREIGN">
198+ | <K_FORMAT:"FORMAT">
195199| <K_FROM:"FROM">
196200| <K_FULL:"FULL">
197201| <K_FULLTEXT:"FULLTEXT">
@@ -220,6 +224,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
220224| <K_IS:"IS">
221225| <K_ISNULL:"ISNULL">
222226| <K_JOIN:"JOIN">
227+ | <K_JSON:"JSON">
223228| <K_KEEP:"KEEP">
224229| <K_KEY:"KEY">
225230| <K_FN:"FN">
@@ -256,6 +261,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
256261| <K_NULLS: "NULLS">
257262| <K_NOWAIT: "NOWAIT">
258263| <K_OF:"OF">
264+ | <K_OFF:"OFF">
259265| <K_OFFSET:"OFFSET">
260266| <K_ON:"ON">
261267| <K_ONLY:"ONLY">
@@ -332,6 +338,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
332338| <K_VALUE:"VALUE">
333339| <K_VALUES:"VALUES">
334340| <K_VARYING:"VARYING">
341+ | <K_VERBOSE: "VERBOSE">
335342| <K_VIEW:"VIEW">
336343| <K_WAIT : "WAIT">
337344| <K_WHEN:"WHEN">
@@ -341,6 +348,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
341348| <K_WITHIN:"WITHIN">
342349| <K_WITHOUT:"WITHOUT">
343350| <K_XML:"XML">
351+ | <K_YAML:"YAML">
344352| <K_ZONE:"ZONE">
345353}
346354
@@ -635,13 +643,109 @@ DescribeStatement Describe(): {
635643
636644ExplainStatement Explain(): {
637645 Select select;
646+ List<ExplainStatement.Option> options = null;
638647} {
639- <K_EXPLAIN> select = Select()
648+ <K_EXPLAIN>
649+ options=ExplainStatementOptions()
650+ select = Select()
640651 {
641- return new ExplainStatement(select);
652+ ExplainStatement es = new ExplainStatement(select);
653+ if(options != null && !options.isEmpty()) {
654+ for(ExplainStatement.Option o : options) {
655+ es.addOption(o);
656+ }
657+ }
658+ return es;
642659 }
643660}
644661
662+ /**
663+ * Postgres supports TRUE,ON,1,FALSE,OFF,0 as values
664+ */
665+ String ExplainOptionBoolean():
666+ {
667+ Token tk = null;
668+ }
669+ {
670+ // intentionally not supporting 0,1 at the moment
671+ [( tk=<K_TRUE> | tk=<K_FALSE> | tk=<K_ON> | tk=<K_OFF> )] // optional
672+ {
673+ return tk != null ? tk.image : null;
674+ }
675+ }
676+
677+ /**
678+ * The output format, which can be TEXT, XML, JSON, or YAML
679+ */
680+ String ExplainFormatOption():
681+ {
682+ Token tk = null;
683+ }
684+ {
685+ // TODO support Text
686+ [( tk=<K_XML> | tk=<K_JSON> | tk=<K_YAML> )] // optional
687+ {
688+ return tk != null ? tk.image : null;
689+ }
690+ }
691+
692+ /**
693+ * Options for explain, see https://www.postgresql.org/docs/9.1/sql-explain.html
694+ */
695+ List<ExplainStatement.Option> ExplainStatementOptions():
696+ {
697+ List<ExplainStatement.Option> options = new ArrayList<ExplainStatement.Option>();
698+ ExplainStatement.Option option = null;
699+ Token token = null;
700+ String value = null;
701+ }
702+ {
703+ (
704+ (<K_ANALYZE> value=ExplainOptionBoolean()
705+ {
706+ option = new ExplainStatement.Option(ExplainStatement.OptionType.ANALYZE);
707+ option.setValue(value);
708+ options.add(option);
709+ }
710+ )
711+ |
712+ (<K_BUFFERS> value=ExplainOptionBoolean()
713+ {
714+ option = new ExplainStatement.Option(ExplainStatement.OptionType.BUFFERS);
715+ option.setValue(value);
716+ options.add(option);
717+ }
718+ )
719+ |
720+ (<K_COSTS> value=ExplainOptionBoolean()
721+ {
722+ option = new ExplainStatement.Option(ExplainStatement.OptionType.COSTS);
723+ option.setValue(value);
724+ options.add(option);
725+ }
726+ )
727+ |
728+ (<K_VERBOSE> value=ExplainOptionBoolean()
729+ {
730+ option = new ExplainStatement.Option(ExplainStatement.OptionType.VERBOSE);
731+ option.setValue(value);
732+ options.add(option);
733+ }
734+ )
735+ |
736+ (<K_FORMAT> value=ExplainFormatOption()
737+ {
738+ option = new ExplainStatement.Option(ExplainStatement.OptionType.FORMAT);
739+ option.setValue(value);
740+ options.add(option);
741+ }
742+ )
743+ )* //zero or many times those productions
744+ {
745+ return options;
746+ }
747+ }
748+
645749UseStatement Use(): {
646750 String name;
647751}
@@ -1175,7 +1279,7 @@ String RelObjectNameWithoutValue() :
11751279 | tk=<K_DATE_LITERAL> | tk=<K_NEXTVAL> | tk=<K_TRUE> | tk=<K_FALSE> | tk=<K_DUPLICATE>
11761280 | tk=<K_READ> | tk=<K_SCHEMA> | tk=<K_SIZE> | tk=<K_SESSION>
11771281 | tk=<K_VIEW> | tk=<K_NOLOCK> | tk=<K_VALIDATE> | tk=<K_CYCLE>
1178- /* | tk=<K_PLACING> | tk=<K_BOTH> | tk=<K_LEADING> | tk=<K_TRAILING> */
1282+ /* | tk=<K_PLACING> | tk=<K_BOTH> | tk=<K_LEADING> | tk=<K_TRAILING> */
11791283 )
11801284
11811285 { return tk.image; }
0 commit comments