{-
= 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
    ,hidden
    ,failure
    ,ErrorItem(..)

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

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

import qualified Data.Set           as Set
import qualified Data.List.NonEmpty as NE
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, fromMaybe)
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
--import Text.Megaparsec.Debug (dbg)
import Text.Read (readMaybe)

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

-- = 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
    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
-> Bool
-> Text
-> Maybe (Int, Int)
-> Text
-> Either ParseError [WithPos Token]
L.lexSQLWithPositions Dialect
d Bool
True Text
f Maybe (Int, Int)
p Text
src
    either (Left . ParseError) Right $
        runReader (runParserT (parser <* (hidden eof)) (T.unpack f)
                   $ L.SQLStream (T.unpack src) $ filter notSpace lx) 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 :: Text -> Parser Name
name :: Text -> Parser Name
name Text
lbl = Text -> Parser Name -> Parser Name
forall a. Text -> Parser a -> Parser a
label Text
lbl (Parser Name -> Parser Name) -> Parser Name -> Parser Name
forall a b. (a -> b) -> a -> b
$ do
    bl <- (Dialect -> [Text]) -> Parser [Text]
forall a. (Dialect -> a) -> Parser a
askDialect Dialect -> [Text]
diKeywords
    uncurry Name <$> identifierTok bl

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

names :: Text -> Parser [Name]
names :: Text -> Parser [Name]
names Text
lbl =
    Text -> Parser [Name] -> Parser [Name]
forall a. Text -> Parser a -> Parser a
label Text
lbl ([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
<$> Text -> Parser Name
name Text
lbl) Parser [Name] -> Parser ([Name] -> [Name]) -> Parser [Name]
forall a. Parser a -> Parser (a -> a) -> Parser a
`chainrSuffix` 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
<$> (Parser Name -> Parser 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
hidden (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
*> Text -> Parser Name
name Text
lbl)))

{-
= 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.

the hideArg is used when the typename is used as part of a typed
literal expression, to hide what comes after the paren in
'typename('. This is so 'arbitrary_fn(' gives an 'expecting expression',
instead of 'expecting expression or number', which is odd.

-}

typeName :: Parser TypeName
typeName :: Parser TypeName
typeName = Bool -> Parser TypeName
typeName' Bool
False

typeName' :: Bool -> Parser TypeName
typeName' :: Bool -> Parser TypeName
typeName' Bool
hideArg =
    Text -> Parser TypeName -> Parser TypeName
forall a. Text -> Parser a -> Parser a
label Text
"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
`chainrSuffix` 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
<$> (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
hidden (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
<$> Text -> Parser Name
name Text
"type 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 =
        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
hidden (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)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Integer -> [Name] -> TypeName)
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
hidden 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)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Integer -> [Name] -> TypeName)
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
hidden 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
<|> Text -> Parser [Name]
names Text
"type name"
    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]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([Name] -> [Name] -> Maybe Integer -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([Name] -> Maybe Integer -> [Name] -> TypeName)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> ([Name] -> [Name] -> Maybe Integer -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([Name] -> [Name] -> Maybe Integer -> [Name] -> TypeName)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (([Name] -> Maybe Integer -> [Name] -> [Name] -> TypeName)
-> [Name] -> [Name] -> Maybe Integer -> [Name] -> TypeName
forall a b c d t. (a -> b -> c -> d -> t) -> d -> c -> b -> a -> t
flip4 [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]
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([Name] -> [Name] -> Maybe Integer -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([Name] -> Maybe Integer -> [Name] -> TypeName)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> ([Name] -> [Name] -> Maybe Integer -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([Name] -> [Name] -> Maybe Integer -> [Name] -> TypeName)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (([Name] -> Maybe Integer -> [Name] -> [Name] -> TypeName)
-> [Name] -> [Name] -> Maybe Integer -> [Name] -> TypeName
forall a b c d t. (a -> b -> c -> d -> t) -> d -> c -> b -> a -> t
flip4 [Name] -> Maybe Integer -> [Name] -> [Name] -> TypeName
CharTypeName))
    typeNameWithParens :: ParsecT Void SQLStream (Reader Dialect) ([Name] -> TypeName)
typeNameWithParens =
        (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
hidden ParsecT Void SQLStream (Reader Dialect) ()
openParen 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
*> (if Bool
hideArg then 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
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
hidden ParsecT Void SQLStream (Reader Dialect) Integer
unsignedInteger else 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
<**> (ParsecT Void SQLStream (Reader Dialect) ()
closeParen ParsecT Void SQLStream (Reader Dialect) ()
-> 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)
-> ParsecT
     Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
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
hidden 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)
-> ParsecT
     Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
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
hidden (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)
-> ParsecT Void SQLStream (Reader Dialect) ()
-> 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
<* ParsecT Void SQLStream (Reader Dialect) ()
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 =
        (ParsecT Void SQLStream (Reader Dialect) Char
-> ParsecT Void SQLStream (Reader Dialect) Char
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
hidden ParsecT Void SQLStream (Reader Dialect) Char
comma ParsecT Void SQLStream (Reader Dialect) 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
*> (if Bool
hideArg then 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
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
hidden ParsecT Void SQLStream (Reader Dialect) Integer
unsignedInteger else ParsecT Void SQLStream (Reader Dialect) Integer
unsignedInteger))
        ParsecT Void SQLStream (Reader Dialect) Integer
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Integer -> Integer -> [Name] -> TypeName)
-> ParsecT
     Void SQLStream (Reader Dialect) (Integer -> [Name] -> TypeName)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (Integer -> Integer -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Integer -> Integer -> [Name] -> TypeName)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (([Name] -> Integer -> Integer -> TypeName)
-> Integer -> Integer -> [Name] -> TypeName
forall a b c t. (a -> b -> c -> t) -> c -> b -> a -> t
flip3 [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)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe PrecUnits
      -> Maybe PrecMultiplier -> Integer -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe PrecMultiplier -> Integer -> [Name] -> TypeName)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (Maybe PrecUnits
 -> Maybe PrecMultiplier -> Integer -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe PrecUnits
      -> Maybe PrecMultiplier -> Integer -> [Name] -> TypeName)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (([Name]
 -> Integer -> Maybe PrecMultiplier -> Maybe PrecUnits -> TypeName)
-> Maybe PrecUnits
-> Maybe PrecMultiplier
-> Integer
-> [Name]
-> TypeName
forall a b c d t. (a -> b -> c -> d -> t) -> d -> c -> b -> a -> t
flip4 [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)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe PrecUnits
      -> Maybe PrecMultiplier -> Integer -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe PrecMultiplier -> Integer -> [Name] -> TypeName)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (Maybe PrecUnits
 -> Maybe PrecMultiplier -> Integer -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe PrecUnits
      -> Maybe PrecMultiplier -> Integer -> [Name] -> TypeName)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (([Name]
 -> Integer -> Maybe PrecMultiplier -> Maybe PrecUnits -> TypeName)
-> Maybe PrecUnits
-> Maybe PrecMultiplier
-> Integer
-> [Name]
-> TypeName
forall a b c d t. (a -> b -> c -> d -> t) -> d -> c -> b -> a -> t
flip4 [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
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Bool -> 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
<**> (Bool -> Maybe Integer -> [Name] -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Bool -> Maybe Integer -> [Name] -> TypeName)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (([Name] -> Maybe Integer -> Bool -> TypeName)
-> Bool -> Maybe Integer -> [Name] -> TypeName
forall a b c t. (a -> b -> c -> t) -> c -> b -> a -> t
flip3 [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
*> Text -> Parser [Name]
names Text
"character set name"
    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
*> Text -> Parser [Name]
names Text
"collation name"
    ----------------------------
    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)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Integer -> TypeName -> TypeName)
-> Parser (TypeName -> TypeName)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (Maybe Integer -> TypeName -> TypeName)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Integer -> TypeName -> TypeName)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((TypeName -> Maybe Integer -> TypeName)
-> Maybe Integer -> TypeName -> TypeName
forall a b c. (a -> b -> c) -> b -> a -> c
flip 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
        stn <- (Dialect -> [Text]) -> Parser [Text]
forall a. (Dialect -> a) -> Parser a
askDialect Dialect -> [Text]
diSpecialTypeNames
        (:[]) . Name Nothing . T.unwords <$> makeKeywordTree 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 makes an attempt to not parse star in
most contexts, to provide better experience when the user makes a mistake
in an expression containing * meaning multiple. It will parse a *
at the top level of a select item, or in arg in a app argument list.
-}

star :: Parser ScalarExpr
star :: Parser ScalarExpr
star =
    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
hidden (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
    [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
"*"
    -- much easier to use try here than to left factor where
    -- this is allowed and not allowed
    ,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 ([Name] -> ScalarExpr
QStar ([Name] -> ScalarExpr) -> Parser [Name] -> Parser ScalarExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Text -> Parser [Name]
names Text
"qualified star" Parser [Name] -> Parser Text -> Parser [Name]
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 [Name] -> Parser Text -> Parser [Name]
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
"*"))]

{-
== 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
-> ParsecT Void SQLStream (Reader Dialect) 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
<$ ParsecT Void SQLStream (Reader Dialect) 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 a. Parser a -> Parser (Maybe a)
hoptional (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
    -- no parens here used for nested parens expressions
    -- this could be fixed to be general with some refactoring, but at
    -- the moment, you can't use additional redundant parens in a
    -- subqueryexpr
    [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
queryExprNoParens
    ,[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
<*> (ParsecT Void SQLStream (Reader Dialect) Char
comma ParsecT Void SQLStream (Reader Dialect) 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 (ParsecT Void SQLStream (Reader Dialect) Char
comma ParsecT Void SQLStream (Reader Dialect) 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
<$> Text -> Parser [Name]
names Text
"sequence generator name"

{-
=== 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 =
    Text -> Parser ScalarExpr -> Parser ScalarExpr
forall a. Text -> Parser a -> Parser a
label Text
"interval literal" (Parser ScalarExpr -> Parser ScalarExpr)
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b. (a -> b) -> a -> b
$ 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
    s <- Parser Sign -> Parser (Maybe Sign)
forall a. Parser a -> Parser (Maybe a)
hoptional (Parser Sign -> Parser (Maybe Sign))
-> Parser Sign -> Parser (Maybe Sign)
forall a b. (a -> b) -> a -> b
$ [Parser Sign] -> Parser Sign
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [Sign
Plus Sign -> ParsecT Void SQLStream (Reader Dialect) () -> Parser 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) () -> Parser 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
"-"]
    lit <- singleQuotesOnlyStringTok
    q <- hoptional intervalQualifier
    mkIt s lit 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: try reversing these
    -- then if it parses as a typename as part of a typed literal
    -- and not a regularapplike, then you'll get a better error message
    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
typedLiteral 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
regularAppLike
  where
    -- parse regular iden or app
    -- if it could potentially be a typed literal typename 'literaltext'
    -- optionally try to parse that
    regularAppLike :: Parser ScalarExpr
regularAppLike = do
        e <- (Parser ScalarExpr
keywordFunctionOrIden
              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
<|> (Text -> Parser [Name]
names Text
"identifier" 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)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> 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
hidden ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
app ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ([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
<|> ([Name] -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Name] -> ScalarExpr
Iden)))
        let getInt Text
s = String -> Maybe a
forall a. Read a => String -> Maybe a
readMaybe (Text -> String
T.unpack Text
s)
        case e of
            Iden [Name]
nm -> TypeName -> Parser ScalarExpr
tryTypedLiteral ([Name] -> TypeName
TypeName [Name]
nm) 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
<|> ScalarExpr -> Parser ScalarExpr
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ScalarExpr
e
            App [Name]
nm [NumLit Text
prec]
                | Just Integer
prec' <- Text -> Maybe Integer
forall {a}. Read a => Text -> Maybe a
getInt Text
prec ->
                TypeName -> Parser ScalarExpr
tryTypedLiteral ([Name] -> Integer -> TypeName
PrecTypeName [Name]
nm Integer
prec') 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
<|> ScalarExpr -> Parser ScalarExpr
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ScalarExpr
e
            App [Name]
nm [NumLit Text
prec,NumLit Text
scale]
                | Just Integer
prec' <- Text -> Maybe Integer
forall {a}. Read a => Text -> Maybe a
getInt Text
prec
                , Just Integer
scale' <- Text -> Maybe Integer
forall {a}. Read a => Text -> Maybe a
getInt Text
scale ->
                TypeName -> Parser ScalarExpr
tryTypedLiteral ([Name] -> Integer -> Integer -> TypeName
PrecScaleTypeName [Name]
nm Integer
prec' Integer
scale') 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
<|> ScalarExpr -> Parser ScalarExpr
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ScalarExpr
e
            ScalarExpr
_ -> ScalarExpr -> Parser ScalarExpr
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ScalarExpr
e
    tryTypedLiteral :: TypeName -> Parser ScalarExpr
tryTypedLiteral TypeName
tn =
        TypeName -> Text -> ScalarExpr
TypedLit TypeName
tn (Text -> ScalarExpr) -> Parser Text -> Parser ScalarExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Text -> Parser Text
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
hidden Parser Text
singleQuotesOnlyStringTok
    typedLiteral :: Parser ScalarExpr
typedLiteral =
        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 -> Parser TypeName
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
hidden (Bool -> Parser TypeName
typeName' Bool
True) 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
    keywordFunctionOrIden :: Parser ScalarExpr
keywordFunctionOrIden = do
        d <- (Dialect -> Dialect) -> Parser Dialect
forall a. (Dialect -> a) -> Parser a
askDialect Dialect -> Dialect
forall a. a -> a
id
        x <- hidden (keywordTok (diIdentifierKeywords d ++ diAppKeywords d))
        let 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 = 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
<**> (ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> 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
hidden ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
app ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ([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
<|> ([Name] -> ScalarExpr)
-> ParsecT Void SQLStream (Reader Dialect) ([Name] -> ScalarExpr)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Name] -> ScalarExpr
Iden)
              | 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 -> -- shouldn't get here
                    String -> Parser ScalarExpr
forall a. String -> ParsecT Void SQLStream (Reader Dialect) a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser ScalarExpr) -> String -> Parser ScalarExpr
forall a b. (a -> b) -> a -> b
$ String
"unexpected keyword: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack Text
x

{-
=== 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
    ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void ParsecT Void SQLStream (Reader Dialect) ()
openParen
    let pfa :: Parser ScalarExpr
pfa = do
              e <- Parser ScalarExpr
scalarExpr
              -- check we haven't parsed the first
              -- keyword as an identifier
              case (e,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
"unexpected " 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 ()
              pure e
    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
    as <- mapM parseArg kws
    void closeParen
    pure $ SpecialOpK [Name Nothing opName] fa $ catMaybes 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 =
    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
hidden ParsecT Void SQLStream (Reader Dialect) ()
openParen ParsecT Void SQLStream (Reader Dialect) ()
-> 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
    [ParsecT Void SQLStream (Reader Dialect) SetQuantifier
-> ParsecT Void SQLStream (Reader Dialect) SetQuantifier
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
hidden ParsecT Void SQLStream (Reader Dialect) SetQuantifier
duplicates
     ParsecT Void SQLStream (Reader Dialect) 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
scalarExprOrStar
           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] -> Parser [SortSpec] -> Parser [SortSpec]
forall a. a -> Parser a -> Parser a
hoption [] Parser [SortSpec]
orderBy Parser [SortSpec]
-> ParsecT Void SQLStream (Reader Dialect) () -> Parser [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
<* ParsecT Void SQLStream (Reader Dialect) ()
closeParen)
                 Parser [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 a. Parser a -> Parser (Maybe a)
hoptional Parser ScalarExpr
afilter ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe ScalarExpr
      -> [SortSpec]
      -> [ScalarExpr]
      -> SetQuantifier
      -> [Name]
      -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([SortSpec]
      -> [ScalarExpr] -> SetQuantifier -> [Name] -> ScalarExpr)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (Maybe ScalarExpr
 -> [SortSpec]
 -> [ScalarExpr]
 -> SetQuantifier
 -> [Name]
 -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe ScalarExpr
      -> [SortSpec]
      -> [ScalarExpr]
      -> SetQuantifier
      -> [Name]
      -> ScalarExpr)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (([Name]
 -> SetQuantifier
 -> [ScalarExpr]
 -> [SortSpec]
 -> Maybe ScalarExpr
 -> ScalarExpr)
-> Maybe ScalarExpr
-> [SortSpec]
-> [ScalarExpr]
-> SetQuantifier
-> [Name]
-> ScalarExpr
forall a b c d e t.
(a -> b -> c -> d -> e -> t) -> e -> d -> c -> b -> a -> t
flip5 [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
scalarExprOrStar
     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
          [ParsecT Void SQLStream (Reader Dialect) ()
closeParen ParsecT Void SQLStream (Reader Dialect) ()
-> 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 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
hidden ([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)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe 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
<**> (Maybe ScalarExpr -> [ScalarExpr] -> [Name] -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe ScalarExpr -> [ScalarExpr] -> [Name] -> ScalarExpr)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (([Name] -> [ScalarExpr] -> Maybe ScalarExpr -> ScalarExpr)
-> Maybe ScalarExpr -> [ScalarExpr] -> [Name] -> ScalarExpr
forall a b c t. (a -> b -> c -> t) -> c -> b -> a -> t
flip3 [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)])
          ,Parser [SortSpec] -> Parser [SortSpec]
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
hidden Parser [SortSpec]
orderBy Parser [SortSpec]
-> ParsecT Void SQLStream (Reader Dialect) () -> Parser [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
<* ParsecT Void SQLStream (Reader Dialect) ()
closeParen
           Parser [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 a. Parser a -> Parser (Maybe a)
hoptional Parser ScalarExpr
afilter ParsecT Void SQLStream (Reader Dialect) (Maybe ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe ScalarExpr
      -> [SortSpec] -> [ScalarExpr] -> [Name] -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([SortSpec] -> [ScalarExpr] -> [Name] -> ScalarExpr)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (Maybe ScalarExpr
 -> [SortSpec] -> [ScalarExpr] -> [Name] -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe ScalarExpr
      -> [SortSpec] -> [ScalarExpr] -> [Name] -> ScalarExpr)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (([Name]
 -> [ScalarExpr] -> [SortSpec] -> Maybe ScalarExpr -> ScalarExpr)
-> Maybe ScalarExpr
-> [SortSpec]
-> [ScalarExpr]
-> [Name]
-> ScalarExpr
forall a b c d t. (a -> b -> c -> d -> t) -> d -> c -> b -> a -> t
flip4 [Name]
-> [ScalarExpr] -> [SortSpec] -> Maybe ScalarExpr -> ScalarExpr
aggAppWithoutDupe))]
     -- no scalarExprs: duplicates and order by not allowed
    ,([] [ScalarExpr]
-> ParsecT Void SQLStream (Reader Dialect) ()
-> 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
<$ ParsecT Void SQLStream (Reader Dialect) ()
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
<**> [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] -> [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 (([ScalarExpr] -> [Name] -> ScalarExpr)
 -> ParsecT
      Void
      SQLStream
      (Reader Dialect)
      ([ScalarExpr] -> [Name] -> ScalarExpr))
-> ([ScalarExpr] -> [Name] -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([ScalarExpr] -> [Name] -> ScalarExpr)
forall a b. (a -> b) -> a -> b
$ ([Name] -> [ScalarExpr] -> ScalarExpr)
-> [ScalarExpr] -> [Name] -> ScalarExpr
forall a b c. (a -> b -> c) -> b -> a -> c
flip [Name] -> [ScalarExpr] -> ScalarExpr
App]
    ]
  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) ()
-> Parser [SortSpec] -> Parser [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] -> Parser [SortSpec]
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens Parser [SortSpec]
orderBy) Parser [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
<**> ([SortSpec] -> [ScalarExpr] -> [Name] -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([SortSpec] -> [ScalarExpr] -> [Name] -> ScalarExpr)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (([Name] -> [ScalarExpr] -> [SortSpec] -> ScalarExpr)
-> [SortSpec] -> [ScalarExpr] -> [Name] -> ScalarExpr
forall a b c t. (a -> b -> c -> t) -> c -> b -> a -> t
flip3 [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) ()
-> 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
*> ParsecT Void SQLStream (Reader Dialect) ()
openParen 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
*> [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] -> Parser [SortSpec] -> Parser [SortSpec]
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option [] Parser [SortSpec]
orderBy
        Parser [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)
-> ParsecT Void SQLStream (Reader Dialect) ()
-> 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
<* ParsecT Void SQLStream (Reader Dialect) ()
closeParen) ParsecT Void SQLStream (Reader Dialect) (Maybe Frame)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Frame
      -> [SortSpec]
      -> [ScalarExpr]
      -> [ScalarExpr]
      -> [Name]
      -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     ([SortSpec]
      -> [ScalarExpr] -> [ScalarExpr] -> [Name] -> ScalarExpr)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (Maybe Frame
 -> [SortSpec]
 -> [ScalarExpr]
 -> [ScalarExpr]
 -> [Name]
 -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe Frame
      -> [SortSpec]
      -> [ScalarExpr]
      -> [ScalarExpr]
      -> [Name]
      -> ScalarExpr)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (([Name]
 -> [ScalarExpr]
 -> [ScalarExpr]
 -> [SortSpec]
 -> Maybe Frame
 -> ScalarExpr)
-> Maybe Frame
-> [SortSpec]
-> [ScalarExpr]
-> [ScalarExpr]
-> [Name]
-> ScalarExpr
forall a b c d e t.
(a -> b -> c -> d -> e -> t) -> e -> d -> c -> b -> a -> t
flip5 [Name]
-> [ScalarExpr]
-> [ScalarExpr]
-> [SortSpec]
-> Maybe Frame
-> ScalarExpr
WindowApp)))
  where
    partitionBy :: ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
partitionBy =
        Text
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a. Text -> Parser a -> Parser a
label Text
"partition by" (ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
 -> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr])
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
-> ParsecT Void SQLStream (Reader Dialect) [ScalarExpr]
forall a b. (a -> b) -> a -> b
$
        [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 =
        Text
-> ParsecT Void SQLStream (Reader Dialect) Frame
-> ParsecT Void SQLStream (Reader Dialect) Frame
forall a. Text -> Parser a -> Parser a
label Text
"frame clause" (ParsecT Void SQLStream (Reader Dialect) Frame
 -> ParsecT Void SQLStream (Reader Dialect) Frame)
-> ParsecT Void SQLStream (Reader Dialect) Frame
-> ParsecT Void SQLStream (Reader Dialect) Frame
forall a b. (a -> b) -> a -> b
$
        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
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (FramePos -> FramePos -> FrameRows -> Frame)
-> ParsecT
     Void SQLStream (Reader Dialect) (FramePos -> FrameRows -> Frame)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (FramePos -> FramePos -> FrameRows -> Frame)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (FramePos -> FramePos -> FrameRows -> Frame)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((FrameRows -> FramePos -> FramePos -> Frame)
-> FramePos -> FramePos -> FrameRows -> Frame
forall a b c t. (a -> b -> c -> t) -> c -> b -> a -> t
flip3 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
    c <- Parser Name
comp
    cq <- compQuan
    q <- parens queryExpr
    pure $ \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"
    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")
    q <- parens queryExpr
    pure $ \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
    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)
    pure $ \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"
    i <- Text -> Parser [Name]
names Text
"collation name"
    pure $ \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 ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
braces (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 :: * -> *} {e} {s} {a}.
MonadParsec e s 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 :: * -> *} {e} {s} {a}.
MonadParsec e s 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 :: * -> *} {e} {s} {a}.
MonadParsec e s 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 :: * -> *} {e} {s} {a}.
MonadParsec e s 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 :: * -> *} {e} {s} {a}.
MonadParsec e s 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 :: * -> *} {e} {s} {a}.
MonadParsec e s 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 (ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> 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
hidden (ParsecT
   Void
   SQLStream
   (Reader Dialect)
   (ScalarExpr -> ScalarExpr -> ScalarExpr)
 -> ParsecT
      Void
      SQLStream
      (Reader Dialect)
      (ScalarExpr -> ScalarExpr -> ScalarExpr))
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ScalarExpr -> ScalarExpr)
forall a b. (a -> b) -> a -> b
$ 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 (ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> 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
hidden (ParsecT
   Void
   SQLStream
   (Reader Dialect)
   (ScalarExpr -> ScalarExpr -> ScalarExpr)
 -> ParsecT
      Void
      SQLStream
      (Reader Dialect)
      (ScalarExpr -> ScalarExpr -> ScalarExpr))
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ScalarExpr -> ScalarExpr)
forall a b. (a -> b) -> a -> b
$ 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 (ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> 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
hidden (ParsecT
   Void
   SQLStream
   (Reader Dialect)
   (ScalarExpr -> ScalarExpr -> ScalarExpr)
 -> ParsecT
      Void
      SQLStream
      (Reader Dialect)
      (ScalarExpr -> ScalarExpr -> ScalarExpr))
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ScalarExpr -> ScalarExpr)
forall a b. (a -> b) -> a -> b
$ 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 (ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> 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
hidden (ParsecT
   Void
   SQLStream
   (Reader Dialect)
   (ScalarExpr -> ScalarExpr -> ScalarExpr)
 -> ParsecT
      Void
      SQLStream
      (Reader Dialect)
      (ScalarExpr -> ScalarExpr -> ScalarExpr))
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ScalarExpr -> ScalarExpr)
forall a b. (a -> b) -> a -> b
$ 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 (ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> 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
hidden (ParsecT
   Void
   SQLStream
   (Reader Dialect)
   (ScalarExpr -> ScalarExpr -> ScalarExpr)
 -> ParsecT
      Void
      SQLStream
      (Reader Dialect)
      (ScalarExpr -> ScalarExpr -> ScalarExpr))
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ScalarExpr -> ScalarExpr)
forall a b. (a -> b) -> a -> b
$ 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 :: * -> *} {e} {s} {a}.
MonadParsec e s m =>
m (a -> a) -> Operator m a
prefix (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
hidden (ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
 -> ParsecT
      Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr))
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
forall a b. (a -> b) -> a -> b
$ [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 :: * -> *} {e} {s} {a}.
MonadParsec e s m =>
m (a -> a) -> Operator m a
prefix (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
hidden (ParsecT Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
 -> ParsecT
      Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr))
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
-> ParsecT
     Void SQLStream (Reader Dialect) (ScalarExpr -> ScalarExpr)
forall a b. (a -> b) -> a -> b
$ [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 (m (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> m (ScalarExpr -> ScalarExpr -> ScalarExpr)
forall a. m a -> m a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
hidden (m (ScalarExpr -> ScalarExpr -> ScalarExpr)
 -> m (ScalarExpr -> ScalarExpr -> ScalarExpr))
-> m (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> m (ScalarExpr -> ScalarExpr -> ScalarExpr)
forall a b. (a -> b) -> a -> b
$ do
                 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
                 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 (ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> 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
hidden (ParsecT
   Void
   SQLStream
   (Reader Dialect)
   (ScalarExpr -> ScalarExpr -> ScalarExpr)
 -> ParsecT
      Void
      SQLStream
      (Reader Dialect)
      (ScalarExpr -> ScalarExpr -> ScalarExpr))
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ScalarExpr -> ScalarExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> ScalarExpr -> ScalarExpr)
forall a b. (a -> b) -> a -> b
$ do
        Text -> ParsecT Void SQLStream (Reader Dialect) ()
keyword_ Text
"multiset"
        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"]
        d <- hoption SQDefault duplicates
        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 :: * -> *} {e} {s} {a}.
MonadParsec e s 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
$ m (ScalarExpr -> ScalarExpr) -> m (ScalarExpr -> ScalarExpr)
forall a. m a -> m a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
hidden (m (ScalarExpr -> ScalarExpr) -> m (ScalarExpr -> ScalarExpr))
-> m (ScalarExpr -> ScalarExpr) -> m (ScalarExpr -> ScalarExpr)
forall a b. (a -> b) -> a -> b
$ do
          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
          pure $ PostfixOp [Name Nothing $ T.unwords 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) -> m (a -> a)
forall a. m a -> m a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
hidden 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) -> m (a -> a)
forall a. m a -> m a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
hidden 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 -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
expressionLabel (Parser ScalarExpr -> Parser ScalarExpr)
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b. (a -> b) -> a -> b
$ 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)

-- used when parsing contexts where a * or x.* is allowed
-- currently at the top level of a select item or top level of
-- argument passed to an app-like. This list may need to be extended.

scalarExprOrStar :: Parser ScalarExpr
scalarExprOrStar :: Parser ScalarExpr
scalarExprOrStar = Parser ScalarExpr
star 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
scalarExpr

-- use this to get a nice unexpected keyword error which doesn't also
-- mangle other errors
expressionLabel :: Parser a -> Parser a
expressionLabel :: forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
expressionLabel Parser a
p = Text -> Parser a -> Parser a
forall a. Text -> Parser a -> Parser a
label Text
"expression" Parser a
p Parser a -> Parser a -> Parser a
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 a
forall a. Parser a
failOnKeyword

term :: Parser ScalarExpr
term :: Parser ScalarExpr
term = Parser ScalarExpr -> Parser ScalarExpr
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
expressionLabel (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
    [Parser ScalarExpr
simpleLiteral
    ,Parser ScalarExpr
parameter
    ,Parser ScalarExpr
positionalArg
    ,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]

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

scalarExprB :: Parser ScalarExpr
scalarExprB :: Parser ScalarExpr
scalarExprB = Text -> Parser ScalarExpr -> Parser ScalarExpr
forall a. Text -> Parser a -> Parser a
label Text
"expression" (Parser ScalarExpr -> Parser ScalarExpr)
-> Parser ScalarExpr -> Parser ScalarExpr
forall a b. (a -> b) -> a -> b
$ 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 (ParsecT Void SQLStream (Reader Dialect) Char
comma ParsecT Void SQLStream (Reader Dialect) 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 -> Text -> Parser Text
forall a. Parser a -> Text -> Parser a
<?> Text
"datetime field"

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

duplicates :: Parser SetQuantifier
duplicates :: ParsecT Void SQLStream (Reader Dialect) SetQuantifier
duplicates =
    [ParsecT Void SQLStream (Reader Dialect) SetQuantifier]
-> ParsecT Void SQLStream (Reader Dialect) SetQuantifier
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice [SetQuantifier
All SetQuantifier
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) 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
-> ParsecT Void SQLStream (Reader Dialect) 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 =
    Text
-> Parser (ScalarExpr, Maybe Name)
-> Parser (ScalarExpr, Maybe Name)
forall a. Text -> Parser a -> Parser a
label Text
"select item" (Parser (ScalarExpr, Maybe Name)
 -> Parser (ScalarExpr, Maybe Name))
-> Parser (ScalarExpr, Maybe Name)
-> Parser (ScalarExpr, Maybe Name)
forall a b. (a -> b) -> a -> b
$ [Parser (ScalarExpr, Maybe Name)]
-> Parser (ScalarExpr, Maybe Name)
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
    [(,Maybe Name
forall a. Maybe a
Nothing) (ScalarExpr -> (ScalarExpr, Maybe Name))
-> Parser ScalarExpr -> Parser (ScalarExpr, Maybe Name)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ScalarExpr
star
    ,(,) (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 = Text -> Parser Name -> Parser Name
forall a. Text -> Parser a -> Parser a
label Text
"alias" (Parser Name -> Parser Name) -> Parser Name -> Parser Name
forall a b. (a -> b) -> a -> 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 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
*> Text -> Parser Name
name Text
"alias"

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 -> Parser [TableRef] -> Parser [TableRef]
forall a. Text -> Parser a -> Parser a
label Text
"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
    tref :: Parser TableRef
tref = Parser TableRef
nonJoinTref Parser TableRef
-> ParsecT Void SQLStream (Reader Dialect) (TableRef -> TableRef)
-> Parser TableRef
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (ParsecT Void SQLStream (Reader Dialect) (TableRef -> TableRef)
-> ParsecT Void SQLStream (Reader Dialect) (TableRef -> TableRef)
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
hidden (ParsecT Void SQLStream (Reader Dialect) (TableRef -> TableRef)
-> ParsecT Void SQLStream (Reader Dialect) (TableRef -> TableRef)
forall {f :: * -> *} {b}. MonadPlus f => f (b -> b) -> f (b -> b)
chainl ParsecT Void SQLStream (Reader Dialect) (TableRef -> TableRef)
tjoin) ParsecT Void SQLStream (Reader Dialect) (TableRef -> TableRef)
-> ParsecT Void SQLStream (Reader Dialect) (TableRef -> TableRef)
-> ParsecT Void SQLStream (Reader Dialect) (TableRef -> TableRef)
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
<|> (TableRef -> TableRef)
-> ParsecT Void SQLStream (Reader Dialect) (TableRef -> TableRef)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TableRef -> TableRef
forall a. a -> a
id)
    nonJoinTref :: Parser TableRef
nonJoinTref =
        Text -> Parser TableRef -> Parser TableRef
forall a. Text -> Parser a -> Parser a
label Text
"table ref" (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
        [Parser TableRef -> Parser TableRef
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
hidden (Parser TableRef -> Parser TableRef)
-> Parser TableRef -> Parser TableRef
forall a b. (a -> b) -> a -> b
$ 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
             -- will be tricky to figure out how to support mixes of nested
             -- query expr parens and table ref parens
             [QueryExpr -> TableRef
TRQueryExpr (QueryExpr -> TableRef) -> Parser QueryExpr -> Parser TableRef
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser QueryExpr
queryExprNoParens
             ,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
<$> (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
hidden (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
         n <- Text -> Parser [Name]
names Text
"table name"
         choice [TRFunction n <$> hidden (parens (commaSep scalarExpr))
                ,pure $ TRSimple 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
<$> (Parser TableRef -> Parser TableRef
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
hidden (Parser TableRef -> Parser TableRef
forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
braces (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
-> ParsecT Void SQLStream (Reader Dialect) (TableRef -> TableRef)
-> Parser TableRef
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (ParsecT Void SQLStream (Reader Dialect) (TableRef -> TableRef)
talias ParsecT Void SQLStream (Reader Dialect) (TableRef -> TableRef)
-> ParsecT Void SQLStream (Reader Dialect) (TableRef -> TableRef)
-> ParsecT Void SQLStream (Reader Dialect) (TableRef -> TableRef)
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
<|> (TableRef -> TableRef)
-> ParsecT Void SQLStream (Reader Dialect) (TableRef -> TableRef)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TableRef -> TableRef
forall a. a -> a
id)
    talias :: ParsecT Void SQLStream (Reader Dialect) (TableRef -> TableRef)
talias = Parser Alias
fromAlias Parser Alias
-> ParsecT
     Void SQLStream (Reader Dialect) (Alias -> TableRef -> TableRef)
-> ParsecT Void SQLStream (Reader Dialect) (TableRef -> TableRef)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (Alias -> TableRef -> TableRef)
-> ParsecT
     Void SQLStream (Reader Dialect) (Alias -> TableRef -> TableRef)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((TableRef -> Alias -> TableRef) -> Alias -> TableRef -> TableRef
forall a b c. (a -> b -> c) -> b -> a -> c
flip TableRef -> Alias -> TableRef
TRAlias)
    tjoin :: ParsecT Void SQLStream (Reader Dialect) (TableRef -> TableRef)
tjoin =
        (\Bool
jn JoinType
jt TableRef
tr1 Maybe JoinCondition
jc TableRef
tr0 -> TableRef
-> Bool -> JoinType -> TableRef -> Maybe JoinCondition -> TableRef
TRJoin TableRef
tr0 Bool
jn JoinType
jt TableRef
tr1 Maybe JoinCondition
jc)
        (Bool
 -> JoinType
 -> TableRef
 -> Maybe JoinCondition
 -> TableRef
 -> TableRef)
-> ParsecT Void SQLStream (Reader Dialect) Bool
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (JoinType
      -> TableRef -> Maybe JoinCondition -> TableRef -> 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 -> TableRef)
-> ParsecT Void SQLStream (Reader Dialect) JoinType
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (TableRef -> Maybe JoinCondition -> TableRef -> 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 -> TableRef)
-> Parser TableRef
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (Maybe JoinCondition -> TableRef -> 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 -> TableRef)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe JoinCondition)
-> ParsecT Void SQLStream (Reader Dialect) (TableRef -> 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 JoinCondition
-> ParsecT Void SQLStream (Reader Dialect) (Maybe JoinCondition)
forall a. Parser a -> Parser (Maybe a)
hoptional Parser JoinCondition
joinCondition
    chainl :: f (b -> b) -> f (b -> b)
chainl f (b -> b)
p = ((b -> b) -> (b -> b) -> b -> b) -> (b -> b) -> [b -> b] -> b -> b
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (b -> b) -> (b -> b) -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) b -> b
forall a. a -> a
id ([b -> b] -> b -> b)
-> ([b -> b] -> [b -> b]) -> [b -> b] -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [b -> b] -> [b -> b]
forall a. [a] -> [a]
reverse ([b -> b] -> b -> b) -> f [b -> b] -> f (b -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (b -> b) -> f [b -> b]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
some f (b -> b)
p


{-
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 :: Parser JoinCondition
joinCondition = [Parser JoinCondition] -> Parser 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) ()
-> Parser JoinCondition -> Parser 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 -> Parser 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) ()
-> Parser JoinCondition -> Parser 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] -> Parser 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 (Text -> Parser Name
name Text
"column 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 a. Parser a -> Parser (Maybe a)
hoptional (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
*> Text -> Parser Name
name Text
"alias"
    columnAliases :: ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
columnAliases = Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) (Maybe [Name])
forall a. Parser a -> Parser (Maybe a)
hoptional (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 (Text -> Parser Name
name Text
"column 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 -> Parser ScalarExpr -> Parser ScalarExpr
forall a. Text -> Parser a -> Parser a
label Text
"where" (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 -> Parser [GroupingExpr] -> Parser [GroupingExpr]
forall a. Text -> Parser a -> Parser a
label Text
"group by" ([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 =
      Text -> Parser GroupingExpr -> Parser GroupingExpr
forall a. Text -> Parser a -> Parser a
label Text
"grouping expression" (Parser GroupingExpr -> Parser GroupingExpr)
-> Parser GroupingExpr -> Parser GroupingExpr
forall a b. (a -> b) -> a -> b
$
      [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 -> Parser ScalarExpr -> Parser ScalarExpr
forall a. Text -> Parser a -> Parser a
label Text
"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 :: Parser [SortSpec]
orderBy = Text -> Parser [SortSpec] -> Parser [SortSpec]
forall a. Text -> Parser a -> Parser a
label Text
"order by" ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"order",Text
"by"] ParsecT Void SQLStream (Reader Dialect) ()
-> Parser [SortSpec] -> Parser [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 -> Parser [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 a. a -> Parser a -> Parser a
hoption 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 a. a -> Parser a -> Parser a
hoption 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 -> Parser ScalarExpr -> Parser ScalarExpr
forall a. Text -> Parser a -> Parser a
label Text
"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 a. a -> Parser a -> Parser a
hoption 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
<$> Text -> Parser Name
name Text
"alias" 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 a. Parser a -> Parser (Maybe a)
hoptional (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 (Text -> Parser Name
name Text
"column alias")


{-
== query expression

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

queryExpr :: Parser QueryExpr
queryExpr :: Parser QueryExpr
queryExpr = Bool -> Parser QueryExpr
queryExpr' Bool
True
queryExprNoParens :: Parser QueryExpr
queryExprNoParens :: Parser QueryExpr
queryExprNoParens = Bool -> Parser QueryExpr
queryExpr' Bool
False

queryExpr' :: Bool -> Parser QueryExpr
queryExpr' :: Bool -> Parser QueryExpr
queryExpr' Bool
allowParens = Text -> Parser QueryExpr -> Parser QueryExpr
forall a. Text -> Parser a -> Parser a
label Text
"query expr" (Parser QueryExpr -> Parser QueryExpr)
-> Parser QueryExpr -> Parser QueryExpr
forall a b. (a -> b) -> a -> b
$ 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
        | Bool
allowParens =
              Text -> Parser QueryExpr -> Parser QueryExpr
forall a. Text -> Parser a -> Parser a
label Text
"query expr" (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 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
qeParens)
        | Bool
otherwise =
              Text -> Parser QueryExpr -> Parser QueryExpr
forall a. Text -> Parser a -> Parser a
label Text
"query expr" (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)
-> ParsecT Void SQLStream (Reader Dialect) 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
-> ParsecT Void SQLStream (Reader Dialect) SetQuantifier
-> ParsecT Void SQLStream (Reader Dialect) SetQuantifier
forall a. a -> Parser a -> Parser a
hoption SetQuantifier
SQDefault ParsecT Void SQLStream (Reader Dialect) 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
    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
<$> Text -> Parser [Name]
names Text
"table name"
    qeParens :: Parser QueryExpr
qeParens = QueryExpr -> QueryExpr
QueryExprParens (QueryExpr -> QueryExpr) -> Parser QueryExpr -> Parser QueryExpr
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

    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 = ParsecT
  Void
  SQLStream
  (Reader Dialect)
  (QueryExpr -> QueryExpr -> QueryExpr)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (QueryExpr -> QueryExpr -> QueryExpr)
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
hidden (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)
-> ParsecT Void SQLStream (Reader Dialect) 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
-> ParsecT Void SQLStream (Reader Dialect) SetQuantifier
-> ParsecT Void SQLStream (Reader Dialect) SetQuantifier
forall a. a -> Parser a -> Parser a
hoption SetQuantifier
SQDefault ParsecT Void SQLStream (Reader Dialect) 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)
    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 a. a -> Parser a -> Parser a
hoption 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 =
    Text
-> ParsecT Void SQLStream (Reader Dialect) TableExpression
-> ParsecT Void SQLStream (Reader Dialect) TableExpression
forall a. Text -> Parser a -> Parser a
label Text
"from" (ParsecT Void SQLStream (Reader Dialect) TableExpression
 -> ParsecT Void SQLStream (Reader Dialect) TableExpression)
-> ParsecT Void SQLStream (Reader Dialect) TableExpression
-> ParsecT Void SQLStream (Reader Dialect) TableExpression
forall a b. (a -> b) -> a -> b
$
    [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
    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)
  ([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
    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)
  ([SortSpec]
   -> (Maybe ScalarExpr, Maybe ScalarExpr) -> TableExpression)
-> Parser [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] -> Parser [SortSpec] -> Parser [SortSpec]
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option [] Parser [SortSpec]
orderBy
    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)
-> Parser (Maybe ScalarExpr, Maybe 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
hidden Parser (Maybe ScalarExpr, Maybe ScalarExpr)
offsetFetch)
 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)
-> ParsecT Void SQLStream (Reader Dialect) 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
<$ ParsecT Void SQLStream (Reader Dialect) Char
semi)

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

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

= Statements
-}

statementWithoutSemicolon :: Parser Statement
statementWithoutSemicolon :: Parser Statement
statementWithoutSemicolon =
    Text -> Parser Statement -> Parser Statement
forall a. Text -> Parser a -> Parser a
label Text
"statement" (Parser Statement -> Parser Statement)
-> Parser Statement -> Parser Statement
forall a b. (a -> b) -> a -> b
$ [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
<* ParsecT Void SQLStream (Reader Dialect) Char
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ParsecT Void SQLStream (Reader Dialect) 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
-> ParsecT Void SQLStream (Reader Dialect) 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
<$ ParsecT Void SQLStream (Reader Dialect) Char
-> ParsecT Void SQLStream (Reader Dialect) Char
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
hidden ParsecT Void SQLStream (Reader Dialect) 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
<$> Text -> Parser [Name]
names Text
"schema name"

createTable :: Parser Statement
createTable :: Parser Statement
createTable = do
  d <- (Dialect -> Dialect) -> Parser Dialect
forall a. (Dialect -> a) -> Parser a
askDialect Dialect -> Dialect
forall a. a -> a
id
  let
    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 = (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 = if Dialect -> Bool
diNonCommaSeparatedConstraints Dialect
d
      then ParsecT Void SQLStream (Reader Dialect) Char
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ParsecT Void SQLStream (Reader Dialect) Char
comma
      else Char -> Maybe Char
forall a. a -> Maybe a
Just (Char -> Maybe Char)
-> ParsecT Void SQLStream (Reader Dialect) Char
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Char)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) Char
comma
    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)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Char)
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
hidden ParsecT Void SQLStream (Reader Dialect) (Maybe Char)
separator)
    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
<*> ((ParsecT Void SQLStream (Reader Dialect) Char
comma ParsecT Void SQLStream (Reader Dialect) 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
    withoutRowid = if Dialect -> Bool
diWithoutRowidTables Dialect
d
      then Bool -> Maybe Bool -> Bool
forall a. a -> Maybe a -> a
fromMaybe Bool
False (Maybe Bool -> Bool)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Bool)
-> ParsecT Void SQLStream (Reader Dialect) Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void SQLStream (Reader Dialect) Bool
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Bool)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ([Text] -> ParsecT Void SQLStream (Reader Dialect) ()
keywords_ [Text
"without", Text
"rowid"] 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)
      else 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

  keyword_ "table" >>
    CreateTable
    <$> names  "table name"
    <*> parens entries
    <*> withoutRowid

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
<*> Text -> Parser [Name]
names Text
"index name"
    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
>> Text -> Parser [Name]
names Text
"table name")
    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 (Text -> Parser Name
name Text
"column name"))

columnDef :: Parser ColumnDef
columnDef :: ParsecT Void SQLStream (Reader Dialect) ColumnDef
columnDef = do
  optionalType <- (Dialect -> Bool) -> ParsecT Void SQLStream (Reader Dialect) Bool
forall a. (Dialect -> a) -> Parser a
askDialect Dialect -> Bool
diOptionalColumnTypes
  ColumnDef <$> name "column name"
    <*> (if optionalType then optional typeName else Just <$> typeName)
    <*> option [] (some colConstraintDef)

tableConstraintDef :: Parser (Maybe [Name], TableConstraint)
tableConstraintDef :: ParsecT
  Void SQLStream (Reader Dialect) (Maybe [Name], TableConstraint)
tableConstraintDef =
    Text
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe [Name], TableConstraint)
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe [Name], TableConstraint)
forall a. Text -> Parser a -> Parser a
label Text
"table constraint" (ParsecT
   Void SQLStream (Reader Dialect) (Maybe [Name], TableConstraint)
 -> ParsecT
      Void SQLStream (Reader Dialect) (Maybe [Name], TableConstraint))
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe [Name], TableConstraint)
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe [Name], TableConstraint)
forall a b. (a -> b) -> a -> b
$
    (,)
    (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
*> Text -> Parser [Name]
names Text
"constraint name")
    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 -> Parser [Name]) -> Parser Name -> Parser [Name]
forall a b. (a -> b) -> a -> b
$ Text -> Parser Name
name Text
"column 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 -> Parser [Name]) -> Parser Name -> Parser [Name]
forall a b. (a -> b) -> a -> b
$ Text -> Parser Name
name Text
"column 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 -> Parser [Name]) -> Parser Name -> Parser [Name]
forall a b. (a -> b) -> a -> b
$ Text -> Parser Name
name Text
"column 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
*> Text -> Parser [Name]
names Text
"table name")
        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 a. Parser a -> Parser (Maybe a)
hoptional (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 -> Parser [Name]) -> Parser Name -> Parser [Name]
forall a b. (a -> b) -> a -> b
$ Text -> Parser Name
name Text
"column 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 a. a -> Parser a -> Parser a
hoption 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
*> Text -> Parser [Name]
names Text
"constraint name")
    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
          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
defaultClause
        )
  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"]
      d <- (Dialect -> Dialect) -> Parser Dialect
forall a. (Dialect -> a) -> Parser a
askDialect Dialect -> Dialect
forall a. a -> a
id
      autoincrement <- if diAutoincrement d
        then optional (keyword_ "autoincrement")
        else pure Nothing
      pure $ ColPrimaryKeyConstraint $ isJust 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
<$> Text -> Parser [Name]
names Text
"table name"
        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 -> Parser Name) -> Parser Name -> Parser Name
forall a b. (a -> b) -> a -> b
$ Text -> Parser Name
name Text
"column 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
    defaultClause :: ParsecT Void SQLStream (Reader Dialect) ColConstraint
defaultClause = Text
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
forall a. Text -> Parser a -> Parser a
label Text
"column default clause" (ParsecT Void SQLStream (Reader Dialect) ColConstraint
 -> ParsecT Void SQLStream (Reader Dialect) ColConstraint)
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
forall a b. (a -> b) -> a -> b
$
        DefaultClause -> ColConstraint
ColDefaultClause (DefaultClause -> ColConstraint)
-> ParsecT Void SQLStream (Reader Dialect) DefaultClause
-> ParsecT Void SQLStream (Reader Dialect) ColConstraint
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [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))
          ]

-- 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
<$> Text -> Parser [Name]
names Text
"table name"
    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
>>
                  Text -> Parser Name
name Text
"column 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
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> Name -> AlterTableAction)
-> ParsecT
     Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (ScalarExpr -> Name -> AlterTableAction)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (ScalarExpr -> Name -> AlterTableAction)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Name -> ScalarExpr -> AlterTableAction)
-> ScalarExpr -> Name -> AlterTableAction
forall a b c. (a -> b -> c) -> b -> a -> c
flip 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
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (TypeName -> Name -> AlterTableAction)
-> ParsecT
     Void SQLStream (Reader Dialect) (Name -> AlterTableAction)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> (TypeName -> Name -> AlterTableAction)
-> ParsecT
     Void
     SQLStream
     (Reader Dialect)
     (TypeName -> Name -> AlterTableAction)
forall a. a -> ParsecT Void SQLStream (Reader Dialect) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Name -> TypeName -> AlterTableAction)
-> TypeName -> Name -> AlterTableAction
forall a b c. (a -> b -> c) -> b -> a -> c
flip 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
<$> Text -> Parser Name
name Text
"column 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
<$> Text -> Parser [Name]
names Text
"constraint 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


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
<$> Text -> Parser [Name]
names Text
"schema 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

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
<$> Text -> Parser [Name]
names Text
"table 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

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 a. a -> Parser a -> Parser a
hoption 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
<*> Text -> Parser [Name]
names Text
"view name"
    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 -> Parser [Name]) -> Parser Name -> Parser [Name]
forall a b. (a -> b) -> a -> b
$ Text -> Parser Name
name Text
"column 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
<*> Parser CheckOption
-> ParsecT Void SQLStream (Reader Dialect) (Maybe CheckOption)
forall a. Parser a -> Parser (Maybe a)
hoptional ([Parser CheckOption] -> Parser 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) () -> Parser 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) () -> Parser 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) () -> Parser 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
<$> Text -> Parser [Name]
names Text
"view 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

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
<$> Text -> Parser [Name]
names Text
"domain name"
    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) Parser TypeName -> Text -> Parser TypeName
forall a. Parser a -> Text -> Parser a
<?> Text
"alias")
    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
*> Text -> Parser [Name]
names Text
"constraint name")
          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
<$> Text -> Parser [Name]
names Text
"domain name"
    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
*> Text -> Parser [Name]
names Text
"constraint name")
       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
<$> Text -> Parser [Name]
names Text
"constraint name"

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
<$> Text -> Parser [Name]
names Text
"domain 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

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
<$> Text -> Parser [Name]
names Text
"sequence name"
    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
<$> Text -> Parser [Name]
names Text
"sequence name"
    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
<$> Text -> Parser [Name]
names Text
"sequence 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

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
<$> Text -> Parser [Name]
names Text
"assertion name"
    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
<$> Text -> Parser [Name]
names Text
"assertion 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

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

= 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
<$> Text -> Parser [Name]
names Text
"table name"
    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 a. Parser a -> Parser (Maybe a)
hoptional (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
*> Text -> Parser Name
name Text
"alias")
    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
<$> Text -> Parser [Name]
names Text
"table name"
    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 a. a -> Parser a -> Parser a
hoption 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
<$> Text -> Parser [Name]
names Text
"table name"
    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 a. Parser a -> Parser (Maybe a)
hoptional (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 -> Parser [Name]) -> Parser Name -> Parser [Name]
forall a b. (a -> b) -> a -> b
$ Text -> Parser Name
name Text
"column 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
<*>
        -- slight hack
        (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) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a. Text -> Parser a -> Parser a
label Text
"values" ([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
<$> Text -> Parser [Name]
names Text
"table name"
    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
<*> Text
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Name)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe Name)
forall a. Text -> Parser a -> Parser a
label Text
"alias" (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
*> Text -> Parser Name
name Text
"alias"))
    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 = Text -> Parser SetClause -> Parser SetClause
forall a. Text -> Parser a -> Parser a
label Text
"set clause" (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] -> ParsecT Void SQLStream (Reader Dialect) [[Name]])
-> Parser [Name]
-> ParsecT Void SQLStream (Reader Dialect) [[Name]]
forall a b. (a -> b) -> a -> b
$ Text -> Parser [Name]
names Text
"column name")
                  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
<$> Text -> Parser [Name]
names Text
"column name"
                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
<$> Text -> Parser Name
name Text
"savepoint 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
<$> Text -> Parser Name
name Text
"savepoint 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 a. Parser a -> Parser (Maybe a)
hoptional (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 a. Parser a -> Parser (Maybe a)
hoptional (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
*> Text -> Parser Name
name Text
"savepoint 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 (Text -> Parser Name
name Text
"role 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 (Text -> Parser Name
name Text
"role 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 (Text -> Parser Name
name Text
"role 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
<$> Text -> Parser Name
name Text
"role 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
<$> Text -> Parser Name
name Text
"role 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 (Text -> Parser Name
name Text
"role 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 (Text -> Parser Name
name Text
"role 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 (Text -> Parser Name
name Text
"role 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 -> Parser [Name]) -> Parser Name -> Parser [Name]
forall a b. (a -> b) -> a -> b
$ Text -> Parser Name
name Text
"column 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 -> Parser [Name]) -> Parser Name -> Parser [Name]
forall a b. (a -> b) -> a -> b
$ Text -> Parser Name
name Text
"column 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 -> Parser [Name]) -> Parser Name -> Parser [Name]
forall a b. (a -> b) -> a -> b
$ Text -> Parser Name
name Text
"column 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 -> Parser [Name]) -> Parser Name -> Parser [Name]
forall a b. (a -> b) -> a -> b
$ Text -> Parser Name
name Text
"column 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
<$> Text -> Parser [Name]
names Text
"domain name"
    ,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
<$> Text -> Parser [Name]
names Text
"type name"
    ,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
<$> Text -> Parser [Name]
names Text
"sequence name"
    ,[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
<$> Text -> Parser [Name]
names Text
"function name"
    ,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
<$> Text -> Parser [Name]
names Text
"table name"
    ]


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

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] -> Parser [Text]
forall a. Text -> Parser a -> Parser a
label (Text -> [Text] -> Text
T.intercalate Text
", " [Text]
sets) (Parser [Text] -> Parser [Text]) -> Parser [Text] -> Parser [Text]
forall a b. (a -> b) -> a -> b
$
    [[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

{-
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

TODO: this name is not so good because it's similar to <?> which does
something completely different
-}

(<??>) :: 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 a. a -> Parser a -> Parser a
hoption a -> a
forall a. a -> a
id Parser (a -> a)
q

-- 0 to many repeated applications of suffix parser

chainrSuffix :: Parser a -> Parser (a -> a) -> Parser a
chainrSuffix :: forall a. Parser a -> Parser (a -> a) -> Parser a
chainrSuffix Parser a
p 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) -> Parser (a -> a)
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
hidden 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 Parser Text -> Text -> Parser Text
forall a. Parser a -> Text -> Parser a
<?> Text
"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 -> Text -> Parser Text
forall a. Parser a -> Text -> Parser a
<?> Text
s

singleCharSymbol :: Char -> Parser Char
singleCharSymbol :: Char -> ParsecT Void SQLStream (Reader Dialect) Char
singleCharSymbol Char
c = Char
c Char -> Parser Text -> ParsecT Void SQLStream (Reader Dialect) 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 :: ParsecT Void SQLStream (Reader Dialect) Char
questionMark = Char -> ParsecT Void SQLStream (Reader Dialect) Char
singleCharSymbol Char
'?' ParsecT Void SQLStream (Reader Dialect) Char
-> Text -> ParsecT Void SQLStream (Reader Dialect) Char
forall a. Parser a -> Text -> Parser a
<?> Text
"question mark"

openParen :: Parser ()
openParen :: ParsecT Void SQLStream (Reader Dialect) ()
openParen = ParsecT Void SQLStream (Reader Dialect) Char
-> ParsecT Void SQLStream (Reader Dialect) ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT Void SQLStream (Reader Dialect) Char
 -> ParsecT Void SQLStream (Reader Dialect) ())
-> ParsecT Void SQLStream (Reader Dialect) Char
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a b. (a -> b) -> a -> b
$ Char -> ParsecT Void SQLStream (Reader Dialect) Char
singleCharSymbol Char
'('

closeParen :: Parser ()
closeParen :: ParsecT Void SQLStream (Reader Dialect) ()
closeParen = ParsecT Void SQLStream (Reader Dialect) Char
-> ParsecT Void SQLStream (Reader Dialect) ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT Void SQLStream (Reader Dialect) Char
 -> ParsecT Void SQLStream (Reader Dialect) ())
-> ParsecT Void SQLStream (Reader Dialect) Char
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a b. (a -> b) -> a -> b
$ Char -> ParsecT Void SQLStream (Reader Dialect) Char
singleCharSymbol Char
')'

comma :: Parser Char
comma :: ParsecT Void SQLStream (Reader Dialect) Char
comma = Char -> ParsecT Void SQLStream (Reader Dialect) Char
singleCharSymbol Char
','

semi :: Parser Char
semi :: ParsecT Void SQLStream (Reader Dialect) Char
semi = Char -> ParsecT Void SQLStream (Reader Dialect) Char
singleCharSymbol Char
';'

-- = helper functions

keyword :: Text -> Parser Text
keyword :: Text -> Parser Text
keyword Text
k = [Text] -> Parser Text
keywordTok [Text
k] Parser Text -> Text -> Parser Text
forall a. Parser a -> Text -> Parser a
<?> 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) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a. Text -> Parser a -> Parser a
label ([Text] -> Text
T.unwords [Text]
ks) (ParsecT Void SQLStream (Reader Dialect) ()
 -> ParsecT Void SQLStream (Reader Dialect) ())
-> ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
forall a b. (a -> b) -> a -> b
$ (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

parens :: Parser a -> Parser a
parens :: forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
parens = ParsecT Void SQLStream (Reader Dialect) ()
-> ParsecT Void SQLStream (Reader Dialect) ()
-> 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 ParsecT Void SQLStream (Reader Dialect) ()
openParen ParsecT Void SQLStream (Reader Dialect) ()
closeParen

brackets :: Parser a -> Parser a
brackets :: forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
brackets = ParsecT Void SQLStream (Reader Dialect) Char
-> ParsecT Void SQLStream (Reader Dialect) 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 (Char -> ParsecT Void SQLStream (Reader Dialect) Char
singleCharSymbol Char
'[') (Char -> ParsecT Void SQLStream (Reader Dialect) Char
singleCharSymbol Char
']')

braces :: Parser a -> Parser a
braces :: forall a.
ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
braces = ParsecT Void SQLStream (Reader Dialect) Char
-> ParsecT Void SQLStream (Reader Dialect) 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 (Char -> ParsecT Void SQLStream (Reader Dialect) Char
singleCharSymbol Char
'{') (Char -> ParsecT Void SQLStream (Reader Dialect) Char
singleCharSymbol Char
'}')

commaSep :: Parser a -> Parser [a]
commaSep :: forall a. Parser a -> Parser [a]
commaSep = (ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) Char
-> ParsecT Void SQLStream (Reader Dialect) [a]
forall (m :: * -> *) a sep. MonadPlus m => m a -> m sep -> m [a]
`sepBy` ParsecT Void SQLStream (Reader Dialect) Char
-> ParsecT Void SQLStream (Reader Dialect) Char
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
hidden ParsecT Void SQLStream (Reader Dialect) 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
-> ParsecT Void SQLStream (Reader Dialect) Char
-> ParsecT Void SQLStream (Reader Dialect) [a]
forall (m :: * -> *) a sep. MonadPlus m => m a -> m sep -> m [a]
`sepBy1` ParsecT Void SQLStream (Reader Dialect) Char
-> ParsecT Void SQLStream (Reader Dialect) Char
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
hidden ParsecT Void SQLStream (Reader Dialect) Char
comma)

hoptional :: Parser a -> Parser (Maybe a)
hoptional :: forall a. Parser a -> Parser (Maybe a)
hoptional = ParsecT Void SQLStream (Reader Dialect) (Maybe a)
-> ParsecT Void SQLStream (Reader Dialect) (Maybe a)
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
hidden (ParsecT Void SQLStream (Reader Dialect) (Maybe a)
 -> ParsecT Void SQLStream (Reader Dialect) (Maybe a))
-> (Parser a -> ParsecT Void SQLStream (Reader Dialect) (Maybe a))
-> Parser a
-> ParsecT Void SQLStream (Reader Dialect) (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser a -> ParsecT Void SQLStream (Reader Dialect) (Maybe a)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional

hoption :: a -> Parser a -> Parser a
hoption :: forall a. a -> Parser a -> Parser a
hoption a
a Parser a
p = Parser a -> Parser a
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
hidden (Parser a -> Parser a) -> Parser a -> Parser a
forall a b. (a -> b) -> a -> b
$ a -> Parser a -> Parser a
forall (m :: * -> *) a. Alternative m => a -> m a -> m a
option a
a Parser a
p

label :: Text -> Parser a -> Parser a
label :: forall a. Text -> Parser a -> Parser a
label Text
x = String
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall a.
String
-> ParsecT Void SQLStream (Reader Dialect) a
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a.
MonadParsec e s m =>
String -> m a -> m a
M.label (Text -> String
T.unpack Text
x)

(<?>) :: Parser a -> Text -> Parser a
<?> :: forall a. Parser a -> Text -> Parser a
(<?>) Parser a
p Text
a = Parser a -> String -> Parser a
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
(M.<?>) Parser a
p (Text -> String
T.unpack Text
a)

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

-- 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)
-> Text
-> ParsecT Void SQLStream (Reader Dialect) (Text, Text, Text)
forall a. Parser a -> Text -> Parser a
<?> Text
"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 -> Text -> Parser Text
forall a. Parser a -> Text -> Parser a
<?> Text
"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.

TODO: move this to the lexer?
-}

