Skip to content

Instantly share code, notes, and snippets.

@danielneal
Created February 13, 2017 14:07
Show Gist options
  • Save danielneal/c039916144f57af2d7d4e010e16a6886 to your computer and use it in GitHub Desktop.
Save danielneal/c039916144f57af2d7d4e010e16a6886 to your computer and use it in GitHub Desktop.
;; [ WITH [ RECURSIVE ] with_query [, ...] ]
;; SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
;; * | expression [ [ AS ] output_name ] [, ...]
;; [ FROM from_item [, ...] ]
;; [ WHERE condition ]
;; [ GROUP BY expression [, ...] ]
;; [ HAVING condition [, ...] ]
;; [ WINDOW window_name AS ( window_definition ) [, ...] ]
;; [ { UNION | INTERSECT | EXCEPT } [ ALL ] select ]
;; [ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
;; [ LIMIT { count | ALL } ]
;; [ OFFSET start [ ROW | ROWS ] ]
;; [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ]
;; [ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] [...] ]
;; where from_item can be one of:
;; [ ONLY ] table_name [ * ] [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
;; ( select ) [ AS ] alias [ ( column_alias [, ...] ) ]
;; with_query_name [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
;; function_name ( [ argument [, ...] ] ) [ AS ] alias [ ( column_alias [, ...] | column_definition [, ...] ) ]
;; function_name ( [ argument [, ...] ] ) AS ( column_definition [, ...] )
;; from_item [ NATURAL ] join_type from_item [ ON join_condition | USING ( join_column [, ...] ) ]
;; and with_query is:
;; with_query_name [ ( column_name [, ...] ) ] AS ( select )
;; TABLE { [ ONLY ] table_name [ * ] | with_query_name }
(s/def ::with
(s/cat :with #{:with} :recursive (s/? #{:recursive})))
(s/def ::distinct-on
(s/cat :distinct #{:distinct} :on #{:on} :expression (constantly true)))
(s/def ::select-expression
(s/cat :select #{:select}
:distinct (s/or :all #{:all} :distinct #{:distinct} :distinct-on ::distinct-on)))
(s/def ::select-query
(s/cat :common-table-expressions (s/? (s/coll-of ::with))
:select-expression ::select-expression))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment