{-
= TOC:

notes
Public api
Names - parsing identifiers
Typenames
Scalar expressions
  simple literals
  star, param
  parens expression, row constructor and scalar subquery
  case, cast, exists, unique, array/ multiset constructor
  typed literal, app, special function, aggregate, window function
  suffixes: in, between, quantified comparison, match predicate, array
    subscript, escape, collate
  operators
  scalar expression top level
  helpers
query expressions
  select lists
  from clause
  other table expression clauses:
    where, group by, having, order by, offset and fetch
  common table expressions
  query expression
  set operations
lexers
utilities

= Notes about the code

The lexers appear at the bottom of the file. There tries to be a clear
separation between the lexers and the other parser which only use the
lexers, this isn't 100% complete at the moment and needs fixing.

== Left factoring

The parsing code is aggressively left factored, and try is avoided as
much as possible. Try is avoided because:

 * when it is overused it makes the code hard to follow
 * when it is overused it makes the parsing code harder to debug
 * it makes the parser error messages much worse

The code could be made a bit simpler with a few extra 'trys', but this
isn't done because of the impact on the parser error
messages. Apparently it can also help the speed but this hasn't been
looked into.

== Parser error messages

A lot of care has been given to generating good parser error messages
for invalid syntax. There are a few utils below which partially help
in this area.

There is a set of crafted bad expressions in ErrorMessages.hs, these
are used to guage the quality of the error messages and monitor
regressions by hand. The use of <?> is limited as much as possible:
each instance should justify itself by improving an actual error
message.

There is also a plan to write a really simple expression parser which
doesn't do precedence and associativity, and the fix these with a pass
over the ast. I don't think there is any other way to sanely handle
the common prefixes between many infix and postfix multiple keyword
operators, and some other ambiguities also. This should help a lot in
generating good error messages also.

Both the left factoring and error message work are greatly complicated
by the large number of shared prefixes of the various elements in SQL
syntax.

== Main left factoring issues

There are three big areas which are tricky to left factor:

 * typenames
 * scalar expressions which can start with an identifier
 * infix and suffix operators

=== typenames

There are a number of variations of typename syntax. The standard
deals with this by switching on the name of the type which is parsed
first. This code doesn't do this currently, but might in the
future. Taking the approach in the standard grammar will limit the
extensibility of the parser and might affect the ease of adapting to
support other sql dialects.

=== identifier scalar expressions

There are a lot of scalar expression nodes which start with
identifiers, and can't be distinguished the tokens after the initial
identifier are parsed. Using try to implement these variations is very
simple but makes the code much harder to debug and makes the parser
error messages really bad.

Here is a list of these nodes:

 * identifiers
 * function application
 * aggregate application
 * window application
 * typed literal: typename 'literal string'
 * interval literal which is like the typed literal with some extras

There is further ambiguity e.g. with typed literals with precision,
functions, aggregates, etc. - these are an identifier, followed by
parens comma separated scalar expressions or something similar, and it
is only later that we can find a token which tells us which flavour it
is.

There is also a set of nodes which start with an identifier/keyword
but can commit since no other syntax can start the same way:

 * case
 * cast
 * exists, unique subquery
 * array constructor
 * multiset constructor
 * all the special syntax functions: extract, position, substring,
  convert, translate, overlay, trim, etc.

The interval literal mentioned above is treated in this group at the
moment: if we see 'interval' we parse it either as a full interval
literal or a typed literal only.

Some items in this list might have to be fixed in the future, e.g. to
support standard 'substring(a from 3 for 5)' as well as regular
function substring syntax 'substring(a,3,5) at the same time.

The work in left factoring all this is mostly done, but there is still
a substantial bit to complete and this is by far the most difficult
bit. At the moment, the work around is to use try, the downsides of
which is the poor parsing error messages.

=== infix and suffix operators

== permissiveness

The parser is very permissive in many ways. This departs from the
standard which is able to eliminate a number of possibilities just in
the grammar, which this parser allows. This is done for a number of
reasons:

 * it makes the parser simple - less variations
 * it should allow for dialects and extensibility more easily in the
  future (e.g. new infix binary operators with custom precedence)
 * many things which are effectively checked in the grammar in the
  standard, can be checked using a typechecker or other simple static
  analysis

To use this code as a front end for a sql engine, or as a sql validity
checker, you will need to do a lot of checks on the ast. A
typechecker/static checker plus annotation to support being a compiler
front end is planned but not likely to happen too soon.

Some of the areas this affects:

typenames: the variation of the type name should switch on the actual
name given according to the standard, but this code only does this for
the special case of interval type names. E.g. you can write 'int
collate C' or 'int(15,2)' and this will parse as a character type name
or a precision scale type name instead of being rejected.

scalar expressions: every variation on scalar expressions uses the same
parser/syntax. This means we don't try to stop non boolean valued
expressions in boolean valued contexts in the parser. Another area
this affects is that we allow general scalar expressions in group by,
whereas the standard only allows column names with optional collation.

These are all areas which are specified (roughly speaking) in the
syntax rather than the semantics in the standard, and we are not
fixing them in the syntax but leaving them till the semantic checking
(which doesn't exist in this code at this time).
-}

{-# LANGUAGE TupleSections #-}
{-# LANGUAGE OverloadedStrings #-}
-- | This is the module with the parser functions.
module Language.SQL.SimpleSQL.Parse
    (parseQueryExpr
    ,parseScalarExpr
    ,parseStatement
    ,parseStatements
    ,ParseError(..)
    ,prettyError
    ,ansi2011
    ) where

import Text.Megaparsec
    (ParsecT
    ,runParserT

    ,ParseErrorBundle(..)
    ,errorBundlePretty

    ,(<?>)
    ,(<|>)
    ,token
    ,choice
    ,eof
    ,try
    ,sepBy
    ,sepBy1
    ,optional
    ,option
    ,some
    ,many
    ,between
    )
import qualified Control.Monad.Combinators.Expr as E
import qualified Control.Monad.Permutations as P

import Control.Monad.Reader
    (Reader
    ,runReader
    ,ask
    ,asks
    )

import qualified Data.Set           as Set
import Data.Void (Void)

import Control.Monad (guard, void)
import Control.Applicative ((<**>))
import Data.Char (isDigit)
import Data.List (sort,groupBy)
import Data.Function (on)
import Data.Maybe (catMaybes, isJust, mapMaybe)
import Data.Text (Text)
import qualified Data.Text as T

import Language.SQL.SimpleSQL.Syntax 
import Language.SQL.SimpleSQL.Dialect
import qualified Language.SQL.SimpleSQL.Lex as L

------------------------------------------------------------------------------

-- = Public API

-- | Parses a query expr, trailing semicolon optional.
parseQueryExpr
    :: Dialect
    -- ^ dialect of SQL to use
    -> Text
    -- ^ filename to use in error messages
    -> Maybe (Int,Int)
    -- ^ line number and column number of the first character
    -- in the source to use in error messages
    -> Text
    -- ^ the SQL source to parse
    -> Either ParseError QueryExpr
parseQueryExpr :: Dialect
-> Text -> Maybe (Int, Int) -> Text -> Either ParseError QueryExpr
parseQueryExpr = Parser QueryExpr
-> Dialect
-> Text
-> Maybe (Int, Int)
-> Text
-> Either ParseError QueryExpr
forall a.
Parser a
-> Dialect
-> Text
-> Maybe (Int, Int)
-> Text
-> Either ParseError a
wrapParse Parser QueryExpr
topLevelQueryExpr

-- | Parses a statement, trailing semicolon optional.
parseStatement
    :: Dialect
    -- ^ dialect of SQL to use
    -> Text
    -- ^ filename to use in error messages
    -> Maybe (Int,Int)
    -- ^ line number and column number of the first character
    -- in the source to use in error messages
    -> Text
    -- ^ the SQL source to parse
    -> Either ParseError Statement
parseStatement :: Dialect
-> Text -> Maybe (Int, Int) -> Text -> Either ParseError Statement
parseStatement = Parser Statement
-> Dialect
-> Text
-> Maybe (Int, Int)
-> Text
-> Either ParseError Statement
forall a.
Parser a
-> Dialect
-> Text
-> Maybe (Int, Int)
-> Text
-> Either ParseError a
wrapParse Parser Statement
topLevelStatement

-- | Parses a list of statements, with semi colons between
-- them. The final semicolon is optional.
parseStatements
    :: Dialect
    -- ^ dialect of SQL to use
    -> Text
    -- ^ filename to use in error messages
    -> Maybe (Int,Int)
    -- ^ line number and column number of the first character
    -- in the source to use in error messages
    -> Text
    -- ^ the SQL source to parse
    -> Either ParseError [Statement]
parseStatements :: Dialect
-> Text
-> Maybe (Int, Int)
-> Text
-> Either ParseError [Statement]
parseStatements = Parser [Statement]
-> Dialect
-> Text
-> Maybe (Int, Int)
-> Text
-> Either ParseError [Statement]
forall a.
Parser a
-> Dialect
-> Text
-> Maybe (Int, Int)
-> Text
-> Either ParseError a
wrapParse Parser [Statement]
statements

-- | Parses a scalar expression.
parseScalarExpr
    :: Dialect
    -- ^ dialect of SQL to use
    -> Text
    -- ^ filename to use in error messages
    -> Maybe (Int,Int)
    -- ^ line number and column number of the first character
    -- in the source to use in error messages
    -> Text
    -- ^ the SQL source to parse
    -> Either ParseError ScalarExpr
parseScalarExpr :: Dialect
-> Text -> Maybe (Int, Int) -> Text -> Either ParseError ScalarExpr
parseScalarExpr = Parser ScalarExpr
-> Dialect
-> Text
-> Maybe (Int, Int)
-> Text
-> Either ParseError ScalarExpr
forall a.
Parser a
-> Dialect
-> Text
-> Maybe (Int, Int)
-> Text
-> Either ParseError a
wrapParse Parser ScalarExpr
scalarExpr

-- Megaparsec is too clever, so have to create a new type to represent
-- either a lex error or a parse error

data ParseError
    = LexError L.ParseError
    | ParseError (ParseErrorBundle L.SQLStream Void)

prettyError :: ParseError -> Text
prettyError :: ParseError -> Text
prettyError (LexError ParseError
e) = String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ ParseError -> String
forall s e.
(VisualStream s, TraversableStream s, ShowErrorComponent e) =>
ParseErrorBundle s e -> String
errorBundlePretty ParseError
e
prettyError (ParseError ParseErrorBundle SQLStream Void
e) = String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ ParseErrorBundle SQLStream Void -> String
forall s e.
(VisualStream s, TraversableStream s, ShowErrorComponent e) =>
ParseErrorBundle s e -> String
errorBundlePretty ParseErrorBundle SQLStream Void
e

{-
This helper function takes the parser given and:

sets the position when parsing
automatically skips leading whitespace
checks the parser parses all the input using eof
converts the error return to the nice wrapper
-}

wrapParse :: Parser a
          -> Dialect
          -> Text
          -> Maybe (Int,Int)
          -> Text
          -> Either ParseError a
wrapParse :: forall a.
Parser a
-> Dialect
-> Text
-> Maybe (Int, Int)
-> Text
-> Either ParseError a
wrapParse Parser a
parser Dialect
d Text
f Maybe (Int, Int)
p Text
src = do
    [WithPos Token]
lx <- (ParseError -> Either ParseError [WithPos Token])
-> ([WithPos Token] -> Either ParseError [WithPos Token])
-> Either ParseError [WithPos Token]
-> Either ParseError [WithPos Token]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (ParseError -> Either ParseError [WithPos Token]
forall a b. a -> Either a b
Left (ParseError -> Either ParseError [WithPos Token])
-> (ParseError -> ParseError)
-> ParseError
-> Either ParseError [WithPos Token]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseError -> ParseError
LexError) [WithPos Token] -> Either ParseError [WithPos Token]
forall a b. b -> Either a b
Right (Either ParseError [WithPos Token]
 -> Either ParseError [WithPos Token])
-> Either ParseError [WithPos Token]
-> Either ParseError [WithPos Token]
forall a b. (a -> b) -> a -> b
$ Dialect
-> Text
-> Maybe (Int, Int)
-> Text
-> Either ParseError [WithPos Token]
L.lexSQLWithPositions Dialect
d Text
f Maybe (Int, Int)
p Text
src
    (ParseErrorBundle SQLStream Void -> Either ParseError a)
-> (a -> Either ParseError a)
-> Either (ParseErrorBundle SQLStream Void) a
-> Either ParseError a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (ParseError -> Either ParseError a
forall a b. a -> Either a b
Left (ParseError -> Either ParseError a)
-> (ParseErrorBundle SQLStream Void -> ParseError)
-> ParseErrorBundle SQLStream Void
-> Either ParseError a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseErrorBundle SQLStream Void -> ParseError
ParseError) a -> Either ParseError a
forall a b. b -> Either a b
Right (Either (ParseErrorBundle SQLStream Void) a -> Either ParseError a)
-> Either (ParseErrorBundle SQLStream Void) a
-> Either ParseError a
forall a b. (a -> b) -> a -> b
$ 
        Reader Dialect (Either (ParseErrorBundle SQLStream Void) a)
-> Dialect -> Either (ParseErrorBundle SQLStream Void) a
forall r a. Reader r a -> r -> a
runReader (Parser a
-> String
-> SQLStream
-> Reader Dialect (Either (ParseErrorBundle SQLStream Void) a)
forall (m :: * -> *) e s a.
Monad m =>
ParsecT e s m a
-> String -> s -> m (Either (ParseErrorBundle s e) a)
runParserT (Parser a
parser Parser a -> ParsecT Void SQLStream (Reader Dialect) () -> Parser a
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* (ParsecT Void SQLStream (Reader Dialect) ()
forall e s (m :: * -> *). MonadParsec e s m => m ()
eof ParsecT Void SQLStream (Reader Dialect) ()
-> String -> ParsecT Void SQLStream (Reader Dialect) ()
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"")) (Text -> String
T.unpack Text
f)
                   (SQLStream
 -> Reader Dialect (Either (ParseErrorBundle SQLStream Void) a))
-> SQLStream
-> Reader Dialect (Either (ParseErrorBundle SQLStream Void) a)
forall a b. (a -> b) -> a -> b
$ String -> [WithPos Token] -> SQLStream
L.SQLStream (Text -> String
T.unpack Text
src) ([WithPos Token] -> SQLStream) -> [WithPos Token] -> SQLStream
forall a b. (a -> b) -> a -> b
$ (WithPos Token -> Bool) -> [WithPos Token] -> [WithPos Token]
forall a. (a -> Bool) -> [a] -> [a]
filter WithPos Token -> Bool
notSpace [WithPos Token]
lx) Dialect
d
  where
    notSpace :: WithPos Token -> Bool
notSpace = Token -> Bool
notSpace' (Token -> Bool)
-> (WithPos Token -> Token) -> WithPos Token -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WithPos Token -> Token
forall a. WithPos a -> a
L.tokenVal
    notSpace' :: Token -> Bool
notSpace' (L.Whitespace {}) = Bool
False
    notSpace' (L.LineComment {}) = Bool
False
    notSpace' (L.BlockComment {}) = Bool
False
    notSpace' Token
_ = Bool
True

------------------------------------------------------------------------------

-- parsing code

type Parser = ParsecT Void L.SQLStream (Reader Dialect)

{-
------------------------------------------------

= Names

Names represent identifiers and a few other things. The parser here
handles regular identifiers, dotten chain identifiers, quoted
identifiers and unicode quoted identifiers.

Dots: dots in identifier chains are parsed here and represented in the
Iden constructor usually. If parts of the chains are non identifier
scalar expressions, then this is represented by a BinOp "."
instead. Dotten chain identifiers which appear in other contexts (such
as function names, table names, are represented as [Name] only.

Identifier grammar:

unquoted:
underscore <|> letter : many (underscore <|> alphanum

example
_example123

quoted:

double quote, many (non quote character or two double quotes
together), double quote

"example quoted"
"example with "" quote"

unicode quoted is the same as quoted in this parser, except it starts
with U& or u&

u&"example quoted"
-}

name :: Parser Name
name :: Parser Name
name = do
    [Text]
bl <- (Dialect -> [Text]) -> Parser [Text]
forall a. (Dialect -> a) -> Parser a
askDialect Dialect -> [Text]
diKeywords
    (Maybe (Text, Text) -> Text -> Name)
-> (Maybe (Text, Text), Text) -> Name
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Maybe (Text, Text) -> Text -> Name
Name ((Maybe (Text, Text), Text) -> Name)
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe (Text, Text), Text)
-> Parser Name
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Text]
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe (Text, Text), Text)
identifierTok [Text]
bl

-- todo: replace (:[]) with a named function all over

names :: Parser [Name]
names :: Parser [Name]
names = [Name] -> [Name]
forall a. [a] -> [a]
reverse ([Name] -> [Name]) -> Parser [Name] -> Parser [Name]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (((Name -> [Name] -> [Name]
forall a. a -> [a] -> [a]
:[]) (Name -> [Name]) -> Parser Name -> Parser [Name]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Name
name) Parser [Name] -> Parser ([Name] -> [Name]) -> Parser [Name]
forall a. Parser a -> Parser (a -> a) -> Parser a
<??*> Parser ([Name] -> [Name])
anotherName)
  -- can't use a simple chain here since we
  -- want to wrap the . + name in a try
  -- this will change when this is left factored
  where
    anotherName :: Parser ([Name] -> [Name])
    anotherName :: Parser ([Name] -> [Name])
anotherName = Parser ([Name] -> [Name]) -> Parser ([Name] -> [Name])
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ((:) (Name -> [Name] -> [Name])
-> Parser Name -> Parser ([Name] -> [Name])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Text -> Parser Text
symbol Text
"." Parser Text -> Parser Name -> Parser Name
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Name
name) Parser Name -> String -> Parser Name
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
""))

{-
= Type Names

Typenames are used in casts, and also in the typed literal syntax,
which is a typename followed by a string literal.

Here are the grammar notes:

== simple type name

just an identifier chain or a multi word identifier (this is a fixed
list of possibilities, e.g. as 'character varying', see below in the
parser code for the exact list).

<simple-type-name> ::= <identifier-chain>
     | multiword-type-identifier

== Precision type name

<precision-type-name> ::= <simple-type-name> <left paren> <unsigned-int> <right paren>

e.g. char(5)

note: above and below every where a simple type name can appear, this
means a single identifier/quoted or a dotted chain, or a multi word
identifier

== Precision scale type name

<precision-type-name> ::= <simple-type-name> <left paren> <unsigned-int> <comma> <unsigned-int> <right paren>

e.g. decimal(15,2)

== Lob type name

this is a variation on the precision type name with some extra info on
the units:

<lob-type-name> ::=
   <simple-type-name> <left paren> <unsigned integer> [ <multiplier> ] [ <char length units> ] <right paren>

<multiplier>    ::=   K | M | G
<char length units>    ::=   CHARACTERS | CODE_UNITS | OCTETS

(if both multiplier and char length units are missing, then this will
parse as a precision type name)

e.g.
clob(5M octets)

== char type name

this is a simple type with optional precision which allows the
character set or the collation to appear as a suffix:

<char type name> ::=
    <simple type name>
    [ <left paren> <unsigned-int> <right paren> ]
    [ CHARACTER SET <identifier chain> ]
    [ COLLATE <identifier chain> ]

e.g.

char(5) character set my_charset collate my_collation

= Time typename

this is typename with optional precision and either 'with time zone'
or 'without time zone' suffix, e.g.:

<datetime type> ::=
    [ <left paren> <unsigned-int> <right paren> ]
    <with or without time zone>
<with or without time zone> ::= WITH TIME ZONE | WITHOUT TIME ZONE
    WITH TIME ZONE | WITHOUT TIME ZONE

= row type name

<row type> ::=
    ROW <left paren> <field definition> [ { <comma> <field definition> }... ] <right paren>

<field definition> ::= <identifier> <type name>

e.g.
row(a int, b char(5))

= interval type name

<interval type> ::= INTERVAL <interval datetime field> [TO <interval datetime field>]

<interval datetime field> ::=
  <datetime field> [ <left paren> <unsigned int> [ <comma> <unsigned int> ] <right paren> ]

= array type name

<array type> ::= <data type> ARRAY [ <left bracket> <unsigned integer> <right bracket> ]

= multiset type name

<multiset type>    ::=   <data type> MULTISET

A type name will parse into the 'smallest' constructor it will fit in
syntactically, e.g. a clob(5) will parse to a precision type name, not
a lob type name.

Unfortunately, to improve the error messages, there is a lot of (left)
factoring in this function, and it is a little dense.
-}

typeName :: Parser TypeName
typeName :: Parser TypeName
typeName =
    (Parser TypeName
rowTypeName Parser TypeName -> Parser TypeName -> Parser TypeName
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser TypeName
intervalTypeName Parser TypeName -> Parser TypeName -> Parser TypeName
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser TypeName
otherTypeName)
    Parser TypeName -> Parser (TypeName -> TypeName) -> Parser TypeName
forall a. Parser a -> Parser (a -> a) -> Parser a
<??*> Parser (TypeName -> TypeName)
tnSuffix
  where
    rowTypeName :: Parser TypeName
rowTypeName =
        [(Name, TypeName)] -> TypeName
RowTypeName ([(Name, TypeName)] -> TypeName)
-> ParsecT Void SQLStream (Reader Dialect) [(Name, TypeName)]
-> Parser TypeName
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"row" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) [(Name, TypeName)]
-> ParsecT Void SQLStream (Reader Dialect) [(Name, TypeName)]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Void SQLStream (Reader Dialect) [(Name, TypeName)]
-> ParsecT Void SQLStream (Reader Dialect) [(Name, TypeName)]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser (Name, TypeName)
-> ParsecT Void SQLStream (Reader Dialect) [(Name, TypeName)]
forall a. Parser a -> Parser [a]
commaSep1 Parser (Name, TypeName)
rowField))
    rowField :: Parser (Name, TypeName)
rowField = (,) (Name -> TypeName -> (Name, TypeName))
-> Parser Name
-> ParsecT
     Void SQLStream (Reader Dialect) (TypeName -> (Name, TypeName))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Name
name ParsecT
  Void SQLStream (Reader Dialect) (TypeName -> (Name, TypeName))
-> Parser TypeName -> Parser (Name, TypeName)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser TypeName
typeName
    ----------------------------
    intervalTypeName :: Parser TypeName
intervalTypeName =
        Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"interval" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser TypeName -> Parser TypeName
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*>
        ((IntervalTypeField -> Maybe IntervalTypeField -> TypeName)
-> (IntervalTypeField, Maybe IntervalTypeField) -> TypeName
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry IntervalTypeField -> Maybe IntervalTypeField -> TypeName
IntervalTypeName ((IntervalTypeField, Maybe IntervalTypeField) -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (IntervalTypeField, Maybe IntervalTypeField)
-> Parser TypeName
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (IntervalTypeField, Maybe IntervalTypeField)
intervalQualifier)
    ----------------------------
    otherTypeName :: Parser TypeName
otherTypeName =
        Parser [Name]
nameOfType Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> TypeName)
-> Parser TypeName
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**>
            (ParsecT Void SQLStream (Reader Dialect) ([Name] -> TypeName)
typeNameWithParens
             ParsecT Void SQLStream (Reader Dialect) ([Name] -> TypeName)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> TypeName)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> TypeName)
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe Integer
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Integer)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Integer
forall a. Maybe a
Nothing ParsecT Void SQLStream (Reader Dialect) (Maybe Integer)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Integer -> [Name] -> TypeName)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> TypeName)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe Integer -> [Name] -> TypeName)
timeTypeName ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe Integer -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Integer -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Integer -> [Name] -> TypeName)
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe Integer -> [Name] -> TypeName)
charTypeName)
             ParsecT Void SQLStream (Reader Dialect) ([Name] -> TypeName)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> TypeName)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> TypeName)
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ([Name] -> TypeName)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> TypeName)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Name] -> TypeName
TypeName)
    nameOfType :: Parser [Name]
nameOfType = Parser [Name]
reservedTypeNames Parser [Name] -> Parser [Name] -> Parser [Name]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser [Name]
names
    charTypeName :: ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe Integer -> [Name] -> TypeName)
charTypeName = Parser [Name]
charSet Parser [Name]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([Name] -> Maybe Integer -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Integer -> [Name] -> TypeName)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> ([Name] -> Parser [Name] -> Parser [Name]
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option [] Parser [Name]
tcollate Parser [Name]
-> ([Name] -> Maybe Integer -> [Name] -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([Name] -> Maybe Integer -> [Name] -> TypeName)
forall (f :: * -> *) d a b c t.
Applicative f =>
f d -> (a -> b -> c -> d -> t) -> f (c -> b -> a -> t)
<$$$$> [Name] -> Maybe Integer -> [Name] -> [Name] -> TypeName
CharTypeName)
                   ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe Integer -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Integer -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Integer -> [Name] -> TypeName)
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [Name] -> Parser [Name]
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [] Parser [Name]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([Name] -> Maybe Integer -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Integer -> [Name] -> TypeName)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (Parser [Name]
tcollate Parser [Name]
-> ([Name] -> Maybe Integer -> [Name] -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([Name] -> Maybe Integer -> [Name] -> TypeName)
forall (f :: * -> *) d a b c t.
Applicative f =>
f d -> (a -> b -> c -> d -> t) -> f (c -> b -> a -> t)
<$$$$> [Name] -> Maybe Integer -> [Name] -> [Name] -> TypeName
CharTypeName)
    typeNameWithParens :: ParsecT Void SQLStream (Reader Dialect) ([Name] -> TypeName)
typeNameWithParens =
        (Parser Char
openParen Parser Char
-> ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT Void SQLStream (Reader Dialect) Integer
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Void SQLStream (Reader Dialect) Integer
unsignedInteger)
        ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT
     Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> TypeName)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (Parser Char
closeParen Parser Char
-> ParsecT
     Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
-> ParsecT
     Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT
  Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
precMaybeSuffix
              ParsecT
  Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
-> ParsecT
     Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
-> ParsecT
     Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (ParsecT
  Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
precScaleTypeName ParsecT
  Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
-> ParsecT
     Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
-> ParsecT
     Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT
  Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
precLengthTypeName) ParsecT
  Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
-> Parser Char
-> ParsecT
     Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser Char
closeParen)
    precMaybeSuffix :: ParsecT
  Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
precMaybeSuffix = ((Maybe Integer -> [Name] -> TypeName)
-> (Integer -> Maybe Integer) -> Integer -> [Name] -> TypeName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Maybe Integer
forall a. a -> Maybe a
Just) ((Maybe Integer -> [Name] -> TypeName)
 -> Integer -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Integer -> [Name] -> TypeName)
-> ParsecT
     Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe Integer -> [Name] -> TypeName)
timeTypeName ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe Integer -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Integer -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Integer -> [Name] -> TypeName)
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe Integer -> [Name] -> TypeName)
charTypeName)
                      ParsecT
  Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
-> ParsecT
     Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
-> ParsecT
     Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Integer -> [Name] -> TypeName)
-> ParsecT
     Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (([Name] -> Integer -> TypeName) -> Integer -> [Name] -> TypeName
forall a b c. (a -> b -> c) -> b -> a -> c
flip [Name] -> Integer -> TypeName
PrecTypeName)
    precScaleTypeName :: ParsecT
  Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
precScaleTypeName = (Parser Char
comma Parser Char
-> ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT Void SQLStream (Reader Dialect) Integer
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Void SQLStream (Reader Dialect) Integer
unsignedInteger) ParsecT Void SQLStream (Reader Dialect) Integer
-> ([Name] -> Integer -> Integer -> TypeName)
-> ParsecT
     Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
forall (f :: * -> *) c a b t.
Applicative f =>
f c -> (a -> b -> c -> t) -> f (b -> a -> t)
<$$$> [Name] -> Integer -> Integer -> TypeName
PrecScaleTypeName
    precLengthTypeName :: ParsecT
  Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
precLengthTypeName =
        PrecMultiplier -> Maybe PrecMultiplier
forall a. a -> Maybe a
Just (PrecMultiplier -> Maybe PrecMultiplier)
-> ParsecT Void SQLStream (Reader Dialect) PrecMultiplier
-> ParsecT Void SQLStream (Reader Dialect) (Maybe PrecMultiplier)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) PrecMultiplier
lobPrecSuffix
        ParsecT Void SQLStream (Reader Dialect) (Maybe PrecMultiplier)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe PrecMultiplier -> Integer -> [Name] -> TypeName)
-> ParsecT
     Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (ParsecT Void SQLStream (Reader Dialect) PrecUnits
-> ParsecT Void SQLStream (Reader Dialect) (Maybe PrecUnits)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ParsecT Void SQLStream (Reader Dialect) PrecUnits
lobUnits ParsecT Void SQLStream (Reader Dialect) (Maybe PrecUnits)
-> ([Name]
    -> Integer -> Maybe PrecMultiplier -> Maybe PrecUnits -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe PrecMultiplier -> Integer -> [Name] -> TypeName)
forall (f :: * -> *) d a b c t.
Applicative f =>
f d -> (a -> b -> c -> d -> t) -> f (c -> b -> a -> t)
<$$$$> [Name]
-> Integer -> Maybe PrecMultiplier -> Maybe PrecUnits -> TypeName
PrecLengthTypeName)
        ParsecT
  Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
-> ParsecT
     Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
-> ParsecT
     Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe PrecMultiplier
-> ParsecT Void SQLStream (Reader Dialect) (Maybe PrecMultiplier)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe PrecMultiplier
forall a. Maybe a
Nothing ParsecT Void SQLStream (Reader Dialect) (Maybe PrecMultiplier)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe PrecMultiplier -> Integer -> [Name] -> TypeName)
-> ParsecT
     Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> ((PrecUnits -> Maybe PrecUnits
forall a. a -> Maybe a
Just (PrecUnits -> Maybe PrecUnits)
-> ParsecT Void SQLStream (Reader Dialect) PrecUnits
-> ParsecT Void SQLStream (Reader Dialect) (Maybe PrecUnits)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) PrecUnits
lobUnits) ParsecT Void SQLStream (Reader Dialect) (Maybe PrecUnits)
-> ([Name]
    -> Integer -> Maybe PrecMultiplier -> Maybe PrecUnits -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe PrecMultiplier -> Integer -> [Name] -> TypeName)
forall (f :: * -> *) d a b c t.
Applicative f =>
f d -> (a -> b -> c -> d -> t) -> f (c -> b -> a -> t)
<$$$$> [Name]
-> Integer -> Maybe PrecMultiplier -> Maybe PrecUnits -> TypeName
PrecLengthTypeName)
    timeTypeName :: ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe Integer -> [Name] -> TypeName)
timeTypeName = ParsecT Void SQLStream (Reader Dialect) Bool
tz ParsecT Void SQLStream (Reader Dialect) Bool
-> ([Name] -> Maybe Integer -> Bool -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Integer -> [Name] -> TypeName)
forall (f :: * -> *) c a b t.
Applicative f =>
f c -> (a -> b -> c -> t) -> f (b -> a -> t)
<$$$> [Name] -> Maybe Integer -> Bool -> TypeName
TimeTypeName
    ----------------------------
    lobPrecSuffix :: ParsecT Void SQLStream (Reader Dialect) PrecMultiplier