stringTokExtend :: Parser (Text,Text,Text)
stringTokExtend :: ParsecT Void SQLStream (Reader Dialect) (Text, Text, Text)
stringTokExtend = do
    (s,e,x) <- ParsecT Void SQLStream (Reader Dialect) (Text, Text, Text)
stringTok
    choice [
         do
         guard (s == "'" && e == "'")
         (s',e',y) <- stringTokExtend
         guard (s' == "'" && e' == "'")
         pure (s,e,x <> y)
        ,pure (s,e,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 -> Text -> Parser Text
forall a. Parser a -> Text -> Parser a
<?> Text
"host param"
  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
-> Text -> ParsecT Void SQLStream (Reader Dialect) Int
forall a. Parser a -> Text -> Parser a
<?> Text
"positional arg"
  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 -> Text -> Parser Text
forall a. Parser a -> Text -> Parser a
<?> Text
"number"
  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 -> Text -> Parser Text
forall a. Parser a -> Text -> Parser a
<?> Text
lbl
  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
    lbl :: Text
lbl = case Maybe Text
sym of
              Maybe Text
Nothing -> Text
"symbol"
              Just Text
p -> Text
p

{-
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 = do
    (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)
-> Text
-> ParsecT
     Void SQLStream (Reader Dialect) (Maybe (Text, Text), Text)
forall a. Parser a -> Text -> Parser a
<?> Text
"identifier"
  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 q :: Maybe (Text, Text)
q@Maybe (Text, Text)
Nothing 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

keywordTok :: [Text] -> Parser Text
keywordTok :: [Text] -> Parser Text
keywordTok [Text]
allowed = do
    (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  where
    test :: WithPos Token -> Maybe Text
test (L.WithPos SourcePos
_ SourcePos
_ Int
_ (L.Identifier Maybe (Text, Text)
Nothing Text
p))
        | Text -> Text
T.toLower Text
p Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
allowed = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
p
    test WithPos Token
_ = Maybe Text
forall a. Maybe a
Nothing


unexpectedKeywordError :: Text -> Parser a
unexpectedKeywordError :: forall a. Text -> Parser a
unexpectedKeywordError Text
kw =
    Maybe (ErrorItem (Token SQLStream))
-> Set (ErrorItem (Token SQLStream))
-> ParsecT Void SQLStream (Reader Dialect) a
forall e s (m :: * -> *) a.
MonadParsec e s m =>
Maybe (ErrorItem (Token s)) -> Set (ErrorItem (Token s)) -> m a
failure (ErrorItem (Token SQLStream) -> Maybe (ErrorItem (Token SQLStream))
forall a. a -> Maybe a
Just (ErrorItem (Token SQLStream)
 -> Maybe (ErrorItem (Token SQLStream)))
-> ErrorItem (Token SQLStream)
-> Maybe (ErrorItem (Token SQLStream))
forall a b. (a -> b) -> a -> b
$ NonEmpty Char -> ErrorItem (Token SQLStream)
forall t. NonEmpty Char -> ErrorItem t
Label (String -> NonEmpty Char
forall a. HasCallStack => [a] -> NonEmpty a
NE.fromList (String -> NonEmpty Char) -> String -> NonEmpty Char
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ Text
"keyword " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
kw)) Set (ErrorItem (Token SQLStream))
forall a. Set a
Set.empty

failOnKeyword :: Parser a
failOnKeyword :: forall a. Parser a
failOnKeyword = do
    kws <- (Dialect -> [Text]) -> Parser [Text]
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Dialect -> [Text]
diKeywords
    i <- lookAhead $ keywordTok kws
    unexpectedKeywordError i

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

-- 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