lobPrecSuffix = PrecMultiplier
PrecK PrecMultiplier
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) PrecMultiplier
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"k"
                    ParsecT Void SQLStream (Reader Dialect) PrecMultiplier
-> ParsecT Void SQLStream (Reader Dialect) PrecMultiplier
-> ParsecT Void SQLStream (Reader Dialect) PrecMultiplier
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> PrecMultiplier
PrecM PrecMultiplier
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) PrecMultiplier
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"m"
                    ParsecT Void SQLStream (Reader Dialect) PrecMultiplier
-> ParsecT Void SQLStream (Reader Dialect) PrecMultiplier
-> ParsecT Void SQLStream (Reader Dialect) PrecMultiplier
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> PrecMultiplier
PrecG PrecMultiplier
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) PrecMultiplier
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"g"
                    ParsecT Void SQLStream (Reader Dialect) PrecMultiplier
-> ParsecT Void SQLStream (Reader Dialect) PrecMultiplier
-> ParsecT Void SQLStream (Reader Dialect) PrecMultiplier
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> PrecMultiplier
PrecT PrecMultiplier
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) PrecMultiplier
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"t"
                    ParsecT Void SQLStream (Reader Dialect) PrecMultiplier
-> ParsecT Void SQLStream (Reader Dialect) PrecMultiplier
-> ParsecT Void SQLStream (Reader Dialect) PrecMultiplier
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> PrecMultiplier
PrecP PrecMultiplier
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) PrecMultiplier
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"p"
    lobUnits :: ParsecT Void SQLStream (Reader Dialect) PrecUnits
lobUnits = PrecUnits
PrecCharacters PrecUnits
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) PrecUnits
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"characters"
               -- char and byte are the oracle spelling
               -- todo: move these to oracle dialect
               ParsecT Void SQLStream (Reader Dialect) PrecUnits
-> ParsecT Void SQLStream (Reader Dialect) PrecUnits
-> ParsecT Void SQLStream (Reader Dialect) PrecUnits
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> PrecUnits
PrecCharacters PrecUnits
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) PrecUnits
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"char"
               ParsecT Void SQLStream (Reader Dialect) PrecUnits
-> ParsecT Void SQLStream (Reader Dialect) PrecUnits
-> ParsecT Void SQLStream (Reader Dialect) PrecUnits
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> PrecUnits
PrecOctets PrecUnits
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) PrecUnits
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"octets"
               ParsecT Void SQLStream (Reader Dialect) PrecUnits
-> ParsecT Void SQLStream (Reader Dialect) PrecUnits
-> ParsecT Void SQLStream (Reader Dialect) PrecUnits
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> PrecUnits
PrecOctets PrecUnits
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) PrecUnits
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"byte"
    tz :: ParsecT Void SQLStream (Reader Dialect) Bool
tz = Bool
True Bool
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) Bool
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"with", Text
"time",Text
"zone"]
         ParsecT Void SQLStream (Reader Dialect) Bool
-> ParsecT Void SQLStream (Reader Dialect) Bool
-> ParsecT Void SQLStream (Reader Dialect) Bool
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Bool
False Bool
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) Bool
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"without", Text
"time",Text
"zone"]
    charSet :: Parser [Name]
charSet = [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"character", Text
"set"] ParsecT Void SQLStream (Reader Dialect) ()
-> Parser [Name] -> Parser [Name]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser [Name]
names
    tcollate :: Parser [Name]
tcollate = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"collate" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser [Name] -> Parser [Name]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser [Name]
names
    ----------------------------
    tnSuffix :: Parser (TypeName -> TypeName)
tnSuffix = Parser (TypeName -> TypeName)
multiset Parser (TypeName -> TypeName)
-> Parser (TypeName -> TypeName) -> Parser (TypeName -> TypeName)
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser (TypeName -> TypeName)
array
    multiset :: Parser (TypeName -> TypeName)
multiset = TypeName -> TypeName
MultisetTypeName (TypeName -> TypeName)
-> ParsecT Void SQLStream (Reader Dialect) ()
-> Parser (TypeName -> TypeName)
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"multiset"
    array :: Parser (TypeName -> TypeName)
array = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"array" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser (TypeName -> TypeName) -> Parser (TypeName -> TypeName)
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*>
        (ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Integer)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT Void SQLStream (Reader Dialect) Integer
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
brackets ParsecT Void SQLStream (Reader Dialect) Integer
unsignedInteger) ParsecT Void SQLStream (Reader Dialect) (Maybe Integer)
-> (TypeName -> Maybe Integer -> TypeName)
-> Parser (TypeName -> TypeName)
forall (f :: * -> *) b a c.
Applicative f =>
f b -> (a -> b -> c) -> f (a -> c)
<$$> TypeName -> Maybe Integer -> TypeName
ArrayTypeName)
    ----------------------------
    -- this parser handles the fixed set of multi word
    -- type names, plus all the type names which are
    -- reserved words
    reservedTypeNames :: Parser [Name]
reservedTypeNames = do
        [Text]
stn <- (Dialect -> [Text]) -> Parser [Text]
forall a. (Dialect -> a) -> Parser a
askDialect Dialect -> [Text]
diSpecialTypeNames
        (Name -> [Name] -> [Name]
forall a. a -> [a] -> [a]
:[]) (Name -> [Name]) -> ([Text] -> Name) -> [Text] -> [Name]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe (Text, Text) -> Text -> Name
Name Maybe (Text, Text)
forall a. Maybe a
Nothing (Text -> Name) -> ([Text] -> Text) -> [Text] -> Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> Text
T.unwords ([Text] -> [Name]) -> Parser [Text] -> Parser [Name]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Text] -> Parser [Text]
makeKeywordTree [Text]
stn
        

{-
= Scalar expressions

== simple literals

See the stringToken lexer below for notes on string literal syntax.
-}

stringLit :: Parser ScalarExpr
stringLit :: Parser ScalarExpr
stringLit = (\(Text
s,Text
e,Text
t) -> Text -> Text -> Text -> ScalarExpr
StringLit Text
s Text
e Text
t) ((Text, Text, Text) -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) (Text, Text, Text)
-> Parser ScalarExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) (Text, Text, Text)
stringTokExtend

numberLit :: Parser ScalarExpr
numberLit :: Parser ScalarExpr
numberLit = Text -> ScalarExpr
NumLit (Text -> ScalarExpr) -> Parser Text -> Parser ScalarExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool -> Parser Text
sqlNumberTok Bool
False

simpleLiteral :: Parser ScalarExpr
simpleLiteral :: Parser ScalarExpr
simpleLiteral = Parser ScalarExpr
numberLit Parser ScalarExpr -> Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser ScalarExpr
stringLit

{-
== star, param, host param

=== star

used in select *, select x.*, and agg(*) variations, and some other
places as well. The parser doesn't attempt to check that the star is
in a valid context, it parses it OK in any scalar expression context.
-}

star :: Parser ScalarExpr
star :: Parser ScalarExpr
star = ScalarExpr
Star ScalarExpr -> Parser Text -> Parser ScalarExpr
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> Parser Text
symbol Text
"*"

{-
== parameter

unnamed parameter or named parameter
use in e.g. select * from t where a = ?
select x from t where x > :param
-}

parameter :: Parser ScalarExpr
parameter :: Parser ScalarExpr
parameter = [Parser ScalarExpr] -> Parser ScalarExpr
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
    [ScalarExpr
Parameter ScalarExpr -> Parser Char -> Parser ScalarExpr
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Parser Char
questionMark
    ,Text -> Maybe Text -> ScalarExpr
HostParameter
     (Text -> Maybe Text -> ScalarExpr)
-> Parser Text
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe Text -> ScalarExpr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Text
hostParamTok
     ParsecT Void SQLStream (Reader Dialect) (Maybe Text -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Text)
-> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Text -> ParsecT Void SQLStream (Reader Dialect) (Maybe Text)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> Parser Text
keyword Text
"indicator" Parser Text -> Parser Text -> Parser Text
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Text
hostParamTok)]

-- == positional arg

positionalArg :: Parser ScalarExpr
positionalArg :: Parser ScalarExpr
positionalArg = Int -> ScalarExpr
PositionalArg (Int -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) Int -> Parser ScalarExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) Int
positionalArgTok

{-
== parens

scalar expression parens, row ctor and scalar subquery
-}

parensExpr :: Parser ScalarExpr
parensExpr :: Parser ScalarExpr
parensExpr = Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser ScalarExpr -> Parser ScalarExpr)
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b. (a -> b) -> a -> b
$ [Parser ScalarExpr] -> Parser ScalarExpr
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
    [SubQueryExprType -> QueryExpr -> ScalarExpr
SubQueryExpr SubQueryExprType
SqSq (QueryExpr -> ScalarExpr) -> Parser QueryExpr -> Parser ScalarExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser QueryExpr
queryExpr
    ,[ScalarExpr] -> ScalarExpr
ctor ([ScalarExpr] -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> Parser ScalarExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a. Parser a -> Parser [a]
commaSep1 Parser ScalarExpr
scalarExpr]
  where
    ctor :: [ScalarExpr] -> ScalarExpr
ctor [ScalarExpr
a] = ScalarExpr -> ScalarExpr
Parens ScalarExpr
a
    ctor [ScalarExpr]
as = [Name] -> [ScalarExpr] -> ScalarExpr
SpecialOp [Maybe (Text, Text) -> Text -> Name
Name Maybe (Text, Text)
forall a. Maybe a
Nothing Text
"rowctor"] [ScalarExpr]
as

{-
== case, cast, exists, unique, array/multiset constructor, interval

All of these start with a fixed keyword which is reserved, so no other
syntax can start with the same keyword.

=== case expression
-}

caseExpr :: Parser ScalarExpr
caseExpr :: Parser ScalarExpr
caseExpr =
    Maybe ScalarExpr
-> [([ScalarExpr], ScalarExpr)] -> Maybe ScalarExpr -> ScalarExpr
Case (Maybe ScalarExpr
 -> [([ScalarExpr], ScalarExpr)] -> Maybe ScalarExpr -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([([ScalarExpr], ScalarExpr)] -> Maybe ScalarExpr -> ScalarExpr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"case" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional Parser ScalarExpr
scalarExpr)
         ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([([ScalarExpr], ScalarExpr)] -> Maybe ScalarExpr -> ScalarExpr)
-> ParsecT
     Void SQLStream (Reader Dialect) [([ScalarExpr], ScalarExpr)]
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe ScalarExpr -> ScalarExpr)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) ([ScalarExpr], ScalarExpr)
-> ParsecT
     Void SQLStream (Reader Dialect) [([ScalarExpr], ScalarExpr)]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
some ParsecT Void SQLStream (Reader Dialect) ([ScalarExpr], ScalarExpr)
whenClause
         ParsecT
  Void SQLStream (Reader Dialect) (Maybe ScalarExpr -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
-> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional Parser ScalarExpr
elseClause
         Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) () -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"end"
  where
   whenClause :: ParsecT Void SQLStream (Reader Dialect) ([ScalarExpr], ScalarExpr)
whenClause = (,) ([ScalarExpr] -> ScalarExpr -> ([ScalarExpr], ScalarExpr))
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ([ScalarExpr], ScalarExpr))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"when" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a. Parser a -> Parser [a]
commaSep1 Parser ScalarExpr
scalarExpr)
                    ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ScalarExpr -> ([ScalarExpr], ScalarExpr))
-> Parser ScalarExpr
-> ParsecT
     Void SQLStream (Reader Dialect) ([ScalarExpr], ScalarExpr)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"then" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr
scalarExpr)
   elseClause :: Parser ScalarExpr
elseClause = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"else" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr
scalarExpr

{-
=== cast

cast: cast(expr as type)
-}

cast :: Parser ScalarExpr
cast :: Parser ScalarExpr
cast = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"cast" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*>
       Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (ScalarExpr -> TypeName -> ScalarExpr
Cast (ScalarExpr -> TypeName -> ScalarExpr)
-> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) (TypeName -> ScalarExpr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ScalarExpr
scalarExpr
                    ParsecT Void SQLStream (Reader Dialect) (TypeName -> ScalarExpr)
-> Parser TypeName -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"as" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser TypeName -> Parser TypeName
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser TypeName
typeName))

{-
=== convert

convertSqlServer: SqlServer dialect CONVERT(data_type(length), expression, style)
-}

convertSqlServer :: Parser ScalarExpr
convertSqlServer :: Parser ScalarExpr
convertSqlServer = (Dialect -> Bool) -> ParsecT Void SQLStream (Reader Dialect) ()
guardDialect Dialect -> Bool
diConvertFunction
                   ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"convert" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*>
                   Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (TypeName -> ScalarExpr -> Maybe Integer -> ScalarExpr
Convert (TypeName -> ScalarExpr -> Maybe Integer -> ScalarExpr)
-> Parser TypeName
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> Maybe Integer -> ScalarExpr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser TypeName
typeName ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ScalarExpr -> Maybe Integer -> ScalarExpr)
-> Parser ScalarExpr
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe Integer -> ScalarExpr)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Parser Char
comma Parser Char -> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr
scalarExpr)
                      ParsecT
  Void SQLStream (Reader Dialect) (Maybe Integer -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Integer)
-> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Integer)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Parser Char
comma Parser Char
-> ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT Void SQLStream (Reader Dialect) Integer
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Void SQLStream (Reader Dialect) Integer
unsignedInteger))

{-
=== exists, unique

subquery expression:
[exists|unique] (queryexpr)
-}

subquery :: Parser ScalarExpr
subquery :: Parser ScalarExpr
subquery = SubQueryExprType -> QueryExpr -> ScalarExpr
SubQueryExpr (SubQueryExprType -> QueryExpr -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) SubQueryExprType
-> ParsecT
     Void SQLStream (Reader Dialect) (QueryExpr -> ScalarExpr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) SubQueryExprType
sqkw ParsecT Void SQLStream (Reader Dialect) (QueryExpr -> ScalarExpr)
-> Parser QueryExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser QueryExpr -> Parser QueryExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens Parser QueryExpr
queryExpr
  where
    sqkw :: ParsecT Void SQLStream (Reader Dialect) SubQueryExprType
sqkw = SubQueryExprType
SqExists SubQueryExprType
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) SubQueryExprType
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"exists" ParsecT Void SQLStream (Reader Dialect) SubQueryExprType
-> ParsecT Void SQLStream (Reader Dialect) SubQueryExprType
-> ParsecT Void SQLStream (Reader Dialect) SubQueryExprType
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> SubQueryExprType
SqUnique SubQueryExprType
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) SubQueryExprType
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"unique"

-- === array/multiset constructor

arrayCtor :: Parser ScalarExpr
arrayCtor :: Parser ScalarExpr
arrayCtor = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"array" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    [Parser ScalarExpr] -> Parser ScalarExpr
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
    [QueryExpr -> ScalarExpr
ArrayCtor (QueryExpr -> ScalarExpr) -> Parser QueryExpr -> Parser ScalarExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser QueryExpr -> Parser QueryExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens Parser QueryExpr
queryExpr
    ,ScalarExpr -> [ScalarExpr] -> ScalarExpr
Array ([Name] -> ScalarExpr
Iden [Maybe (Text, Text) -> Text -> Name
Name Maybe (Text, Text)
forall a. Maybe a
Nothing Text
"array"]) ([ScalarExpr] -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> Parser ScalarExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
brackets (Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a. Parser a -> Parser [a]
commaSep Parser ScalarExpr
scalarExpr)]

{-
As far as I can tell, table(query expr) is just syntax sugar for
multiset(query expr). It must be there for compatibility or something.
-}

multisetCtor :: Parser ScalarExpr
multisetCtor :: Parser ScalarExpr
multisetCtor =
    [Parser ScalarExpr] -> Parser ScalarExpr
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
    [Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"multiset" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
     [Parser ScalarExpr] -> Parser ScalarExpr
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
     [QueryExpr -> ScalarExpr
MultisetQueryCtor (QueryExpr -> ScalarExpr) -> Parser QueryExpr -> Parser ScalarExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser QueryExpr -> Parser QueryExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens Parser QueryExpr
queryExpr
     ,[ScalarExpr] -> ScalarExpr
MultisetCtor ([ScalarExpr] -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> Parser ScalarExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
brackets (Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a. Parser a -> Parser [a]
commaSep Parser ScalarExpr
scalarExpr)]
    ,Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"table" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
     QueryExpr -> ScalarExpr
MultisetQueryCtor (QueryExpr -> ScalarExpr) -> Parser QueryExpr -> Parser ScalarExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser QueryExpr -> Parser QueryExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens Parser QueryExpr
queryExpr]

nextValueFor :: Parser ScalarExpr
nextValueFor :: Parser ScalarExpr
nextValueFor = [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"next",Text
"value",Text
"for"] ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    [Name] -> ScalarExpr
NextValueFor ([Name] -> ScalarExpr) -> Parser [Name] -> Parser ScalarExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names

{-
=== interval

interval literals are a special case and we follow the grammar less
permissively here

parse SQL interval literals, something like
interval '5' day (3)
or
interval '5' month

if the literal looks like this:
interval 'something'

then it is parsed as a regular typed literal. It must have a
interval-datetime-field suffix to parse as an intervallit

It uses try because of a conflict with interval type names: todo, fix
this. also fix the monad -> applicative
-}

intervalLit :: Parser ScalarExpr
intervalLit :: Parser ScalarExpr
intervalLit = Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"interval" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> do
    Maybe Sign
s <- ParsecT Void SQLStream (Reader Dialect) Sign
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Sign)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (ParsecT Void SQLStream (Reader Dialect) Sign
 -> ParsecT Void SQLStream (Reader Dialect) (Maybe Sign))
-> ParsecT Void SQLStream (Reader Dialect) Sign
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Sign)
forall a b. (a -> b) -> a -> b
$ [ParsecT Void SQLStream (Reader Dialect) Sign]
-> ParsecT Void SQLStream (Reader Dialect) Sign
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [Sign
Plus Sign
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) Sign
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
symbol_ Text
"+"
                              ,Sign
Minus Sign
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) Sign
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
symbol_ Text
"-"]
    Text
lit <- Parser Text
singleQuotesOnlyStringTok
    Maybe (IntervalTypeField, Maybe IntervalTypeField)
q <- ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (IntervalTypeField, Maybe IntervalTypeField)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe (IntervalTypeField, Maybe IntervalTypeField))
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (IntervalTypeField, Maybe IntervalTypeField)
intervalQualifier
    Maybe Sign
-> Text
-> Maybe (IntervalTypeField, Maybe IntervalTypeField)
-> Parser ScalarExpr
forall {f :: * -> *}.
MonadFail f =>
Maybe Sign
-> Text
-> Maybe (IntervalTypeField, Maybe IntervalTypeField)
-> f ScalarExpr
mkIt Maybe Sign
s Text
lit Maybe (IntervalTypeField, Maybe IntervalTypeField)
q)
  where
    mkIt :: Maybe Sign
-> Text
-> Maybe (IntervalTypeField, Maybe IntervalTypeField)
-> f ScalarExpr
mkIt Maybe Sign
Nothing Text
val Maybe (IntervalTypeField, Maybe IntervalTypeField)
Nothing = ScalarExpr -> f ScalarExpr
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ScalarExpr -> f ScalarExpr) -> ScalarExpr -> f ScalarExpr
forall a b. (a -> b) -> a -> b
$ TypeName -> Text -> ScalarExpr
TypedLit ([Name] -> TypeName
TypeName [Maybe (Text, Text) -> Text -> Name
Name Maybe (Text, Text)
forall a. Maybe a
Nothing Text
"interval"]) Text
val
    mkIt Maybe Sign
s Text
val (Just (IntervalTypeField
a,Maybe IntervalTypeField
b)) = ScalarExpr -> f ScalarExpr
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ScalarExpr -> f ScalarExpr) -> ScalarExpr -> f ScalarExpr
forall a b. (a -> b) -> a -> b
$ Maybe Sign
-> Text
-> IntervalTypeField
-> Maybe IntervalTypeField
-> ScalarExpr
IntervalLit Maybe Sign
s Text
val IntervalTypeField
a Maybe IntervalTypeField
b
    mkIt (Just {}) Text
_val Maybe (IntervalTypeField, Maybe IntervalTypeField)
Nothing = String -> f ScalarExpr
forall a. String -> f a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"cannot use sign without interval qualifier"

{-
== typed literal, app, special, aggregate, window, iden

All of these start with identifiers (some of the special functions
start with reserved keywords).

they are all variations on suffixes on the basic identifier parser

The windows is a suffix on the app parser

=== iden prefix term

all the scalar expressions which start with an identifier

(todo: really put all of them here instead of just some of them)
-}

idenExpr :: Parser ScalarExpr
idenExpr :: Parser ScalarExpr
idenExpr =
    -- todo: work out how to left factor this
    Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try (TypeName -> Text -> ScalarExpr
TypedLit (TypeName -> Text -> ScalarExpr)
-> Parser TypeName
-> ParsecT Void SQLStream (Reader Dialect) (Text -> ScalarExpr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser TypeName
typeName ParsecT Void SQLStream (Reader Dialect) (Text -> ScalarExpr)
-> Parser Text -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Text
singleQuotesOnlyStringTok)
    Parser ScalarExpr -> Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Parser [Name]
names Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
-> Parser ScalarExpr
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> ([Name] -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option [Name] -> ScalarExpr
Iden ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
app)
    Parser ScalarExpr -> Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser ScalarExpr
keywordFunctionOrIden
  where
    -- special cases for keywords that can be parsed as an iden or app
    keywordFunctionOrIden :: Parser ScalarExpr
keywordFunctionOrIden = Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try (Parser ScalarExpr -> Parser ScalarExpr)
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b. (a -> b) -> a -> b
$ do
        Text
x <- [Text] -> Maybe Text -> Parser Text
unquotedIdentifierTok [] Maybe Text
forall a. Maybe a
Nothing
        Dialect
d <- (Dialect -> Dialect) -> Parser Dialect
forall a. (Dialect -> a) -> Parser a
askDialect Dialect -> Dialect
forall a. a -> a
id
        let i :: Bool
i = Text -> Text
T.toLower Text
x Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` Dialect -> [Text]
diIdentifierKeywords Dialect
d
            a :: Bool
a = Text -> Text
T.toLower Text
x Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` Dialect -> [Text]
diAppKeywords Dialect
d
        case () of
            ()
_  | Bool
i Bool -> Bool -> Bool
&& Bool
a -> [Name] -> Parser [Name]
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Maybe (Text, Text) -> Text -> Name
Name Maybe (Text, Text)
forall a. Maybe a
Nothing Text
x] Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
-> Parser ScalarExpr
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> ([Name] -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option [Name] -> ScalarExpr
Iden ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
app
               | Bool
i -> ScalarExpr -> Parser ScalarExpr
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Name] -> ScalarExpr
Iden [Maybe (Text, Text) -> Text -> Name
Name Maybe (Text, Text)
forall a. Maybe a
Nothing Text
x])
               | Bool
a -> [Name] -> Parser [Name]
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Maybe (Text, Text) -> Text -> Name
Name Maybe (Text, Text)
forall a. Maybe a
Nothing Text
x] Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
-> Parser ScalarExpr
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
app
               | Bool
otherwise -> String -> Parser ScalarExpr
forall a. String -> ParsecT Void SQLStream (Reader Dialect) a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
""


{-
=== special

These are keyword operators which don't look like normal prefix,
postfix or infix binary operators. They mostly look like function
application but with keywords in the argument list instead of commas
to separate the arguments.

the special op keywords
parse an operator which is
operatorname(firstArg keyword0 arg0 keyword1 arg1 etc.)
-}

data SpecialOpKFirstArg = SOKNone
                        | SOKOptional
                        | SOKMandatory

specialOpK :: Text -- name of the operator
           -> SpecialOpKFirstArg -- has a first arg without a keyword
           -> [(Text,Bool)] -- the other args with their keywords
                              -- and whether they are optional
           -> Parser ScalarExpr
specialOpK :: Text -> SpecialOpKFirstArg -> [(Text, Bool)] -> Parser ScalarExpr
specialOpK Text
opName SpecialOpKFirstArg
firstArg [(Text, Bool)]
kws =
    Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
opName ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> do
    Parser Char -> ParsecT Void SQLStream (Reader Dialect) ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void Parser Char
openParen
    let pfa :: Parser ScalarExpr
pfa = do
              ScalarExpr
e <- Parser ScalarExpr
scalarExpr
              -- check we haven't parsed the first
              -- keyword as an identifier
              case (ScalarExpr
e,[(Text, Bool)]
kws) of
                  (Iden [Name Maybe (Text, Text)
Nothing Text
i], (Text
k,Bool
_):[(Text, Bool)]
_)
                      | Text -> Text
T.toLower Text
i Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
k ->
                          String -> ParsecT Void SQLStream (Reader Dialect) ()
forall a. String -> ParsecT Void SQLStream (Reader Dialect) a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> ParsecT Void SQLStream (Reader Dialect) ())
-> String -> ParsecT Void SQLStream (Reader Dialect) ()
forall a b. (a -> b) -> a -> b
$ String
"cannot use keyword here: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Text -> String
T.unpack Text
i
                  (ScalarExpr, [(Text, Bool)])
_ -> () -> ParsecT Void SQLStream (Reader Dialect) ()
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
              ScalarExpr -> Parser ScalarExpr
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ScalarExpr
e
    Maybe ScalarExpr
fa <- case SpecialOpKFirstArg
firstArg of
         SpecialOpKFirstArg
SOKNone -> Maybe ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe ScalarExpr
forall a. Maybe a
Nothing
         SpecialOpKFirstArg
SOKOptional -> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try Parser ScalarExpr
pfa)
         SpecialOpKFirstArg
SOKMandatory -> ScalarExpr -> Maybe ScalarExpr
forall a. a -> Maybe a
Just (ScalarExpr -> Maybe ScalarExpr)
-> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ScalarExpr
pfa
    [Maybe (Text, ScalarExpr)]
as <- ((Text, Bool)
 -> ParsecT
      Void SQLStream (Reader Dialect) (Maybe (Text, ScalarExpr)))
-> [(Text, Bool)]
-> ParsecT
     Void SQLStream (Reader Dialect) [Maybe (Text, ScalarExpr)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Text, Bool)
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe (Text, ScalarExpr))
parseArg [(Text, Bool)]
kws
    Parser Char -> ParsecT Void SQLStream (Reader Dialect) ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void Parser Char
closeParen
    ScalarExpr -> Parser ScalarExpr
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ScalarExpr -> Parser ScalarExpr)
-> ScalarExpr -> Parser ScalarExpr
forall a b. (a -> b) -> a -> b
$ [Name] -> Maybe ScalarExpr -> [(Text, ScalarExpr)] -> ScalarExpr
SpecialOpK [Maybe (Text, Text) -> Text -> Name
Name Maybe (Text, Text)
forall a. Maybe a
Nothing Text
opName] Maybe ScalarExpr
fa ([(Text, ScalarExpr)] -> ScalarExpr)
-> [(Text, ScalarExpr)] -> ScalarExpr
forall a b. (a -> b) -> a -> b
$ [Maybe (Text, ScalarExpr)] -> [(Text, ScalarExpr)]
forall a. [Maybe a] -> [a]
catMaybes [Maybe (Text, ScalarExpr)]
as
  where
    parseArg :: (Text, Bool)
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe (Text, ScalarExpr))
parseArg (Text
nm,Bool
mand) =
        let p :: Parser ScalarExpr
p = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
nm ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Parser ScalarExpr
scalarExpr
        in (ScalarExpr -> (Text, ScalarExpr))
-> Maybe ScalarExpr -> Maybe (Text, ScalarExpr)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text
nm,) (Maybe ScalarExpr -> Maybe (Text, ScalarExpr))
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe (Text, ScalarExpr))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> if Bool
mand
                          then ScalarExpr -> Maybe ScalarExpr
forall a. a -> Maybe a
Just (ScalarExpr -> Maybe ScalarExpr)
-> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ScalarExpr
p
                          else Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try Parser ScalarExpr
p)

{-
The actual operators:

EXTRACT( date_part FROM expression )

POSITION( string1 IN string2 )

SUBSTRING(extraction_string FROM starting_position [FOR length]
[COLLATE collation_name])

CONVERT(char_value USING conversion_char_name)

TRANSLATE(char_value USING translation_name)

OVERLAY(string PLACING embedded_string FROM start
[FOR length])

TRIM( [ [{LEADING | TRAILING | BOTH}] [removal_char] FROM ]
target_string
[COLLATE collation_name] )
-}

specialOpKs :: Parser ScalarExpr
specialOpKs :: Parser ScalarExpr
specialOpKs = [Parser ScalarExpr] -> Parser ScalarExpr
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice ([Parser ScalarExpr] -> Parser ScalarExpr)
-> [Parser ScalarExpr] -> Parser ScalarExpr
forall a b. (a -> b) -> a -> b
$ (Parser ScalarExpr -> Parser ScalarExpr)
-> [Parser ScalarExpr] -> [Parser ScalarExpr]
forall a b. (a -> b) -> [a] -> [b]
map Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try
    [Parser ScalarExpr
extract, Parser ScalarExpr
position, Parser ScalarExpr
substring, Parser ScalarExpr
convert, Parser ScalarExpr
translate, Parser ScalarExpr
overlay, Parser ScalarExpr
trim]

extract :: Parser ScalarExpr
extract :: Parser ScalarExpr
extract = Text -> SpecialOpKFirstArg -> [(Text, Bool)] -> Parser ScalarExpr
specialOpK Text
"extract" SpecialOpKFirstArg
SOKMandatory [(Text
"from", Bool
True)]

position :: Parser ScalarExpr
position :: Parser ScalarExpr
position = Text -> SpecialOpKFirstArg -> [(Text, Bool)] -> Parser ScalarExpr
specialOpK Text
"position" SpecialOpKFirstArg
SOKMandatory [(Text
"in", Bool
True)]

{-
strictly speaking, the substring must have at least one of from and
for, but the parser doens't enforce this
-}

substring :: Parser ScalarExpr
substring :: Parser ScalarExpr
substring = Text -> SpecialOpKFirstArg -> [(Text, Bool)] -> Parser ScalarExpr
specialOpK Text
"substring" SpecialOpKFirstArg
SOKMandatory
                [(Text
"from", Bool
False),(Text
"for", Bool
False)]

convert :: Parser ScalarExpr
convert :: Parser ScalarExpr
convert = Text -> SpecialOpKFirstArg -> [(Text, Bool)] -> Parser ScalarExpr
specialOpK Text
"convert" SpecialOpKFirstArg
SOKMandatory [(Text
"using", Bool
True)]


translate :: Parser ScalarExpr
translate :: Parser ScalarExpr
translate = Text -> SpecialOpKFirstArg -> [(Text, Bool)] -> Parser ScalarExpr
specialOpK Text
"translate" SpecialOpKFirstArg
SOKMandatory [(Text
"using", Bool
True)]

overlay :: Parser ScalarExpr
overlay :: Parser ScalarExpr
overlay = Text -> SpecialOpKFirstArg -> [(Text, Bool)] -> Parser ScalarExpr
specialOpK Text
"overlay" SpecialOpKFirstArg
SOKMandatory
                [(Text
"placing", Bool
True),(Text
"from", Bool
True),(Text
"for", Bool
False)]

{-
trim is too different because of the optional char, so a custom parser
the both ' ' is filled in as the default if either parts are missing
in the source
-}

trim :: Parser ScalarExpr
trim :: Parser ScalarExpr
trim =
    Text -> Parser Text
keyword Text
"trim" Parser Text -> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Text -> Text -> ScalarExpr -> ScalarExpr
mkTrim
            (Text -> Text -> ScalarExpr -> ScalarExpr)
-> Parser Text
-> ParsecT
     Void SQLStream (Reader Dialect) (Text -> ScalarExpr -> ScalarExpr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Parser Text -> Parser Text
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option Text
"both" Parser Text
sides
            ParsecT
  Void SQLStream (Reader Dialect) (Text -> ScalarExpr -> ScalarExpr)
-> Parser Text
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Parser Text -> Parser Text
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option Text
" " Parser Text
singleQuotesOnlyStringTok
            ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"from" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr
scalarExpr))
  where
    sides :: Parser Text
sides = [Parser Text] -> Parser Text
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [Text
"leading" Text -> ParsecT Void SQLStream (Reader Dialect) () -> Parser Text
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"leading"
                   ,Text
"trailing" Text -> ParsecT Void SQLStream (Reader Dialect) () -> Parser Text
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"trailing"
                   ,Text
"both" Text -> ParsecT Void SQLStream (Reader Dialect) () -> Parser Text
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"both"]
    mkTrim :: Text -> Text -> ScalarExpr -> ScalarExpr
mkTrim Text
fa Text
ch ScalarExpr
fr =
      [Name] -> Maybe ScalarExpr -> [(Text, ScalarExpr)] -> ScalarExpr
SpecialOpK [Maybe (Text, Text) -> Text -> Name
Name Maybe (Text, Text)
forall a. Maybe a
Nothing Text
"trim"] Maybe ScalarExpr
forall a. Maybe a
Nothing
          ([(Text, ScalarExpr)] -> ScalarExpr)
-> [(Text, ScalarExpr)] -> ScalarExpr
forall a b. (a -> b) -> a -> b
$ [Maybe (Text, ScalarExpr)] -> [(Text, ScalarExpr)]
forall a. [Maybe a] -> [a]
catMaybes [(Text, ScalarExpr) -> Maybe (Text, ScalarExpr)
forall a. a -> Maybe a
Just (Text
fa,Text -> Text -> Text -> ScalarExpr
StringLit Text
"'" Text
"'" Text
ch)
                      ,(Text, ScalarExpr) -> Maybe (Text, ScalarExpr)
forall a. a -> Maybe a
Just (Text
"from", ScalarExpr
fr)]

{-
=== app, aggregate, window

This parses all these variations:
normal function application with just a csv of scalar exprs
aggregate variations (distinct, order by in parens, filter and where
  suffixes)
window apps (fn/agg followed by over)

This code is also a little dense like the typename code because of
left factoring, later they will even have to be partially combined
together.
-}

app :: Parser ([Name] -> ScalarExpr)
app :: ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
app =
    Parser Char
openParen Parser Char
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> [ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)]
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
    [Parser SetQuantifier
duplicates
     Parser SetQuantifier
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (SetQuantifier -> [Name] -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a. Parser a -> Parser [a]
commaSep1 Parser ScalarExpr
scalarExpr
           ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([ScalarExpr] -> SetQuantifier -> [Name] -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (SetQuantifier -> [Name] -> ScalarExpr)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (([SortSpec]
-> ParsecT Void SQLStream (Reader Dialect) [SortSpec]
-> ParsecT Void SQLStream (Reader Dialect) [SortSpec]
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option [] ParsecT Void SQLStream (Reader Dialect) [SortSpec]
orderBy ParsecT Void SQLStream (Reader Dialect) [SortSpec]
-> Parser Char
-> ParsecT Void SQLStream (Reader Dialect) [SortSpec]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser Char
closeParen)
                 ParsecT Void SQLStream (Reader Dialect) [SortSpec]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([SortSpec]
      -> [ScalarExpr] -> SetQuantifier -> [Name] -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([ScalarExpr] -> SetQuantifier -> [Name] -> ScalarExpr)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional Parser ScalarExpr
afilter ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
-> ([Name]
    -> SetQuantifier
    -> [ScalarExpr]
    -> [SortSpec]
    -> Maybe ScalarExpr
    -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([SortSpec]
      -> [ScalarExpr] -> SetQuantifier -> [Name] -> ScalarExpr)
forall (f :: * -> *) e a b c d t.
Applicative f =>
f e -> (a -> b -> c -> d -> e -> t) -> f (d -> c -> b -> a -> t)
<$$$$$> [Name]
-> SetQuantifier
-> [ScalarExpr]
-> [SortSpec]
-> Maybe ScalarExpr
-> ScalarExpr
AggregateApp)))
     -- separate cases with no all or distinct which must have at
     -- least one scalar expr
    ,Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a. Parser a -> Parser [a]
commaSep1 Parser ScalarExpr
scalarExpr
     ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([ScalarExpr] -> [Name] -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> [ParsecT
   Void
   SQLStream
   (Reader Dialect)
   ([ScalarExpr] -> [Name] -> ScalarExpr)]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([ScalarExpr] -> [Name] -> ScalarExpr)
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
          [Parser Char
closeParen Parser Char
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([ScalarExpr] -> [Name] -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([ScalarExpr] -> [Name] -> ScalarExpr)
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> [ParsecT
   Void
   SQLStream
   (Reader Dialect)
   ([ScalarExpr] -> [Name] -> ScalarExpr)]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([ScalarExpr] -> [Name] -> ScalarExpr)
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
                         [ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([ScalarExpr] -> [Name] -> ScalarExpr)
window
                         ,ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([ScalarExpr] -> [Name] -> ScalarExpr)
withinGroup
                         ,(ScalarExpr -> Maybe ScalarExpr
forall a. a -> Maybe a
Just (ScalarExpr -> Maybe ScalarExpr)
-> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ScalarExpr
afilter) ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
-> ([Name] -> [ScalarExpr] -> Maybe ScalarExpr -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([ScalarExpr] -> [Name] -> ScalarExpr)
forall (f :: * -> *) c a b t.
Applicative f =>
f c -> (a -> b -> c -> t) -> f (b -> a -> t)
<$$$> [Name] -> [ScalarExpr] -> Maybe ScalarExpr -> ScalarExpr
aggAppWithoutDupeOrd
                         ,([ScalarExpr] -> [Name] -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([ScalarExpr] -> [Name] -> ScalarExpr)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (([Name] -> [ScalarExpr] -> ScalarExpr)
-> [ScalarExpr] -> [Name] -> ScalarExpr
forall a b c. (a -> b -> c) -> b -> a -> c
flip [Name] -> [ScalarExpr] -> ScalarExpr
App)]
          ,ParsecT Void SQLStream (Reader Dialect) [SortSpec]
orderBy ParsecT Void SQLStream (Reader Dialect) [SortSpec]
-> Parser Char
-> ParsecT Void SQLStream (Reader Dialect) [SortSpec]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser Char
closeParen
           ParsecT Void SQLStream (Reader Dialect) [SortSpec]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([SortSpec] -> [ScalarExpr] -> [Name] -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([ScalarExpr] -> [Name] -> ScalarExpr)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional Parser ScalarExpr
afilter ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
-> ([Name]
    -> [ScalarExpr] -> [SortSpec] -> Maybe ScalarExpr -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([SortSpec] -> [ScalarExpr] -> [Name] -> ScalarExpr)
forall (f :: * -> *) d a b c t.
Applicative f =>
f d -> (a -> b -> c -> d -> t) -> f (c -> b -> a -> t)
<$$$$> [Name]
-> [ScalarExpr] -> [SortSpec] -> Maybe ScalarExpr -> ScalarExpr
aggAppWithoutDupe)]
     -- no scalarExprs: duplicates and order by not allowed
    ,([] [ScalarExpr]
-> Parser Char
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Parser Char
closeParen) ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([ScalarExpr] -> [Name] -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> ([ScalarExpr] -> [Name] -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([ScalarExpr] -> [Name] -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([ScalarExpr] -> [Name] -> ScalarExpr)
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option (([Name] -> [ScalarExpr] -> ScalarExpr)
-> [ScalarExpr] -> [Name] -> ScalarExpr
forall a b c. (a -> b -> c) -> b -> a -> c
flip [Name] -> [ScalarExpr] -> ScalarExpr
App) (ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([ScalarExpr] -> [Name] -> ScalarExpr)
window ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([ScalarExpr] -> [Name] -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([ScalarExpr] -> [Name] -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([ScalarExpr] -> [Name] -> ScalarExpr)
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([ScalarExpr] -> [Name] -> ScalarExpr)
withinGroup)
    ]
  where
    aggAppWithoutDupeOrd :: [Name] -> [ScalarExpr] -> Maybe ScalarExpr -> ScalarExpr
aggAppWithoutDupeOrd [Name]
n [ScalarExpr]
es Maybe ScalarExpr
f = [Name]
-> SetQuantifier
-> [ScalarExpr]
-> [SortSpec]
-> Maybe ScalarExpr
-> ScalarExpr
AggregateApp [Name]
n SetQuantifier
SQDefault [ScalarExpr]
es [] Maybe ScalarExpr
f
    aggAppWithoutDupe :: [Name]
-> [ScalarExpr] -> [SortSpec] -> Maybe ScalarExpr -> ScalarExpr
aggAppWithoutDupe [Name]
n = [Name]
-> SetQuantifier
-> [ScalarExpr]
-> [SortSpec]
-> Maybe ScalarExpr
-> ScalarExpr
AggregateApp [Name]
n SetQuantifier
SQDefault

afilter :: Parser ScalarExpr
afilter :: Parser ScalarExpr
afilter = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"filter" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"where" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr
scalarExpr)

withinGroup :: Parser ([ScalarExpr] -> [Name] -> ScalarExpr)
withinGroup :: ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([ScalarExpr] -> [Name] -> ScalarExpr)
withinGroup =
    ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"within", Text
"group"] ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) [SortSpec]
-> ParsecT Void SQLStream (Reader Dialect) [SortSpec]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Void SQLStream (Reader Dialect) [SortSpec]
-> ParsecT Void SQLStream (Reader Dialect) [SortSpec]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens ParsecT Void SQLStream (Reader Dialect) [SortSpec]
orderBy) ParsecT Void SQLStream (Reader Dialect) [SortSpec]
-> ([Name] -> [ScalarExpr] -> [SortSpec] -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([ScalarExpr] -> [Name] -> ScalarExpr)
forall (f :: * -> *) c a b t.
Applicative f =>
f c -> (a -> b -> c -> t) -> f (b -> a -> t)
<$$$> [Name] -> [ScalarExpr] -> [SortSpec] -> ScalarExpr
AggregateAppGroup

{-
==== window

parse a window call as a suffix of a regular function call
this looks like this:
functionname(args) over ([partition by ids] [order by orderitems])

No support for explicit frames yet.

TODO: add window support for other aggregate variations, needs some
changes to the syntax also
-}

window :: Parser ([ScalarExpr] -> [Name] -> ScalarExpr)
window :: ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([ScalarExpr] -> [Name] -> ScalarExpr)
window =
  Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"over" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Char -> Parser Char
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Char
openParen Parser Char
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> [ScalarExpr]
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option [] ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
partitionBy
  ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([ScalarExpr] -> [ScalarExpr] -> [Name] -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([ScalarExpr] -> [Name] -> ScalarExpr)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> ([SortSpec]
-> ParsecT Void SQLStream (Reader Dialect) [SortSpec]
-> ParsecT Void SQLStream (Reader Dialect) [SortSpec]
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option [] ParsecT Void SQLStream (Reader Dialect) [SortSpec]
orderBy
        ParsecT Void SQLStream (Reader Dialect) [SortSpec]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([SortSpec]
      -> [ScalarExpr] -> [ScalarExpr] -> [Name] -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([ScalarExpr] -> [ScalarExpr] -> [Name] -> ScalarExpr)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> ((ParsecT Void SQLStream (Reader Dialect) Frame
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Frame)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ParsecT Void SQLStream (Reader Dialect) Frame
frameClause ParsecT Void SQLStream (Reader Dialect) (Maybe Frame)
-> Parser Char
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Frame)
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser Char
closeParen) ParsecT Void SQLStream (Reader Dialect) (Maybe Frame)
-> ([Name]
    -> [ScalarExpr]
    -> [ScalarExpr]
    -> [SortSpec]
    -> Maybe Frame
    -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([SortSpec]
      -> [ScalarExpr] -> [ScalarExpr] -> [Name] -> ScalarExpr)
forall (f :: * -> *) e a b c d t.
Applicative f =>
f e -> (a -> b -> c -> d -> e -> t) -> f (d -> c -> b -> a -> t)
<$$$$$> [Name]
-> [ScalarExpr]
-> [ScalarExpr]
-> [SortSpec]
-> Maybe Frame
-> ScalarExpr
WindowApp))
  where
    partitionBy :: ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
partitionBy = [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"partition",Text
"by"] ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a. Parser a -> Parser [a]
commaSep1 Parser ScalarExpr
scalarExpr
    frameClause :: ParsecT Void SQLStream (Reader Dialect) Frame
frameClause =
        ParsecT Void SQLStream (Reader Dialect) FrameRows
frameRowsRange -- TODO: this 'and' could be an issue
        ParsecT Void SQLStream (Reader Dialect) FrameRows
-> ParsecT Void SQLStream (Reader Dialect) (FrameRows -> Frame)
-> ParsecT Void SQLStream (Reader Dialect) Frame
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> [ParsecT Void SQLStream (Reader Dialect) (FrameRows -> Frame)]
-> ParsecT Void SQLStream (Reader Dialect) (FrameRows -> Frame)
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [(Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"between" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) FramePos
-> ParsecT Void SQLStream (Reader Dialect) FramePos
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Bool -> ParsecT Void SQLStream (Reader Dialect) FramePos
frameLimit Bool
True)
                      ParsecT Void SQLStream (Reader Dialect) FramePos
-> ParsecT
     Void SQLStream (Reader Dialect) (FramePos -> FrameRows -> Frame)
-> ParsecT Void SQLStream (Reader Dialect) (FrameRows -> Frame)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> ((Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"and" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) FramePos
-> ParsecT Void SQLStream (Reader Dialect) FramePos
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Bool -> ParsecT Void SQLStream (Reader Dialect) FramePos
frameLimit Bool
True)
                            ParsecT Void SQLStream (Reader Dialect) FramePos
-> (FrameRows -> FramePos -> FramePos -> Frame)
-> ParsecT
     Void SQLStream (Reader Dialect) (FramePos -> FrameRows -> Frame)
forall (f :: * -> *) c a b t.
Applicative f =>
f c -> (a -> b -> c -> t) -> f (b -> a -> t)
<$$$> FrameRows -> FramePos -> FramePos -> Frame
FrameBetween)
                      -- maybe this should still use a b expression
                      -- for consistency
                     ,Bool -> ParsecT Void SQLStream (Reader Dialect) FramePos
frameLimit Bool
False ParsecT Void SQLStream (Reader Dialect) FramePos
-> ParsecT
     Void SQLStream (Reader Dialect) (FramePos -> FrameRows -> Frame)
-> ParsecT Void SQLStream (Reader Dialect) (FrameRows -> Frame)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (FramePos -> FrameRows -> Frame)
-> ParsecT
     Void SQLStream (Reader Dialect) (FramePos -> FrameRows -> Frame)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((FrameRows -> FramePos -> Frame) -> FramePos -> FrameRows -> Frame
forall a b c. (a -> b -> c) -> b -> a -> c
flip FrameRows -> FramePos -> Frame
FrameFrom)]
    frameRowsRange :: ParsecT Void SQLStream (Reader Dialect) FrameRows
frameRowsRange = FrameRows
FrameRows FrameRows
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) FrameRows
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"rows"
                     ParsecT Void SQLStream (Reader Dialect) FrameRows
-> ParsecT Void SQLStream (Reader Dialect) FrameRows
-> ParsecT Void SQLStream (Reader Dialect) FrameRows
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> FrameRows
FrameRange FrameRows
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) FrameRows
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"range"
    frameLimit :: Bool -> ParsecT Void SQLStream (Reader Dialect) FramePos
frameLimit Bool
useB =
        [ParsecT Void SQLStream (Reader Dialect) FramePos]
-> ParsecT Void SQLStream (Reader Dialect) FramePos
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
        [FramePos
Current FramePos
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) FramePos
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"current", Text
"row"]
         -- todo: create an automatic left factor for stuff like this
        ,Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"unbounded" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) FramePos
-> ParsecT Void SQLStream (Reader Dialect) FramePos
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*>
         [ParsecT Void SQLStream (Reader Dialect) FramePos]
-> ParsecT Void SQLStream (Reader Dialect) FramePos
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [FramePos
UnboundedPreceding FramePos
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) FramePos
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"preceding"
                ,FramePos
UnboundedFollowing FramePos
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) FramePos
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"following"]
        ,(if Bool
useB then Parser ScalarExpr
scalarExprB else Parser ScalarExpr
scalarExpr)
         Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> FramePos)
-> ParsecT Void SQLStream (Reader Dialect) FramePos
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (ScalarExpr -> FramePos
Preceding (ScalarExpr -> FramePos)
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> FramePos)
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"preceding"
               ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> FramePos)
-> ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> FramePos)
-> ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> FramePos)
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ScalarExpr -> FramePos
Following (ScalarExpr -> FramePos)
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> FramePos)
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"following")
        ]

{-
== suffixes

These are all generic suffixes on any scalar expr

=== in

in: two variations:
a in (expr0, expr1, ...)
a in (queryexpr)
-}

inSuffix :: Parser (ScalarExpr -> ScalarExpr)
inSuffix :: ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
inSuffix =
    Bool -> InPredValue -> ScalarExpr -> ScalarExpr
mkIn (Bool -> InPredValue -> ScalarExpr -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) Bool
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (InPredValue -> ScalarExpr -> ScalarExpr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) Bool
inty
         ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (InPredValue -> ScalarExpr -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) InPredValue
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) InPredValue
-> ParsecT Void SQLStream (Reader Dialect) InPredValue
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens ([ParsecT Void SQLStream (Reader Dialect) InPredValue]
-> ParsecT Void SQLStream (Reader Dialect) InPredValue
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
                     [QueryExpr -> InPredValue
InQueryExpr (QueryExpr -> InPredValue)
-> Parser QueryExpr
-> ParsecT Void SQLStream (Reader Dialect) InPredValue
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser QueryExpr
queryExpr
                     ,[ScalarExpr] -> InPredValue
InList ([ScalarExpr] -> InPredValue)
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> ParsecT Void SQLStream (Reader Dialect) InPredValue
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a. Parser a -> Parser [a]
commaSep1 Parser ScalarExpr
scalarExpr])
  where
    inty :: ParsecT Void SQLStream (Reader Dialect) Bool
inty = [ParsecT Void SQLStream (Reader Dialect) Bool]
-> ParsecT Void SQLStream (Reader Dialect) Bool
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [Bool
True Bool
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) Bool
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"in"
                  ,Bool
False Bool
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) Bool
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"not",Text
"in"]]
    mkIn :: Bool -> InPredValue -> ScalarExpr -> ScalarExpr
mkIn Bool
i InPredValue
v ScalarExpr
e = Bool -> ScalarExpr -> InPredValue -> ScalarExpr
In Bool
i ScalarExpr
e InPredValue
v

{-
=== between

between:
expr between expr and expr

There is a complication when parsing between - when parsing the second
expression it is ambiguous when you hit an 'and' whether it is a
binary operator or part of the between. This code follows what
postgres does, which might be standard across SQL implementations,
which is that you can't have a binary and operator in the middle
expression in a between unless it is wrapped in parens. The 'bExpr
parsing' is used to create alternative scalar expression parser which
is identical to the normal one expect it doesn't recognise the binary
and operator. This is the call to scalarExprB.
-}

betweenSuffix :: Parser (ScalarExpr -> ScalarExpr)
betweenSuffix :: ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
betweenSuffix =
    Name -> ScalarExpr -> ScalarExpr -> ScalarExpr -> ScalarExpr
makeOp (Name -> ScalarExpr -> ScalarExpr -> ScalarExpr -> ScalarExpr)
-> (Text -> Name)
-> Text
-> ScalarExpr
-> ScalarExpr
-> ScalarExpr
-> ScalarExpr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe (Text, Text) -> Text -> Name
Name Maybe (Text, Text)
forall a. Maybe a
Nothing
           (Text -> ScalarExpr -> ScalarExpr -> ScalarExpr -> ScalarExpr)
-> Parser Text
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ScalarExpr -> ScalarExpr -> ScalarExpr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Text
opName
           ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ScalarExpr -> ScalarExpr -> ScalarExpr -> ScalarExpr)
-> Parser ScalarExpr
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ScalarExpr -> ScalarExpr)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser ScalarExpr
scalarExprB
           ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> Parser ScalarExpr
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"and" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr
scalarExprB)
  where
    opName :: Parser Text
opName = [Parser Text] -> Parser Text
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
             [Text
"between" Text -> ParsecT Void SQLStream (Reader Dialect) () -> Parser Text
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"between"
             ,Text
"not between" Text -> ParsecT Void SQLStream (Reader Dialect) () -> Parser Text
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"not",Text
"between"])]
    makeOp :: Name -> ScalarExpr -> ScalarExpr -> ScalarExpr -> ScalarExpr
makeOp Name
n ScalarExpr
b ScalarExpr
c ScalarExpr
a = [Name] -> [ScalarExpr] -> ScalarExpr
SpecialOp [Name
n] [ScalarExpr
a,ScalarExpr
b,ScalarExpr
c]

{-
=== quantified comparison

a = any (select * from t)
-}

quantifiedComparisonSuffix :: Parser (ScalarExpr -> ScalarExpr)
quantifiedComparisonSuffix :: ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
quantifiedComparisonSuffix = do
    Name
c <- Parser Name
comp
    CompPredQuantifier
cq <- ParsecT Void SQLStream (Reader Dialect) CompPredQuantifier
compQuan
    QueryExpr
q <- Parser QueryExpr -> Parser QueryExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens Parser QueryExpr
queryExpr
    (ScalarExpr -> ScalarExpr)
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((ScalarExpr -> ScalarExpr)
 -> ParsecT
      Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr))
-> (ScalarExpr -> ScalarExpr)
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
forall a b. (a -> b) -> a -> b
$ \ScalarExpr
v -> ScalarExpr
-> [Name] -> CompPredQuantifier -> QueryExpr -> ScalarExpr
QuantifiedComparison ScalarExpr
v [Name
c] CompPredQuantifier
cq QueryExpr
q
  where
    comp :: Parser Name
comp = Maybe (Text, Text) -> Text -> Name
Name Maybe (Text, Text)
forall a. Maybe a
Nothing (Text -> Name) -> Parser Text -> Parser Name
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Parser Text] -> Parser Text
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice ((Text -> Parser Text) -> [Text] -> [Parser Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Parser Text
symbol
           [Text
"=", Text
"<>", Text
"<=", Text
"<", Text
">", Text
">="])
    compQuan :: ParsecT Void SQLStream (Reader Dialect) CompPredQuantifier
compQuan = [ParsecT Void SQLStream (Reader Dialect) CompPredQuantifier]
-> ParsecT Void SQLStream (Reader Dialect) CompPredQuantifier
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
               [CompPredQuantifier
CPAny CompPredQuantifier
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) CompPredQuantifier
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"any"
               ,CompPredQuantifier
CPSome CompPredQuantifier
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) CompPredQuantifier
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"some"
               ,CompPredQuantifier
CPAll CompPredQuantifier
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) CompPredQuantifier
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"all"]

{-
=== match

a match (select a from t)
-}

matchPredicateSuffix :: Parser (ScalarExpr -> ScalarExpr)
matchPredicateSuffix :: ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
matchPredicateSuffix = do
    Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"match"
    Bool
u <- Bool
-> ParsecT Void SQLStream (Reader Dialect) Bool
-> ParsecT Void SQLStream (Reader Dialect) Bool
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option Bool
False (Bool
True Bool
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) Bool
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"unique")
    QueryExpr
q <- Parser QueryExpr -> Parser QueryExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens Parser QueryExpr
queryExpr
    (ScalarExpr -> ScalarExpr)
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((ScalarExpr -> ScalarExpr)
 -> ParsecT
      Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr))
-> (ScalarExpr -> ScalarExpr)
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
forall a b. (a -> b) -> a -> b
$ \ScalarExpr
v -> ScalarExpr -> Bool -> QueryExpr -> ScalarExpr
Match ScalarExpr
v Bool
u QueryExpr
q

-- === array subscript

arraySuffix :: Parser (ScalarExpr -> ScalarExpr)
arraySuffix :: ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
arraySuffix = do
    [ScalarExpr]
es <- ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
brackets (Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a. Parser a -> Parser [a]
commaSep Parser ScalarExpr
scalarExpr)
    (ScalarExpr -> ScalarExpr)
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((ScalarExpr -> ScalarExpr)
 -> ParsecT
      Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr))
-> (ScalarExpr -> ScalarExpr)
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
forall a b. (a -> b) -> a -> b
$ \ScalarExpr
v -> ScalarExpr -> [ScalarExpr] -> ScalarExpr
Array ScalarExpr
v [ScalarExpr]
es

{-
=== escape

It is going to be really difficult to support an arbitrary character
for the escape now there is a separate lexer ...

TODO: this needs fixing. Escape is only part of other nodes, and not a
separate suffix.
-}

{-escapeSuffix :: Parser (ScalarExpr -> ScalarExpr)
escapeSuffix = do
    ctor <- choice
            [Escape <$ keyword_ "escape"
            ,UEscape <$ keyword_ "uescape"]
    c <- escapeChar
    pure $ \v -> ctor v c
  where
    escapeChar :: Parser Char
    escapeChar = (identifierTok [] Nothing <|> symbolTok Nothing) >>= oneOnly
    oneOnly :: String -> Parser Char
    oneOnly c = case c of
                   [c'] -> pure c'
                   _ -> fail "escape char must be single char"
-}

-- === collate

collateSuffix:: Parser (ScalarExpr -> ScalarExpr)
collateSuffix :: ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
collateSuffix = do
    Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"collate"
    [Name]
i <- Parser [Name]
names
    (ScalarExpr -> ScalarExpr)
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((ScalarExpr -> ScalarExpr)
 -> ParsecT
      Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr))
-> (ScalarExpr -> ScalarExpr)
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
forall a b. (a -> b) -> a -> b
$ \ScalarExpr
v -> ScalarExpr -> [Name] -> ScalarExpr
Collate ScalarExpr
v [Name]
i

{-
== odbc syntax

the parser supports three kinds of odbc syntax, two of which are
scalar expressions (the other is a variation on joins)
-}


odbcExpr :: Parser ScalarExpr
odbcExpr :: Parser ScalarExpr
odbcExpr = Parser Text
-> Parser Text -> Parser ScalarExpr -> Parser ScalarExpr
forall (m :: * -> *) open close a.
Applicative m =>
m open -> m close -> m a -> m a
between (Text -> Parser Text
symbol Text
"{") (Text -> Parser Text
symbol Text
"}")
           (Parser ScalarExpr
odbcTimeLit Parser ScalarExpr -> Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser ScalarExpr
odbcFunc)
  where
    odbcTimeLit :: Parser ScalarExpr
odbcTimeLit =
        OdbcLiteralType -> Text -> ScalarExpr
OdbcLiteral (OdbcLiteralType -> Text -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) OdbcLiteralType
-> ParsecT Void SQLStream (Reader Dialect) (Text -> ScalarExpr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [ParsecT Void SQLStream (Reader Dialect) OdbcLiteralType]
-> ParsecT Void SQLStream (Reader Dialect) OdbcLiteralType
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [OdbcLiteralType
OLDate OdbcLiteralType
-> Parser Text
-> ParsecT Void SQLStream (Reader Dialect) OdbcLiteralType
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> Parser Text
keyword Text
"d"
                               ,OdbcLiteralType
OLTime OdbcLiteralType
-> Parser Text
-> ParsecT Void SQLStream (Reader Dialect) OdbcLiteralType
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> Parser Text
keyword Text
"t"
                               ,OdbcLiteralType
OLTimestamp OdbcLiteralType
-> Parser Text
-> ParsecT Void SQLStream (Reader Dialect) OdbcLiteralType
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> Parser Text
keyword Text
"ts"]
                    ParsecT Void SQLStream (Reader Dialect) (Text -> ScalarExpr)
-> Parser Text -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Text
singleQuotesOnlyStringTok
    -- todo: this parser is too general, the expr part
    -- should be only a function call (from a whitelist of functions)
    -- or the extract operator
    odbcFunc :: Parser ScalarExpr
odbcFunc = ScalarExpr -> ScalarExpr
OdbcFunc (ScalarExpr -> ScalarExpr)
-> Parser ScalarExpr -> Parser ScalarExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Text -> Parser Text
keyword Text
"fn" Parser Text -> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr
scalarExpr)

{-
==  operators

The 'regular' operators in this parsing and in the abstract syntax are
unary prefix, unary postfix and binary infix operators. The operators
can be symbols (a + b), single keywords (a and b) or multiple keywords
(a is similar to b).

TODO: carefully review the precedences and associativities.

TODO: to fix the parsing completely, I think will need to parse
without precedence and associativity and fix up afterwards, since SQL
syntax is way too messy. It might be possible to avoid this if we
wanted to avoid extensibility and to not be concerned with parse error
messages, but both of these are too important.
-}

opTable :: Bool -> [[E.Operator Parser ScalarExpr]]
opTable :: Bool
-> [[Operator
       (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr]]
opTable Bool
bExpr =
        [-- parse match and quantified comparisons as postfix ops
          -- todo: left factor the quantified comparison with regular
          -- binary comparison, somehow
         [ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall {m :: * -> *} {a}. MonadPlus m => m (a -> a) -> Operator m a
postfix (ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
 -> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr)
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall a b. (a -> b) -> a -> b
$ ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
quantifiedComparisonSuffix
         ,ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall {m :: * -> *} {a}. MonadPlus m => m (a -> a) -> Operator m a
postfix ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
matchPredicateSuffix]

        ,[Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binarySymL Text
"."]

        ,[ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall {m :: * -> *} {a}. MonadPlus m => m (a -> a) -> Operator m a
postfix ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
arraySuffix
         ,ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall {m :: * -> *} {a}. MonadPlus m => m (a -> a) -> Operator m a
postfix ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
collateSuffix]

        ,[Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
prefixSym Text
"+", Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
prefixSym Text
"-"]

        ,[Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binarySymL Text
"^"]

        ,[Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binarySymL Text
"*"
         ,Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binarySymL Text
"/"
         ,Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binarySymL Text
"%"]

        ,[Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binarySymL Text
"+"
         ,Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binarySymL Text
"-"]

        ,[Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binarySymR Text
"||"
         ,Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
prefixSym Text
"~"
         ,Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binarySymR Text
"&"
         ,Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binarySymR Text
"|"]

        ,[Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binaryKeywordN Text
"overlaps"]

        ,[Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binaryKeywordN Text
"like"
         -- have to use try with inSuffix because of a conflict
         -- with 'in' in position function, and not between
         -- between also has a try in it to deal with 'not'
         -- ambiguity
         ,ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall {m :: * -> *} {a}. MonadPlus m => m (a -> a) -> Operator m a
postfix (ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
 -> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr)
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall a b. (a -> b) -> a -> b
$ ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
inSuffix
         ,ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall {m :: * -> *} {a}. MonadPlus m => m (a -> a) -> Operator m a
postfix ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
betweenSuffix]
         -- todo: figure out where to put the try?
         [Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr]
-> [Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr]
-> [Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr]
forall a. [a] -> [a] -> [a]
++ [Parser [Text]
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall {m :: * -> *} {e} {s}.
MonadParsec e s m =>
m [Text] -> Operator m ScalarExpr
binaryKeywordsN (Parser [Text]
 -> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr)
-> Parser [Text]
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall a b. (a -> b) -> a -> b
$ [Text] -> Parser [Text]
makeKeywordTree
             [Text
"not like"
             ,Text
"is similar to"
             ,Text
"is not similar to"]]
          [Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr]
-> [Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr]
-> [Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr]
forall a. [a] -> [a] -> [a]
++ [Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
multisetBinOp]

        ,[Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binarySymN Text
"<"
         ,Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binarySymN Text
">"
         ,Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binarySymN Text
">="
         ,Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binarySymN Text
"<="
         ,Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binarySymR Text
"!="
         ,Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binarySymR Text
"<>"
         ,Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binarySymR Text
"="]

        ,[Parser [Text]
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall {m :: * -> *} {e} {s}.
MonadParsec e s m =>
m [Text] -> Operator m ScalarExpr
postfixKeywords (Parser [Text]
 -> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr)
-> Parser [Text]
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall a b. (a -> b) -> a -> b
$ [Text] -> Parser [Text]
makeKeywordTree
             [Text
"is null"
             ,Text
"is not null"
             ,Text
"is true"
             ,Text
"is not true"
             ,Text
"is false"
             ,Text
"is not false"
             ,Text
"is unknown"
             ,Text
"is not unknown"]]
         [Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr]
-> [Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr]
-> [Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr]
forall a. [a] -> [a] -> [a]
++ [Parser [Text]
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall {m :: * -> *} {e} {s}.
MonadParsec e s m =>
m [Text] -> Operator m ScalarExpr
binaryKeywordsN (Parser [Text]
 -> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr)
-> Parser [Text]
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall a b. (a -> b) -> a -> b
$ [Text] -> Parser [Text]
makeKeywordTree
             [Text
"is distinct from"
             ,Text
"is not distinct from"]]

        ,[Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
prefixKeyword Text
"not"]

        ,[Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binaryKeywordL Text
"and" | Bool -> Bool
not Bool
bExpr]

        ,[Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binaryKeywordL Text
"or"]

       ]
  where
    binarySymL :: Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binarySymL Text
nm = ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall (m :: * -> *) a. m (a -> a -> a) -> Operator m a
E.InfixL (Text -> ScalarExpr -> ScalarExpr -> ScalarExpr
mkBinOp Text
nm (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ScalarExpr -> ScalarExpr)
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
symbol_ Text
nm)
    binarySymR :: Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binarySymR Text
nm = ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall (m :: * -> *) a. m (a -> a -> a) -> Operator m a
E.InfixR (Text -> ScalarExpr -> ScalarExpr -> ScalarExpr
mkBinOp Text
nm (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ScalarExpr -> ScalarExpr)
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
symbol_ Text
nm)
    binarySymN :: Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binarySymN Text
nm = ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall (m :: * -> *) a. m (a -> a -> a) -> Operator m a
E.InfixN (Text -> ScalarExpr -> ScalarExpr -> ScalarExpr
mkBinOp Text
nm (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ScalarExpr -> ScalarExpr)
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
symbol_ Text
nm)
    binaryKeywordN :: Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binaryKeywordN Text
nm = ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall (m :: * -> *) a. m (a -> a -> a) -> Operator m a
E.InfixN (Text -> ScalarExpr -> ScalarExpr -> ScalarExpr
mkBinOp Text
nm (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ScalarExpr -> ScalarExpr)
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
nm)
    binaryKeywordL :: Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
binaryKeywordL Text
nm = ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall (m :: * -> *) a. m (a -> a -> a) -> Operator m a
E.InfixL (Text -> ScalarExpr -> ScalarExpr -> ScalarExpr
mkBinOp Text
nm (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ScalarExpr -> ScalarExpr)
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
nm)
    mkBinOp :: Text -> ScalarExpr -> ScalarExpr -> ScalarExpr
mkBinOp Text
nm ScalarExpr
a ScalarExpr
b = ScalarExpr -> [Name] -> ScalarExpr -> ScalarExpr
BinOp ScalarExpr
a (Text -> [Name]
mkNm Text
nm) ScalarExpr
b
    prefixSym :: Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
prefixSym  Text
nm = ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall {m :: * -> *} {a}. MonadPlus m => m (a -> a) -> Operator m a
prefix ([Name] -> ScalarExpr -> ScalarExpr
PrefixOp (Text -> [Name]
mkNm Text
nm) (ScalarExpr -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
symbol_ Text
nm)
    prefixKeyword :: Text
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
prefixKeyword  Text
nm = ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall {m :: * -> *} {a}. MonadPlus m => m (a -> a) -> Operator m a
prefix ([Name] -> ScalarExpr -> ScalarExpr
PrefixOp (Text -> [Name]
mkNm Text
nm) (ScalarExpr -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
nm)
    mkNm :: Text -> [Name]
mkNm Text
nm = [Maybe (Text, Text) -> Text -> Name
Name Maybe (Text, Text)
forall a. Maybe a
Nothing Text
nm]
    binaryKeywordsN :: m [Text] -> Operator m ScalarExpr
binaryKeywordsN m [Text]
p =
        m (ScalarExpr -> ScalarExpr -> ScalarExpr) -> Operator m ScalarExpr
forall (m :: * -> *) a. m (a -> a -> a) -> Operator m a
E.InfixN (do
                 [Text]
o <- m [Text] -> m [Text]
forall a. m a -> m a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try m [Text]
p
                 (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> m (ScalarExpr -> ScalarExpr -> ScalarExpr)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (\ScalarExpr
a ScalarExpr
b -> ScalarExpr -> [Name] -> ScalarExpr -> ScalarExpr
BinOp ScalarExpr
a [Maybe (Text, Text) -> Text -> Name
Name Maybe (Text, Text)
forall a. Maybe a
Nothing (Text -> Name) -> Text -> Name
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
T.unwords [Text]
o] ScalarExpr
b))
    multisetBinOp :: Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
multisetBinOp = ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr
forall (m :: * -> *) a. m (a -> a -> a) -> Operator m a
E.InfixL (do
        Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"multiset"
        SetOperatorName
o <- [ParsecT Void SQLStream (Reader Dialect) SetOperatorName]
-> ParsecT Void SQLStream (Reader Dialect) SetOperatorName
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [SetOperatorName
Union SetOperatorName
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) SetOperatorName
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"union"
                    ,SetOperatorName
Intersect SetOperatorName
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) SetOperatorName
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"intersect"
                    ,SetOperatorName
Except SetOperatorName
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) SetOperatorName
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"except"]
        SetQuantifier
d <- SetQuantifier -> Parser SetQuantifier -> Parser SetQuantifier
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option SetQuantifier
SQDefault Parser SetQuantifier
duplicates
        (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ScalarExpr -> ScalarExpr)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (\ScalarExpr
a ScalarExpr
b -> ScalarExpr
-> SetOperatorName -> SetQuantifier -> ScalarExpr -> ScalarExpr
MultisetBinOp ScalarExpr
a SetOperatorName
o SetQuantifier
d ScalarExpr
b))
    postfixKeywords :: m [Text] -> Operator m ScalarExpr
postfixKeywords m [Text]
p =
      m (ScalarExpr -> ScalarExpr) -> Operator m ScalarExpr
forall {m :: * -> *} {a}. MonadPlus m => m (a -> a) -> Operator m a
postfix (m (ScalarExpr -> ScalarExpr) -> Operator m ScalarExpr)
-> m (ScalarExpr -> ScalarExpr) -> Operator m ScalarExpr
forall a b. (a -> b) -> a -> b
$ do
          [Text]
o <- m [Text] -> m [Text]
forall a. m a -> m a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try m [Text]
p
          (ScalarExpr -> ScalarExpr) -> m (ScalarExpr -> ScalarExpr)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((ScalarExpr -> ScalarExpr) -> m (ScalarExpr -> ScalarExpr))
-> (ScalarExpr -> ScalarExpr) -> m (ScalarExpr -> ScalarExpr)
forall a b. (a -> b) -> a -> b
$ [Name] -> ScalarExpr -> ScalarExpr
PostfixOp [Maybe (Text, Text) -> Text -> Name
Name Maybe (Text, Text)
forall a. Maybe a
Nothing (Text -> Name) -> Text -> Name
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
T.unwords [Text]
o]
    -- parse repeated prefix or postfix operators
    postfix :: m (a -> a) -> Operator m a
postfix m (a -> a)
p = m (a -> a) -> Operator m a
forall (m :: * -> *) a. m (a -> a) -> Operator m a
E.Postfix (m (a -> a) -> Operator m a) -> m (a -> a) -> Operator m a
forall a b. (a -> b) -> a -> b
$ ((a -> a) -> (a -> a) -> a -> a) -> [a -> a] -> a -> a
forall a. (a -> a -> a) -> [a] -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 (((a -> a) -> (a -> a) -> a -> a) -> (a -> a) -> (a -> a) -> a -> a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.)) ([a -> a] -> a -> a) -> m [a -> a] -> m (a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (a -> a) -> m [a -> a]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
some m (a -> a)
p
    prefix :: m (a -> a) -> Operator m a
prefix m (a -> a)
p = m (a -> a) -> Operator m a
forall (m :: * -> *) a. m (a -> a) -> Operator m a
E.Prefix (m (a -> a) -> Operator m a) -> m (a -> a) -> Operator m a
forall a b. (a -> b) -> a -> b
$ ((a -> a) -> (a -> a) -> a -> a) -> [a -> a] -> a -> a
forall a. (a -> a -> a) -> [a] -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) ([a -> a] -> a -> a) -> m [a -> a] -> m (a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (a -> a) -> m [a -> a]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
some m (a -> a)
p

{-
== scalar expression top level

This parses most of the scalar exprs.The order of the parsers and use
of try is carefully done to make everything work. It is a little
fragile and could at least do with some heavy explanation. Update: the
'try's have migrated into the individual parsers, they still need
documenting/fixing.
-}

scalarExpr :: Parser ScalarExpr
scalarExpr :: Parser ScalarExpr
scalarExpr = Parser ScalarExpr
-> [[Operator
       (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr]]
-> Parser ScalarExpr
forall (m :: * -> *) a.
MonadPlus m =>
m a -> [[Operator m a]] -> m a
E.makeExprParser Parser ScalarExpr
term (Bool
-> [[Operator
       (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr]]
opTable Bool
False)

term :: Parser ScalarExpr
term :: Parser ScalarExpr
term = [Parser ScalarExpr] -> Parser ScalarExpr
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [Parser ScalarExpr
simpleLiteral
              ,Parser ScalarExpr
parameter
              ,Parser ScalarExpr
positionalArg
              ,Parser ScalarExpr
star
              ,Parser ScalarExpr
parensExpr
              ,Parser ScalarExpr
caseExpr
              ,Parser ScalarExpr
cast
              ,Parser ScalarExpr
convertSqlServer
              ,Parser ScalarExpr
arrayCtor
              ,Parser ScalarExpr
multisetCtor
              ,Parser ScalarExpr
nextValueFor
              ,Parser ScalarExpr
subquery
              ,Parser ScalarExpr
intervalLit
              ,Parser ScalarExpr
specialOpKs
              ,Parser ScalarExpr
idenExpr
              ,Parser ScalarExpr
odbcExpr]
       Parser ScalarExpr -> String -> Parser ScalarExpr
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"scalar expression"

-- expose the b expression for window frame clause range between

scalarExprB :: Parser ScalarExpr
scalarExprB :: Parser ScalarExpr
scalarExprB = Parser ScalarExpr
-> [[Operator
       (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr]]
-> Parser ScalarExpr
forall (m :: * -> *) a.
MonadPlus m =>
m a -> [[Operator m a]] -> m a
E.makeExprParser Parser ScalarExpr
term (Bool
-> [[Operator
       (ParsecT Void SQLStream (Reader Dialect)) ScalarExpr]]
opTable Bool
True)

{-
== helper parsers

This is used in interval literals and in interval type names.
-}

intervalQualifier :: Parser (IntervalTypeField,Maybe IntervalTypeField)
intervalQualifier :: ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (IntervalTypeField, Maybe IntervalTypeField)
intervalQualifier =
    (,) (IntervalTypeField
 -> Maybe IntervalTypeField
 -> (IntervalTypeField, Maybe IntervalTypeField))
-> ParsecT Void SQLStream (Reader Dialect) IntervalTypeField
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe IntervalTypeField
      -> (IntervalTypeField, Maybe IntervalTypeField))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) IntervalTypeField
intervalField
        ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe IntervalTypeField
   -> (IntervalTypeField, Maybe IntervalTypeField))
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe IntervalTypeField)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (IntervalTypeField, Maybe IntervalTypeField)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) IntervalTypeField
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe IntervalTypeField)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"to" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) IntervalTypeField
-> ParsecT Void SQLStream (Reader Dialect) IntervalTypeField
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Void SQLStream (Reader Dialect) IntervalTypeField
intervalField)
  where
    intervalField :: ParsecT Void SQLStream (Reader Dialect) IntervalTypeField
intervalField =
        Text -> Maybe (Integer, Maybe Integer) -> IntervalTypeField
Itf
        (Text -> Maybe (Integer, Maybe Integer) -> IntervalTypeField)
-> Parser Text
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe (Integer, Maybe Integer) -> IntervalTypeField)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Text
datetimeField
        ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe (Integer, Maybe Integer) -> IntervalTypeField)
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe (Integer, Maybe Integer))
-> ParsecT Void SQLStream (Reader Dialect) IntervalTypeField
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) (Integer, Maybe Integer)
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe (Integer, Maybe Integer))
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional
            (ParsecT Void SQLStream (Reader Dialect) (Integer, Maybe Integer)
-> ParsecT Void SQLStream (Reader Dialect) (Integer, Maybe Integer)
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens ((,) (Integer -> Maybe Integer -> (Integer, Maybe Integer))
-> ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Integer -> (Integer, Maybe Integer))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) Integer
unsignedInteger
                         ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe Integer -> (Integer, Maybe Integer))
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Integer)
-> ParsecT Void SQLStream (Reader Dialect) (Integer, Maybe Integer)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Integer)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Parser Char
comma Parser Char
-> ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT Void SQLStream (Reader Dialect) Integer
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Void SQLStream (Reader Dialect) Integer
unsignedInteger)))

{-
TODO: use datetime field in extract also
use a data type for the datetime field?
-}

datetimeField :: Parser Text
datetimeField :: Parser Text
datetimeField = [Parser Text] -> Parser Text
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice ((Text -> Parser Text) -> [Text] -> [Parser Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Parser Text
keyword [Text
"year",Text
"month",Text
"day"
                                    ,Text
"hour",Text
"minute",Text
"second"])
                Parser Text -> String -> Parser Text
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"datetime field"

{-
This is used in multiset operations (scalar expr), selects (query expr)
and set operations (query expr).
-}

duplicates :: Parser SetQuantifier
duplicates :: Parser SetQuantifier
duplicates =
    [Parser SetQuantifier] -> Parser SetQuantifier
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [SetQuantifier
All SetQuantifier
-> ParsecT Void SQLStream (Reader Dialect) ()
-> Parser SetQuantifier
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"all"
           ,SetQuantifier
Distinct SetQuantifier -> Parser Text -> Parser SetQuantifier
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> Parser Text
keyword Text
"distinct"]

{-
-------------------------------------------------

= query expressions

== select lists
-}

selectItem :: Parser (ScalarExpr,Maybe Name)
selectItem :: Parser (ScalarExpr, Maybe Name)
selectItem = (,) (ScalarExpr -> Maybe Name -> (ScalarExpr, Maybe Name))
-> Parser ScalarExpr
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Name -> (ScalarExpr, Maybe Name))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ScalarExpr
scalarExpr ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe Name -> (ScalarExpr, Maybe Name))
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Name)
-> Parser (ScalarExpr, Maybe Name)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Name -> ParsecT Void SQLStream (Reader Dialect) (Maybe Name)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional Parser Name
als
  where als :: Parser Name
als = ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"as") ParsecT Void SQLStream (Reader Dialect) (Maybe ())
-> Parser Name -> Parser Name
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Name
name

selectList :: Parser [(ScalarExpr,Maybe Name)]
selectList :: Parser [(ScalarExpr, Maybe Name)]
selectList = Parser (ScalarExpr, Maybe Name)
-> Parser [(ScalarExpr, Maybe Name)]
forall a. Parser a -> Parser [a]
commaSep1 Parser (ScalarExpr, Maybe Name)
selectItem

{-
== from

Here is the rough grammar for joins

tref
(cross | [natural] ([inner] | (left | right | full) [outer])) join
tref
[on expr | using (...)]

TODO: either use explicit 'operator precedence' parsers or build
expression parser for the 'tref operators' such as joins, lateral,
aliases.
-}

from :: Parser [TableRef]
from :: Parser [TableRef]
from = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"from" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser [TableRef] -> Parser [TableRef]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser TableRef -> Parser [TableRef]
forall a. Parser a -> Parser [a]
commaSep1 Parser TableRef
tref
  where
    -- TODO: use P (a->) for the join tref suffix
    -- chainl or buildexpressionparser
    tref :: Parser TableRef
tref = (Parser TableRef
nonJoinTref Parser TableRef -> String -> Parser TableRef
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"table ref") Parser TableRef -> (TableRef -> Parser TableRef) -> Parser TableRef
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> (a -> ParsecT Void SQLStream (Reader Dialect) b)
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (TableRef -> Parser TableRef) -> TableRef -> Parser TableRef
forall a. (a -> Parser a) -> a -> Parser a
optionSuffix TableRef -> Parser TableRef
joinTrefSuffix
    nonJoinTref :: Parser TableRef
nonJoinTref = [Parser TableRef] -> Parser TableRef
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
        [Parser TableRef -> Parser TableRef
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser TableRef -> Parser TableRef)
-> Parser TableRef -> Parser TableRef
forall a b. (a -> b) -> a -> b
$ [Parser TableRef] -> Parser TableRef
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
             [QueryExpr -> TableRef
TRQueryExpr (QueryExpr -> TableRef) -> Parser QueryExpr -> Parser TableRef
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser QueryExpr
queryExpr
             ,TableRef -> TableRef
TRParens (TableRef -> TableRef) -> Parser TableRef -> Parser TableRef
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser TableRef
tref]
        ,TableRef -> TableRef
TRLateral (TableRef -> TableRef) -> Parser TableRef -> Parser TableRef
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"lateral"
                        ParsecT Void SQLStream (Reader Dialect) ()
-> Parser TableRef -> Parser TableRef
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser TableRef
nonJoinTref)
        ,do
         [Name]
n <- Parser [Name]
names
         [Parser TableRef] -> Parser TableRef
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [[Name] -> [ScalarExpr] -> TableRef
TRFunction [Name]
n
                 ([ScalarExpr] -> TableRef)
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> Parser TableRef
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a. Parser a -> Parser [a]
commaSep Parser ScalarExpr
scalarExpr)
                ,TableRef -> Parser TableRef
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TableRef -> Parser TableRef) -> TableRef -> Parser TableRef
forall a b. (a -> b) -> a -> b
$ [Name] -> TableRef
TRSimple [Name]
n]
         -- todo: I think you can only have outer joins inside the oj,
         -- not sure.
        ,TableRef -> TableRef
TROdbc (TableRef -> TableRef) -> Parser TableRef -> Parser TableRef
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Text -> Parser Text
symbol Text
"{" Parser Text
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"oj" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser TableRef -> Parser TableRef
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser TableRef
tref Parser TableRef -> Parser Text -> Parser TableRef
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Text -> Parser Text
symbol Text
"}")
        ] Parser TableRef -> Parser (TableRef -> TableRef) -> Parser TableRef
forall a. Parser a -> Parser (a -> a) -> Parser a
<??> Parser (TableRef -> TableRef)
aliasSuffix
    aliasSuffix :: Parser (TableRef -> TableRef)
aliasSuffix = Parser Alias
fromAlias Parser Alias
-> (TableRef -> Alias -> TableRef) -> Parser (TableRef -> TableRef)
forall (f :: * -> *) b a c.
Applicative f =>
f b -> (a -> b -> c) -> f (a -> c)
<$$> TableRef -> Alias -> TableRef
TRAlias
    joinTrefSuffix :: TableRef -> Parser TableRef
joinTrefSuffix TableRef
t =
        ((TableRef
-> Bool -> JoinType -> TableRef -> Maybe JoinCondition -> TableRef
TRJoin TableRef
t (Bool -> JoinType -> TableRef -> Maybe JoinCondition -> TableRef)
-> ParsecT Void SQLStream (Reader Dialect) Bool
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (JoinType -> TableRef -> Maybe JoinCondition -> TableRef)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool
-> ParsecT Void SQLStream (Reader Dialect) Bool
-> ParsecT Void SQLStream (Reader Dialect) Bool
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option Bool
False (Bool
True Bool
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) Bool
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"natural")
                  ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (JoinType -> TableRef -> Maybe JoinCondition -> TableRef)
-> ParsecT Void SQLStream (Reader Dialect) JoinType
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (TableRef -> Maybe JoinCondition -> TableRef)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) JoinType
joinType
                  ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (TableRef -> Maybe JoinCondition -> TableRef)
-> Parser TableRef
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe JoinCondition -> TableRef)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser TableRef
nonJoinTref
                  ParsecT
  Void SQLStream (Reader Dialect) (Maybe JoinCondition -> TableRef)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe JoinCondition)
-> Parser TableRef
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) JoinCondition
-> ParsecT Void SQLStream (Reader Dialect) (Maybe JoinCondition)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ParsecT Void SQLStream (Reader Dialect) JoinCondition
joinCondition)
        Parser TableRef -> (TableRef -> Parser TableRef) -> Parser TableRef
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> (a -> ParsecT Void SQLStream (Reader Dialect) b)
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (TableRef -> Parser TableRef) -> TableRef -> Parser TableRef
forall a. (a -> Parser a) -> a -> Parser a
optionSuffix TableRef -> Parser TableRef
joinTrefSuffix) Parser TableRef -> String -> Parser TableRef
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
""

{-
TODO: factor the join stuff to produce better error messages (and make
it more readable)
-}

joinType :: Parser JoinType
joinType :: ParsecT Void SQLStream (Reader Dialect) JoinType
joinType = [ParsecT Void SQLStream (Reader Dialect) JoinType]
-> ParsecT Void SQLStream (Reader Dialect) JoinType
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
    [JoinType
JCross JoinType
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) JoinType
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"cross" ParsecT Void SQLStream (Reader Dialect) JoinType
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) JoinType
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"join"
    ,JoinType
JInner JoinType
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) JoinType
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"inner" ParsecT Void SQLStream (Reader Dialect) JoinType
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) JoinType
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"join"
    ,JoinType
JLeft JoinType
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) JoinType
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"left"
           ParsecT Void SQLStream (Reader Dialect) JoinType
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
-> ParsecT Void SQLStream (Reader Dialect) JoinType
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"outer")
           ParsecT Void SQLStream (Reader Dialect) JoinType
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) JoinType
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"join"
    ,JoinType
JRight JoinType
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) JoinType
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"right"
            ParsecT Void SQLStream (Reader Dialect) JoinType
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
-> ParsecT Void SQLStream (Reader Dialect) JoinType
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"outer")
            ParsecT Void SQLStream (Reader Dialect) JoinType
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) JoinType
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"join"
    ,JoinType
JFull JoinType
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) JoinType
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"full"
           ParsecT Void SQLStream (Reader Dialect) JoinType
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
-> ParsecT Void SQLStream (Reader Dialect) JoinType
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"outer")
           ParsecT Void SQLStream (Reader Dialect) JoinType
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) JoinType
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"join"
    ,JoinType
JInner JoinType
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) JoinType
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"join"]

joinCondition :: Parser JoinCondition
joinCondition :: ParsecT Void SQLStream (Reader Dialect) JoinCondition
joinCondition = [ParsecT Void SQLStream (Reader Dialect) JoinCondition]
-> ParsecT Void SQLStream (Reader Dialect) JoinCondition
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
    [Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"on" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) JoinCondition
-> ParsecT Void SQLStream (Reader Dialect) JoinCondition
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ScalarExpr -> JoinCondition
JoinOn (ScalarExpr -> JoinCondition)
-> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) JoinCondition
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ScalarExpr
scalarExpr
    ,Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"using" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) JoinCondition
-> ParsecT Void SQLStream (Reader Dialect) JoinCondition
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [Name] -> JoinCondition
JoinUsing ([Name] -> JoinCondition)
-> Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) JoinCondition
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name] -> Parser [Name]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser Name -> Parser [Name]
forall a. Parser a -> Parser [a]
commaSep1 Parser Name
name)]

fromAlias :: Parser Alias
fromAlias :: Parser Alias
fromAlias = Name -> Maybe [Name] -> Alias
Alias (Name -> Maybe [Name] -> Alias)
-> Parser Name
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name] -> Alias)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Name
tableAlias ParsecT Void SQLStream (Reader Dialect) (Maybe [Name] -> Alias)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
-> Parser Alias
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
columnAliases
  where
    tableAlias :: Parser Name
tableAlias = ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"as") ParsecT Void SQLStream (Reader Dialect) (Maybe ())
-> Parser Name -> Parser Name
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Name
name
    columnAliases :: ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
columnAliases = Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Parser [Name]
 -> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name]))
-> Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
forall a b. (a -> b) -> a -> b
$ Parser [Name] -> Parser [Name]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser [Name] -> Parser [Name]) -> Parser [Name] -> Parser [Name]
forall a b. (a -> b) -> a -> b
$ Parser Name -> Parser [Name]
forall a. Parser a -> Parser [a]
commaSep1 Parser Name
name

{-
== simple other parts

Parsers for where, group by, having, order by and limit, which are
pretty trivial.
-}

whereClause :: Parser ScalarExpr
whereClause :: Parser ScalarExpr
whereClause = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"where" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr
scalarExpr

groupByClause :: Parser [GroupingExpr]
groupByClause :: Parser [GroupingExpr]
groupByClause = [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"group",Text
"by"] ParsecT Void SQLStream (Reader Dialect) ()
-> Parser [GroupingExpr] -> Parser [GroupingExpr]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser GroupingExpr -> Parser [GroupingExpr]
forall a. Parser a -> Parser [a]
commaSep1 Parser GroupingExpr
groupingExpression
  where
    groupingExpression :: Parser GroupingExpr
groupingExpression = [Parser GroupingExpr] -> Parser GroupingExpr
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
      [Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"cube" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser GroupingExpr -> Parser GroupingExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
       [GroupingExpr] -> GroupingExpr
Cube ([GroupingExpr] -> GroupingExpr)
-> Parser [GroupingExpr] -> Parser GroupingExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [GroupingExpr] -> Parser [GroupingExpr]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser GroupingExpr -> Parser [GroupingExpr]
forall a. Parser a -> Parser [a]
commaSep Parser GroupingExpr
groupingExpression)
      ,Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"rollup" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser GroupingExpr -> Parser GroupingExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
       [GroupingExpr] -> GroupingExpr
Rollup ([GroupingExpr] -> GroupingExpr)
-> Parser [GroupingExpr] -> Parser GroupingExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [GroupingExpr] -> Parser [GroupingExpr]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser GroupingExpr -> Parser [GroupingExpr]
forall a. Parser a -> Parser [a]
commaSep Parser GroupingExpr
groupingExpression)
      ,[GroupingExpr] -> GroupingExpr
GroupingParens ([GroupingExpr] -> GroupingExpr)
-> Parser [GroupingExpr] -> Parser GroupingExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [GroupingExpr] -> Parser [GroupingExpr]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser GroupingExpr -> Parser [GroupingExpr]
forall a. Parser a -> Parser [a]
commaSep Parser GroupingExpr
groupingExpression)
      ,[Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"grouping", Text
"sets"] ParsecT Void SQLStream (Reader Dialect) ()
-> Parser GroupingExpr -> Parser GroupingExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
       [GroupingExpr] -> GroupingExpr
GroupingSets ([GroupingExpr] -> GroupingExpr)
-> Parser [GroupingExpr] -> Parser GroupingExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [GroupingExpr] -> Parser [GroupingExpr]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser GroupingExpr -> Parser [GroupingExpr]
forall a. Parser a -> Parser [a]
commaSep Parser GroupingExpr
groupingExpression)
      ,ScalarExpr -> GroupingExpr
SimpleGroup (ScalarExpr -> GroupingExpr)
-> Parser ScalarExpr -> Parser GroupingExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ScalarExpr
scalarExpr
      ]

having :: Parser ScalarExpr
having :: Parser ScalarExpr
having = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"having" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr
scalarExpr

orderBy :: Parser [SortSpec]
orderBy :: ParsecT Void SQLStream (Reader Dialect) [SortSpec]
orderBy = [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"order",Text
"by"] ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) [SortSpec]
-> ParsecT Void SQLStream (Reader Dialect) [SortSpec]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser SortSpec
-> ParsecT Void SQLStream (Reader Dialect) [SortSpec]
forall a. Parser a -> Parser [a]
commaSep1 Parser SortSpec
ob
  where
    ob :: Parser SortSpec
ob = ScalarExpr -> Direction -> NullsOrder -> SortSpec
SortSpec
         (ScalarExpr -> Direction -> NullsOrder -> SortSpec)
-> Parser ScalarExpr
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Direction -> NullsOrder -> SortSpec)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ScalarExpr
scalarExpr
         ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Direction -> NullsOrder -> SortSpec)
-> ParsecT Void SQLStream (Reader Dialect) Direction
-> ParsecT Void SQLStream (Reader Dialect) (NullsOrder -> SortSpec)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Direction
-> ParsecT Void SQLStream (Reader Dialect) Direction
-> ParsecT Void SQLStream (Reader Dialect) Direction
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option Direction
DirDefault ([ParsecT Void SQLStream (Reader Dialect) Direction]
-> ParsecT Void SQLStream (Reader Dialect) Direction
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [Direction
Asc Direction
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) Direction
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"asc"
                                       ,Direction
Desc Direction
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) Direction
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"desc"])
         ParsecT Void SQLStream (Reader Dialect) (NullsOrder -> SortSpec)
-> ParsecT Void SQLStream (Reader Dialect) NullsOrder
-> Parser SortSpec
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> NullsOrder
-> ParsecT Void SQLStream (Reader Dialect) NullsOrder
-> ParsecT Void SQLStream (Reader Dialect) NullsOrder
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option NullsOrder
NullsOrderDefault
             -- todo: left factor better
             (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"nulls" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) NullsOrder
-> ParsecT Void SQLStream (Reader Dialect) NullsOrder
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                    [ParsecT Void SQLStream (Reader Dialect) NullsOrder]
-> ParsecT Void SQLStream (Reader Dialect) NullsOrder
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [NullsOrder
NullsFirst NullsOrder
-> Parser Text
-> ParsecT Void SQLStream (Reader Dialect) NullsOrder
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> Parser Text
keyword Text
"first"
                           ,NullsOrder
NullsLast NullsOrder
-> Parser Text
-> ParsecT Void SQLStream (Reader Dialect) NullsOrder
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> Parser Text
keyword Text
"last"])

{-
allows offset and fetch in either order
+ postgresql offset without row(s) and limit instead of fetch also
-}

offsetFetch :: Parser (Maybe ScalarExpr, Maybe ScalarExpr)
offsetFetch :: Parser (Maybe ScalarExpr, Maybe ScalarExpr)
offsetFetch =
    Permutation
  (ParsecT Void SQLStream (Reader Dialect))
  (Maybe ScalarExpr, Maybe ScalarExpr)
-> Parser (Maybe ScalarExpr, Maybe ScalarExpr)
forall (m :: * -> *) a.
(Alternative m, Monad m) =>
Permutation m a -> m a
P.runPermutation (Permutation
   (ParsecT Void SQLStream (Reader Dialect))
   (Maybe ScalarExpr, Maybe ScalarExpr)
 -> Parser (Maybe ScalarExpr, Maybe ScalarExpr))
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe ScalarExpr, Maybe ScalarExpr)
-> Parser (Maybe ScalarExpr, Maybe ScalarExpr)
forall a b. (a -> b) -> a -> b
$ (,) (Maybe ScalarExpr
 -> Maybe ScalarExpr -> (Maybe ScalarExpr, Maybe ScalarExpr))
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect)) (Maybe ScalarExpr)
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe ScalarExpr -> (Maybe ScalarExpr, Maybe ScalarExpr))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ScalarExpr
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect)) (Maybe ScalarExpr)
forall {m :: * -> *} {a}.
Alternative m =>
m a -> Permutation m (Maybe a)
maybePermutation Parser ScalarExpr
offset Permutation
  (ParsecT Void SQLStream (Reader Dialect))
  (Maybe ScalarExpr -> (Maybe ScalarExpr, Maybe ScalarExpr))
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect)) (Maybe ScalarExpr)
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe ScalarExpr, Maybe ScalarExpr)
forall a b.
Permutation (ParsecT Void SQLStream (Reader Dialect)) (a -> b)
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) a
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser ScalarExpr
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect)) (Maybe ScalarExpr)
forall {m :: * -> *} {a}.
Alternative m =>
m a -> Permutation m (Maybe a)
maybePermutation Parser ScalarExpr
fetch
  where
    maybePermutation :: m a -> Permutation m (Maybe a)
maybePermutation m a
p = Maybe a -> m (Maybe a) -> Permutation m (Maybe a)
forall (m :: * -> *) a.
Alternative m =>
a -> m a -> Permutation m a
P.toPermutationWithDefault Maybe a
forall a. Maybe a
Nothing (a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> m a -> m (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
p)

offset :: Parser ScalarExpr
offset :: Parser ScalarExpr
offset = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"offset" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr
scalarExpr
         Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) () -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ()
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option () ([ParsecT Void SQLStream (Reader Dialect) ()]
-> ParsecT Void SQLStream (Reader Dialect) ()
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"rows"
                              ,Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"row"])

fetch :: Parser ScalarExpr
fetch :: Parser ScalarExpr
fetch = Parser ScalarExpr
fetchFirst Parser ScalarExpr -> Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser ScalarExpr
limit
  where
    fetchFirst :: Parser ScalarExpr
fetchFirst = (Dialect -> Bool) -> ParsecT Void SQLStream (Reader Dialect) ()
guardDialect Dialect -> Bool
diFetchFirst
                 ParsecT Void SQLStream (Reader Dialect) ()
-> Parser [Text] -> Parser [Text]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser [Text]
fs Parser [Text] -> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr
scalarExpr Parser ScalarExpr -> Parser [Text] -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser [Text]
ro
    fs :: Parser [Text]
fs = [Text] -> Parser [Text]
makeKeywordTree [Text
"fetch first", Text
"fetch next"]
    ro :: Parser [Text]
ro = [Text] -> Parser [Text]
makeKeywordTree [Text
"rows only", Text
"row only"]
    -- todo: not in ansi sql dialect
    limit :: Parser ScalarExpr
limit = (Dialect -> Bool) -> ParsecT Void SQLStream (Reader Dialect) ()
guardDialect Dialect -> Bool
diLimit ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*>
            Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"limit" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr
scalarExpr

-- == common table expressions

with :: Parser QueryExpr
with :: Parser QueryExpr
with = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"with" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser QueryExpr -> Parser QueryExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    Bool -> [(Alias, QueryExpr)] -> QueryExpr -> QueryExpr
With (Bool -> [(Alias, QueryExpr)] -> QueryExpr -> QueryExpr)
-> ParsecT Void SQLStream (Reader Dialect) Bool
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([(Alias, QueryExpr)] -> QueryExpr -> QueryExpr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool
-> ParsecT Void SQLStream (Reader Dialect) Bool
-> ParsecT Void SQLStream (Reader Dialect) Bool
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option Bool
False (Bool
True Bool
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) Bool
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"recursive")
         ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([(Alias, QueryExpr)] -> QueryExpr -> QueryExpr)
-> ParsecT Void SQLStream (Reader Dialect) [(Alias, QueryExpr)]
-> ParsecT Void SQLStream (Reader Dialect) (QueryExpr -> QueryExpr)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (Alias, QueryExpr)
-> ParsecT Void SQLStream (Reader Dialect) [(Alias, QueryExpr)]
forall a. Parser a -> Parser [a]
commaSep1 Parser (Alias, QueryExpr)
withQuery ParsecT Void SQLStream (Reader Dialect) (QueryExpr -> QueryExpr)
-> Parser QueryExpr -> Parser QueryExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser QueryExpr
queryExpr
  where
    withQuery :: Parser (Alias, QueryExpr)
withQuery = (,) (Alias -> QueryExpr -> (Alias, QueryExpr))
-> Parser Alias
-> ParsecT
     Void SQLStream (Reader Dialect) (QueryExpr -> (Alias, QueryExpr))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Parser Alias
withAlias Parser Alias
-> ParsecT Void SQLStream (Reader Dialect) () -> Parser Alias
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"as")
                    ParsecT
  Void SQLStream (Reader Dialect) (QueryExpr -> (Alias, QueryExpr))
-> Parser QueryExpr -> Parser (Alias, QueryExpr)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser QueryExpr -> Parser QueryExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens Parser QueryExpr
queryExpr
    withAlias :: Parser Alias
withAlias = Name -> Maybe [Name] -> Alias
Alias (Name -> Maybe [Name] -> Alias)
-> Parser Name
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name] -> Alias)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Name
name ParsecT Void SQLStream (Reader Dialect) (Maybe [Name] -> Alias)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
-> Parser Alias
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
columnAliases
    columnAliases :: ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
columnAliases = Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Parser [Name]
 -> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name]))
-> Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
forall a b. (a -> b) -> a -> b
$ Parser [Name] -> Parser [Name]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser [Name] -> Parser [Name]) -> Parser [Name] -> Parser [Name]
forall a b. (a -> b) -> a -> b
$ Parser Name -> Parser [Name]
forall a. Parser a -> Parser [a]
commaSep1 Parser Name
name


{-
== query expression

This parser parses any query expression variant: normal select, cte,
and union, etc..
-}

queryExpr :: Parser QueryExpr
queryExpr :: Parser QueryExpr
queryExpr = Parser QueryExpr
-> [[Operator (ParsecT Void SQLStream (Reader Dialect)) QueryExpr]]
-> Parser QueryExpr
forall (m :: * -> *) a.
MonadPlus m =>
m a -> [[Operator m a]] -> m a
E.makeExprParser Parser QueryExpr
qeterm [[Operator (ParsecT Void SQLStream (Reader Dialect)) QueryExpr]]
qeOpTable
  where
    qeterm :: Parser QueryExpr
qeterm = Parser QueryExpr
with Parser QueryExpr -> Parser QueryExpr -> Parser QueryExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser QueryExpr
select Parser QueryExpr -> Parser QueryExpr -> Parser QueryExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser QueryExpr
table Parser QueryExpr -> Parser QueryExpr -> Parser QueryExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser QueryExpr
values
    
    select :: Parser QueryExpr
select = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"select" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser QueryExpr -> Parser QueryExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
        SetQuantifier
-> [(ScalarExpr, Maybe Name)] -> Maybe TableExpression -> QueryExpr
mkSelect
        (SetQuantifier
 -> [(ScalarExpr, Maybe Name)]
 -> Maybe TableExpression
 -> QueryExpr)
-> Parser SetQuantifier
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([(ScalarExpr, Maybe Name)] -> Maybe TableExpression -> QueryExpr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SetQuantifier -> Parser SetQuantifier -> Parser SetQuantifier
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option SetQuantifier
SQDefault Parser SetQuantifier
duplicates
        ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([(ScalarExpr, Maybe Name)] -> Maybe TableExpression -> QueryExpr)
-> Parser [(ScalarExpr, Maybe Name)]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe TableExpression -> QueryExpr)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser [(ScalarExpr, Maybe Name)]
selectList
        ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe TableExpression -> QueryExpr)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe TableExpression)
-> Parser QueryExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) TableExpression
-> ParsecT Void SQLStream (Reader Dialect) (Maybe TableExpression)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ParsecT Void SQLStream (Reader Dialect) TableExpression
tableExpression Parser QueryExpr -> String -> Parser QueryExpr
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"table expression"
    mkSelect :: SetQuantifier
-> [(ScalarExpr, Maybe Name)] -> Maybe TableExpression -> QueryExpr
mkSelect SetQuantifier
d [(ScalarExpr, Maybe Name)]
sl Maybe TableExpression
Nothing =
        MakeSelect -> QueryExpr
toQueryExpr (MakeSelect -> QueryExpr) -> MakeSelect -> QueryExpr
forall a b. (a -> b) -> a -> b
$ MakeSelect
makeSelect {msSetQuantifier = d, msSelectList = sl}
    mkSelect SetQuantifier
d [(ScalarExpr, Maybe Name)]
sl (Just (TableExpression [TableRef]
f Maybe ScalarExpr
w [GroupingExpr]
g Maybe ScalarExpr
h [SortSpec]
od Maybe ScalarExpr
ofs Maybe ScalarExpr
fe)) =
        SetQuantifier
-> [(ScalarExpr, Maybe Name)]
-> [TableRef]
-> Maybe ScalarExpr
-> [GroupingExpr]
-> Maybe ScalarExpr
-> [SortSpec]
-> Maybe ScalarExpr
-> Maybe ScalarExpr
-> QueryExpr
Select SetQuantifier
d [(ScalarExpr, Maybe Name)]
sl [TableRef]
f Maybe ScalarExpr
w [GroupingExpr]
g Maybe ScalarExpr
h [SortSpec]
od Maybe ScalarExpr
ofs Maybe ScalarExpr
fe
    values :: Parser QueryExpr
values = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"values"
             ParsecT Void SQLStream (Reader Dialect) ()
-> Parser QueryExpr -> Parser QueryExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [[ScalarExpr]] -> QueryExpr
Values ([[ScalarExpr]] -> QueryExpr)
-> ParsecT Void SQLStream (Reader Dialect) [[ScalarExpr]]
-> Parser QueryExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> ParsecT Void SQLStream (Reader Dialect) [[ScalarExpr]]
forall a. Parser a -> Parser [a]
commaSep (ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a. Parser a -> Parser [a]
commaSep Parser ScalarExpr
scalarExpr))
    table :: Parser QueryExpr
table = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"table" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser QueryExpr -> Parser QueryExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [Name] -> QueryExpr
Table ([Name] -> QueryExpr) -> Parser [Name] -> Parser QueryExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names

    qeOpTable :: [[Operator (ParsecT Void SQLStream (Reader Dialect)) QueryExpr]]
qeOpTable =
        [[ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (QueryExpr -> QueryExpr -> QueryExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) QueryExpr
forall (m :: * -> *) a. m (a -> a -> a) -> Operator m a
E.InfixL (ParsecT
   Void
   SQLStream
   (Reader Dialect)
   (QueryExpr -> QueryExpr -> QueryExpr)
 -> Operator (ParsecT Void SQLStream (Reader Dialect)) QueryExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (QueryExpr -> QueryExpr -> QueryExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) QueryExpr
forall a b. (a -> b) -> a -> b
$ SetOperatorName
-> Text
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (QueryExpr -> QueryExpr -> QueryExpr)
setOp SetOperatorName
Intersect Text
"intersect"]
        ,[ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (QueryExpr -> QueryExpr -> QueryExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) QueryExpr
forall (m :: * -> *) a. m (a -> a -> a) -> Operator m a
E.InfixL (ParsecT
   Void
   SQLStream
   (Reader Dialect)
   (QueryExpr -> QueryExpr -> QueryExpr)
 -> Operator (ParsecT Void SQLStream (Reader Dialect)) QueryExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (QueryExpr -> QueryExpr -> QueryExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) QueryExpr
forall a b. (a -> b) -> a -> b
$ SetOperatorName
-> Text
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (QueryExpr -> QueryExpr -> QueryExpr)
setOp SetOperatorName
Except Text
"except"
         ,ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (QueryExpr -> QueryExpr -> QueryExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) QueryExpr
forall (m :: * -> *) a. m (a -> a -> a) -> Operator m a
E.InfixL (ParsecT
   Void
   SQLStream
   (Reader Dialect)
   (QueryExpr -> QueryExpr -> QueryExpr)
 -> Operator (ParsecT Void SQLStream (Reader Dialect)) QueryExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (QueryExpr -> QueryExpr -> QueryExpr)
-> Operator (ParsecT Void SQLStream (Reader Dialect)) QueryExpr
forall a b. (a -> b) -> a -> b
$ SetOperatorName
-> Text
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (QueryExpr -> QueryExpr -> QueryExpr)
setOp SetOperatorName
Union Text
"union"]]
    setOp :: SetOperatorName -> Text -> Parser (QueryExpr -> QueryExpr -> QueryExpr)
    setOp :: SetOperatorName
-> Text
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (QueryExpr -> QueryExpr -> QueryExpr)
setOp SetOperatorName
ctor Text
opName = (SetOperatorName
-> SetQuantifier
-> Corresponding
-> QueryExpr
-> QueryExpr
-> QueryExpr
cq
        (SetOperatorName
 -> SetQuantifier
 -> Corresponding
 -> QueryExpr
 -> QueryExpr
 -> QueryExpr)
-> ParsecT Void SQLStream (Reader Dialect) SetOperatorName
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (SetQuantifier
      -> Corresponding -> QueryExpr -> QueryExpr -> QueryExpr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (SetOperatorName
ctor SetOperatorName
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) SetOperatorName
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
opName)
        ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (SetQuantifier
   -> Corresponding -> QueryExpr -> QueryExpr -> QueryExpr)
-> Parser SetQuantifier
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Corresponding -> QueryExpr -> QueryExpr -> QueryExpr)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> SetQuantifier -> Parser SetQuantifier -> Parser SetQuantifier
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option SetQuantifier
SQDefault Parser SetQuantifier
duplicates
        ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Corresponding -> QueryExpr -> QueryExpr -> QueryExpr)
-> ParsecT Void SQLStream (Reader Dialect) Corresponding
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (QueryExpr -> QueryExpr -> QueryExpr)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) Corresponding
corr) ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (QueryExpr -> QueryExpr -> QueryExpr)
-> String
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (QueryExpr -> QueryExpr -> QueryExpr)
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
""
    cq :: SetOperatorName
-> SetQuantifier
-> Corresponding
-> QueryExpr
-> QueryExpr
-> QueryExpr
cq SetOperatorName
o SetQuantifier
d Corresponding
c QueryExpr
q0 QueryExpr
q1 = QueryExpr
-> SetOperatorName
-> SetQuantifier
-> Corresponding
-> QueryExpr
-> QueryExpr
QueryExprSetOp QueryExpr
q0 SetOperatorName
o SetQuantifier
d Corresponding
c QueryExpr
q1
    corr :: ParsecT Void SQLStream (Reader Dialect) Corresponding
corr = Corresponding
-> ParsecT Void SQLStream (Reader Dialect) Corresponding
-> ParsecT Void SQLStream (Reader Dialect) Corresponding
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option Corresponding
Respectively (Corresponding
Corresponding Corresponding
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) Corresponding
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"corresponding")

    
{-
local data type to help with parsing the bit after the select list,
called 'table expression' in the ansi sql grammar. Maybe this should
be in the public syntax?
-}

data TableExpression
    = TableExpression
      {TableExpression -> [TableRef]
_teFrom :: [TableRef]
      ,TableExpression -> Maybe ScalarExpr
_teWhere :: Maybe ScalarExpr
      ,TableExpression -> [GroupingExpr]
_teGroupBy :: [GroupingExpr]
      ,TableExpression -> Maybe ScalarExpr
_teHaving :: Maybe ScalarExpr
      ,TableExpression -> [SortSpec]
_teOrderBy :: [SortSpec]
      ,TableExpression -> Maybe ScalarExpr
_teOffset :: Maybe ScalarExpr
      ,TableExpression -> Maybe ScalarExpr
_teFetchFirst :: Maybe ScalarExpr}

tableExpression :: Parser TableExpression
tableExpression :: ParsecT Void SQLStream (Reader Dialect) TableExpression
tableExpression = [TableRef]
-> Maybe ScalarExpr
-> [GroupingExpr]
-> Maybe ScalarExpr
-> [SortSpec]
-> (Maybe ScalarExpr, Maybe ScalarExpr)
-> TableExpression
mkTe ([TableRef]
 -> Maybe ScalarExpr
 -> [GroupingExpr]
 -> Maybe ScalarExpr
 -> [SortSpec]
 -> (Maybe ScalarExpr, Maybe ScalarExpr)
 -> TableExpression)
-> Parser [TableRef]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe ScalarExpr
      -> [GroupingExpr]
      -> Maybe ScalarExpr
      -> [SortSpec]
      -> (Maybe ScalarExpr, Maybe ScalarExpr)
      -> TableExpression)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Parser [TableRef]
from Parser [TableRef] -> String -> Parser [TableRef]
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"from clause")
                       ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe ScalarExpr
   -> [GroupingExpr]
   -> Maybe ScalarExpr
   -> [SortSpec]
   -> (Maybe ScalarExpr, Maybe ScalarExpr)
   -> TableExpression)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([GroupingExpr]
      -> Maybe ScalarExpr
      -> [SortSpec]
      -> (Maybe ScalarExpr, Maybe ScalarExpr)
      -> TableExpression)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional Parser ScalarExpr
whereClause ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
-> String
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"where clause")
                       ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([GroupingExpr]
   -> Maybe ScalarExpr
   -> [SortSpec]
   -> (Maybe ScalarExpr, Maybe ScalarExpr)
   -> TableExpression)
-> Parser [GroupingExpr]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe ScalarExpr
      -> [SortSpec]
      -> (Maybe ScalarExpr, Maybe ScalarExpr)
      -> TableExpression)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ([GroupingExpr] -> Parser [GroupingExpr] -> Parser [GroupingExpr]
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option [] Parser [GroupingExpr]
groupByClause Parser [GroupingExpr] -> String -> Parser [GroupingExpr]
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"group by clause")
                       ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe ScalarExpr
   -> [SortSpec]
   -> (Maybe ScalarExpr, Maybe ScalarExpr)
   -> TableExpression)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([SortSpec]
      -> (Maybe ScalarExpr, Maybe ScalarExpr) -> TableExpression)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional Parser ScalarExpr
having ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
-> String
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"having clause")
                       ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([SortSpec]
   -> (Maybe ScalarExpr, Maybe ScalarExpr) -> TableExpression)
-> ParsecT Void SQLStream (Reader Dialect) [SortSpec]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ((Maybe ScalarExpr, Maybe ScalarExpr) -> TableExpression)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ([SortSpec]
-> ParsecT Void SQLStream (Reader Dialect) [SortSpec]
-> ParsecT Void SQLStream (Reader Dialect) [SortSpec]
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option [] ParsecT Void SQLStream (Reader Dialect) [SortSpec]
orderBy ParsecT Void SQLStream (Reader Dialect) [SortSpec]
-> String -> ParsecT Void SQLStream (Reader Dialect) [SortSpec]
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"order by clause")
                       ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ((Maybe ScalarExpr, Maybe ScalarExpr) -> TableExpression)
-> Parser (Maybe ScalarExpr, Maybe ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) TableExpression
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Parser (Maybe ScalarExpr, Maybe ScalarExpr)
offsetFetch Parser (Maybe ScalarExpr, Maybe ScalarExpr)
-> String -> Parser (Maybe ScalarExpr, Maybe ScalarExpr)
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"")
 where
    mkTe :: [TableRef]
-> Maybe ScalarExpr
-> [GroupingExpr]
-> Maybe ScalarExpr
-> [SortSpec]
-> (Maybe ScalarExpr, Maybe ScalarExpr)
-> TableExpression
mkTe [TableRef]
f Maybe ScalarExpr
w [GroupingExpr]
g Maybe ScalarExpr
h [SortSpec]
od (Maybe ScalarExpr
ofs,Maybe ScalarExpr
fe) =
        [TableRef]
-> Maybe ScalarExpr
-> [GroupingExpr]
-> Maybe ScalarExpr
-> [SortSpec]
-> Maybe ScalarExpr
-> Maybe ScalarExpr
-> TableExpression
TableExpression [TableRef]
f Maybe ScalarExpr
w [GroupingExpr]
g Maybe ScalarExpr
h [SortSpec]
od Maybe ScalarExpr
ofs Maybe ScalarExpr
fe

{-
wrapper for query expr which ignores optional trailing semicolon.

TODO: change style
-}

topLevelQueryExpr :: Parser QueryExpr
topLevelQueryExpr :: Parser QueryExpr
topLevelQueryExpr = Parser QueryExpr
queryExpr Parser QueryExpr
-> ParsecT Void SQLStream (Reader Dialect) (QueryExpr -> QueryExpr)
-> Parser QueryExpr
forall a. Parser a -> Parser (a -> a) -> Parser a
<??> (QueryExpr -> QueryExpr
forall a. a -> a
id (QueryExpr -> QueryExpr)
-> Parser Char
-> ParsecT Void SQLStream (Reader Dialect) (QueryExpr -> QueryExpr)
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Parser Char
semi)

topLevelStatement :: Parser Statement
topLevelStatement :: Parser Statement
topLevelStatement = Parser Statement
statement

{-
-------------------------

= Statements
-}

statementWithoutSemicolon :: Parser Statement
statementWithoutSemicolon :: Parser Statement
statementWithoutSemicolon = [Parser Statement] -> Parser Statement
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
    [Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"create" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> [Parser Statement] -> Parser Statement
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [Parser Statement
createSchema
                                 ,Parser Statement
createTable
                                 ,Parser Statement
createIndex
                                 ,Parser Statement
createView
                                 ,Parser Statement
createDomain
                                 ,Parser Statement
createSequence
                                 ,Parser Statement
createRole
                                 ,Parser Statement
createAssertion]
    ,Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"alter" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> [Parser Statement] -> Parser Statement
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [Parser Statement
alterTable
                                ,Parser Statement
alterDomain
                                ,Parser Statement
alterSequence]
    ,Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"drop" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> [Parser Statement] -> Parser Statement
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [Parser Statement
dropSchema
                               ,Parser Statement
dropTable
                               ,Parser Statement
dropView
                               ,Parser Statement
dropDomain
                               ,Parser Statement
dropSequence
                               ,Parser Statement
dropRole
                               ,Parser Statement
dropAssertion]
    ,Parser Statement
delete
    ,Parser Statement
truncateSt
    ,Parser Statement
insert
    ,Parser Statement
update
    ,Parser Statement
startTransaction
    ,Parser Statement
savepoint
    ,Parser Statement
releaseSavepoint
    ,Parser Statement
commit
    ,Parser Statement
rollback
    ,Parser Statement
grant
    ,Parser Statement
revoke
    ,QueryExpr -> Statement
SelectStatement (QueryExpr -> Statement) -> Parser QueryExpr -> Parser Statement
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser QueryExpr
queryExpr
    ]

statement :: Parser Statement
statement :: Parser Statement
statement = Parser Statement
statementWithoutSemicolon Parser Statement
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Char)
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser Char -> ParsecT Void SQLStream (Reader Dialect) (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional Parser Char
semi Parser Statement -> Parser Statement -> Parser Statement
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Statement
EmptyStatement Statement -> Parser Char -> Parser Statement
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Parser Char
semi

createSchema :: Parser Statement
createSchema :: Parser Statement
createSchema = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"schema" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    [Name] -> Statement
CreateSchema ([Name] -> Statement) -> Parser [Name] -> Parser Statement
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names

createTable :: Parser Statement
createTable :: Parser Statement
createTable = do 
  Dialect
d <- (Dialect -> Dialect) -> Parser Dialect
forall a. (Dialect -> a) -> Parser a
askDialect Dialect -> Dialect
forall a. a -> a
id
  let 
    parseColumnDef :: ParsecT Void SQLStream (Reader Dialect) TableElement
parseColumnDef = ColumnDef -> TableElement
TableColumnDef (ColumnDef -> TableElement)
-> ParsecT Void SQLStream (Reader Dialect) ColumnDef
-> ParsecT Void SQLStream (Reader Dialect) TableElement
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) ColumnDef
columnDef 
    parseConstraintDef :: ParsecT Void SQLStream (Reader Dialect) TableElement
parseConstraintDef = (Maybe [Name] -> TableConstraint -> TableElement)
-> (Maybe [Name], TableConstraint) -> TableElement
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Maybe [Name] -> TableConstraint -> TableElement
TableConstraintDef ((Maybe [Name], TableConstraint) -> TableElement)
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe [Name], TableConstraint)
-> ParsecT Void SQLStream (Reader Dialect) TableElement
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT
  Void SQLStream (Reader Dialect) (Maybe [Name], TableConstraint)
tableConstraintDef
    separator :: ParsecT Void SQLStream (Reader Dialect) (Maybe Char)
separator = if Dialect -> Bool
diNonCommaSeparatedConstraints Dialect
d
      then Parser Char -> ParsecT Void SQLStream (Reader Dialect) (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional Parser Char
comma
      else Char -> Maybe Char
forall a. a -> Maybe a
Just (Char -> Maybe Char)
-> Parser Char
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Char)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Char
comma
    constraints :: ParsecT Void SQLStream (Reader Dialect) [TableElement]
constraints = ParsecT Void SQLStream (Reader Dialect) TableElement
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Char)
-> ParsecT Void SQLStream (Reader Dialect) [TableElement]
forall (m :: * -> *) a sep. MonadPlus m => m a -> m sep -> m [a]
sepBy ParsecT Void SQLStream (Reader Dialect) TableElement
parseConstraintDef ParsecT Void SQLStream (Reader Dialect) (Maybe Char)
separator
    entries :: ParsecT Void SQLStream (Reader Dialect) [TableElement]
entries = ((:) (TableElement -> [TableElement] -> [TableElement])
-> ParsecT Void SQLStream (Reader Dialect) TableElement
-> ParsecT
     Void SQLStream (Reader Dialect) ([TableElement] -> [TableElement])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) TableElement
parseColumnDef ParsecT
  Void SQLStream (Reader Dialect) ([TableElement] -> [TableElement])
-> ParsecT Void SQLStream (Reader Dialect) [TableElement]
-> ParsecT Void SQLStream (Reader Dialect) [TableElement]
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ((Parser Char
comma Parser Char
-> ParsecT Void SQLStream (Reader Dialect) [TableElement]
-> ParsecT Void SQLStream (Reader Dialect) [TableElement]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT Void SQLStream (Reader Dialect) [TableElement]
entries) ParsecT Void SQLStream (Reader Dialect) [TableElement]
-> ParsecT Void SQLStream (Reader Dialect) [TableElement]
-> ParsecT Void SQLStream (Reader Dialect) [TableElement]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [TableElement]
-> ParsecT Void SQLStream (Reader Dialect) [TableElement]
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [])) ParsecT Void SQLStream (Reader Dialect) [TableElement]
-> ParsecT Void SQLStream (Reader Dialect) [TableElement]
-> ParsecT Void SQLStream (Reader Dialect) [TableElement]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT Void SQLStream (Reader Dialect) [TableElement]
constraints

  Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"table" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    [Name] -> [TableElement] -> Statement
CreateTable
    ([Name] -> [TableElement] -> Statement)
-> Parser [Name]
-> ParsecT
     Void SQLStream (Reader Dialect) ([TableElement] -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names 
    ParsecT
  Void SQLStream (Reader Dialect) ([TableElement] -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) [TableElement]
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) [TableElement]
-> ParsecT Void SQLStream (Reader Dialect) [TableElement]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens ParsecT Void SQLStream (Reader Dialect) [TableElement]
entries

createIndex :: Parser Statement
createIndex :: Parser Statement
createIndex =
    Bool -> [Name] -> [Name] -> [Name] -> Statement
CreateIndex
    (Bool -> [Name] -> [Name] -> [Name] -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) Bool
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([Name] -> [Name] -> [Name] -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"index" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) Bool
-> ParsecT Void SQLStream (Reader Dialect) Bool
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> ParsecT Void SQLStream (Reader Dialect) Bool
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False) ParsecT Void SQLStream (Reader Dialect) Bool
-> ParsecT Void SQLStream (Reader Dialect) Bool
-> ParsecT Void SQLStream (Reader Dialect) Bool
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
         ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"unique", Text
"index"] ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) Bool
-> ParsecT Void SQLStream (Reader Dialect) Bool
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> ParsecT Void SQLStream (Reader Dialect) Bool
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True))
    ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([Name] -> [Name] -> [Name] -> Statement)
-> Parser [Name]
-> ParsecT
     Void SQLStream (Reader Dialect) ([Name] -> [Name] -> Statement)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser [Name]
names
    ParsecT
  Void SQLStream (Reader Dialect) ([Name] -> [Name] -> Statement)
-> Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> Statement)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"on" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser [Name] -> Parser [Name]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Parser [Name]
names)
    ParsecT Void SQLStream (Reader Dialect) ([Name] -> Statement)
-> Parser [Name] -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser [Name] -> Parser [Name]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser Name -> Parser [Name]
forall a. Parser a -> Parser [a]
commaSep1 Parser Name
name)

columnDef :: Parser ColumnDef
columnDef :: ParsecT Void SQLStream (Reader Dialect) ColumnDef
columnDef = Name
-> TypeName
-> Maybe DefaultClause
-> [ColConstraintDef]
-> ColumnDef
ColumnDef (Name
 -> TypeName
 -> Maybe DefaultClause
 -> [ColConstraintDef]
 -> ColumnDef)
-> Parser Name
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (TypeName
      -> Maybe DefaultClause -> [ColConstraintDef] -> ColumnDef)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Name
name ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (TypeName
   -> Maybe DefaultClause -> [ColConstraintDef] -> ColumnDef)
-> Parser TypeName
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe DefaultClause -> [ColConstraintDef] -> ColumnDef)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser TypeName
typeName
            ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe DefaultClause -> [ColConstraintDef] -> ColumnDef)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe DefaultClause)
-> ParsecT
     Void SQLStream (Reader Dialect) ([ColConstraintDef] -> ColumnDef)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) DefaultClause
-> ParsecT Void SQLStream (Reader Dialect) (Maybe DefaultClause)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ParsecT Void SQLStream (Reader Dialect) DefaultClause
defaultClause
            ParsecT
  Void SQLStream (Reader Dialect) ([ColConstraintDef] -> ColumnDef)
-> ParsecT Void SQLStream (Reader Dialect) [ColConstraintDef]
-> ParsecT Void SQLStream (Reader Dialect) ColumnDef
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [ColConstraintDef]
-> ParsecT Void SQLStream (Reader Dialect) [ColConstraintDef]
-> ParsecT Void SQLStream (Reader Dialect) [ColConstraintDef]
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option [] (ParsecT Void SQLStream (Reader Dialect) ColConstraintDef
-> ParsecT Void SQLStream (Reader Dialect) [ColConstraintDef]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
some ParsecT Void SQLStream (Reader Dialect) ColConstraintDef
colConstraintDef)
  where
    defaultClause :: ParsecT Void SQLStream (Reader Dialect) DefaultClause
defaultClause = [ParsecT Void SQLStream (Reader Dialect) DefaultClause]
-> ParsecT Void SQLStream (Reader Dialect) DefaultClause
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [
        Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"default" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) DefaultClause
-> ParsecT Void SQLStream (Reader Dialect) DefaultClause
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
        ScalarExpr -> DefaultClause
DefaultClause (ScalarExpr -> DefaultClause)
-> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) DefaultClause
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ScalarExpr
scalarExpr
        -- todo: left factor
       ,ParsecT Void SQLStream (Reader Dialect) DefaultClause
-> ParsecT Void SQLStream (Reader Dialect) DefaultClause
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"generated",Text
"always",Text
"as"] ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) DefaultClause
-> ParsecT Void SQLStream (Reader Dialect) DefaultClause
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
             ScalarExpr -> DefaultClause
GenerationClause (ScalarExpr -> DefaultClause)
-> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) DefaultClause
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens Parser ScalarExpr
scalarExpr)
       ,Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"generated" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) DefaultClause
-> ParsecT Void SQLStream (Reader Dialect) DefaultClause
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
        IdentityWhen -> [SequenceGeneratorOption] -> DefaultClause
IdentityColumnSpec
        (IdentityWhen -> [SequenceGeneratorOption] -> DefaultClause)
-> ParsecT Void SQLStream (Reader Dialect) IdentityWhen
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([SequenceGeneratorOption] -> DefaultClause)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (IdentityWhen
GeneratedAlways IdentityWhen
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) IdentityWhen
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"always"
             ParsecT Void SQLStream (Reader Dialect) IdentityWhen
-> ParsecT Void SQLStream (Reader Dialect) IdentityWhen
-> ParsecT Void SQLStream (Reader Dialect) IdentityWhen
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> IdentityWhen
GeneratedByDefault IdentityWhen
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) IdentityWhen
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"by", Text
"default"])
        ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([SequenceGeneratorOption] -> DefaultClause)
-> ParsecT
     Void SQLStream (Reader Dialect) [SequenceGeneratorOption]
-> ParsecT Void SQLStream (Reader Dialect) DefaultClause
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"as", Text
"identity"] ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT
     Void SQLStream (Reader Dialect) [SequenceGeneratorOption]
-> ParsecT
     Void SQLStream (Reader Dialect) [SequenceGeneratorOption]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*>
             [SequenceGeneratorOption]
-> ParsecT
     Void SQLStream (Reader Dialect) [SequenceGeneratorOption]
-> ParsecT
     Void SQLStream (Reader Dialect) [SequenceGeneratorOption]
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option [] (ParsecT Void SQLStream (Reader Dialect) [SequenceGeneratorOption]
-> ParsecT
     Void SQLStream (Reader Dialect) [SequenceGeneratorOption]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens ParsecT Void SQLStream (Reader Dialect) [SequenceGeneratorOption]
sequenceGeneratorOptions))
       ]

tableConstraintDef :: Parser (Maybe [Name], TableConstraint)
tableConstraintDef :: ParsecT
  Void SQLStream (Reader Dialect) (Maybe [Name], TableConstraint)
tableConstraintDef =
    (,)
    (Maybe [Name]
 -> TableConstraint -> (Maybe [Name], TableConstraint))
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (TableConstraint -> (Maybe [Name], TableConstraint))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"constraint" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser [Name] -> Parser [Name]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser [Name]
names)
    ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (TableConstraint -> (Maybe [Name], TableConstraint))
-> ParsecT Void SQLStream (Reader Dialect) TableConstraint
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe [Name], TableConstraint)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (ParsecT Void SQLStream (Reader Dialect) TableConstraint
unique ParsecT Void SQLStream (Reader Dialect) TableConstraint
-> ParsecT Void SQLStream (Reader Dialect) TableConstraint
-> ParsecT Void SQLStream (Reader Dialect) TableConstraint
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT Void SQLStream (Reader Dialect) TableConstraint
primaryKey ParsecT Void SQLStream (Reader Dialect) TableConstraint
-> ParsecT Void SQLStream (Reader Dialect) TableConstraint
-> ParsecT Void SQLStream (Reader Dialect) TableConstraint
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT Void SQLStream (Reader Dialect) TableConstraint
check ParsecT Void SQLStream (Reader Dialect) TableConstraint
-> ParsecT Void SQLStream (Reader Dialect) TableConstraint
-> ParsecT Void SQLStream (Reader Dialect) TableConstraint
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT Void SQLStream (Reader Dialect) TableConstraint
references)
  where
    unique :: ParsecT Void SQLStream (Reader Dialect) TableConstraint
unique = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"unique" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) TableConstraint
-> ParsecT Void SQLStream (Reader Dialect) TableConstraint
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
        [Name] -> TableConstraint
TableUniqueConstraint ([Name] -> TableConstraint)
-> Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) TableConstraint
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name] -> Parser [Name]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser Name -> Parser [Name]
forall a. Parser a -> Parser [a]
commaSep1 Parser Name
name)
    primaryKey :: ParsecT Void SQLStream (Reader Dialect) TableConstraint
primaryKey = [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"primary", Text
"key"] ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) TableConstraint
-> ParsecT Void SQLStream (Reader Dialect) TableConstraint
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
        [Name] -> TableConstraint
TablePrimaryKeyConstraint ([Name] -> TableConstraint)
-> Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) TableConstraint
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name] -> Parser [Name]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser Name -> Parser [Name]
forall a. Parser a -> Parser [a]
commaSep1 Parser Name
name)
    check :: ParsecT Void SQLStream (Reader Dialect) TableConstraint
check = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"check" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) TableConstraint
-> ParsecT Void SQLStream (Reader Dialect) TableConstraint
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ScalarExpr -> TableConstraint
TableCheckConstraint (ScalarExpr -> TableConstraint)
-> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) TableConstraint
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens Parser ScalarExpr
scalarExpr
    references :: ParsecT Void SQLStream (Reader Dialect) TableConstraint
references = [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"foreign", Text
"key"] ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) TableConstraint
-> ParsecT Void SQLStream (Reader Dialect) TableConstraint
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
        (\[Name]
cs [Name]
ft Maybe [Name]
ftcs ReferenceMatch
m (ReferentialAction
u,ReferentialAction
d) -> [Name]
-> [Name]
-> Maybe [Name]
-> ReferenceMatch
-> ReferentialAction
-> ReferentialAction
-> TableConstraint
TableReferencesConstraint [Name]
cs [Name]
ft Maybe [Name]
ftcs ReferenceMatch
m ReferentialAction
u ReferentialAction
d)
        ([Name]
 -> [Name]
 -> Maybe [Name]
 -> ReferenceMatch
 -> (ReferentialAction, ReferentialAction)
 -> TableConstraint)
-> Parser [Name]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([Name]
      -> Maybe [Name]
      -> ReferenceMatch
      -> (ReferentialAction, ReferentialAction)
      -> TableConstraint)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name] -> Parser [Name]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser Name -> Parser [Name]
forall a. Parser a -> Parser [a]
commaSep1 Parser Name
name)
        ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([Name]
   -> Maybe [Name]
   -> ReferenceMatch
   -> (ReferentialAction, ReferentialAction)
   -> TableConstraint)
-> Parser [Name]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe [Name]
      -> ReferenceMatch
      -> (ReferentialAction, ReferentialAction)
      -> TableConstraint)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"references" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser [Name] -> Parser [Name]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser [Name]
names)
        ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe [Name]
   -> ReferenceMatch
   -> (ReferentialAction, ReferentialAction)
   -> TableConstraint)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ReferenceMatch
      -> (ReferentialAction, ReferentialAction) -> TableConstraint)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Parser [Name] -> Parser [Name]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser [Name] -> Parser [Name]) -> Parser [Name] -> Parser [Name]
forall a b. (a -> b) -> a -> b
$ Parser Name -> Parser [Name]
forall a. Parser a -> Parser [a]
commaSep1 Parser Name
name)
        ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ReferenceMatch
   -> (ReferentialAction, ReferentialAction) -> TableConstraint)
-> ParsecT Void SQLStream (Reader Dialect) ReferenceMatch
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ((ReferentialAction, ReferentialAction) -> TableConstraint)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) ReferenceMatch
refMatch
        ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ((ReferentialAction, ReferentialAction) -> TableConstraint)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ReferentialAction, ReferentialAction)
-> ParsecT Void SQLStream (Reader Dialect) TableConstraint
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ReferentialAction, ReferentialAction)
refActions

refMatch :: Parser ReferenceMatch
refMatch :: ParsecT Void SQLStream (Reader Dialect) ReferenceMatch
refMatch = ReferenceMatch
-> ParsecT Void SQLStream (Reader Dialect) ReferenceMatch
-> ParsecT Void SQLStream (Reader Dialect) ReferenceMatch
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option ReferenceMatch
DefaultReferenceMatch
            (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"match" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ReferenceMatch
-> ParsecT Void SQLStream (Reader Dialect) ReferenceMatch
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*>
             [ParsecT Void SQLStream (Reader Dialect) ReferenceMatch]
-> ParsecT Void SQLStream (Reader Dialect) ReferenceMatch
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [ReferenceMatch
MatchFull ReferenceMatch
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ReferenceMatch
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"full"
                    ,ReferenceMatch
MatchPartial ReferenceMatch
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ReferenceMatch
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"partial"
                    ,ReferenceMatch
MatchSimple ReferenceMatch
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ReferenceMatch
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"simple"])
refActions :: Parser (ReferentialAction,ReferentialAction)
refActions :: ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ReferentialAction, ReferentialAction)
refActions =
    Permutation
  (ParsecT Void SQLStream (Reader Dialect))
  (ReferentialAction, ReferentialAction)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ReferentialAction, ReferentialAction)
forall (m :: * -> *) a.
(Alternative m, Monad m) =>
Permutation m a -> m a
P.runPermutation (Permutation
   (ParsecT Void SQLStream (Reader Dialect))
   (ReferentialAction, ReferentialAction)
 -> ParsecT
      Void
      SQLStream
      (Reader Dialect)
      (ReferentialAction, ReferentialAction))
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (ReferentialAction, ReferentialAction)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ReferentialAction, ReferentialAction)
forall a b. (a -> b) -> a -> b
$ (,)
    (ReferentialAction
 -> ReferentialAction -> (ReferentialAction, ReferentialAction))
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect)) ReferentialAction
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (ReferentialAction -> (ReferentialAction, ReferentialAction))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReferentialAction
-> ParsecT Void SQLStream (Reader Dialect) ReferentialAction
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect)) ReferentialAction
forall (m :: * -> *) a.
Alternative m =>
a -> m a -> Permutation m a
P.toPermutationWithDefault ReferentialAction
DefaultReferentialAction ParsecT Void SQLStream (Reader Dialect) ReferentialAction
onUpdate
    Permutation
  (ParsecT Void SQLStream (Reader Dialect))
  (ReferentialAction -> (ReferentialAction, ReferentialAction))
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect)) ReferentialAction
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (ReferentialAction, ReferentialAction)
forall a b.
Permutation (ParsecT Void SQLStream (Reader Dialect)) (a -> b)
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) a
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ReferentialAction
-> ParsecT Void SQLStream (Reader Dialect) ReferentialAction
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect)) ReferentialAction
forall (m :: * -> *) a.
Alternative m =>
a -> m a -> Permutation m a
P.toPermutationWithDefault ReferentialAction
DefaultReferentialAction ParsecT Void SQLStream (Reader Dialect) ReferentialAction
onDelete
  where
    -- todo: left factor?
    onUpdate :: ParsecT Void SQLStream (Reader Dialect) ReferentialAction
onUpdate = ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"on", Text
"update"]) ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ReferentialAction
-> ParsecT Void SQLStream (Reader Dialect) ReferentialAction
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Void SQLStream (Reader Dialect) ReferentialAction
referentialAction
    onDelete :: ParsecT Void SQLStream (Reader Dialect) ReferentialAction
onDelete = ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"on", Text
"delete"]) ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ReferentialAction
-> ParsecT Void SQLStream (Reader Dialect) ReferentialAction
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Void SQLStream (Reader Dialect) ReferentialAction
referentialAction
    referentialAction :: ParsecT Void SQLStream (Reader Dialect) ReferentialAction
referentialAction = [ParsecT Void SQLStream (Reader Dialect) ReferentialAction]
-> ParsecT Void SQLStream (Reader Dialect) ReferentialAction
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [
         ReferentialAction
RefCascade ReferentialAction
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ReferentialAction
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"cascade"
         -- todo: left factor?
        ,ReferentialAction
RefSetNull ReferentialAction
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ReferentialAction
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"set", Text
"null"])
        ,ReferentialAction
RefSetDefault ReferentialAction
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ReferentialAction
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"set", Text
"default"])
        ,ReferentialAction
RefRestrict ReferentialAction
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ReferentialAction
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"restrict"
        ,ReferentialAction
RefNoAction ReferentialAction
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ReferentialAction
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"no", Text
"action"]]

colConstraintDef :: Parser ColConstraintDef
colConstraintDef :: ParsecT Void SQLStream (Reader Dialect) ColConstraintDef
colConstraintDef =
    Maybe [Name] -> ColConstraint -> ColConstraintDef
ColConstraintDef
    (Maybe [Name] -> ColConstraint -> ColConstraintDef)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
-> ParsecT
     Void SQLStream (Reader Dialect) (ColConstraint -> ColConstraintDef)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"constraint" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser [Name] -> Parser [Name]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser [Name]
names)
    ParsecT
  Void SQLStream (Reader Dialect) (ColConstraint -> ColConstraintDef)
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
-> ParsecT Void SQLStream (Reader Dialect) ColConstraintDef
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (ParsecT Void SQLStream (Reader Dialect) ColConstraint
nullable ParsecT Void SQLStream (Reader Dialect) ColConstraint
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT Void SQLStream (Reader Dialect) ColConstraint
notNull ParsecT Void SQLStream (Reader Dialect) ColConstraint
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT Void SQLStream (Reader Dialect) ColConstraint
unique ParsecT Void SQLStream (Reader Dialect) ColConstraint
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT Void SQLStream (Reader Dialect) ColConstraint
primaryKey ParsecT Void SQLStream (Reader Dialect) ColConstraint
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT Void SQLStream (Reader Dialect) ColConstraint
check ParsecT Void SQLStream (Reader Dialect) ColConstraint
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT Void SQLStream (Reader Dialect) ColConstraint
references)
  where
    nullable :: ParsecT Void SQLStream (Reader Dialect) ColConstraint
nullable = ColConstraint
ColNullableConstraint ColConstraint
-> Parser Text
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> Parser Text
keyword Text
"null"
    notNull :: ParsecT Void SQLStream (Reader Dialect) ColConstraint
notNull = ColConstraint
ColNotNullConstraint ColConstraint
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"not", Text
"null"]
    unique :: ParsecT Void SQLStream (Reader Dialect) ColConstraint
unique = ColConstraint
ColUniqueConstraint ColConstraint
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"unique"
    primaryKey :: ParsecT Void SQLStream (Reader Dialect) ColConstraint
primaryKey = do
      [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"primary", Text
"key"] 
      Dialect
d <- (Dialect -> Dialect) -> Parser Dialect
forall a. (Dialect -> a) -> Parser a
askDialect Dialect -> Dialect
forall a. a -> a
id
      Maybe ()
autoincrement <- if Dialect -> Bool
diAutoincrement Dialect
d 
        then ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"autoincrement")
        else Maybe () -> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe ()
forall a. Maybe a
Nothing
      ColConstraint
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ColConstraint
 -> ParsecT Void SQLStream (Reader Dialect) ColConstraint)
-> ColConstraint
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
forall a b. (a -> b) -> a -> b
$ Bool -> ColConstraint
ColPrimaryKeyConstraint (Bool -> ColConstraint) -> Bool -> ColConstraint
forall a b. (a -> b) -> a -> b
$ Maybe () -> Bool
forall a. Maybe a -> Bool
isJust Maybe ()
autoincrement
    check :: ParsecT Void SQLStream (Reader Dialect) ColConstraint
check = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"check" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ScalarExpr -> ColConstraint
ColCheckConstraint (ScalarExpr -> ColConstraint)
-> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens Parser ScalarExpr
scalarExpr
    references :: ParsecT Void SQLStream (Reader Dialect) ColConstraint
references = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"references" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
        (\[Name]
t Maybe Name
c ReferenceMatch
m (ReferentialAction
ou,ReferentialAction
od) -> [Name]
-> Maybe Name
-> ReferenceMatch
-> ReferentialAction
-> ReferentialAction
-> ColConstraint
ColReferencesConstraint [Name]
t Maybe Name
c ReferenceMatch
m ReferentialAction
ou ReferentialAction
od)
        ([Name]
 -> Maybe Name
 -> ReferenceMatch
 -> (ReferentialAction, ReferentialAction)
 -> ColConstraint)
-> Parser [Name]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Name
      -> ReferenceMatch
      -> (ReferentialAction, ReferentialAction)
      -> ColConstraint)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names
        ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe Name
   -> ReferenceMatch
   -> (ReferentialAction, ReferentialAction)
   -> ColConstraint)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Name)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ReferenceMatch
      -> (ReferentialAction, ReferentialAction) -> ColConstraint)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Name -> ParsecT Void SQLStream (Reader Dialect) (Maybe Name)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Parser Name -> Parser Name
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens Parser Name
name)
        ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ReferenceMatch
   -> (ReferentialAction, ReferentialAction) -> ColConstraint)
-> ParsecT Void SQLStream (Reader Dialect) ReferenceMatch
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ((ReferentialAction, ReferentialAction) -> ColConstraint)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) ReferenceMatch
refMatch
        ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ((ReferentialAction, ReferentialAction) -> ColConstraint)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ReferentialAction, ReferentialAction)
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ReferentialAction, ReferentialAction)
refActions

-- slightly hacky parser for signed integers

signedInteger :: Parser Integer
signedInteger :: ParsecT Void SQLStream (Reader Dialect) Integer
signedInteger =
    Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
(*) (Integer -> Integer -> Integer)
-> ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT Void SQLStream (Reader Dialect) (Integer -> Integer)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Integer
-> ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT Void SQLStream (Reader Dialect) Integer
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option Integer
1 (Integer
1 Integer
-> Parser Text -> ParsecT Void SQLStream (Reader Dialect) Integer
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> Parser Text
symbol Text
"+" ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT Void SQLStream (Reader Dialect) Integer
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (-Integer
1) Integer
-> Parser Text -> ParsecT Void SQLStream (Reader Dialect) Integer
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> Parser Text
symbol Text
"-")
    ParsecT Void SQLStream (Reader Dialect) (Integer -> Integer)
-> ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT Void SQLStream (Reader Dialect) Integer
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) Integer
unsignedInteger

sequenceGeneratorOptions :: Parser [SequenceGeneratorOption]
sequenceGeneratorOptions :: ParsecT Void SQLStream (Reader Dialect) [SequenceGeneratorOption]
sequenceGeneratorOptions =
         -- todo: could try to combine exclusive options
         -- such as cycle and nocycle
         -- sort out options which are sometimes not allowed
         -- as datatype, and restart with
    Permutation
  (ParsecT Void SQLStream (Reader Dialect)) [SequenceGeneratorOption]
-> ParsecT
     Void SQLStream (Reader Dialect) [SequenceGeneratorOption]
forall (m :: * -> *) a.
(Alternative m, Monad m) =>
Permutation m a -> m a
P.runPermutation ((\Maybe SequenceGeneratorOption
a Maybe SequenceGeneratorOption
b Maybe SequenceGeneratorOption
c Maybe SequenceGeneratorOption
d Maybe SequenceGeneratorOption
e Maybe SequenceGeneratorOption
f Maybe SequenceGeneratorOption
g Maybe SequenceGeneratorOption
h Maybe SequenceGeneratorOption
j Maybe SequenceGeneratorOption
k -> [Maybe SequenceGeneratorOption] -> [SequenceGeneratorOption]
forall a. [Maybe a] -> [a]
catMaybes [Maybe SequenceGeneratorOption
a,Maybe SequenceGeneratorOption
b,Maybe SequenceGeneratorOption
c,Maybe SequenceGeneratorOption
d,Maybe SequenceGeneratorOption
e,Maybe SequenceGeneratorOption
f,Maybe SequenceGeneratorOption
g,Maybe SequenceGeneratorOption
h,Maybe SequenceGeneratorOption
j,Maybe SequenceGeneratorOption
k])
                  (Maybe SequenceGeneratorOption
 -> Maybe SequenceGeneratorOption
 -> Maybe SequenceGeneratorOption
 -> Maybe SequenceGeneratorOption
 -> Maybe SequenceGeneratorOption
 -> Maybe SequenceGeneratorOption
 -> Maybe SequenceGeneratorOption
 -> Maybe SequenceGeneratorOption
 -> Maybe SequenceGeneratorOption
 -> Maybe SequenceGeneratorOption
 -> [SequenceGeneratorOption])
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption)
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> [SequenceGeneratorOption])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption)
forall {m :: * -> *} {a}.
Alternative m =>
m a -> Permutation m (Maybe a)
maybePermutation ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
startWith
                  Permutation
  (ParsecT Void SQLStream (Reader Dialect))
  (Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> [SequenceGeneratorOption])
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption)
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> [SequenceGeneratorOption])
forall a b.
Permutation (ParsecT Void SQLStream (Reader Dialect)) (a -> b)
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) a
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption)
forall {m :: * -> *} {a}.
Alternative m =>
m a -> Permutation m (Maybe a)
maybePermutation ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
dataType
                  Permutation
  (ParsecT Void SQLStream (Reader Dialect))
  (Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> [SequenceGeneratorOption])
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption)
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> [SequenceGeneratorOption])
forall a b.
Permutation (ParsecT Void SQLStream (Reader Dialect)) (a -> b)
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) a
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption)
forall {m :: * -> *} {a}.
Alternative m =>
m a -> Permutation m (Maybe a)
maybePermutation ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
restart
                  Permutation
  (ParsecT Void SQLStream (Reader Dialect))
  (Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> [SequenceGeneratorOption])
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption)
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> [SequenceGeneratorOption])
forall a b.
Permutation (ParsecT Void SQLStream (Reader Dialect)) (a -> b)
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) a
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption)
forall {m :: * -> *} {a}.
Alternative m =>
m a -> Permutation m (Maybe a)
maybePermutation ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
incrementBy
                  Permutation
  (ParsecT Void SQLStream (Reader Dialect))
  (Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> [SequenceGeneratorOption])
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption)
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> [SequenceGeneratorOption])
forall a b.
Permutation (ParsecT Void SQLStream (Reader Dialect)) (a -> b)
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) a
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption)
forall {m :: * -> *} {a}.
Alternative m =>
m a -> Permutation m (Maybe a)
maybePermutation ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
maxValue
                  Permutation
  (ParsecT Void SQLStream (Reader Dialect))
  (Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> [SequenceGeneratorOption])
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption)
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> [SequenceGeneratorOption])
forall a b.
Permutation (ParsecT Void SQLStream (Reader Dialect)) (a -> b)
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) a
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption)
forall {m :: * -> *} {a}.
Alternative m =>
m a -> Permutation m (Maybe a)
maybePermutation ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
noMaxValue
                  Permutation
  (ParsecT Void SQLStream (Reader Dialect))
  (Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> [SequenceGeneratorOption])
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption)
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption
      -> [SequenceGeneratorOption])
forall a b.
Permutation (ParsecT Void SQLStream (Reader Dialect)) (a -> b)
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) a
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption)
forall {m :: * -> *} {a}.
Alternative m =>
m a -> Permutation m (Maybe a)
maybePermutation ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
minValue
                  Permutation
  (ParsecT Void SQLStream (Reader Dialect))
  (Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption
   -> [SequenceGeneratorOption])
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption)
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption
      -> Maybe SequenceGeneratorOption -> [SequenceGeneratorOption])
forall a b.
Permutation (ParsecT Void SQLStream (Reader Dialect)) (a -> b)
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) a
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption)
forall {m :: * -> *} {a}.
Alternative m =>
m a -> Permutation m (Maybe a)
maybePermutation ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
noMinValue
                  Permutation
  (ParsecT Void SQLStream (Reader Dialect))
  (Maybe SequenceGeneratorOption
   -> Maybe SequenceGeneratorOption -> [SequenceGeneratorOption])
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption)
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption -> [SequenceGeneratorOption])
forall a b.
Permutation (ParsecT Void SQLStream (Reader Dialect)) (a -> b)
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) a
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption)
forall {m :: * -> *} {a}.
Alternative m =>
m a -> Permutation m (Maybe a)
maybePermutation ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
scycle
                  Permutation
  (ParsecT Void SQLStream (Reader Dialect))
  (Maybe SequenceGeneratorOption -> [SequenceGeneratorOption])
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption)
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect)) [SequenceGeneratorOption]
forall a b.
Permutation (ParsecT Void SQLStream (Reader Dialect)) (a -> b)
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) a
-> Permutation (ParsecT Void SQLStream (Reader Dialect)) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
-> Permutation
     (ParsecT Void SQLStream (Reader Dialect))
     (Maybe SequenceGeneratorOption)
forall {m :: * -> *} {a}.
Alternative m =>
m a -> Permutation m (Maybe a)
maybePermutation ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
noCycle
                 )
  where
    maybePermutation :: m a -> Permutation m (Maybe a)
maybePermutation m a
p = Maybe a -> m (Maybe a) -> Permutation m (Maybe a)
forall (m :: * -> *) a.
Alternative m =>
a -> m a -> Permutation m a
P.toPermutationWithDefault Maybe a
forall a. Maybe a
Nothing (a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> m a -> m (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
p)
    startWith :: ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
startWith = [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"start", Text
"with"] ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                Integer -> SequenceGeneratorOption
SGOStartWith (Integer -> SequenceGeneratorOption)
-> ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) Integer
signedInteger
    dataType :: ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
dataType = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"as" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
               TypeName -> SequenceGeneratorOption
SGODataType (TypeName -> SequenceGeneratorOption)
-> Parser TypeName
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser TypeName
typeName
    restart :: ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
restart = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"restart" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
              Maybe Integer -> SequenceGeneratorOption
SGORestart (Maybe Integer -> SequenceGeneratorOption)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Integer)
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Integer)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"with" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT Void SQLStream (Reader Dialect) Integer
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Void SQLStream (Reader Dialect) Integer
signedInteger)
    incrementBy :: ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
incrementBy = [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"increment", Text
"by"] ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                Integer -> SequenceGeneratorOption
SGOIncrementBy (Integer -> SequenceGeneratorOption)
-> ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) Integer
signedInteger
    maxValue :: ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
maxValue = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"maxvalue" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                Integer -> SequenceGeneratorOption
SGOMaxValue (Integer -> SequenceGeneratorOption)
-> ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) Integer
signedInteger
    noMaxValue :: ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
noMaxValue = SequenceGeneratorOption
SGONoMaxValue SequenceGeneratorOption
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"no",Text
"maxvalue"])
    minValue :: ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
minValue = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"minvalue" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                Integer -> SequenceGeneratorOption
SGOMinValue (Integer -> SequenceGeneratorOption)
-> ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) Integer
signedInteger
    noMinValue :: ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
noMinValue = SequenceGeneratorOption
SGONoMinValue SequenceGeneratorOption
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"no",Text
"minvalue"])
    scycle :: ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
scycle = SequenceGeneratorOption
SGOCycle SequenceGeneratorOption
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"cycle"
    noCycle :: ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
noCycle = SequenceGeneratorOption
SGONoCycle SequenceGeneratorOption
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) SequenceGeneratorOption
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"no",Text
"cycle"])


alterTable :: Parser Statement
alterTable :: Parser Statement
alterTable = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"table" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    -- the choices have been ordered so that it works
    [Name] -> AlterTableAction -> Statement
AlterTable ([Name] -> AlterTableAction -> Statement)
-> Parser [Name]
-> ParsecT
     Void SQLStream (Reader Dialect) (AlterTableAction -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names ParsecT
  Void SQLStream (Reader Dialect) (AlterTableAction -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) AlterTableAction
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [ParsecT Void SQLStream (Reader Dialect) AlterTableAction]
-> ParsecT Void SQLStream (Reader Dialect) AlterTableAction
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [ParsecT Void SQLStream (Reader Dialect) AlterTableAction
addConstraint
                                    ,ParsecT Void SQLStream (Reader Dialect) AlterTableAction
dropConstraint
                                    ,ParsecT Void SQLStream (Reader Dialect) AlterTableAction
addColumnDef
                                    ,ParsecT Void SQLStream (Reader Dialect) AlterTableAction
alterColumn
                                    ,ParsecT Void SQLStream (Reader Dialect) AlterTableAction
dropColumn
                                    ]
  where
    addColumnDef :: ParsecT Void SQLStream (Reader Dialect) AlterTableAction
addColumnDef = ParsecT Void SQLStream (Reader Dialect) (Maybe ())
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"add"
                        ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"column")) ParsecT Void SQLStream (Reader Dialect) (Maybe ())
-> ParsecT Void SQLStream (Reader Dialect) AlterTableAction
-> ParsecT Void SQLStream (Reader Dialect) AlterTableAction
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                   ColumnDef -> AlterTableAction
AddColumnDef (ColumnDef -> AlterTableAction)
-> ParsecT Void SQLStream (Reader Dialect) ColumnDef
-> ParsecT Void SQLStream (Reader Dialect) AlterTableAction
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) ColumnDef
columnDef
    alterColumn :: ParsecT Void SQLStream (Reader Dialect) AlterTableAction
alterColumn = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"alter" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"column") ParsecT Void SQLStream (Reader Dialect) (Maybe ())
-> ParsecT Void SQLStream (Reader Dialect) AlterTableAction
-> ParsecT Void SQLStream (Reader Dialect) AlterTableAction
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                  Parser Name
name Parser Name
-> ParsecT
     Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
-> ParsecT Void SQLStream (Reader Dialect) AlterTableAction
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> [ParsecT
   Void SQLStream (Reader Dialect) (Name -> AlterTableAction)]
-> ParsecT
     Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [ParsecT Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
setDefault
                                   ,ParsecT Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
dropDefault
                                   ,ParsecT Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
setNotNull
                                   ,ParsecT Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
dropNotNull
                                   ,ParsecT Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
setDataType]
    setDefault :: Parser (Name -> AlterTableAction)
    -- todo: left factor
    setDefault :: ParsecT Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
setDefault = ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"set",Text
"default"]) ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT
     Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
-> ParsecT
     Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                 Parser ScalarExpr
scalarExpr Parser ScalarExpr
-> (Name -> ScalarExpr -> AlterTableAction)
-> ParsecT
     Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
forall (f :: * -> *) b a c.
Applicative f =>
f b -> (a -> b -> c) -> f (a -> c)
<$$> Name -> ScalarExpr -> AlterTableAction
AlterColumnSetDefault
    dropDefault :: ParsecT Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
dropDefault = Name -> AlterTableAction
AlterColumnDropDefault (Name -> AlterTableAction)
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT
     Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"drop",Text
"default"])
    setNotNull :: ParsecT Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
setNotNull = Name -> AlterTableAction
AlterColumnSetNotNull (Name -> AlterTableAction)
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT
     Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"set",Text
"not",Text
"null"])
    dropNotNull :: ParsecT Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
dropNotNull = Name -> AlterTableAction
AlterColumnDropNotNull (Name -> AlterTableAction)
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT
     Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"drop",Text
"not",Text
"null"])
    setDataType :: ParsecT Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
setDataType = ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"set",Text
"data",Text
"type"]) ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT
     Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
-> ParsecT
     Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                  Parser TypeName
typeName Parser TypeName
-> (Name -> TypeName -> AlterTableAction)
-> ParsecT
     Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
forall (f :: * -> *) b a c.
Applicative f =>
f b -> (a -> b -> c) -> f (a -> c)
<$$> Name -> TypeName -> AlterTableAction
AlterColumnSetDataType
    dropColumn :: ParsecT Void SQLStream (Reader Dialect) AlterTableAction
dropColumn = ParsecT Void SQLStream (Reader Dialect) (Maybe ())
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"drop" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"column")) ParsecT Void SQLStream (Reader Dialect) (Maybe ())
-> ParsecT Void SQLStream (Reader Dialect) AlterTableAction
-> ParsecT Void SQLStream (Reader Dialect) AlterTableAction
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                 Name -> DropBehaviour -> AlterTableAction
DropColumn (Name -> DropBehaviour -> AlterTableAction)
-> Parser Name
-> ParsecT
     Void SQLStream (Reader Dialect) (DropBehaviour -> AlterTableAction)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Name
name ParsecT
  Void SQLStream (Reader Dialect) (DropBehaviour -> AlterTableAction)
-> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
-> ParsecT Void SQLStream (Reader Dialect) AlterTableAction
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
dropBehaviour
    -- todo: left factor, this try is especially bad
    addConstraint :: ParsecT Void SQLStream (Reader Dialect) AlterTableAction
addConstraint = ParsecT Void SQLStream (Reader Dialect) AlterTableAction
-> ParsecT Void SQLStream (Reader Dialect) AlterTableAction
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"add" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) AlterTableAction
-> ParsecT Void SQLStream (Reader Dialect) AlterTableAction
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
        (Maybe [Name] -> TableConstraint -> AlterTableAction)
-> (Maybe [Name], TableConstraint) -> AlterTableAction
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Maybe [Name] -> TableConstraint -> AlterTableAction
AddTableConstraintDef ((Maybe [Name], TableConstraint) -> AlterTableAction)
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe [Name], TableConstraint)
-> ParsecT Void SQLStream (Reader Dialect) AlterTableAction
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT
  Void SQLStream (Reader Dialect) (Maybe [Name], TableConstraint)
tableConstraintDef)
    dropConstraint :: ParsecT Void SQLStream (Reader Dialect) AlterTableAction
dropConstraint = ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"drop",Text
"constraint"]) ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) AlterTableAction
-> ParsecT Void SQLStream (Reader Dialect) AlterTableAction
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
        [Name] -> DropBehaviour -> AlterTableAction
DropTableConstraintDef ([Name] -> DropBehaviour -> AlterTableAction)
-> Parser [Name]
-> ParsecT
     Void SQLStream (Reader Dialect) (DropBehaviour -> AlterTableAction)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names ParsecT
  Void SQLStream (Reader Dialect) (DropBehaviour -> AlterTableAction)
-> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
-> ParsecT Void SQLStream (Reader Dialect) AlterTableAction
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
dropBehaviour


dropSchema :: Parser Statement
dropSchema :: Parser Statement
dropSchema = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"schema" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    [Name] -> DropBehaviour -> Statement
DropSchema ([Name] -> DropBehaviour -> Statement)
-> Parser [Name]
-> ParsecT
     Void SQLStream (Reader Dialect) (DropBehaviour -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names ParsecT
  Void SQLStream (Reader Dialect) (DropBehaviour -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
dropBehaviour

dropTable :: Parser Statement
dropTable :: Parser Statement
dropTable = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"table" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    [Name] -> DropBehaviour -> Statement
DropTable ([Name] -> DropBehaviour -> Statement)
-> Parser [Name]
-> ParsecT
     Void SQLStream (Reader Dialect) (DropBehaviour -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names ParsecT
  Void SQLStream (Reader Dialect) (DropBehaviour -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
dropBehaviour

createView :: Parser Statement
createView :: Parser Statement
createView =
    Bool
-> [Name]
-> Maybe [Name]
-> QueryExpr
-> Maybe CheckOption
-> Statement
CreateView
    (Bool
 -> [Name]
 -> Maybe [Name]
 -> QueryExpr
 -> Maybe CheckOption
 -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) Bool
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([Name]
      -> Maybe [Name] -> QueryExpr -> Maybe CheckOption -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Bool
-> ParsecT Void SQLStream (Reader Dialect) Bool
-> ParsecT Void SQLStream (Reader Dialect) Bool
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option Bool
False (Bool
True Bool
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) Bool
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"recursive") ParsecT Void SQLStream (Reader Dialect) Bool
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) Bool
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"view")
    ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([Name]
   -> Maybe [Name] -> QueryExpr -> Maybe CheckOption -> Statement)
-> Parser [Name]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe [Name] -> QueryExpr -> Maybe CheckOption -> Statement)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser [Name]
names
    ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe [Name] -> QueryExpr -> Maybe CheckOption -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (QueryExpr -> Maybe CheckOption -> Statement)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Parser [Name] -> Parser [Name]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser Name -> Parser [Name]
forall a. Parser a -> Parser [a]
commaSep1 Parser Name
name))
    ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (QueryExpr -> Maybe CheckOption -> Statement)
-> Parser QueryExpr
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe CheckOption -> Statement)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"as" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser QueryExpr -> Parser QueryExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser QueryExpr
queryExpr)
    ParsecT
  Void SQLStream (Reader Dialect) (Maybe CheckOption -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe CheckOption)
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) CheckOption
-> ParsecT Void SQLStream (Reader Dialect) (Maybe CheckOption)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ([ParsecT Void SQLStream (Reader Dialect) CheckOption]
-> ParsecT Void SQLStream (Reader Dialect) CheckOption
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [
            -- todo: left factor
            CheckOption
DefaultCheckOption CheckOption
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) CheckOption
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"with", Text
"check", Text
"option"])
           ,CheckOption
CascadedCheckOption CheckOption
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) CheckOption
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"with", Text
"cascaded", Text
"check", Text
"option"])
           ,CheckOption
LocalCheckOption CheckOption
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) CheckOption
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"with", Text
"local", Text
"check", Text
"option"])
            ])

dropView :: Parser Statement
dropView :: Parser Statement
dropView = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"view" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    [Name] -> DropBehaviour -> Statement
DropView ([Name] -> DropBehaviour -> Statement)
-> Parser [Name]
-> ParsecT
     Void SQLStream (Reader Dialect) (DropBehaviour -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names ParsecT
  Void SQLStream (Reader Dialect) (DropBehaviour -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
dropBehaviour

createDomain :: Parser Statement
createDomain :: Parser Statement
createDomain = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"domain" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    [Name]
-> TypeName
-> Maybe ScalarExpr
-> [(Maybe [Name], ScalarExpr)]
-> Statement
CreateDomain
    ([Name]
 -> TypeName
 -> Maybe ScalarExpr
 -> [(Maybe [Name], ScalarExpr)]
 -> Statement)
-> Parser [Name]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (TypeName
      -> Maybe ScalarExpr -> [(Maybe [Name], ScalarExpr)] -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names
    ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (TypeName
   -> Maybe ScalarExpr -> [(Maybe [Name], ScalarExpr)] -> Statement)
-> Parser TypeName
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe ScalarExpr -> [(Maybe [Name], ScalarExpr)] -> Statement)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"as") ParsecT Void SQLStream (Reader Dialect) (Maybe ())
-> Parser TypeName -> Parser TypeName
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser TypeName
typeName)
    ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe ScalarExpr -> [(Maybe [Name], ScalarExpr)] -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([(Maybe [Name], ScalarExpr)] -> Statement)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"default" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr
scalarExpr)
    ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([(Maybe [Name], ScalarExpr)] -> Statement)
-> ParsecT
     Void SQLStream (Reader Dialect) [(Maybe [Name], ScalarExpr)]
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name], ScalarExpr)
-> ParsecT
     Void SQLStream (Reader Dialect) [(Maybe [Name], ScalarExpr)]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
many ParsecT Void SQLStream (Reader Dialect) (Maybe [Name], ScalarExpr)
con
  where
    con :: ParsecT Void SQLStream (Reader Dialect) (Maybe [Name], ScalarExpr)
con = (,) (Maybe [Name] -> ScalarExpr -> (Maybe [Name], ScalarExpr))
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> (Maybe [Name], ScalarExpr))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"constraint" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser [Name] -> Parser [Name]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser [Name]
names)
          ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ScalarExpr -> (Maybe [Name], ScalarExpr))
-> Parser ScalarExpr
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe [Name], ScalarExpr)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"check" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens Parser ScalarExpr
scalarExpr)

alterDomain :: Parser Statement
alterDomain :: Parser Statement
alterDomain = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"domain" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    [Name] -> AlterDomainAction -> Statement
AlterDomain
    ([Name] -> AlterDomainAction -> Statement)
-> Parser [Name]
-> ParsecT
     Void SQLStream (Reader Dialect) (AlterDomainAction -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names
    ParsecT
  Void SQLStream (Reader Dialect) (AlterDomainAction -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
setDefault ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
-> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
-> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
constraint
         ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
-> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
-> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"drop" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
-> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
dropDefault ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
-> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
-> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
dropConstraint)))
  where
    setDefault :: ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
setDefault = [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"set", Text
"default"] ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
-> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ScalarExpr -> AlterDomainAction
ADSetDefault (ScalarExpr -> AlterDomainAction)
-> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ScalarExpr
scalarExpr
    constraint :: ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
constraint = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"add" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
-> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
       Maybe [Name] -> ScalarExpr -> AlterDomainAction
ADAddConstraint
       (Maybe [Name] -> ScalarExpr -> AlterDomainAction)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> AlterDomainAction)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"constraint" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser [Name] -> Parser [Name]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser [Name]
names)
       ParsecT
  Void SQLStream (Reader Dialect) (ScalarExpr -> AlterDomainAction)
-> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"check" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens Parser ScalarExpr
scalarExpr)
    dropDefault :: ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
dropDefault = AlterDomainAction
ADDropDefault AlterDomainAction
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"default"
    dropConstraint :: ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
dropConstraint = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"constraint" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
-> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [Name] -> AlterDomainAction
ADDropConstraint ([Name] -> AlterDomainAction)
-> Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) AlterDomainAction
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names

dropDomain :: Parser Statement
dropDomain :: Parser Statement
dropDomain = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"domain" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    [Name] -> DropBehaviour -> Statement
DropDomain ([Name] -> DropBehaviour -> Statement)
-> Parser [Name]
-> ParsecT
     Void SQLStream (Reader Dialect) (DropBehaviour -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names ParsecT
  Void SQLStream (Reader Dialect) (DropBehaviour -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
dropBehaviour

createSequence :: Parser Statement
createSequence :: Parser Statement
createSequence = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"sequence" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    [Name] -> [SequenceGeneratorOption] -> Statement
CreateSequence
    ([Name] -> [SequenceGeneratorOption] -> Statement)
-> Parser [Name]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([SequenceGeneratorOption] -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names
    ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([SequenceGeneratorOption] -> Statement)
-> ParsecT
     Void SQLStream (Reader Dialect) [SequenceGeneratorOption]
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) [SequenceGeneratorOption]
sequenceGeneratorOptions

alterSequence :: Parser Statement
alterSequence :: Parser Statement
alterSequence = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"sequence" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    [Name] -> [SequenceGeneratorOption] -> Statement
AlterSequence
    ([Name] -> [SequenceGeneratorOption] -> Statement)
-> Parser [Name]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([SequenceGeneratorOption] -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names
    ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([SequenceGeneratorOption] -> Statement)
-> ParsecT
     Void SQLStream (Reader Dialect) [SequenceGeneratorOption]
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) [SequenceGeneratorOption]
sequenceGeneratorOptions

dropSequence :: Parser Statement
dropSequence :: Parser Statement
dropSequence = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"sequence" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    [Name] -> DropBehaviour -> Statement
DropSequence ([Name] -> DropBehaviour -> Statement)
-> Parser [Name]
-> ParsecT
     Void SQLStream (Reader Dialect) (DropBehaviour -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names ParsecT
  Void SQLStream (Reader Dialect) (DropBehaviour -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
dropBehaviour

createAssertion :: Parser Statement
createAssertion :: Parser Statement
createAssertion = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"assertion" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    [Name] -> ScalarExpr -> Statement
CreateAssertion
    ([Name] -> ScalarExpr -> Statement)
-> Parser [Name]
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names
    ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> Statement)
-> Parser ScalarExpr -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"check" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens Parser ScalarExpr
scalarExpr)


dropAssertion :: Parser Statement
dropAssertion :: Parser Statement
dropAssertion = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"assertion" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    [Name] -> DropBehaviour -> Statement
DropAssertion ([Name] -> DropBehaviour -> Statement)
-> Parser [Name]
-> ParsecT
     Void SQLStream (Reader Dialect) (DropBehaviour -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names ParsecT
  Void SQLStream (Reader Dialect) (DropBehaviour -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
dropBehaviour

{-
-----------------

= dml
-}

delete :: Parser Statement
delete :: Parser Statement
delete = [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"delete",Text
"from"] ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    [Name] -> Maybe Name -> Maybe ScalarExpr -> Statement
Delete
    ([Name] -> Maybe Name -> Maybe ScalarExpr -> Statement)
-> Parser [Name]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Name -> Maybe ScalarExpr -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names
    ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe Name -> Maybe ScalarExpr -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Name)
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe ScalarExpr -> Statement)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Name -> ParsecT Void SQLStream (Reader Dialect) (Maybe Name)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"as") ParsecT Void SQLStream (Reader Dialect) (Maybe ())
-> Parser Name -> Parser Name
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Name
name)
    ParsecT
  Void SQLStream (Reader Dialect) (Maybe ScalarExpr -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"where" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr
scalarExpr)

truncateSt :: Parser Statement
truncateSt :: Parser Statement
truncateSt = [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"truncate", Text
"table"] ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    [Name] -> IdentityRestart -> Statement
Truncate
    ([Name] -> IdentityRestart -> Statement)
-> Parser [Name]
-> ParsecT
     Void SQLStream (Reader Dialect) (IdentityRestart -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names
    ParsecT
  Void SQLStream (Reader Dialect) (IdentityRestart -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) IdentityRestart
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> IdentityRestart
-> ParsecT Void SQLStream (Reader Dialect) IdentityRestart
-> ParsecT Void SQLStream (Reader Dialect) IdentityRestart
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option IdentityRestart
DefaultIdentityRestart
        (IdentityRestart
ContinueIdentity IdentityRestart
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) IdentityRestart
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"continue",Text
"identity"]
         ParsecT Void SQLStream (Reader Dialect) IdentityRestart
-> ParsecT Void SQLStream (Reader Dialect) IdentityRestart
-> ParsecT Void SQLStream (Reader Dialect) IdentityRestart
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> IdentityRestart
RestartIdentity IdentityRestart
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) IdentityRestart
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"restart",Text
"identity"])

insert :: Parser Statement
insert :: Parser Statement
insert = [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"insert", Text
"into"] ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    [Name] -> Maybe [Name] -> InsertSource -> Statement
Insert
    ([Name] -> Maybe [Name] -> InsertSource -> Statement)
-> Parser [Name]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe [Name] -> InsertSource -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names
    ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe [Name] -> InsertSource -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
-> ParsecT
     Void SQLStream (Reader Dialect) (InsertSource -> Statement)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Parser [Name] -> Parser [Name]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser [Name] -> Parser [Name]) -> Parser [Name] -> Parser [Name]
forall a b. (a -> b) -> a -> b
$ Parser Name -> Parser [Name]
forall a. Parser a -> Parser [a]
commaSep1 Parser Name
name)
    ParsecT Void SQLStream (Reader Dialect) (InsertSource -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) InsertSource
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (InsertSource
DefaultInsertValues InsertSource
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) InsertSource
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"default", Text
"values"]
         ParsecT Void SQLStream (Reader Dialect) InsertSource
-> ParsecT Void SQLStream (Reader Dialect) InsertSource
-> ParsecT Void SQLStream (Reader Dialect) InsertSource
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> QueryExpr -> InsertSource
InsertQuery (QueryExpr -> InsertSource)
-> Parser QueryExpr
-> ParsecT Void SQLStream (Reader Dialect) InsertSource
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser QueryExpr
queryExpr)

update :: Parser Statement
update :: Parser Statement
update = [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"update"] ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    [Name]
-> Maybe Name -> [SetClause] -> Maybe ScalarExpr -> Statement
Update
    ([Name]
 -> Maybe Name -> [SetClause] -> Maybe ScalarExpr -> Statement)
-> Parser [Name]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Name -> [SetClause] -> Maybe ScalarExpr -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names
    ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (Maybe Name -> [SetClause] -> Maybe ScalarExpr -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Name)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([SetClause] -> Maybe ScalarExpr -> Statement)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Name -> ParsecT Void SQLStream (Reader Dialect) (Maybe Name)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"as") ParsecT Void SQLStream (Reader Dialect) (Maybe ())
-> Parser Name -> Parser Name
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Name
name)
    ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([SetClause] -> Maybe ScalarExpr -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) [SetClause]
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe ScalarExpr -> Statement)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"set" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) [SetClause]
-> ParsecT Void SQLStream (Reader Dialect) [SetClause]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser SetClause
-> ParsecT Void SQLStream (Reader Dialect) [SetClause]
forall a. Parser a -> Parser [a]
commaSep1 Parser SetClause
setClause)
    ParsecT
  Void SQLStream (Reader Dialect) (Maybe ScalarExpr -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"where" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr
scalarExpr)
  where
    setClause :: Parser SetClause
setClause = Parser SetClause
multipleSet Parser SetClause -> Parser SetClause -> Parser SetClause
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser SetClause
singleSet
    multipleSet :: Parser SetClause
multipleSet = [[Name]] -> [ScalarExpr] -> SetClause
SetMultiple
                  ([[Name]] -> [ScalarExpr] -> SetClause)
-> ParsecT Void SQLStream (Reader Dialect) [[Name]]
-> ParsecT
     Void SQLStream (Reader Dialect) ([ScalarExpr] -> SetClause)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) [[Name]]
-> ParsecT Void SQLStream (Reader Dialect) [[Name]]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser [Name] -> ParsecT Void SQLStream (Reader Dialect) [[Name]]
forall a. Parser a -> Parser [a]
commaSep1 Parser [Name]
names)
                  ParsecT Void SQLStream (Reader Dialect) ([ScalarExpr] -> SetClause)
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> Parser SetClause
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> Parser Text
symbol Text
"=" Parser Text
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser ScalarExpr
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a. Parser a -> Parser [a]
commaSep1 Parser ScalarExpr
scalarExpr))
    singleSet :: Parser SetClause
singleSet = [Name] -> ScalarExpr -> SetClause
Set
                ([Name] -> ScalarExpr -> SetClause)
-> Parser [Name]
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> SetClause)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names
                ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> SetClause)
-> Parser ScalarExpr -> Parser SetClause
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> Parser Text
symbol Text
"=" Parser Text -> Parser ScalarExpr -> Parser ScalarExpr
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ScalarExpr
scalarExpr)

dropBehaviour :: Parser DropBehaviour
dropBehaviour :: ParsecT Void SQLStream (Reader Dialect) DropBehaviour
dropBehaviour =
    DropBehaviour
-> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
-> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option DropBehaviour
DefaultDropBehaviour
    (DropBehaviour
Restrict DropBehaviour
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"restrict"
    ParsecT Void SQLStream (Reader Dialect) DropBehaviour
-> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
-> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> DropBehaviour
Cascade DropBehaviour
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"cascade")

{-
-----------------------------

= transaction management
-}

startTransaction :: Parser Statement
startTransaction :: Parser Statement
startTransaction = Statement
StartTransaction Statement
-> ParsecT Void SQLStream (Reader Dialect) () -> Parser Statement
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"start",Text
"transaction"]

savepoint :: Parser Statement
savepoint :: Parser Statement
savepoint = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"savepoint" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    Name -> Statement
Savepoint (Name -> Statement) -> Parser Name -> Parser Statement
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Name
name

releaseSavepoint :: Parser Statement
releaseSavepoint :: Parser Statement
releaseSavepoint = [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"release",Text
"savepoint"] ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    Name -> Statement
ReleaseSavepoint (Name -> Statement) -> Parser Name -> Parser Statement
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Name
name

commit :: Parser Statement
commit :: Parser Statement
commit = Statement
Commit Statement
-> ParsecT Void SQLStream (Reader Dialect) () -> Parser Statement
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"commit" Parser Statement
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"work")

rollback :: Parser Statement
rollback :: Parser Statement
rollback = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"rollback" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"work") ParsecT Void SQLStream (Reader Dialect) (Maybe ())
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    Maybe Name -> Statement
Rollback (Maybe Name -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Name)
-> Parser Statement
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Name -> ParsecT Void SQLStream (Reader Dialect) (Maybe Name)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"to", Text
"savepoint"] ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Name -> Parser Name
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Name
name)


{-
------------------------------

= Access control

TODO: fix try at the 'on'
-}

grant :: Parser Statement
grant :: Parser Statement
grant = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"grant" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Parser Statement -> Parser Statement
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try Parser Statement
priv Parser Statement -> Parser Statement -> Parser Statement
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Statement
role)
  where
    priv :: Parser Statement
priv = [PrivilegeAction]
-> PrivilegeObject -> [Name] -> GrantOption -> Statement
GrantPrivilege
           ([PrivilegeAction]
 -> PrivilegeObject -> [Name] -> GrantOption -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) [PrivilegeAction]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (PrivilegeObject -> [Name] -> GrantOption -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser PrivilegeAction
-> ParsecT Void SQLStream (Reader Dialect) [PrivilegeAction]
forall a. Parser a -> Parser [a]
commaSep Parser PrivilegeAction
privilegeAction
           ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (PrivilegeObject -> [Name] -> GrantOption -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([Name] -> GrantOption -> Statement)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"on" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
privilegeObject)
           ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([Name] -> GrantOption -> Statement)
-> Parser [Name]
-> ParsecT
     Void SQLStream (Reader Dialect) (GrantOption -> Statement)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"to" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser [Name] -> Parser [Name]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Name -> Parser [Name]
forall a. Parser a -> Parser [a]
commaSep Parser Name
name)
           ParsecT Void SQLStream (Reader Dialect) (GrantOption -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) GrantOption
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> GrantOption
-> ParsecT Void SQLStream (Reader Dialect) GrantOption
-> ParsecT Void SQLStream (Reader Dialect) GrantOption
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option GrantOption
WithoutGrantOption
               (GrantOption
WithGrantOption GrantOption
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) GrantOption
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"with",Text
"grant",Text
"option"])
    role :: Parser Statement
role = [Name] -> [Name] -> AdminOption -> Statement
GrantRole
           ([Name] -> [Name] -> AdminOption -> Statement)
-> Parser [Name]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([Name] -> AdminOption -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Name -> Parser [Name]
forall a. Parser a -> Parser [a]
commaSep Parser Name
name
           ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([Name] -> AdminOption -> Statement)
-> Parser [Name]
-> ParsecT
     Void SQLStream (Reader Dialect) (AdminOption -> Statement)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"to" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser [Name] -> Parser [Name]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Name -> Parser [Name]
forall a. Parser a -> Parser [a]
commaSep Parser Name
name)
           ParsecT Void SQLStream (Reader Dialect) (AdminOption -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) AdminOption
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> AdminOption
-> ParsecT Void SQLStream (Reader Dialect) AdminOption
-> ParsecT Void SQLStream (Reader Dialect) AdminOption
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option AdminOption
WithoutAdminOption
               (AdminOption
WithAdminOption AdminOption
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) AdminOption
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"with",Text
"admin",Text
"option"])

createRole :: Parser Statement
createRole :: Parser Statement
createRole = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"role" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    Name -> Statement
CreateRole (Name -> Statement) -> Parser Name -> Parser Statement
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Name
name

dropRole :: Parser Statement
dropRole :: Parser Statement
dropRole = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"role" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    Name -> Statement
DropRole (Name -> Statement) -> Parser Name -> Parser Statement
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Name
name

-- TODO: fix try at the 'on'

revoke :: Parser Statement
revoke :: Parser Statement
revoke = Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"revoke" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser Statement -> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Parser Statement -> Parser Statement
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try Parser Statement
priv Parser Statement -> Parser Statement -> Parser Statement
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Statement
role)
  where
    priv :: Parser Statement
priv = GrantOptionFor
-> [PrivilegeAction]
-> PrivilegeObject
-> [Name]
-> DropBehaviour
-> Statement
RevokePrivilege
           (GrantOptionFor
 -> [PrivilegeAction]
 -> PrivilegeObject
 -> [Name]
 -> DropBehaviour
 -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) GrantOptionFor
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([PrivilegeAction]
      -> PrivilegeObject -> [Name] -> DropBehaviour -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GrantOptionFor
-> ParsecT Void SQLStream (Reader Dialect) GrantOptionFor
-> ParsecT Void SQLStream (Reader Dialect) GrantOptionFor
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option GrantOptionFor
NoGrantOptionFor
               (GrantOptionFor
GrantOptionFor GrantOptionFor
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) GrantOptionFor
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"grant",Text
"option",Text
"for"])
           ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([PrivilegeAction]
   -> PrivilegeObject -> [Name] -> DropBehaviour -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) [PrivilegeAction]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (PrivilegeObject -> [Name] -> DropBehaviour -> Statement)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser PrivilegeAction
-> ParsecT Void SQLStream (Reader Dialect) [PrivilegeAction]
forall a. Parser a -> Parser [a]
commaSep Parser PrivilegeAction
privilegeAction
           ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (PrivilegeObject -> [Name] -> DropBehaviour -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([Name] -> DropBehaviour -> Statement)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"on" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
privilegeObject)
           ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([Name] -> DropBehaviour -> Statement)
-> Parser [Name]
-> ParsecT
     Void SQLStream (Reader Dialect) (DropBehaviour -> Statement)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"from" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser [Name] -> Parser [Name]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Name -> Parser [Name]
forall a. Parser a -> Parser [a]
commaSep Parser Name
name)
           ParsecT
  Void SQLStream (Reader Dialect) (DropBehaviour -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
dropBehaviour
    role :: Parser Statement
role = AdminOptionFor -> [Name] -> [Name] -> DropBehaviour -> Statement
RevokeRole
           (AdminOptionFor -> [Name] -> [Name] -> DropBehaviour -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) AdminOptionFor
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([Name] -> [Name] -> DropBehaviour -> Statement)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AdminOptionFor
-> ParsecT Void SQLStream (Reader Dialect) AdminOptionFor
-> ParsecT Void SQLStream (Reader Dialect) AdminOptionFor
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option AdminOptionFor
NoAdminOptionFor
               (AdminOptionFor
AdminOptionFor AdminOptionFor
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) AdminOptionFor
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"admin",Text
"option", Text
"for"])
           ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([Name] -> [Name] -> DropBehaviour -> Statement)
-> Parser [Name]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([Name] -> DropBehaviour -> Statement)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Name -> Parser [Name]
forall a. Parser a -> Parser [a]
commaSep Parser Name
name
           ParsecT
  Void
  SQLStream
  (Reader Dialect)
  ([Name] -> DropBehaviour -> Statement)
-> Parser [Name]
-> ParsecT
     Void SQLStream (Reader Dialect) (DropBehaviour -> Statement)
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"from" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser [Name] -> Parser [Name]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Name -> Parser [Name]
forall a. Parser a -> Parser [a]
commaSep Parser Name
name)
           ParsecT
  Void SQLStream (Reader Dialect) (DropBehaviour -> Statement)
-> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
-> Parser Statement
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Void SQLStream (Reader Dialect) DropBehaviour
dropBehaviour

privilegeAction :: Parser PrivilegeAction
privilegeAction :: Parser PrivilegeAction
privilegeAction = [Parser PrivilegeAction] -> Parser PrivilegeAction
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
    [PrivilegeAction
PrivAll PrivilegeAction
-> ParsecT Void SQLStream (Reader Dialect) ()
-> Parser PrivilegeAction
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"all",Text
"privileges"]
    ,Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"select" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser PrivilegeAction -> Parser PrivilegeAction
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
     [Name] -> PrivilegeAction
PrivSelect ([Name] -> PrivilegeAction)
-> Parser [Name] -> Parser PrivilegeAction
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Name] -> Parser [Name] -> Parser [Name]
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option [] (Parser [Name] -> Parser [Name]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser [Name] -> Parser [Name]) -> Parser [Name] -> Parser [Name]
forall a b. (a -> b) -> a -> b
$ Parser Name -> Parser [Name]
forall a. Parser a -> Parser [a]
commaSep Parser Name
name)
    ,PrivilegeAction
PrivDelete PrivilegeAction
-> ParsecT Void SQLStream (Reader Dialect) ()
-> Parser PrivilegeAction
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"delete"
    ,PrivilegeAction
PrivUsage PrivilegeAction
-> ParsecT Void SQLStream (Reader Dialect) ()
-> Parser PrivilegeAction
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"usage"
    ,PrivilegeAction
PrivTrigger PrivilegeAction
-> ParsecT Void SQLStream (Reader Dialect) ()
-> Parser PrivilegeAction
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"trigger"
    ,PrivilegeAction
PrivExecute PrivilegeAction
-> ParsecT Void SQLStream (Reader Dialect) ()
-> Parser PrivilegeAction
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"execute"
    ,Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"insert" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser PrivilegeAction -> Parser PrivilegeAction
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
     [Name] -> PrivilegeAction
PrivInsert ([Name] -> PrivilegeAction)
-> Parser [Name] -> Parser PrivilegeAction
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Name] -> Parser [Name] -> Parser [Name]
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option [] (Parser [Name] -> Parser [Name]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser [Name] -> Parser [Name]) -> Parser [Name] -> Parser [Name]
forall a b. (a -> b) -> a -> b
$ Parser Name -> Parser [Name]
forall a. Parser a -> Parser [a]
commaSep Parser Name
name)
    ,Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"update" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser PrivilegeAction -> Parser PrivilegeAction
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
     [Name] -> PrivilegeAction
PrivUpdate ([Name] -> PrivilegeAction)
-> Parser [Name] -> Parser PrivilegeAction
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Name] -> Parser [Name] -> Parser [Name]
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option [] (Parser [Name] -> Parser [Name]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser [Name] -> Parser [Name]) -> Parser [Name] -> Parser [Name]
forall a b. (a -> b) -> a -> b
$ Parser Name -> Parser [Name]
forall a. Parser a -> Parser [a]
commaSep Parser Name
name)
    ,Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"references" ParsecT Void SQLStream (Reader Dialect) ()
-> Parser PrivilegeAction -> Parser PrivilegeAction
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
     [Name] -> PrivilegeAction
PrivReferences ([Name] -> PrivilegeAction)
-> Parser [Name] -> Parser PrivilegeAction
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Name] -> Parser [Name] -> Parser [Name]
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option [] (Parser [Name] -> Parser [Name]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens (Parser [Name] -> Parser [Name]) -> Parser [Name] -> Parser [Name]
forall a b. (a -> b) -> a -> b
$ Parser Name -> Parser [Name]
forall a. Parser a -> Parser [a]
commaSep Parser Name
name)
    ]

privilegeObject :: Parser PrivilegeObject
privilegeObject :: ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
privilegeObject = [ParsecT Void SQLStream (Reader Dialect) PrivilegeObject]
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
    [Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"domain" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [Name] -> PrivilegeObject
PrivDomain ([Name] -> PrivilegeObject)
-> Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names
    ,Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"type" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [Name] -> PrivilegeObject
PrivType ([Name] -> PrivilegeObject)
-> Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names
    ,Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"sequence" ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [Name] -> PrivilegeObject
PrivSequence ([Name] -> PrivilegeObject)
-> Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names
    ,[Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"specific",Text
"function"] ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [Name] -> PrivilegeObject
PrivFunction ([Name] -> PrivilegeObject)
-> Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names
    ,ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) (Maybe ())
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"table") ParsecT Void SQLStream (Reader Dialect) (Maybe ())
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [Name] -> PrivilegeObject
PrivTable ([Name] -> PrivilegeObject)
-> Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) PrivilegeObject
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Name]
names
    ]


{-
----------------------------

wrapper to parse a series of statements. They must be separated by
semicolon, but for the last statement, the trailing semicolon is
optional.
-}

statements :: Parser [Statement]
statements :: Parser [Statement]
statements = Parser Statement -> Parser [Statement]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
many Parser Statement
statement

{-
----------------------------------------------

= multi keyword helper

This helper is to help parsing multiple options of multiple keywords
with similar prefixes, e.g. parsing 'is null' and 'is not null'.

use to left factor/ improve:
typed literal and general identifiers
not like, not in, not between operators
help with factoring keyword functions and other app-likes
the join keyword sequences
fetch first/next
row/rows only

There is probably a simpler way of doing this but I am a bit
thick.
-}

makeKeywordTree :: [Text] -> Parser [Text]
makeKeywordTree :: [Text] -> Parser [Text]
makeKeywordTree [Text]
sets =
    [[Text]] -> Parser [Text]
parseTrees ([[Text]] -> [[Text]]
forall a. Ord a => [a] -> [a]
sort ([[Text]] -> [[Text]]) -> [[Text]] -> [[Text]]
forall a b. (a -> b) -> a -> b
$ (Text -> [Text]) -> [Text] -> [[Text]]
forall a b. (a -> b) -> [a] -> [b]
map Text -> [Text]
T.words [Text]
sets)
  where
    parseTrees :: [[Text]] -> Parser [Text]
    parseTrees :: [[Text]] -> Parser [Text]
parseTrees [[Text]]
ws = do
      let gs :: [[[Text]]]
          gs :: [[[Text]]]
gs = ([Text] -> [Text] -> Bool) -> [[Text]] -> [[[Text]]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy (Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
(==) (Maybe Text -> Maybe Text -> Bool)
-> ([Text] -> Maybe Text) -> [Text] -> [Text] -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` [Text] -> Maybe Text
forall {a}. [a] -> Maybe a
safeHead) [[Text]]
ws
      [Parser [Text]] -> Parser [Text]
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice ([Parser [Text]] -> Parser [Text])
-> [Parser [Text]] -> Parser [Text]
forall a b. (a -> b) -> a -> b
$ ([[Text]] -> Parser [Text]) -> [[[Text]]] -> [Parser [Text]]
forall a b. (a -> b) -> [a] -> [b]
map [[Text]] -> Parser [Text]
parseGroup [[[Text]]]
gs
    parseGroup :: [[Text]] -> Parser [Text]
    parseGroup :: [[Text]] -> Parser [Text]
parseGroup l :: [[Text]]
l@((Text
k:[Text]
_):[[Text]]
_) = do
        Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
k
        let tls :: [[Text]]
tls = ([Text] -> Maybe [Text]) -> [[Text]] -> [[Text]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe [Text] -> Maybe [Text]
forall {a}. [a] -> Maybe [a]
safeTail [[Text]]
l
            pr :: Parser [Text]
pr = (Text
kText -> [Text] -> [Text]
forall a. a -> [a] -> [a]
:) ([Text] -> [Text]) -> Parser [Text] -> Parser [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [[Text]] -> Parser [Text]
parseTrees [[Text]]
tls
        if ([Text] -> Bool) -> [[Text]] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any [Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[Text]]
tls
          then Parser [Text]
pr Parser [Text] -> Parser [Text] -> Parser [Text]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [Text] -> Parser [Text]
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Text
k]
          else Parser [Text]
pr
    parseGroup [[Text]]
_ = Bool -> ParsecT Void SQLStream (Reader Dialect) ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
False ParsecT Void SQLStream (Reader Dialect) ()
-> Parser [Text] -> Parser [Text]
forall a b.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> String -> Parser [Text]
forall a. String -> ParsecT Void SQLStream (Reader Dialect) a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"impossible"
    safeHead :: [a] -> Maybe a
safeHead (a
x:[a]
_) = a -> Maybe a
forall a. a -> Maybe a
Just a
x
    safeHead [] = Maybe a
forall a. Maybe a
Nothing
    safeTail :: [a] -> Maybe [a]
safeTail (a
_:[a]
x) = [a] -> Maybe [a]
forall a. a -> Maybe a
Just [a]
x
    safeTail [] = Maybe [a]
forall a. Maybe a
Nothing

------------------------------------------------------------------------------

-- parser helpers

(<$$>) :: Applicative f =>
      f b -> (a -> b -> c) -> f (a -> c)
<$$> :: forall (f :: * -> *) b a c.
Applicative f =>
f b -> (a -> b -> c) -> f (a -> c)
(<$$>) f b
pa a -> b -> c
c = f b
pa f b -> f (b -> a -> c) -> f (a -> c)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (b -> a -> c) -> f (b -> a -> c)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((a -> b -> c) -> b -> a -> c
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> b -> c
c)

(<$$$>) :: Applicative f =>
          f c -> (a -> b -> c -> t) -> f (b -> a -> t)
f c
p <$$$> :: forall (f :: * -> *) c a b t.
Applicative f =>
f c -> (a -> b -> c -> t) -> f (b -> a -> t)
<$$$> a -> b -> c -> t
c = f c
p f c -> f (c -> b -> a -> t) -> f (b -> a -> t)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (c -> b -> a -> t) -> f (c -> b -> a -> t)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((a -> b -> c -> t) -> c -> b -> a -> t
forall a b c t. (a -> b -> c -> t) -> c -> b -> a -> t
flip3 a -> b -> c -> t
c)

(<$$$$>) :: Applicative f =>
          f d -> (a -> b -> c -> d -> t) -> f (c -> b -> a -> t)
f d
p <$$$$> :: forall (f :: * -> *) d a b c t.
Applicative f =>
f d -> (a -> b -> c -> d -> t) -> f (c -> b -> a -> t)
<$$$$> a -> b -> c -> d -> t
c = f d
p f d -> f (d -> c -> b -> a -> t) -> f (c -> b -> a -> t)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (d -> c -> b -> a -> t) -> f (d -> c -> b -> a -> t)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((a -> b -> c -> d -> t) -> d -> c -> b -> a -> t
forall a b c d t. (a -> b -> c -> d -> t) -> d -> c -> b -> a -> t
flip4 a -> b -> c -> d -> t
c)

(<$$$$$>) :: Applicative f =>
          f e -> (a -> b -> c -> d -> e -> t) -> f (d -> c -> b -> a -> t)
f e
p <$$$$$> :: forall (f :: * -> *) e a b c d t.
Applicative f =>
f e -> (a -> b -> c -> d -> e -> t) -> f (d -> c -> b -> a -> t)
<$$$$$> a -> b -> c -> d -> e -> t
c = f e
p f e -> f (e -> d -> c -> b -> a -> t) -> f (d -> c -> b -> a -> t)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (e -> d -> c -> b -> a -> t) -> f (e -> d -> c -> b -> a -> t)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((a -> b -> c -> d -> e -> t) -> e -> d -> c -> b -> a -> t
forall a b c d e t.
(a -> b -> c -> d -> e -> t) -> e -> d -> c -> b -> a -> t
flip5 a -> b -> c -> d -> e -> t
c)

optionSuffix :: (a -> Parser a) -> a -> Parser a
optionSuffix :: forall a. (a -> Parser a) -> a -> Parser a
optionSuffix a -> Parser a
p a
a = a -> Parser a -> Parser a
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option a
a (a -> Parser a
p a
a)

{-
parses an optional postfix element and applies its result to its left
hand result, taken from uu-parsinglib

TODO: make sure the precedence higher than <|> and lower than the
other operators so it can be used nicely
-}

(<??>) :: Parser a -> Parser (a -> a) -> Parser a
Parser a
p <??> :: forall a. Parser a -> Parser (a -> a) -> Parser a
<??> Parser (a -> a)
q = Parser a
p Parser a -> Parser (a -> a) -> Parser a
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (a -> a) -> Parser (a -> a) -> Parser (a -> a)
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option a -> a
forall a. a -> a
id Parser (a -> a)
q

-- 0 to many repeated applications of suffix parser

(<??*>) :: Parser a -> Parser (a -> a) -> Parser a
Parser a
p <??*> :: forall a. Parser a -> Parser (a -> a) -> Parser a
<??*> Parser (a -> a)
q = ((a -> a) -> a -> a) -> a -> [a -> a] -> a
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (a -> a) -> a -> a
forall a b. (a -> b) -> a -> b
($) (a -> [a -> a] -> a)
-> Parser a
-> ParsecT Void SQLStream (Reader Dialect) ([a -> a] -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser a
p ParsecT Void SQLStream (Reader Dialect) ([a -> a] -> a)
-> ParsecT Void SQLStream (Reader Dialect) [a -> a] -> Parser a
forall a b.
ParsecT Void SQLStream (Reader Dialect) (a -> b)
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ([a -> a] -> [a -> a]
forall a. [a] -> [a]
reverse ([a -> a] -> [a -> a])
-> ParsecT Void SQLStream (Reader Dialect) [a -> a]
-> ParsecT Void SQLStream (Reader Dialect) [a -> a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser (a -> a) -> ParsecT Void SQLStream (Reader Dialect) [a -> a]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
many Parser (a -> a)
q)

{-
These are to help with left factored parsers:

a <**> (b <**> (c <**> pure (flip3 ctor)))

Not sure the names are correct, but they follow a pattern with flip
a <**> (b <**> pure (flip ctor))
-}

flip3 :: (a -> b -> c -> t) -> c -> b -> a -> t
flip3 :: forall a b c t. (a -> b -> c -> t) -> c -> b -> a -> t
flip3 a -> b -> c -> t
f c
a b
b a
c = a -> b -> c -> t
f a
c b
b c
a

flip4 :: (a -> b -> c -> d -> t) -> d -> c -> b -> a -> t
flip4 :: forall a b c d t. (a -> b -> c -> d -> t) -> d -> c -> b -> a -> t
flip4 a -> b -> c -> d -> t
f d
a c
b b
c a
d = a -> b -> c -> d -> t
f a
d b
c c
b d
a

flip5 :: (a -> b -> c -> d -> e -> t) -> e -> d -> c -> b -> a -> t
flip5 :: forall a b c d e t.
(a -> b -> c -> d -> e -> t) -> e -> d -> c -> b -> a -> t
flip5 a -> b -> c -> d -> e -> t
f e
a d
b c
c b
d a
e = a -> b -> c -> d -> e -> t
f a
e b
d c
c d
b e
a

--------------------------------------

unsignedInteger :: Parser Integer
unsignedInteger :: ParsecT Void SQLStream (Reader Dialect) Integer
unsignedInteger = String -> Integer
forall a. Read a => String -> a
read (String -> Integer) -> (Text -> String) -> Text -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack (Text -> Integer)
-> Parser Text -> ParsecT Void SQLStream (Reader Dialect) Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool -> Parser Text
sqlNumberTok Bool
True ParsecT Void SQLStream (Reader Dialect) Integer
-> String -> ParsecT Void SQLStream (Reader Dialect) Integer
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"natural number"

-- todo: work out the symbol parsing better

symbol :: Text -> Parser Text
symbol :: Text -> Parser Text
symbol Text
s = Maybe Text -> Parser Text
symbolTok (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
s) Parser Text -> String -> Parser Text
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> Text -> String
T.unpack Text
s

singleCharSymbol :: Char -> Parser Char
singleCharSymbol :: Char -> Parser Char
singleCharSymbol Char
c = Char
c Char -> Parser Text -> Parser Char
forall a b.
a
-> ParsecT Void SQLStream (Reader Dialect) b
-> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> Parser Text
symbol (Char -> Text
T.singleton Char
c)

questionMark :: Parser Char
questionMark :: Parser Char
questionMark = Char -> Parser Char
singleCharSymbol Char
'?' Parser Char -> String -> Parser Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"question mark"

openParen :: Parser Char
openParen :: Parser Char
openParen = Char -> Parser Char
singleCharSymbol Char
'('

closeParen :: Parser Char
closeParen :: Parser Char
closeParen = Char -> Parser Char
singleCharSymbol Char
')'

openBracket :: Parser Char
openBracket :: Parser Char
openBracket = Char -> Parser Char
singleCharSymbol Char
'['

closeBracket :: Parser Char
closeBracket :: Parser Char
closeBracket = Char -> Parser Char
singleCharSymbol Char
']'


comma :: Parser Char
comma :: Parser Char
comma = Char -> Parser Char
singleCharSymbol Char
',' Parser Char -> String -> Parser Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
""

semi :: Parser Char
semi :: Parser Char
semi = Char -> Parser Char
singleCharSymbol Char
';' Parser Char -> String -> Parser Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
""

-- = helper functions

keyword :: Text -> Parser Text
keyword :: Text -> Parser Text
keyword Text
k = [Text] -> Maybe Text -> Parser Text
unquotedIdentifierTok [] (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
k) Parser Text -> String -> Parser Text
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> Text -> String
T.unpack Text
k

-- helper function to improve error messages

keywords_ :: [Text] -> Parser ()
keywords_ :: [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text]
ks = (Text -> ParsecT Void SQLStream (Reader Dialect) ())
-> [Text] -> ParsecT Void SQLStream (Reader Dialect) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ [Text]
ks ParsecT Void SQLStream (Reader Dialect) ()
-> String -> ParsecT Void SQLStream (Reader Dialect) ()
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> Text -> String
T.unpack ([Text] -> Text
T.unwords [Text]
ks)


parens :: Parser a -> Parser a
parens :: forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens = Parser Char
-> Parser Char
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (m :: * -> *) open close a.
Applicative m =>
m open -> m close -> m a -> m a
between Parser Char
openParen Parser Char
closeParen

brackets :: Parser a -> Parser a
brackets :: forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
brackets = Parser Char
-> Parser Char
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall (m :: * -> *) open close a.
Applicative m =>
m open -> m close -> m a -> m a
between Parser Char
openBracket Parser Char
closeBracket

commaSep :: Parser a -> Parser [a]
commaSep :: forall a. Parser a -> Parser [a]
commaSep = (ParsecT Void SQLStream (Reader Dialect) a
-> Parser Char -> ParsecT Void SQLStream (Reader Dialect) [a]
forall (m :: * -> *) a sep. MonadPlus m => m a -> m sep -> m [a]
`sepBy` Parser Char
comma)

keyword_ :: Text -> Parser ()
keyword_ :: Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ = Parser Text -> ParsecT Void SQLStream (Reader Dialect) ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Parser Text -> ParsecT Void SQLStream (Reader Dialect) ())
-> (Text -> Parser Text)
-> Text
-> ParsecT Void SQLStream (Reader Dialect) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Parser Text
keyword

symbol_ :: Text -> Parser ()
symbol_ :: Text -> ParsecT Void SQLStream (Reader Dialect) ()
symbol_ = Parser Text -> ParsecT Void SQLStream (Reader Dialect) ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Parser Text -> ParsecT Void SQLStream (Reader Dialect) ())
-> (Text -> Parser Text)
-> Text
-> ParsecT Void SQLStream (Reader Dialect) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Parser Text
symbol

commaSep1 :: Parser a -> Parser [a]
commaSep1 :: forall a. Parser a -> Parser [a]
commaSep1 = (ParsecT Void SQLStream (Reader Dialect) a
-> Parser Char -> ParsecT Void SQLStream (Reader Dialect) [a]
forall (m :: * -> *) a sep. MonadPlus m => m a -> m sep -> m [a]
`sepBy1` Parser Char
comma)

------------------------------------------------------------------------------

-- interfacing with the lexing
{-
TODO: push checks into here:
keyword blacklists
unsigned integer match
symbol matching
keyword matching

-}
stringTok :: Parser (Text,Text,Text)
stringTok :: ParsecT Void SQLStream (Reader Dialect) (Text, Text, Text)
stringTok = (Token SQLStream -> Maybe (Text, Text, Text))
-> Set (ErrorItem (Token SQLStream))
-> ParsecT Void SQLStream (Reader Dialect) (Text, Text, Text)
forall a.
(Token SQLStream -> Maybe a)
-> Set (ErrorItem (Token SQLStream))
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a.
MonadParsec e s m =>
(Token s -> Maybe a) -> Set (ErrorItem (Token s)) -> m a
token Token SQLStream -> Maybe (Text, Text, Text)
WithPos Token -> Maybe (Text, Text, Text)
test Set (ErrorItem (Token SQLStream))
Set (ErrorItem (WithPos Token))
forall a. Set a
Set.empty ParsecT Void SQLStream (Reader Dialect) (Text, Text, Text)
-> String
-> ParsecT Void SQLStream (Reader Dialect) (Text, Text, Text)
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"string literal"
  where
    test :: WithPos Token -> Maybe (Text, Text, Text)
test (L.WithPos SourcePos
_ SourcePos
_ Int
_ (L.SqlString Text
s Text
e Text
t)) = (Text, Text, Text) -> Maybe (Text, Text, Text)
forall a. a -> Maybe a
Just (Text
s,Text
e,Text
t)
    test WithPos Token
_ = Maybe (Text, Text, Text)
forall a. Maybe a
Nothing

singleQuotesOnlyStringTok :: Parser Text
singleQuotesOnlyStringTok :: Parser Text
singleQuotesOnlyStringTok = (Token SQLStream -> Maybe Text)
-> Set (ErrorItem (Token SQLStream)) -> Parser Text
forall a.
(Token SQLStream -> Maybe a)
-> Set (ErrorItem (Token SQLStream))
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a.
MonadParsec e s m =>
(Token s -> Maybe a) -> Set (ErrorItem (Token s)) -> m a
token Token SQLStream -> Maybe Text
WithPos Token -> Maybe Text
test Set (ErrorItem (Token SQLStream))
Set (ErrorItem (WithPos Token))
forall a. Set a
Set.empty Parser Text -> String -> Parser Text
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"string literal"
  where
    test :: WithPos Token -> Maybe Text
test (L.WithPos SourcePos
_ SourcePos
_ Int
_ (L.SqlString Text
"'" Text
"'" Text
t)) = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
t
    test WithPos Token
_ = Maybe Text
forall a. Maybe a
Nothing

{-
This is to support SQL strings where you can write
'part of a string' ' another part'
and it will parse as a single string

It is only allowed when all the strings are quoted with ' atm.
-}

stringTokExtend :: Parser (Text,Text,Text)
stringTokExtend :: ParsecT Void SQLStream (Reader Dialect) (Text, Text, Text)
stringTokExtend = do
    (Text
s,Text
e,Text
x) <- ParsecT Void SQLStream (Reader Dialect) (Text, Text, Text)
stringTok
    [ParsecT Void SQLStream (Reader Dialect) (Text, Text, Text)]
-> ParsecT Void SQLStream (Reader Dialect) (Text, Text, Text)
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [
         do
         Bool -> ParsecT Void SQLStream (Reader Dialect) ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Text
s Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"'" Bool -> Bool -> Bool
&& Text
e Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"'")
         (Text
s',Text
e',Text
y) <- ParsecT Void SQLStream (Reader Dialect) (Text, Text, Text)
stringTokExtend
         Bool -> ParsecT Void SQLStream (Reader Dialect) ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Text
s' Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"'" Bool -> Bool -> Bool
&& Text
e' Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"'")
         (Text, Text, Text)
-> ParsecT Void SQLStream (Reader Dialect) (Text, Text, Text)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text
s,Text
e,Text
x Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
y)
        ,(Text, Text, Text)
-> ParsecT Void SQLStream (Reader Dialect) (Text, Text, Text)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text
s,Text
e,Text
x)
        ]

hostParamTok :: Parser Text
hostParamTok :: Parser Text
hostParamTok = (Token SQLStream -> Maybe Text)
-> Set (ErrorItem (Token SQLStream)) -> Parser Text
forall a.
(Token SQLStream -> Maybe a)
-> Set (ErrorItem (Token SQLStream))
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a.
MonadParsec e s m =>
(Token s -> Maybe a) -> Set (ErrorItem (Token s)) -> m a
token Token SQLStream -> Maybe Text
WithPos Token -> Maybe Text
test Set (ErrorItem (Token SQLStream))
Set (ErrorItem (WithPos Token))
forall a. Set a
Set.empty Parser Text -> String -> Parser Text
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
""
  where
    test :: WithPos Token -> Maybe Text
test (L.WithPos SourcePos
_ SourcePos
_ Int
_ (L.PrefixedVariable Char
c Text
p)) = Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Maybe Text) -> Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ Char -> Text -> Text
T.cons Char
c Text
p
    test WithPos Token
_ = Maybe Text
forall a. Maybe a
Nothing

positionalArgTok :: Parser Int
positionalArgTok :: ParsecT Void SQLStream (Reader Dialect) Int
positionalArgTok = (Token SQLStream -> Maybe Int)
-> Set (ErrorItem (Token SQLStream))
-> ParsecT Void SQLStream (Reader Dialect) Int
forall a.
(Token SQLStream -> Maybe a)
-> Set (ErrorItem (Token SQLStream))
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a.
MonadParsec e s m =>
(Token s -> Maybe a) -> Set (ErrorItem (Token s)) -> m a
token Token SQLStream -> Maybe Int
WithPos Token -> Maybe Int
test Set (ErrorItem (Token SQLStream))
Set (ErrorItem (WithPos Token))
forall a. Set a
Set.empty ParsecT Void SQLStream (Reader Dialect) Int
-> String -> ParsecT Void SQLStream (Reader Dialect) Int
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
""
  where
    test :: WithPos Token -> Maybe Int
test (L.WithPos SourcePos
_ SourcePos
_ Int
_ (L.PositionalArg Int
p)) = Int -> Maybe Int
forall a. a -> Maybe a
Just Int
p
    test WithPos Token
_ = Maybe Int
forall a. Maybe a
Nothing

sqlNumberTok :: Bool -> Parser Text
sqlNumberTok :: Bool -> Parser Text
sqlNumberTok Bool
intOnly = (Token SQLStream -> Maybe Text)
-> Set (ErrorItem (Token SQLStream)) -> Parser Text
forall a.
(Token SQLStream -> Maybe a)
-> Set (ErrorItem (Token SQLStream))
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a.
MonadParsec e s m =>
(Token s -> Maybe a) -> Set (ErrorItem (Token s)) -> m a
token Token SQLStream -> Maybe Text
WithPos Token -> Maybe Text
test Set (ErrorItem (Token SQLStream))
Set (ErrorItem (WithPos Token))
forall a. Set a
Set.empty Parser Text -> String -> Parser Text
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
""
  where
    test :: WithPos Token -> Maybe Text
test (L.WithPos SourcePos
_ SourcePos
_ Int
_ (L.SqlNumber Text
p)) | Bool -> Bool
not Bool
intOnly Bool -> Bool -> Bool
|| (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isDigit Text
p = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
p
    test WithPos Token
_ = Maybe Text
forall a. Maybe a
Nothing

symbolTok :: Maybe Text -> Parser Text
symbolTok :: Maybe Text -> Parser Text
symbolTok Maybe Text
sym = (Token SQLStream -> Maybe Text)
-> Set (ErrorItem (Token SQLStream)) -> Parser Text
forall a.
(Token SQLStream -> Maybe a)
-> Set (ErrorItem (Token SQLStream))
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a.
MonadParsec e s m =>
(Token s -> Maybe a) -> Set (ErrorItem (Token s)) -> m a
token Token SQLStream -> Maybe Text
WithPos Token -> Maybe Text
test Set (ErrorItem (Token SQLStream))
Set (ErrorItem (WithPos Token))
forall a. Set a
Set.empty Parser Text -> String -> Parser Text
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
""
  where
    test :: WithPos Token -> Maybe Text
test (L.WithPos SourcePos
_ SourcePos
_ Int
_ (L.Symbol Text
p)) =
        case Maybe Text
sym of
            Maybe Text
Nothing -> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
p
            Just Text
sym' | Text
sym' Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
p -> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
p
            Maybe Text
_ -> Maybe Text
forall a. Maybe a
Nothing
    test WithPos Token
_ = Maybe Text
forall a. Maybe a
Nothing

{-
The blacklisted names are mostly needed when we parse something with
an optional alias, e.g. select a a from t. If we write select a from
t, we have to make sure the from isn't parsed as an alias. I'm not
sure what other places strictly need the blacklist, and in theory it
could be tuned differently for each place the identifierString/
identifier parsers are used to only blacklist the bare
minimum. Something like this might be needed for dialect support, even
if it is pretty silly to use a keyword as an unquoted identifier when
there is a quoting syntax as well.

The standard has a weird mix of reserved keywords and unreserved
keywords (I'm not sure what exactly being an unreserved keyword
means).

The current approach tries to have everything which is a keyword only
in the keyword list - so it can only be used in some other context if
quoted. If something is a 'ansi keyword', but appears only as an
identifier or function name for instance in the syntax (or something
that looks identical to this), then it isn't treated as a keyword at
all. When there is some overlap (e.g. 'set'), then there is either
special case parsing code to handle this (in the case of set), or it
is not treated as a keyword (not perfect, but if it more or less
works, ok for now).

An exception to this is the standard type names are considered as
keywords at the moment, with a special case in the type parser to
make this work. Maybe this isn't necessary or is a bad idea.

It is possible to have a problem if you remove something which is a
keyword from this list, and still want to parse statements using it
as a keyword - for instance, removing things like 'from' or 'as',
will likely mean many things don't parse anymore.

-}

identifierTok :: [Text] -> Parser (Maybe (Text,Text), Text)
identifierTok :: [Text]
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe (Text, Text), Text)
identifierTok [Text]
blackList = (Token SQLStream -> Maybe (Maybe (Text, Text), Text))
-> Set (ErrorItem (Token SQLStream))
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe (Text, Text), Text)
forall a.
(Token SQLStream -> Maybe a)
-> Set (ErrorItem (Token SQLStream))
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a.
MonadParsec e s m =>
(Token s -> Maybe a) -> Set (ErrorItem (Token s)) -> m a
token Token SQLStream -> Maybe (Maybe (Text, Text), Text)
WithPos Token -> Maybe (Maybe (Text, Text), Text)
test Set (ErrorItem (Token SQLStream))
Set (ErrorItem (WithPos Token))
forall a. Set a
Set.empty ParsecT Void SQLStream (Reader Dialect) (Maybe (Text, Text), Text)
-> String
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe (Text, Text), Text)
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
""
  where
    test :: WithPos Token -> Maybe (Maybe (Text, Text), Text)
test (L.WithPos SourcePos
_ SourcePos
_ Int
_ (L.Identifier q :: Maybe (Text, Text)
q@(Just {}) Text
p)) = (Maybe (Text, Text), Text) -> Maybe (Maybe (Text, Text), Text)
forall a. a -> Maybe a
Just (Maybe (Text, Text)
q,Text
p)
    test (L.WithPos SourcePos
_ SourcePos
_ Int
_ (L.Identifier Maybe (Text, Text)
q Text
p))
        | Text -> Text
T.toLower Text
p Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Text]
blackList = (Maybe (Text, Text), Text) -> Maybe (Maybe (Text, Text), Text)
forall a. a -> Maybe a
Just (Maybe (Text, Text)
q,Text
p)
    test WithPos Token
_ = Maybe (Maybe (Text, Text), Text)
forall a. Maybe a
Nothing

unquotedIdentifierTok :: [Text] -> Maybe Text -> Parser Text
unquotedIdentifierTok :: [Text] -> Maybe Text -> Parser Text
unquotedIdentifierTok [Text]
blackList Maybe Text
kw = (Token SQLStream -> Maybe Text)
-> Set (ErrorItem (Token SQLStream)) -> Parser Text
forall a.
(Token SQLStream -> Maybe a)
-> Set (ErrorItem (Token SQLStream))
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a.
MonadParsec e s m =>
(Token s -> Maybe a) -> Set (ErrorItem (Token s)) -> m a
token Token SQLStream -> Maybe Text
WithPos Token -> Maybe Text
test Set (ErrorItem (Token SQLStream))
Set (ErrorItem (WithPos Token))
forall a. Set a
Set.empty Parser Text -> String -> Parser Text
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
""
  where
    test :: WithPos Token -> Maybe Text
test (L.WithPos SourcePos
_ SourcePos
_ Int
_ (L.Identifier Maybe (Text, Text)
Nothing Text
p)) =
        case Maybe Text
kw of
            Maybe Text
Nothing | Text -> Text
T.toLower Text
p Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Text]
blackList -> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
p
            Just Text
k | Text
k Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Text
T.toLower Text
p -> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
p
            Maybe Text
_ -> Maybe Text
forall a. Maybe a
Nothing
    test WithPos Token
_ = Maybe Text
forall a. Maybe a
Nothing

------------------------------------------------------------------------------

-- dialect

guardDialect :: (Dialect -> Bool) -> Parser ()
guardDialect :: (Dialect -> Bool) -> ParsecT Void SQLStream (Reader Dialect) ()
guardDialect Dialect -> Bool
p = Bool -> ParsecT Void SQLStream (Reader Dialect) ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> ParsecT Void SQLStream (Reader Dialect) ())
-> (Dialect -> Bool)
-> Dialect
-> ParsecT Void SQLStream (Reader Dialect) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dialect -> Bool
p (Dialect -> ParsecT Void SQLStream (Reader Dialect) ())
-> Parser Dialect -> ParsecT Void SQLStream (Reader Dialect) ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Parser Dialect
forall r (m :: * -> *). MonadReader r m => m r
ask

askDialect :: (Dialect -> a) -> Parser a
askDialect :: forall a. (Dialect -> a) -> Parser a
askDialect = (Dialect -> a) -> ParsecT Void SQLStream (Reader Dialect) a
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks