> -- | These is the pretty printing functions, which produce SQL
> -- source from ASTs. The code attempts to format the output in a
> -- readable way.
> module Language.SQL.SimpleSQL.Pretty
>     (prettyQueryExpr
>     ,prettyScalarExpr
>     ,prettyStatement
>     ,prettyStatements
>     ) where

> import Prelude hiding ((<>))

TODO: there should be more comments in this file, especially the bits
which have been changed to try to improve the layout of the output.
Try to do this when this code is ported to a modern pretty printing lib.

> --import Language.SQL.SimpleSQL.Dialect
> import Text.PrettyPrint (render, vcat, text, (<>), (<+>), empty, parens,
>                          nest, Doc, punctuate, comma, sep, quotes,
>                          brackets,hcat)
> import Data.Maybe (maybeToList, catMaybes)
> import Data.List (intercalate)


> import Language.SQL.SimpleSQL.Syntax
> import Language.SQL.SimpleSQL.Dialect


> -- | Convert a query expr ast to concrete syntax.
> prettyQueryExpr :: Dialect -> QueryExpr -> String
> prettyQueryExpr :: Dialect -> QueryExpr -> String
prettyQueryExpr d :: Dialect
d = Doc -> String
render (Doc -> String) -> (QueryExpr -> Doc) -> QueryExpr -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dialect -> QueryExpr -> Doc
queryExpr Dialect
d

> -- | Convert a value expr ast to concrete syntax.
> prettyScalarExpr :: Dialect -> ScalarExpr -> String
> prettyScalarExpr :: Dialect -> ScalarExpr -> String
prettyScalarExpr d :: Dialect
d = Doc -> String
render (Doc -> String) -> (ScalarExpr -> Doc) -> ScalarExpr -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d

> -- | Convert a statement ast to concrete syntax.
> prettyStatement :: Dialect -> Statement -> String
> prettyStatement :: Dialect -> Statement -> String
prettyStatement d :: Dialect
d = Doc -> String
render (Doc -> String) -> (Statement -> Doc) -> Statement -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dialect -> Statement -> Doc
statement Dialect
d

> -- | Convert a list of statements to concrete syntax. A semicolon
> -- is inserted after each statement.
> prettyStatements :: Dialect -> [Statement] -> String
> prettyStatements :: Dialect -> [Statement] -> String
prettyStatements d :: Dialect
d = Doc -> String
render (Doc -> String) -> ([Statement] -> Doc) -> [Statement] -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Doc] -> Doc
vcat ([Doc] -> Doc) -> ([Statement] -> [Doc]) -> [Statement] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Statement -> Doc) -> [Statement] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map ((Doc -> Doc -> Doc
<> String -> Doc
text ";\n") (Doc -> Doc) -> (Statement -> Doc) -> Statement -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dialect -> Statement -> Doc
statement Dialect
d)

= scalar expressions

> scalarExpr :: Dialect -> ScalarExpr -> Doc
> scalarExpr :: Dialect -> ScalarExpr -> Doc
scalarExpr _ (StringLit s :: String
s e :: String
e t :: String
t) = String -> Doc
text String
s Doc -> Doc -> Doc
<> String -> Doc
text String
t Doc -> Doc -> Doc
<> String -> Doc
text String
e

> scalarExpr _ (NumLit s :: String
s) = String -> Doc
text String
s
> scalarExpr _ (IntervalLit s :: Maybe Sign
s v :: String
v f :: IntervalTypeField
f t :: Maybe IntervalTypeField
t) =
>     String -> Doc
text "interval"
>     Doc -> Doc -> Doc
<+> (Sign -> Doc) -> Maybe Sign -> Doc
forall a. (a -> Doc) -> Maybe a -> Doc
me (\x :: Sign
x -> String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ case Sign
x of
>                              Plus -> "+"
>                              Minus -> "-") Maybe Sign
s
>     Doc -> Doc -> Doc
<+> Doc -> Doc
quotes (String -> Doc
text String
v)
>     Doc -> Doc -> Doc
<+> IntervalTypeField -> Doc
intervalTypeField IntervalTypeField
f
>     Doc -> Doc -> Doc
<+> (IntervalTypeField -> Doc) -> Maybe IntervalTypeField -> Doc
forall a. (a -> Doc) -> Maybe a -> Doc
me (\x :: IntervalTypeField
x -> String -> Doc
text "to" Doc -> Doc -> Doc
<+> IntervalTypeField -> Doc
intervalTypeField IntervalTypeField
x) Maybe IntervalTypeField
t
> scalarExpr _ (Iden i :: [Name]
i) = [Name] -> Doc
names [Name]
i
> scalarExpr _ Star = String -> Doc
text "*"
> scalarExpr _ Parameter = String -> Doc
text "?"
> scalarExpr _ (PositionalArg n :: Int
n) = String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ "$" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
n
> scalarExpr _ (HostParameter p :: String
p i :: Maybe String
i) =
>     String -> Doc
text String
p
>     Doc -> Doc -> Doc
<+> (String -> Doc) -> Maybe String -> Doc
forall a. (a -> Doc) -> Maybe a -> Doc
me (\i' :: String
i' -> String -> Doc
text "indicator" Doc -> Doc -> Doc
<+> String -> Doc
text String
i') Maybe String
i

> scalarExpr d :: Dialect
d (App f :: [Name]
f es :: [ScalarExpr]
es) = [Name] -> Doc
names [Name]
f Doc -> Doc -> Doc
<> Doc -> Doc
parens ([Doc] -> Doc
commaSep ((ScalarExpr -> Doc) -> [ScalarExpr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d) [ScalarExpr]
es))

> scalarExpr dia :: Dialect
dia (AggregateApp f :: [Name]
f d :: SetQuantifier
d es :: [ScalarExpr]
es od :: [SortSpec]
od fil :: Maybe ScalarExpr
fil) =
>     [Name] -> Doc
names [Name]
f
>     Doc -> Doc -> Doc
<> Doc -> Doc
parens ((case SetQuantifier
d of
>                   Distinct -> String -> Doc
text "distinct"
>                   All -> String -> Doc
text "all"
>                   SQDefault -> Doc
empty)
>                Doc -> Doc -> Doc
<+> [Doc] -> Doc
commaSep ((ScalarExpr -> Doc) -> [ScalarExpr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
dia) [ScalarExpr]
es)
>                Doc -> Doc -> Doc
<+> Dialect -> [SortSpec] -> Doc
orderBy Dialect
dia [SortSpec]
od)
>     Doc -> Doc -> Doc
<+> (ScalarExpr -> Doc) -> Maybe ScalarExpr -> Doc
forall a. (a -> Doc) -> Maybe a -> Doc
me (\x :: ScalarExpr
x -> String -> Doc
text "filter"
>                   Doc -> Doc -> Doc
<+> Doc -> Doc
parens (String -> Doc
text "where" Doc -> Doc -> Doc
<+> Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
dia ScalarExpr
x)) Maybe ScalarExpr
fil

> scalarExpr d :: Dialect
d (AggregateAppGroup f :: [Name]
f es :: [ScalarExpr]
es od :: [SortSpec]
od) =
>     [Name] -> Doc
names [Name]
f
>     Doc -> Doc -> Doc
<> Doc -> Doc
parens ([Doc] -> Doc
commaSep ((ScalarExpr -> Doc) -> [ScalarExpr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d) [ScalarExpr]
es))
>     Doc -> Doc -> Doc
<+> if [SortSpec] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [SortSpec]
od
>         then Doc
empty
>         else String -> Doc
text "within group" Doc -> Doc -> Doc
<+> Doc -> Doc
parens (Dialect -> [SortSpec] -> Doc
orderBy Dialect
d [SortSpec]
od)

> scalarExpr d :: Dialect
d (WindowApp f :: [Name]
f es :: [ScalarExpr]
es pb :: [ScalarExpr]
pb od :: [SortSpec]
od fr :: Maybe Frame
fr) =
>     [Name] -> Doc
names [Name]
f Doc -> Doc -> Doc
<> Doc -> Doc
parens ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (ScalarExpr -> Doc) -> [ScalarExpr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d) [ScalarExpr]
es)
>     Doc -> Doc -> Doc
<+> String -> Doc
text "over"
>     Doc -> Doc -> Doc
<+> Doc -> Doc
parens ((case [ScalarExpr]
pb of
>                     [] -> Doc
empty
>                     _ -> String -> Doc
text "partition by"
>                           Doc -> Doc -> Doc
<+> Int -> Doc -> Doc
nest 13 ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (ScalarExpr -> Doc) -> [ScalarExpr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d) [ScalarExpr]
pb))
>                 Doc -> Doc -> Doc
<+> Dialect -> [SortSpec] -> Doc
orderBy Dialect
d [SortSpec]
od
>     Doc -> Doc -> Doc
<+> (Frame -> Doc) -> Maybe Frame -> Doc
forall a. (a -> Doc) -> Maybe a -> Doc
me Frame -> Doc
frd Maybe Frame
fr)
>   where
>     frd :: Frame -> Doc
frd (FrameFrom rs :: FrameRows
rs fp :: FramePos
fp) = FrameRows -> Doc
rsd FrameRows
rs Doc -> Doc -> Doc
<+> FramePos -> Doc
fpd FramePos
fp
>     frd (FrameBetween rs :: FrameRows
rs fps :: FramePos
fps fpe :: FramePos
fpe) =
>         FrameRows -> Doc
rsd FrameRows
rs Doc -> Doc -> Doc
<+> String -> Doc
text "between" Doc -> Doc -> Doc
<+> FramePos -> Doc
fpd FramePos
fps
>         Doc -> Doc -> Doc
<+> String -> Doc
text "and" Doc -> Doc -> Doc
<+> FramePos -> Doc
fpd FramePos
fpe
>     rsd :: FrameRows -> Doc
rsd rs :: FrameRows
rs = case FrameRows
rs of
>                  FrameRows -> String -> Doc
text "rows"
>                  FrameRange -> String -> Doc
text "range"
>     fpd :: FramePos -> Doc
fpd UnboundedPreceding = String -> Doc
text "unbounded preceding"
>     fpd UnboundedFollowing = String -> Doc
text "unbounded following"
>     fpd Current = String -> Doc
text "current row"
>     fpd (Preceding e :: ScalarExpr
e) = Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
e Doc -> Doc -> Doc
<+> String -> Doc
text "preceding"
>     fpd (Following e :: ScalarExpr
e) = Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
e Doc -> Doc -> Doc
<+> String -> Doc
text "following"

> scalarExpr dia :: Dialect
dia (SpecialOp nm :: [Name]
nm [a :: ScalarExpr
a,b :: ScalarExpr
b,c :: ScalarExpr
c]) | [Name]
nm [Name] -> [[Name]] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [[Maybe (String, String) -> String -> Name
Name Maybe (String, String)
forall a. Maybe a
Nothing "between"]
>                                                  ,[Maybe (String, String) -> String -> Name
Name Maybe (String, String)
forall a. Maybe a
Nothing "not between"]] =
>   [Doc] -> Doc
sep [Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
dia ScalarExpr
a
>       ,[Name] -> Doc
names [Name]
nm Doc -> Doc -> Doc
<+> Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
dia ScalarExpr
b
>       ,Int -> Doc -> Doc
nest (String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Name] -> String
unnames [Name]
nm) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1) (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ String -> Doc
text "and" Doc -> Doc -> Doc
<+> Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
dia ScalarExpr
c]

> scalarExpr d :: Dialect
d (SpecialOp [Name Nothing "rowctor"] as :: [ScalarExpr]
as) =
>     Doc -> Doc
parens (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ [Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (ScalarExpr -> Doc) -> [ScalarExpr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d) [ScalarExpr]
as

> scalarExpr d :: Dialect
d (SpecialOp nm :: [Name]
nm es :: [ScalarExpr]
es) =
>   [Name] -> Doc
names [Name]
nm Doc -> Doc -> Doc
<+> Doc -> Doc
parens ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (ScalarExpr -> Doc) -> [ScalarExpr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d) [ScalarExpr]
es)

> scalarExpr d :: Dialect
d (SpecialOpK nm :: [Name]
nm fs :: Maybe ScalarExpr
fs as :: [(String, ScalarExpr)]
as) =
>     [Name] -> Doc
names [Name]
nm Doc -> Doc -> Doc
<> Doc -> Doc
parens ([Doc] -> Doc
sep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ [Maybe Doc] -> [Doc]
forall a. [Maybe a] -> [a]
catMaybes
>         ((ScalarExpr -> Doc) -> Maybe ScalarExpr -> Maybe Doc
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d) Maybe ScalarExpr
fs
>          Maybe Doc -> [Maybe Doc] -> [Maybe Doc]
forall a. a -> [a] -> [a]
: ((String, ScalarExpr) -> Maybe Doc)
-> [(String, ScalarExpr)] -> [Maybe Doc]
forall a b. (a -> b) -> [a] -> [b]
map (\(n :: String
n,e :: ScalarExpr
e) -> Doc -> Maybe Doc
forall a. a -> Maybe a
Just (String -> Doc
text String
n Doc -> Doc -> Doc
<+> Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
e)) [(String, ScalarExpr)]
as))

> scalarExpr d :: Dialect
d (PrefixOp f :: [Name]
f e :: ScalarExpr
e) = [Name] -> Doc
names [Name]
f Doc -> Doc -> Doc
<+> Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
e
> scalarExpr d :: Dialect
d (PostfixOp f :: [Name]
f e :: ScalarExpr
e) = Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
e Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
f
> scalarExpr d :: Dialect
d e :: ScalarExpr
e@(BinOp _ op :: [Name]
op _) | [Name]
op [Name] -> [[Name]] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [[Maybe (String, String) -> String -> Name
Name Maybe (String, String)
forall a. Maybe a
Nothing "and"]
>                                          ,[Maybe (String, String) -> String -> Name
Name Maybe (String, String)
forall a. Maybe a
Nothing "or"]] =
>     -- special case for and, or, get all the ands so we can vcat them
>     -- nicely
>     case ScalarExpr -> [ScalarExpr]
ands ScalarExpr
e of
>       (e' :: ScalarExpr
e':es :: [ScalarExpr]
es) -> [Doc] -> Doc
vcat (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
e'
>                        Doc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
: (ScalarExpr -> Doc) -> [ScalarExpr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (([Name] -> Doc
names [Name]
op Doc -> Doc -> Doc
<+>) (Doc -> Doc) -> (ScalarExpr -> Doc) -> ScalarExpr -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d) [ScalarExpr]
es)
>       [] -> Doc
empty -- shouldn't be possible
>   where
>     ands :: ScalarExpr -> [ScalarExpr]
ands (BinOp a :: ScalarExpr
a op' :: [Name]
op' b :: ScalarExpr
b) | [Name]
op [Name] -> [Name] -> Bool
forall a. Eq a => a -> a -> Bool
== [Name]
op' = ScalarExpr -> [ScalarExpr]
ands ScalarExpr
a [ScalarExpr] -> [ScalarExpr] -> [ScalarExpr]
forall a. [a] -> [a] -> [a]
++ ScalarExpr -> [ScalarExpr]
ands ScalarExpr
b
>     ands x :: ScalarExpr
x = [ScalarExpr
x]
> -- special case for . we don't use whitespace
> scalarExpr d :: Dialect
d (BinOp e0 :: ScalarExpr
e0 [Name Nothing "."] e1 :: ScalarExpr
e1) =
>     Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
e0 Doc -> Doc -> Doc
<> String -> Doc
text "." Doc -> Doc -> Doc
<> Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
e1
> scalarExpr d :: Dialect
d (BinOp e0 :: ScalarExpr
e0 f :: [Name]
f e1 :: ScalarExpr
e1) =
>     Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
e0 Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
f Doc -> Doc -> Doc
<+> Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
e1

> scalarExpr dia :: Dialect
dia (Case t :: Maybe ScalarExpr
t ws :: [([ScalarExpr], ScalarExpr)]
ws els :: Maybe ScalarExpr
els) =
>     [Doc] -> Doc
sep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ [String -> Doc
text "case" Doc -> Doc -> Doc
<+> (ScalarExpr -> Doc) -> Maybe ScalarExpr -> Doc
forall a. (a -> Doc) -> Maybe a -> Doc
me (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
dia) Maybe ScalarExpr
t]
>           [Doc] -> [Doc] -> [Doc]
forall a. [a] -> [a] -> [a]
++ (([ScalarExpr], ScalarExpr) -> Doc)
-> [([ScalarExpr], ScalarExpr)] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map ([ScalarExpr], ScalarExpr) -> Doc
w [([ScalarExpr], ScalarExpr)]
ws
>           [Doc] -> [Doc] -> [Doc]
forall a. [a] -> [a] -> [a]
++ Maybe Doc -> [Doc]
forall a. Maybe a -> [a]
maybeToList ((ScalarExpr -> Doc) -> Maybe ScalarExpr -> Maybe Doc
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ScalarExpr -> Doc
e Maybe ScalarExpr
els)
>           [Doc] -> [Doc] -> [Doc]
forall a. [a] -> [a] -> [a]
++ [String -> Doc
text "end"]
>   where
>     w :: ([ScalarExpr], ScalarExpr) -> Doc
w (t0 :: [ScalarExpr]
t0,t1 :: ScalarExpr
t1) =
>       String -> Doc
text "when" Doc -> Doc -> Doc
<+> Int -> Doc -> Doc
nest 5 ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (ScalarExpr -> Doc) -> [ScalarExpr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
dia) [ScalarExpr]
t0)
>       Doc -> Doc -> Doc
<+> String -> Doc
text "then" Doc -> Doc -> Doc
<+> Int -> Doc -> Doc
nest 5 (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
dia ScalarExpr
t1)
>     e :: ScalarExpr -> Doc
e el :: ScalarExpr
el = String -> Doc
text "else" Doc -> Doc -> Doc
<+> Int -> Doc -> Doc
nest 5 (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
dia ScalarExpr
el)
> scalarExpr d :: Dialect
d (Parens e :: ScalarExpr
e) = Doc -> Doc
parens (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
e
> scalarExpr d :: Dialect
d (Cast e :: ScalarExpr
e tn :: TypeName
tn) =
>     String -> Doc
text "cast" Doc -> Doc -> Doc
<> Doc -> Doc
parens ([Doc] -> Doc
sep [Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
e
>                                ,String -> Doc
text "as"
>                                ,TypeName -> Doc
typeName TypeName
tn])

> scalarExpr _ (TypedLit tn :: TypeName
tn s :: String
s) =
>     TypeName -> Doc
typeName TypeName
tn Doc -> Doc -> Doc
<+> Doc -> Doc
quotes (String -> Doc
text String
s)

> scalarExpr d :: Dialect
d (SubQueryExpr ty :: SubQueryExprType
ty qe :: QueryExpr
qe) =
>     (case SubQueryExprType
ty of
>         SqSq -> Doc
empty
>         SqExists -> String -> Doc
text "exists"
>         SqUnique -> String -> Doc
text "unique"
>     ) Doc -> Doc -> Doc
<+> Doc -> Doc
parens (Dialect -> QueryExpr -> Doc
queryExpr Dialect
d QueryExpr
qe)

> scalarExpr d :: Dialect
d (QuantifiedComparison v :: ScalarExpr
v c :: [Name]
c cp :: CompPredQuantifier
cp sq :: QueryExpr
sq) =
>     Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
v
>     Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
c
>     Doc -> Doc -> Doc
<+> (String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ case CompPredQuantifier
cp of
>              CPAny -> "any"
>              CPSome -> "some"
>              CPAll -> "all")
>     Doc -> Doc -> Doc
<+> Doc -> Doc
parens (Dialect -> QueryExpr -> Doc
queryExpr Dialect
d QueryExpr
sq)

> scalarExpr d :: Dialect
d (Match v :: ScalarExpr
v u :: Bool
u sq :: QueryExpr
sq) =
>     Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
v
>     Doc -> Doc -> Doc
<+> String -> Doc
text "match"
>     Doc -> Doc -> Doc
<+> (if Bool
u then String -> Doc
text "unique" else Doc
empty)
>     Doc -> Doc -> Doc
<+> Doc -> Doc
parens (Dialect -> QueryExpr -> Doc
queryExpr Dialect
d QueryExpr
sq)

> scalarExpr d :: Dialect
d (In b :: Bool
b se :: ScalarExpr
se x :: InPredValue
x) =
>     Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
se Doc -> Doc -> Doc
<+>
>     (if Bool
b then Doc
empty else String -> Doc
text "not")
>     Doc -> Doc -> Doc
<+> String -> Doc
text "in"
>     Doc -> Doc -> Doc
<+> Doc -> Doc
parens (Int -> Doc -> Doc
nest (if Bool
b then 3 else 7) (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$
>                  case InPredValue
x of
>                      InList es :: [ScalarExpr]
es -> [Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (ScalarExpr -> Doc) -> [ScalarExpr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d) [ScalarExpr]
es
>                      InQueryExpr qe :: QueryExpr
qe -> Dialect -> QueryExpr -> Doc
queryExpr Dialect
d QueryExpr
qe)

> scalarExpr d :: Dialect
d (Array v :: ScalarExpr
v es :: [ScalarExpr]
es) =
>     Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
v Doc -> Doc -> Doc
<> Doc -> Doc
brackets ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (ScalarExpr -> Doc) -> [ScalarExpr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d) [ScalarExpr]
es)

> scalarExpr d :: Dialect
d (ArrayCtor q :: QueryExpr
q) =
>     String -> Doc
text "array" Doc -> Doc -> Doc
<> Doc -> Doc
parens (Dialect -> QueryExpr -> Doc
queryExpr Dialect
d QueryExpr
q)

> scalarExpr d :: Dialect
d (MultisetCtor es :: [ScalarExpr]
es) =
>     String -> Doc
text "multiset" Doc -> Doc -> Doc
<> Doc -> Doc
brackets ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (ScalarExpr -> Doc) -> [ScalarExpr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d) [ScalarExpr]
es)

> scalarExpr d :: Dialect
d (MultisetQueryCtor q :: QueryExpr
q) =
>     String -> Doc
text "multiset" Doc -> Doc -> Doc
<> Doc -> Doc
parens (Dialect -> QueryExpr -> Doc
queryExpr Dialect
d QueryExpr
q)

> scalarExpr d :: Dialect
d (MultisetBinOp a :: ScalarExpr
a c :: SetOperatorName
c q :: SetQuantifier
q b :: ScalarExpr
b) =
>     [Doc] -> Doc
sep
>     [Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
a
>     ,String -> Doc
text "multiset"
>     ,String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ case SetOperatorName
c of
>                 Union -> "union"
>                 Intersect -> "intersect"
>                 Except -> "except"
>     ,case SetQuantifier
q of
>          SQDefault -> Doc
empty
>          All -> String -> Doc
text "all"
>          Distinct -> String -> Doc
text "distinct"
>     ,Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
b]

> {-scalarExpr d (Escape v e) =
>     scalarExpr d v <+> text "escape" <+> text [e]

> scalarExpr d (UEscape v e) =
>     scalarExpr d v <+> text "uescape" <+> text [e]-}

> scalarExpr d :: Dialect
d (Collate v :: ScalarExpr
v c :: [Name]
c) =
>     Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
v Doc -> Doc -> Doc
<+> String -> Doc
text "collate" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
c

> scalarExpr _ (NextValueFor ns :: [Name]
ns) =
>     String -> Doc
text "next value for" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
ns

> scalarExpr d :: Dialect
d (VEComment cmt :: [Comment]
cmt v :: ScalarExpr
v) =
>     [Doc] -> Doc
vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (Comment -> Doc) -> [Comment] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Comment -> Doc
comment [Comment]
cmt [Doc] -> [Doc] -> [Doc]
forall a. [a] -> [a] -> [a]
++ [Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
v]

> scalarExpr _ (OdbcLiteral t :: OdbcLiteralType
t s :: String
s) =
>     String -> Doc
text "{" Doc -> Doc -> Doc
<> OdbcLiteralType -> Doc
lt OdbcLiteralType
t Doc -> Doc -> Doc
<+> Doc -> Doc
quotes (String -> Doc
text String
s) Doc -> Doc -> Doc
<> String -> Doc
text "}"
>   where
>     lt :: OdbcLiteralType -> Doc
lt OLDate = String -> Doc
text "d"
>     lt OLTime = String -> Doc
text "t"
>     lt OLTimestamp = String -> Doc
text "ts"

> scalarExpr d :: Dialect
d (OdbcFunc e :: ScalarExpr
e) =
>     String -> Doc
text "{fn" Doc -> Doc -> Doc
<+> Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
e Doc -> Doc -> Doc
<> String -> Doc
text "}"

> unname :: Name -> String
> unname :: Name -> String
unname (Name Nothing n :: String
n) = String
n
> unname (Name (Just (s :: String
s,e :: String
e)) n :: String
n) =
>     String
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
n String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
e

> unnames :: [Name] -> String
> unnames :: [Name] -> String
unnames ns :: [Name]
ns = String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate "." ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (Name -> String) -> [Name] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Name -> String
unname [Name]
ns


> name :: Name -> Doc
> name :: Name -> Doc
name (Name Nothing n :: String
n) = String -> Doc
text String
n
> name (Name (Just (s :: String
s,e :: String
e)) n :: String
n) = String -> Doc
text String
s Doc -> Doc -> Doc
<> String -> Doc
text String
n Doc -> Doc -> Doc
<> String -> Doc
text String
e

> names :: [Name] -> Doc
> names :: [Name] -> Doc
names ns :: [Name]
ns = [Doc] -> Doc
hcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ Doc -> [Doc] -> [Doc]
punctuate (String -> Doc
text ".") ([Doc] -> [Doc]) -> [Doc] -> [Doc]
forall a b. (a -> b) -> a -> b
$ (Name -> Doc) -> [Name] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Doc
name [Name]
ns

> typeName :: TypeName -> Doc
> typeName :: TypeName -> Doc
typeName (TypeName t :: [Name]
t) = [Name] -> Doc
names [Name]
t
> typeName (PrecTypeName t :: [Name]
t a :: Integer
a) = [Name] -> Doc
names [Name]
t Doc -> Doc -> Doc
<+> Doc -> Doc
parens (String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ Integer -> String
forall a. Show a => a -> String
show Integer
a)
> typeName (PrecScaleTypeName t :: [Name]
t a :: Integer
a b :: Integer
b) =
>     [Name] -> Doc
names [Name]
t Doc -> Doc -> Doc
<+> Doc -> Doc
parens (String -> Doc
text (Integer -> String
forall a. Show a => a -> String
show Integer
a) Doc -> Doc -> Doc
<+> Doc
comma Doc -> Doc -> Doc
<+> String -> Doc
text (Integer -> String
forall a. Show a => a -> String
show Integer
b))
> typeName (PrecLengthTypeName t :: [Name]
t i :: Integer
i m :: Maybe PrecMultiplier
m u :: Maybe PrecUnits
u) =
>     [Name] -> Doc
names [Name]
t
>     Doc -> Doc -> Doc
<> Doc -> Doc
parens (String -> Doc
text (Integer -> String
forall a. Show a => a -> String
show Integer
i)
>                Doc -> Doc -> Doc
<> (PrecMultiplier -> Doc) -> Maybe PrecMultiplier -> Doc
forall a. (a -> Doc) -> Maybe a -> Doc
me (\x :: PrecMultiplier
x -> case PrecMultiplier
x of
>                            PrecK -> String -> Doc
text "K"
>                            PrecM -> String -> Doc
text "M"
>                            PrecG -> String -> Doc
text "G"
>                            PrecT -> String -> Doc
text "T"
>                            PrecP -> String -> Doc
text "P") Maybe PrecMultiplier
m
>                Doc -> Doc -> Doc
<+> (PrecUnits -> Doc) -> Maybe PrecUnits -> Doc
forall a. (a -> Doc) -> Maybe a -> Doc
me (\x :: PrecUnits
x -> case PrecUnits
x of
>                        PrecCharacters -> String -> Doc
text "CHARACTERS"
>                        PrecOctets -> String -> Doc
text "OCTETS") Maybe PrecUnits
u)
> typeName (CharTypeName t :: [Name]
t i :: Maybe Integer
i cs :: [Name]
cs col :: [Name]
col) =
>     [Name] -> Doc
names [Name]
t
>     Doc -> Doc -> Doc
<> (Integer -> Doc) -> Maybe Integer -> Doc
forall a. (a -> Doc) -> Maybe a -> Doc
me (\x :: Integer
x -> Doc -> Doc
parens (String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ Integer -> String
forall a. Show a => a -> String
show Integer
x)) Maybe Integer
i
>     Doc -> Doc -> Doc
<+> (if [Name] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Name]
cs
>          then Doc
empty
>          else String -> Doc
text "character set" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
cs)
>     Doc -> Doc -> Doc
<+> (if [Name] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Name]
col
>          then Doc
empty
>          else String -> Doc
text "collate" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
col)
> typeName (TimeTypeName t :: [Name]
t i :: Maybe Integer
i tz :: Bool
tz) =
>     [Name] -> Doc
names [Name]
t
>     Doc -> Doc -> Doc
<> (Integer -> Doc) -> Maybe Integer -> Doc
forall a. (a -> Doc) -> Maybe a -> Doc
me (\x :: Integer
x -> Doc -> Doc
parens (String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ Integer -> String
forall a. Show a => a -> String
show Integer
x)) Maybe Integer
i
>     Doc -> Doc -> Doc
<+> String -> Doc
text (if Bool
tz
>               then "with time zone"
>               else "without time zone")
> typeName (RowTypeName cs :: [(Name, TypeName)]
cs) =
>     String -> Doc
text "row" Doc -> Doc -> Doc
<> Doc -> Doc
parens ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ ((Name, TypeName) -> Doc) -> [(Name, TypeName)] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Name, TypeName) -> Doc
f [(Name, TypeName)]
cs)
>   where
>     f :: (Name, TypeName) -> Doc
f (n :: Name
n,t :: TypeName
t) = Name -> Doc
name Name
n Doc -> Doc -> Doc
<+> TypeName -> Doc
typeName TypeName
t
> typeName (IntervalTypeName f :: IntervalTypeField
f t :: Maybe IntervalTypeField
t) =
>     String -> Doc
text "interval"
>     Doc -> Doc -> Doc
<+> IntervalTypeField -> Doc
intervalTypeField IntervalTypeField
f
>     Doc -> Doc -> Doc
<+> (IntervalTypeField -> Doc) -> Maybe IntervalTypeField -> Doc
forall a. (a -> Doc) -> Maybe a -> Doc
me (\x :: IntervalTypeField
x -> String -> Doc
text "to" Doc -> Doc -> Doc
<+> IntervalTypeField -> Doc
intervalTypeField IntervalTypeField
x) Maybe IntervalTypeField
t

> typeName (ArrayTypeName tn :: TypeName
tn sz :: Maybe Integer
sz) =
>     TypeName -> Doc
typeName TypeName
tn Doc -> Doc -> Doc
<+> String -> Doc
text "array" Doc -> Doc -> Doc
<+> (Integer -> Doc) -> Maybe Integer -> Doc
forall a. (a -> Doc) -> Maybe a -> Doc
me (Doc -> Doc
brackets (Doc -> Doc) -> (Integer -> Doc) -> Integer -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Doc
text (String -> Doc) -> (Integer -> String) -> Integer -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> String
forall a. Show a => a -> String
show) Maybe Integer
sz

> typeName (MultisetTypeName tn :: TypeName
tn) =
>     TypeName -> Doc
typeName TypeName
tn Doc -> Doc -> Doc
<+> String -> Doc
text "multiset"

> intervalTypeField :: IntervalTypeField -> Doc
> intervalTypeField :: IntervalTypeField -> Doc
intervalTypeField (Itf n :: String
n p :: Maybe (Integer, Maybe Integer)
p) =
>     String -> Doc
text String
n
>     Doc -> Doc -> Doc
<+> ((Integer, Maybe Integer) -> Doc)
-> Maybe (Integer, Maybe Integer) -> Doc
forall a. (a -> Doc) -> Maybe a -> Doc
me (\(x :: Integer
x,x1 :: Maybe Integer
x1) ->
>              Doc -> Doc
parens (String -> Doc
text (Integer -> String
forall a. Show a => a -> String
show Integer
x)
>                      Doc -> Doc -> Doc
<+> (Integer -> Doc) -> Maybe Integer -> Doc
forall a. (a -> Doc) -> Maybe a -> Doc
me (\y :: Integer
y -> ([Doc] -> Doc
sep [Doc
comma,String -> Doc
text (Integer -> String
forall a. Show a => a -> String
show Integer
y)])) Maybe Integer
x1)) Maybe (Integer, Maybe Integer)
p


= query expressions

> queryExpr :: Dialect -> QueryExpr -> Doc
> queryExpr :: Dialect -> QueryExpr -> Doc
queryExpr dia :: Dialect
dia (Select d :: SetQuantifier
d sl :: [(ScalarExpr, Maybe Name)]
sl fr :: [TableRef]
fr wh :: Maybe ScalarExpr
wh gb :: [GroupingExpr]
gb hv :: Maybe ScalarExpr
hv od :: [SortSpec]
od off :: Maybe ScalarExpr
off fe :: Maybe ScalarExpr
fe) =
>   [Doc] -> Doc
sep [String -> Doc
text "select"
>       ,case SetQuantifier
d of
>           SQDefault -> Doc
empty
>           All -> String -> Doc
text "all"
>           Distinct -> String -> Doc
text "distinct"
>       ,Int -> Doc -> Doc
nest 7 (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ [Doc] -> Doc
sep [Dialect -> [(ScalarExpr, Maybe Name)] -> Doc
selectList Dialect
dia [(ScalarExpr, Maybe Name)]
sl]
>       ,Dialect -> [TableRef] -> Doc
from Dialect
dia [TableRef]
fr
>       ,Dialect -> String -> Maybe ScalarExpr -> Doc
maybeScalarExpr Dialect
dia "where" Maybe ScalarExpr
wh
>       ,Dialect -> [GroupingExpr] -> Doc
grpBy Dialect
dia [GroupingExpr]
gb
>       ,Dialect -> String -> Maybe ScalarExpr -> Doc
maybeScalarExpr Dialect
dia "having" Maybe ScalarExpr
hv
>       ,Dialect -> [SortSpec] -> Doc
orderBy Dialect
dia [SortSpec]
od
>       ,(ScalarExpr -> Doc) -> Maybe ScalarExpr -> Doc
forall a. (a -> Doc) -> Maybe a -> Doc
me (\e :: ScalarExpr
e -> String -> Doc
text "offset" Doc -> Doc -> Doc
<+> Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
dia ScalarExpr
e Doc -> Doc -> Doc
<+> String -> Doc
text "rows") Maybe ScalarExpr
off
>       ,Doc
fetchFirst
>       ]
>   where
>     fetchFirst :: Doc
fetchFirst =
>       (ScalarExpr -> Doc) -> Maybe ScalarExpr -> Doc
forall a. (a -> Doc) -> Maybe a -> Doc
me (\e :: ScalarExpr
e -> if Dialect -> Bool
diLimit Dialect
dia
>                 then String -> Doc
text "limit" Doc -> Doc -> Doc
<+> Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
dia ScalarExpr
e
>                 else String -> Doc
text "fetch first" Doc -> Doc -> Doc
<+> Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
dia ScalarExpr
e
>                      Doc -> Doc -> Doc
<+> String -> Doc
text "rows only") Maybe ScalarExpr
fe

> queryExpr dia :: Dialect
dia (QueryExprSetOp q1 :: QueryExpr
q1 ct :: SetOperatorName
ct d :: SetQuantifier
d c :: Corresponding
c q2 :: QueryExpr
q2) =
>   [Doc] -> Doc
sep [Dialect -> QueryExpr -> Doc
queryExpr Dialect
dia QueryExpr
q1
>       ,String -> Doc
text (case SetOperatorName
ct of
>                 Union -> "union"
>                 Intersect -> "intersect"
>                 Except -> "except")
>        Doc -> Doc -> Doc
<+> case SetQuantifier
d of
>                SQDefault -> Doc
empty
>                All -> String -> Doc
text "all"
>                Distinct -> String -> Doc
text "distinct"
>        Doc -> Doc -> Doc
<+> case Corresponding
c of
>                Corresponding -> String -> Doc
text "corresponding"
>                Respectively -> Doc
empty
>       ,Dialect -> QueryExpr -> Doc
queryExpr Dialect
dia QueryExpr
q2]
> queryExpr d :: Dialect
d (With rc :: Bool
rc withs :: [(Alias, QueryExpr)]
withs qe :: QueryExpr
qe) =
>   String -> Doc
text "with" Doc -> Doc -> Doc
<+> (if Bool
rc then String -> Doc
text "recursive" else Doc
empty)
>   Doc -> Doc -> Doc
<+> [Doc] -> Doc
vcat [Int -> Doc -> Doc
nest 5
>             ([Doc] -> Doc
vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ Doc -> [Doc] -> [Doc]
punctuate Doc
comma ([Doc] -> [Doc]) -> [Doc] -> [Doc]
forall a b. (a -> b) -> a -> b
$ (((Alias, QueryExpr) -> Doc) -> [(Alias, QueryExpr)] -> [Doc])
-> [(Alias, QueryExpr)] -> ((Alias, QueryExpr) -> Doc) -> [Doc]
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((Alias, QueryExpr) -> Doc) -> [(Alias, QueryExpr)] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map [(Alias, QueryExpr)]
withs (((Alias, QueryExpr) -> Doc) -> [Doc])
-> ((Alias, QueryExpr) -> Doc) -> [Doc]
forall a b. (a -> b) -> a -> b
$ \(n :: Alias
n,q :: QueryExpr
q) ->
>              Alias -> Doc
withAlias Alias
n Doc -> Doc -> Doc
<+> String -> Doc
text "as" Doc -> Doc -> Doc
<+> Doc -> Doc
parens (Dialect -> QueryExpr -> Doc
queryExpr Dialect
d QueryExpr
q))
>            ,Dialect -> QueryExpr -> Doc
queryExpr Dialect
d QueryExpr
qe]
>   where
>     withAlias :: Alias -> Doc
withAlias (Alias nm :: Name
nm cols :: Maybe [Name]
cols) = Name -> Doc
name Name
nm
>                                 Doc -> Doc -> Doc
<+> ([Name] -> Doc) -> Maybe [Name] -> Doc
forall a. (a -> Doc) -> Maybe a -> Doc
me (Doc -> Doc
parens (Doc -> Doc) -> ([Name] -> Doc) -> [Name] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Doc] -> Doc
commaSep ([Doc] -> Doc) -> ([Name] -> [Doc]) -> [Name] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Name -> Doc) -> [Name] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Doc
name) Maybe [Name]
cols


> queryExpr d :: Dialect
d (Values vs :: [[ScalarExpr]]
vs) =
>     String -> Doc
text "values"
>     Doc -> Doc -> Doc
<+> Int -> Doc -> Doc
nest 7 ([Doc] -> Doc
commaSep (([ScalarExpr] -> Doc) -> [[ScalarExpr]] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Doc -> Doc
parens (Doc -> Doc) -> ([ScalarExpr] -> Doc) -> [ScalarExpr] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Doc] -> Doc
commaSep ([Doc] -> Doc) -> ([ScalarExpr] -> [Doc]) -> [ScalarExpr] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ScalarExpr -> Doc) -> [ScalarExpr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d)) [[ScalarExpr]]
vs))
> queryExpr _ (Table t :: [Name]
t) = String -> Doc
text "table" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
t
> queryExpr d :: Dialect
d (QEComment cmt :: [Comment]
cmt v :: QueryExpr
v) =
>     [Doc] -> Doc
vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (Comment -> Doc) -> [Comment] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Comment -> Doc
comment [Comment]
cmt [Doc] -> [Doc] -> [Doc]
forall a. [a] -> [a] -> [a]
++ [Dialect -> QueryExpr -> Doc
queryExpr Dialect
d QueryExpr
v]


> alias :: Alias -> Doc
> alias :: Alias -> Doc
alias (Alias nm :: Name
nm cols :: Maybe [Name]
cols) =
>     String -> Doc
text "as" Doc -> Doc -> Doc
<+> Name -> Doc
name Name
nm
>     Doc -> Doc -> Doc
<+> ([Name] -> Doc) -> Maybe [Name] -> Doc
forall a. (a -> Doc) -> Maybe a -> Doc
me (Doc -> Doc
parens (Doc -> Doc) -> ([Name] -> Doc) -> [Name] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Doc] -> Doc
commaSep ([Doc] -> Doc) -> ([Name] -> [Doc]) -> [Name] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Name -> Doc) -> [Name] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Doc
name) Maybe [Name]
cols

> selectList :: Dialect -> [(ScalarExpr,Maybe Name)] -> Doc
> selectList :: Dialect -> [(ScalarExpr, Maybe Name)] -> Doc
selectList d :: Dialect
d is :: [(ScalarExpr, Maybe Name)]
is = [Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ ((ScalarExpr, Maybe Name) -> Doc)
-> [(ScalarExpr, Maybe Name)] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (ScalarExpr, Maybe Name) -> Doc
si [(ScalarExpr, Maybe Name)]
is
>   where
>     si :: (ScalarExpr, Maybe Name) -> Doc
si (e :: ScalarExpr
e,al :: Maybe Name
al) = Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
e Doc -> Doc -> Doc
<+> (Name -> Doc) -> Maybe Name -> Doc
forall a. (a -> Doc) -> Maybe a -> Doc
me Name -> Doc
als Maybe Name
al
>     als :: Name -> Doc
als al :: Name
al = String -> Doc
text "as" Doc -> Doc -> Doc
<+> Name -> Doc
name Name
al

> from :: Dialect -> [TableRef] -> Doc
> from :: Dialect -> [TableRef] -> Doc
from _ [] = Doc
empty
> from d :: Dialect
d ts :: [TableRef]
ts =
>     [Doc] -> Doc
sep [String -> Doc
text "from"
>         ,Int -> Doc -> Doc
nest 5 (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ [Doc] -> Doc
vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ Doc -> [Doc] -> [Doc]
punctuate Doc
comma ([Doc] -> [Doc]) -> [Doc] -> [Doc]
forall a b. (a -> b) -> a -> b
$ (TableRef -> Doc) -> [TableRef] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map TableRef -> Doc
tr [TableRef]
ts]
>   where
>     tr :: TableRef -> Doc
tr (TRSimple t :: [Name]
t) = [Name] -> Doc
names [Name]
t
>     tr (TRLateral t :: TableRef
t) = String -> Doc
text "lateral" Doc -> Doc -> Doc
<+> TableRef -> Doc
tr TableRef
t
>     tr (TRFunction f :: [Name]
f as :: [ScalarExpr]
as) =
>         [Name] -> Doc
names [Name]
f Doc -> Doc -> Doc
<> Doc -> Doc
parens ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (ScalarExpr -> Doc) -> [ScalarExpr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d) [ScalarExpr]
as)
>     tr (TRAlias t :: TableRef
t a :: Alias
a) = [Doc] -> Doc
sep [TableRef -> Doc
tr TableRef
t, Alias -> Doc
alias Alias
a]
>     tr (TRParens t :: TableRef
t) = Doc -> Doc
parens (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ TableRef -> Doc
tr TableRef
t
>     tr (TRQueryExpr q :: QueryExpr
q) = Doc -> Doc
parens (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Dialect -> QueryExpr -> Doc
queryExpr Dialect
d QueryExpr
q
>     tr (TRJoin t0 :: TableRef
t0 b :: Bool
b jt :: JoinType
jt t1 :: TableRef
t1 jc :: Maybe JoinCondition
jc) =
>        [Doc] -> Doc
sep [TableRef -> Doc
tr TableRef
t0
>            ,if Bool
b then String -> Doc
text "natural" else Doc
empty
>            ,JoinType -> Doc
joinText JoinType
jt Doc -> Doc -> Doc
<+> TableRef -> Doc
tr TableRef
t1
>            ,Maybe JoinCondition -> Doc
joinCond Maybe JoinCondition
jc]
>     tr (TROdbc t :: TableRef
t) = String -> Doc
text "{oj" Doc -> Doc -> Doc
<+> TableRef -> Doc
tr TableRef
t Doc -> Doc -> Doc
<+> String -> Doc
text "}"
>     joinText :: JoinType -> Doc
joinText jt :: JoinType
jt =
>       [Doc] -> Doc
sep [case JoinType
jt of
>               JInner -> String -> Doc
text "inner"
>               JLeft -> String -> Doc
text "left"
>               JRight -> String -> Doc
text "right"
>               JFull -> String -> Doc
text "full"
>               JCross -> String -> Doc
text "cross"
>           ,String -> Doc
text "join"]
>     joinCond :: Maybe JoinCondition -> Doc
joinCond (Just (JoinOn e :: ScalarExpr
e)) = String -> Doc
text "on" Doc -> Doc -> Doc
<+> Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
e
>     joinCond (Just (JoinUsing es :: [Name]
es)) =
>         String -> Doc
text "using" Doc -> Doc -> Doc
<+> Doc -> Doc
parens ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (Name -> Doc) -> [Name] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Doc
name [Name]
es)
>     joinCond Nothing = Doc
empty

> maybeScalarExpr :: Dialect -> String -> Maybe ScalarExpr -> Doc
> maybeScalarExpr :: Dialect -> String -> Maybe ScalarExpr -> Doc
maybeScalarExpr d :: Dialect
d k :: String
k = (ScalarExpr -> Doc) -> Maybe ScalarExpr -> Doc
forall a. (a -> Doc) -> Maybe a -> Doc
me
>       (\e :: ScalarExpr
e -> [Doc] -> Doc
sep [String -> Doc
text String
k
>                  ,Int -> Doc -> Doc
nest (String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
k Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1) (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
e])

> grpBy :: Dialect -> [GroupingExpr] -> Doc
> grpBy :: Dialect -> [GroupingExpr] -> Doc
grpBy _ [] = Doc
empty
> grpBy d :: Dialect
d gs :: [GroupingExpr]
gs = [Doc] -> Doc
sep [String -> Doc
text "group by"
>                ,Int -> Doc -> Doc
nest 9 (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ [Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (GroupingExpr -> Doc) -> [GroupingExpr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map GroupingExpr -> Doc
ge [GroupingExpr]
gs]
>   where
>     ge :: GroupingExpr -> Doc
ge (SimpleGroup e :: ScalarExpr
e) = Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
e
>     ge (GroupingParens g :: [GroupingExpr]
g) = Doc -> Doc
parens ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (GroupingExpr -> Doc) -> [GroupingExpr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map GroupingExpr -> Doc
ge [GroupingExpr]
g)
>     ge (Cube es :: [GroupingExpr]
es) = String -> Doc
text "cube" Doc -> Doc -> Doc
<> Doc -> Doc
parens ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (GroupingExpr -> Doc) -> [GroupingExpr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map GroupingExpr -> Doc
ge [GroupingExpr]
es)
>     ge (Rollup es :: [GroupingExpr]
es) = String -> Doc
text "rollup" Doc -> Doc -> Doc
<> Doc -> Doc
parens ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (GroupingExpr -> Doc) -> [GroupingExpr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map GroupingExpr -> Doc
ge [GroupingExpr]
es)
>     ge (GroupingSets es :: [GroupingExpr]
es) = String -> Doc
text "grouping sets" Doc -> Doc -> Doc
<> Doc -> Doc
parens ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (GroupingExpr -> Doc) -> [GroupingExpr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map GroupingExpr -> Doc
ge [GroupingExpr]
es)

> orderBy :: Dialect -> [SortSpec] -> Doc
> orderBy :: Dialect -> [SortSpec] -> Doc
orderBy _ [] = Doc
empty
> orderBy dia :: Dialect
dia os :: [SortSpec]
os = [Doc] -> Doc
sep [String -> Doc
text "order by"
>                  ,Int -> Doc -> Doc
nest 9 (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ [Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (SortSpec -> Doc) -> [SortSpec] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map SortSpec -> Doc
f [SortSpec]
os]
>   where
>     f :: SortSpec -> Doc
f (SortSpec e :: ScalarExpr
e d :: Direction
d n :: NullsOrder
n) =
>         Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
dia ScalarExpr
e
>         Doc -> Doc -> Doc
<+> (case Direction
d of
>                   Asc -> String -> Doc
text "asc"
>                   Desc -> String -> Doc
text "desc"
>                   DirDefault -> Doc
empty)
>         Doc -> Doc -> Doc
<+> (case NullsOrder
n of
>                 NullsOrderDefault -> Doc
empty
>                 NullsFirst -> String -> Doc
text "nulls" Doc -> Doc -> Doc
<+> String -> Doc
text "first"
>                 NullsLast -> String -> Doc
text "nulls" Doc -> Doc -> Doc
<+> String -> Doc
text "last")

= statements

> statement :: Dialect -> Statement -> Doc


== ddl

> statement :: Dialect -> Statement -> Doc
statement _ (CreateSchema nm :: [Name]
nm) =
>     String -> Doc
text "create" Doc -> Doc -> Doc
<+> String -> Doc
text "schema" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
nm

> statement d :: Dialect
d (CreateTable nm :: [Name]
nm cds :: [TableElement]
cds) =
>     String -> Doc
text "create" Doc -> Doc -> Doc
<+> String -> Doc
text "table" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
nm
>     Doc -> Doc -> Doc
<+> Doc -> Doc
parens ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (TableElement -> Doc) -> [TableElement] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map TableElement -> Doc
cd [TableElement]
cds)
>   where
>     cd :: TableElement -> Doc
cd (TableConstraintDef n :: Maybe [Name]
n con :: TableConstraint
con) =
>         Doc -> ([Name] -> Doc) -> Maybe [Name] -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\s :: [Name]
s -> String -> Doc
text "constraint" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
s) Maybe [Name]
n
>         Doc -> Doc -> Doc
<+> Dialect -> TableConstraint -> Doc
tableConstraint Dialect
d TableConstraint
con
>     cd (TableColumnDef cd' :: ColumnDef
cd') = Dialect -> ColumnDef -> Doc
columnDef Dialect
d ColumnDef
cd'

> statement d :: Dialect
d (AlterTable t :: [Name]
t act :: AlterTableAction
act) =
>     [String] -> Doc
texts ["alter","table"] Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
t
>     Doc -> Doc -> Doc
<+> Dialect -> AlterTableAction -> Doc
alterTableAction Dialect
d AlterTableAction
act

> statement _ (DropSchema nm :: [Name]
nm db :: DropBehaviour
db) =
>     String -> Doc
text "drop" Doc -> Doc -> Doc
<+> String -> Doc
text "schema" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
nm Doc -> Doc -> Doc
<+> DropBehaviour -> Doc
dropBehav DropBehaviour
db

> statement d :: Dialect
d (CreateDomain nm :: [Name]
nm ty :: TypeName
ty def :: Maybe ScalarExpr
def cs :: [(Maybe [Name], ScalarExpr)]
cs) =
>     String -> Doc
text "create" Doc -> Doc -> Doc
<+> String -> Doc
text "domain" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
nm
>     Doc -> Doc -> Doc
<+> TypeName -> Doc
typeName TypeName
ty
>     Doc -> Doc -> Doc
<+> Doc -> (ScalarExpr -> Doc) -> Maybe ScalarExpr -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\def' :: ScalarExpr
def' -> String -> Doc
text "default" Doc -> Doc -> Doc
<+> Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
def') Maybe ScalarExpr
def
>     Doc -> Doc -> Doc
<+> [Doc] -> Doc
sep (((Maybe [Name], ScalarExpr) -> Doc)
-> [(Maybe [Name], ScalarExpr)] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Maybe [Name], ScalarExpr) -> Doc
con [(Maybe [Name], ScalarExpr)]
cs)
>   where
>     con :: (Maybe [Name], ScalarExpr) -> Doc
con (cn :: Maybe [Name]
cn, e :: ScalarExpr
e) =
>         Doc -> ([Name] -> Doc) -> Maybe [Name] -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\cn' :: [Name]
cn' -> String -> Doc
text "constraint" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
cn') Maybe [Name]
cn
>         Doc -> Doc -> Doc
<+> String -> Doc
text "check" Doc -> Doc -> Doc
<> Doc -> Doc
parens (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
e)

> statement d :: Dialect
d (AlterDomain nm :: [Name]
nm act :: AlterDomainAction
act) =
>     [String] -> Doc
texts ["alter","domain"]
>     Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
nm
>     Doc -> Doc -> Doc
<+> AlterDomainAction -> Doc
a AlterDomainAction
act
>   where
>     a :: AlterDomainAction -> Doc
a (ADSetDefault v :: ScalarExpr
v) = [String] -> Doc
texts ["set","default"] Doc -> Doc -> Doc
<+> Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
v
>     a (AlterDomainAction
ADDropDefault) = [String] -> Doc
texts ["drop","default"]
>     a (ADAddConstraint cnm :: Maybe [Name]
cnm e :: ScalarExpr
e) =
>         String -> Doc
text "add"
>         Doc -> Doc -> Doc
<+> Doc -> ([Name] -> Doc) -> Maybe [Name] -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\cnm' :: [Name]
cnm' -> String -> Doc
text "constraint" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
cnm') Maybe [Name]
cnm
>         Doc -> Doc -> Doc
<+> String -> Doc
text "check" Doc -> Doc -> Doc
<> Doc -> Doc
parens (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
e)
>     a (ADDropConstraint cnm :: [Name]
cnm) = [String] -> Doc
texts ["drop", "constraint"]
>                                Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
cnm


> statement _ (DropDomain nm :: [Name]
nm db :: DropBehaviour
db) =
>     String -> Doc
text "drop" Doc -> Doc -> Doc
<+> String -> Doc
text "domain" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
nm Doc -> Doc -> Doc
<+> DropBehaviour -> Doc
dropBehav DropBehaviour
db

> statement _ (CreateSequence nm :: [Name]
nm sgos :: [SequenceGeneratorOption]
sgos) =
>   [String] -> Doc
texts ["create","sequence"] Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
nm
>   Doc -> Doc -> Doc
<+> [Doc] -> Doc
sep ((SequenceGeneratorOption -> Doc)
-> [SequenceGeneratorOption] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map SequenceGeneratorOption -> Doc
sequenceGeneratorOption [SequenceGeneratorOption]
sgos)

> statement _ (AlterSequence nm :: [Name]
nm sgos :: [SequenceGeneratorOption]
sgos) =
>   [String] -> Doc
texts ["alter","sequence"] Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
nm
>   Doc -> Doc -> Doc
<+> [Doc] -> Doc
sep ((SequenceGeneratorOption -> Doc)
-> [SequenceGeneratorOption] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map SequenceGeneratorOption -> Doc
sequenceGeneratorOption [SequenceGeneratorOption]
sgos)

> statement _ (DropSequence nm :: [Name]
nm db :: DropBehaviour
db) =
>     String -> Doc
text "drop" Doc -> Doc -> Doc
<+> String -> Doc
text "sequence" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
nm Doc -> Doc -> Doc
<+> DropBehaviour -> Doc
dropBehav DropBehaviour
db


> statement d :: Dialect
d (CreateAssertion nm :: [Name]
nm ex :: ScalarExpr
ex) =
>   [String] -> Doc
texts ["create","assertion"] Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
nm
>   Doc -> Doc -> Doc
<+> String -> Doc
text "check" Doc -> Doc -> Doc
<+> Doc -> Doc
parens (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
ex)

> statement _ (DropAssertion nm :: [Name]
nm db :: DropBehaviour
db) =
>     String -> Doc
text "drop" Doc -> Doc -> Doc
<+> String -> Doc
text "assertion" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
nm Doc -> Doc -> Doc
<+> DropBehaviour -> Doc
dropBehav DropBehaviour
db

== dml

> statement d :: Dialect
d (SelectStatement q :: QueryExpr
q) = Dialect -> QueryExpr -> Doc
queryExpr Dialect
d QueryExpr
q

> statement d :: Dialect
d (Delete t :: [Name]
t a :: Maybe Name
a w :: Maybe ScalarExpr
w) =
>     String -> Doc
text "delete" Doc -> Doc -> Doc
<+> String -> Doc
text "from"
>     Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
t Doc -> Doc -> Doc
<+> Doc -> (Name -> Doc) -> Maybe Name -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\x :: Name
x -> String -> Doc
text "as" Doc -> Doc -> Doc
<+> Name -> Doc
name Name
x) Maybe Name
a
>     Doc -> Doc -> Doc
<+> Dialect -> String -> Maybe ScalarExpr -> Doc
maybeScalarExpr Dialect
d "where" Maybe ScalarExpr
w

> statement _ (Truncate t :: [Name]
t ir :: IdentityRestart
ir) =
>     String -> Doc
text "truncate" Doc -> Doc -> Doc
<+> String -> Doc
text "table" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
t
>     Doc -> Doc -> Doc
<+> case IdentityRestart
ir of
>             DefaultIdentityRestart -> Doc
empty
>             ContinueIdentity -> String -> Doc
text "continue" Doc -> Doc -> Doc
<+> String -> Doc
text "identity"
>             RestartIdentity -> String -> Doc
text "restart" Doc -> Doc -> Doc
<+> String -> Doc
text "identity"

> statement d :: Dialect
d (Insert t :: [Name]
t cs :: Maybe [Name]
cs s :: InsertSource
s) =
>     String -> Doc
text "insert" Doc -> Doc -> Doc
<+> String -> Doc
text "into" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
t
>     Doc -> Doc -> Doc
<+> Doc -> ([Name] -> Doc) -> Maybe [Name] -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\cs' :: [Name]
cs' -> Doc -> Doc
parens ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (Name -> Doc) -> [Name] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Doc
name [Name]
cs')) Maybe [Name]
cs
>     Doc -> Doc -> Doc
<+> case InsertSource
s of
>             DefaultInsertValues -> String -> Doc
text "default" Doc -> Doc -> Doc
<+> String -> Doc
text "values"
>             InsertQuery q :: QueryExpr
q -> Dialect -> QueryExpr -> Doc
queryExpr Dialect
d QueryExpr
q

> statement d :: Dialect
d (Update t :: [Name]
t a :: Maybe Name
a sts :: [SetClause]
sts whr :: Maybe ScalarExpr
whr) =
>     String -> Doc
text "update" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
t
>     Doc -> Doc -> Doc
<+> Doc -> (Name -> Doc) -> Maybe Name -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\x :: Name
x -> String -> Doc
text "as" Doc -> Doc -> Doc
<+> Name -> Doc
name Name
x) Maybe Name
a
>     Doc -> Doc -> Doc
<+> String -> Doc
text "set" Doc -> Doc -> Doc
<+> [Doc] -> Doc
commaSep ((SetClause -> Doc) -> [SetClause] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map SetClause -> Doc
sc [SetClause]
sts)
>     Doc -> Doc -> Doc
<+> Dialect -> String -> Maybe ScalarExpr -> Doc
maybeScalarExpr Dialect
d "where" Maybe ScalarExpr
whr
>   where
>     sc :: SetClause -> Doc
sc (Set tg :: [Name]
tg v :: ScalarExpr
v) = [Name] -> Doc
names [Name]
tg Doc -> Doc -> Doc
<+> String -> Doc
text "=" Doc -> Doc -> Doc
<+> Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
v
>     sc (SetMultiple ts :: [[Name]]
ts vs :: [ScalarExpr]
vs) = Doc -> Doc
parens ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ ([Name] -> Doc) -> [[Name]] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map [Name] -> Doc
names [[Name]]
ts) Doc -> Doc -> Doc
<+> String -> Doc
text "="
>                              Doc -> Doc -> Doc
<+> Doc -> Doc
parens ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (ScalarExpr -> Doc) -> [ScalarExpr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d) [ScalarExpr]
vs)

> statement _ (DropTable n :: [Name]
n b :: DropBehaviour
b) =
>     String -> Doc
text "drop" Doc -> Doc -> Doc
<+> String -> Doc
text "table" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
n Doc -> Doc -> Doc
<+> DropBehaviour -> Doc
dropBehav DropBehaviour
b

> statement d :: Dialect
d (CreateView r :: Bool
r nm :: [Name]
nm al :: Maybe [Name]
al q :: QueryExpr
q co :: Maybe CheckOption
co) =
>     String -> Doc
text "create" Doc -> Doc -> Doc
<+> (if Bool
r then String -> Doc
text "recursive" else Doc
empty)
>     Doc -> Doc -> Doc
<+> String -> Doc
text "view" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
nm
>     Doc -> Doc -> Doc
<+> (Doc -> ([Name] -> Doc) -> Maybe [Name] -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\al' :: [Name]
al' -> Doc -> Doc
parens (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ [Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (Name -> Doc) -> [Name] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Doc
name [Name]
al')) Maybe [Name]
al
>     Doc -> Doc -> Doc
<+> String -> Doc
text "as"
>     Doc -> Doc -> Doc
<+> Dialect -> QueryExpr -> Doc
queryExpr Dialect
d QueryExpr
q
>     Doc -> Doc -> Doc
<+> case Maybe CheckOption
co of
>             Nothing -> Doc
empty
>             Just DefaultCheckOption -> [String] -> Doc
texts ["with", "check", "option"]
>             Just CascadedCheckOption -> [String] -> Doc
texts ["with", "cascaded", "check", "option"]
>             Just LocalCheckOption -> [String] -> Doc
texts ["with", "local", "check", "option"]

> statement _ (DropView n :: [Name]
n b :: DropBehaviour
b) =
>     String -> Doc
text "drop" Doc -> Doc -> Doc
<+> String -> Doc
text "view" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
n Doc -> Doc -> Doc
<+> DropBehaviour -> Doc
dropBehav DropBehaviour
b


== transactions

> statement _ StartTransaction =
>     [String] -> Doc
texts ["start", "transaction"]

> statement _ (Savepoint nm :: Name
nm) =
>     String -> Doc
text "savepoint" Doc -> Doc -> Doc
<+> Name -> Doc
name Name
nm

> statement _ (ReleaseSavepoint nm :: Name
nm) =
>     [String] -> Doc
texts ["release", "savepoint"] Doc -> Doc -> Doc
<+> Name -> Doc
name Name
nm

> statement _ Commit =
>     String -> Doc
text "commit"

> statement _ (Rollback mn :: Maybe Name
mn) =
>     String -> Doc
text "rollback"
>     Doc -> Doc -> Doc
<+> Doc -> (Name -> Doc) -> Maybe Name -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\n :: Name
n -> [String] -> Doc
texts ["to","savepoint"] Doc -> Doc -> Doc
<+> Name -> Doc
name Name
n) Maybe Name
mn

== access control

> statement _ (GrantPrivilege pas :: [PrivilegeAction]
pas po :: PrivilegeObject
po rs :: [Name]
rs go :: GrantOption
go) =
>     String -> Doc
text "grant" Doc -> Doc -> Doc
<+> [Doc] -> Doc
commaSep ((PrivilegeAction -> Doc) -> [PrivilegeAction] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map PrivilegeAction -> Doc
privAct [PrivilegeAction]
pas)
>     Doc -> Doc -> Doc
<+> String -> Doc
text "on" Doc -> Doc -> Doc
<+> PrivilegeObject -> Doc
privObj PrivilegeObject
po
>     Doc -> Doc -> Doc
<+> String -> Doc
text "to" Doc -> Doc -> Doc
<+> [Doc] -> Doc
commaSep ((Name -> Doc) -> [Name] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Doc
name [Name]
rs)
>     Doc -> Doc -> Doc
<+> GrantOption -> Doc
grantOpt GrantOption
go
>   where
>     grantOpt :: GrantOption -> Doc
grantOpt WithGrantOption = [String] -> Doc
texts ["with","grant","option"]
>     grantOpt WithoutGrantOption = Doc
empty

> statement _ (GrantRole rs :: [Name]
rs trs :: [Name]
trs ao :: AdminOption
ao) =
>     String -> Doc
text "grant" Doc -> Doc -> Doc
<+> [Doc] -> Doc
commaSep ((Name -> Doc) -> [Name] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Doc
name [Name]
rs)
>     Doc -> Doc -> Doc
<+> String -> Doc
text "to" Doc -> Doc -> Doc
<+> [Doc] -> Doc
commaSep ((Name -> Doc) -> [Name] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Doc
name [Name]
trs)
>     Doc -> Doc -> Doc
<+> AdminOption -> Doc
adminOpt AdminOption
ao
>   where
>     adminOpt :: AdminOption -> Doc
adminOpt WithAdminOption = [String] -> Doc
texts ["with","admin","option"]
>     adminOpt WithoutAdminOption = Doc
empty

> statement _ (CreateRole nm :: Name
nm) =
>     [String] -> Doc
texts ["create","role"] Doc -> Doc -> Doc
<+> Name -> Doc
name Name
nm

> statement _ (DropRole nm :: Name
nm) =
>     [String] -> Doc
texts ["drop","role"] Doc -> Doc -> Doc
<+> Name -> Doc
name Name
nm

> statement _ (RevokePrivilege go :: GrantOptionFor
go pas :: [PrivilegeAction]
pas po :: PrivilegeObject
po rs :: [Name]
rs db :: DropBehaviour
db) =
>     String -> Doc
text "revoke"
>     Doc -> Doc -> Doc
<+> GrantOptionFor -> Doc
grantOptFor GrantOptionFor
go
>     Doc -> Doc -> Doc
<+> [Doc] -> Doc
commaSep ((PrivilegeAction -> Doc) -> [PrivilegeAction] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map PrivilegeAction -> Doc
privAct [PrivilegeAction]
pas)
>     Doc -> Doc -> Doc
<+> String -> Doc
text "on" Doc -> Doc -> Doc
<+> PrivilegeObject -> Doc
privObj PrivilegeObject
po
>     Doc -> Doc -> Doc
<+> String -> Doc
text "from" Doc -> Doc -> Doc
<+> [Doc] -> Doc
commaSep ((Name -> Doc) -> [Name] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Doc
name [Name]
rs)
>     Doc -> Doc -> Doc
<+> DropBehaviour -> Doc
dropBehav DropBehaviour
db
>   where
>     grantOptFor :: GrantOptionFor -> Doc
grantOptFor GrantOptionFor = [String] -> Doc
texts ["grant","option","for"]
>     grantOptFor NoGrantOptionFor = Doc
empty

> statement _ (RevokeRole ao :: AdminOptionFor
ao rs :: [Name]
rs trs :: [Name]
trs db :: DropBehaviour
db) =
>     String -> Doc
text "revoke"
>     Doc -> Doc -> Doc
<+> AdminOptionFor -> Doc
adminOptFor AdminOptionFor
ao
>     Doc -> Doc -> Doc
<+> [Doc] -> Doc
commaSep ((Name -> Doc) -> [Name] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Doc
name [Name]
rs)
>     Doc -> Doc -> Doc
<+> String -> Doc
text "from" Doc -> Doc -> Doc
<+> [Doc] -> Doc
commaSep ((Name -> Doc) -> [Name] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Doc
name [Name]
trs)
>     Doc -> Doc -> Doc
<+> DropBehaviour -> Doc
dropBehav DropBehaviour
db
>   where
>     adminOptFor :: AdminOptionFor -> Doc
adminOptFor AdminOptionFor = [String] -> Doc
texts ["admin","option","for"]
>     adminOptFor NoAdminOptionFor = Doc
empty


> statement _ (StatementComment cs :: [Comment]
cs) = [Doc] -> Doc
vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (Comment -> Doc) -> [Comment] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Comment -> Doc
comment [Comment]
cs


== sessions


== extras

> dropBehav :: DropBehaviour -> Doc
> dropBehav :: DropBehaviour -> Doc
dropBehav DefaultDropBehaviour = Doc
empty
> dropBehav Cascade = String -> Doc
text "cascade"
> dropBehav Restrict = String -> Doc
text "restrict"


> columnDef :: Dialect -> ColumnDef -> Doc
> columnDef :: Dialect -> ColumnDef -> Doc
columnDef d :: Dialect
d (ColumnDef n :: Name
n t :: TypeName
t mdef :: Maybe DefaultClause
mdef cons :: [ColConstraintDef]
cons) =
>       Name -> Doc
name Name
n Doc -> Doc -> Doc
<+> TypeName -> Doc
typeName TypeName
t
>       Doc -> Doc -> Doc
<+> case Maybe DefaultClause
mdef of
>              Nothing -> Doc
empty
>              Just (DefaultClause def :: ScalarExpr
def) ->
>                  String -> Doc
text "default" Doc -> Doc -> Doc
<+> Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
def
>              Just (GenerationClause e :: ScalarExpr
e) ->
>                  [String] -> Doc
texts ["generated","always","as"] Doc -> Doc -> Doc
<+> Doc -> Doc
parens (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
e)
>              Just (IdentityColumnSpec w :: IdentityWhen
w o :: [SequenceGeneratorOption]
o) ->
>                  String -> Doc
text "generated"
>                  Doc -> Doc -> Doc
<+> (case IdentityWhen
w of
>                          GeneratedAlways -> String -> Doc
text "always"
>                          GeneratedByDefault -> String -> Doc
text "by" Doc -> Doc -> Doc
<+> String -> Doc
text "default")
>                  Doc -> Doc -> Doc
<+> String -> Doc
text "as" Doc -> Doc -> Doc
<+> String -> Doc
text "identity"
>                  Doc -> Doc -> Doc
<+> (case [SequenceGeneratorOption]
o of
>                          [] -> Doc
empty
>                          os :: [SequenceGeneratorOption]
os -> Doc -> Doc
parens ([Doc] -> Doc
sep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (SequenceGeneratorOption -> Doc)
-> [SequenceGeneratorOption] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map SequenceGeneratorOption -> Doc
sequenceGeneratorOption [SequenceGeneratorOption]
os))
>       Doc -> Doc -> Doc
<+> [Doc] -> Doc
sep ((ColConstraintDef -> Doc) -> [ColConstraintDef] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map ColConstraintDef -> Doc
cdef [ColConstraintDef]
cons)
>   where
>     cdef :: ColConstraintDef -> Doc
cdef (ColConstraintDef cnm :: Maybe [Name]
cnm con :: ColConstraint
con) =
>         Doc -> ([Name] -> Doc) -> Maybe [Name] -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\s :: [Name]
s -> String -> Doc
text "constraint" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
s) Maybe [Name]
cnm
>         Doc -> Doc -> Doc
<+> ColConstraint -> Doc
pcon ColConstraint
con
>     pcon :: ColConstraint -> Doc
pcon ColNotNullConstraint = [String] -> Doc
texts ["not","null"]
>     pcon ColUniqueConstraint = String -> Doc
text "unique"
>     pcon ColPrimaryKeyConstraint = [String] -> Doc
texts ["primary","key"]
>     pcon (ColCheckConstraint v :: ScalarExpr
v) = String -> Doc
text "check" Doc -> Doc -> Doc
<+> Doc -> Doc
parens (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
v)
>     pcon (ColReferencesConstraint tb :: [Name]
tb c :: Maybe Name
c m :: ReferenceMatch
m u :: ReferentialAction
u del :: ReferentialAction
del) =
>         String -> Doc
text "references"
>         Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
tb
>         Doc -> Doc -> Doc
<+> Doc -> (Name -> Doc) -> Maybe Name -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\c' :: Name
c' -> Doc -> Doc
parens (Name -> Doc
name Name
c')) Maybe Name
c
>         Doc -> Doc -> Doc
<+> ReferenceMatch -> Doc
refMatch ReferenceMatch
m
>         Doc -> Doc -> Doc
<+> String -> ReferentialAction -> Doc
refAct "update" ReferentialAction
u
>         Doc -> Doc -> Doc
<+> String -> ReferentialAction -> Doc
refAct "delete" ReferentialAction
del

> sequenceGeneratorOption :: SequenceGeneratorOption -> Doc
> sequenceGeneratorOption :: SequenceGeneratorOption -> Doc
sequenceGeneratorOption (SGODataType t :: TypeName
t) =
>     String -> Doc
text "as" Doc -> Doc -> Doc
<+> TypeName -> Doc
typeName TypeName
t
> sequenceGeneratorOption (SGORestart mi :: Maybe Integer
mi) =
>     String -> Doc
text "restart" Doc -> Doc -> Doc
<+> Doc -> (Integer -> Doc) -> Maybe Integer -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\mi' :: Integer
mi' -> [String] -> Doc
texts ["with", Integer -> String
forall a. Show a => a -> String
show Integer
mi']) Maybe Integer
mi
> sequenceGeneratorOption (SGOStartWith i :: Integer
i) = [String] -> Doc
texts ["start",  "with", Integer -> String
forall a. Show a => a -> String
show Integer
i]
> sequenceGeneratorOption (SGOIncrementBy i :: Integer
i) = [String] -> Doc
texts ["increment", "by", Integer -> String
forall a. Show a => a -> String
show Integer
i]
> sequenceGeneratorOption (SGOMaxValue i :: Integer
i) = [String] -> Doc
texts ["maxvalue", Integer -> String
forall a. Show a => a -> String
show Integer
i]
> sequenceGeneratorOption SGONoMaxValue = [String] -> Doc
texts ["no", "maxvalue"]
> sequenceGeneratorOption (SGOMinValue i :: Integer
i) = [String] -> Doc
texts ["minvalue", Integer -> String
forall a. Show a => a -> String
show Integer
i]
> sequenceGeneratorOption SGONoMinValue = [String] -> Doc
texts ["no", "minvalue"]
> sequenceGeneratorOption SGOCycle = String -> Doc
text "cycle"
> sequenceGeneratorOption SGONoCycle = String -> Doc
text "no cycle"

> refMatch :: ReferenceMatch -> Doc
> refMatch :: ReferenceMatch -> Doc
refMatch m :: ReferenceMatch
m = case ReferenceMatch
m of
>                      DefaultReferenceMatch -> Doc
empty
>                      MatchFull -> [String] -> Doc
texts ["match", "full"]
>                      MatchPartial -> [String] -> Doc
texts ["match","partial"]
>                      MatchSimple -> [String] -> Doc
texts ["match", "simple"]

> refAct :: String -> ReferentialAction -> Doc
> refAct :: String -> ReferentialAction -> Doc
refAct t :: String
t a :: ReferentialAction
a = case ReferentialAction
a of
>                      DefaultReferentialAction -> Doc
empty
>                      RefCascade -> [String] -> Doc
texts ["on", String
t, "cascade"]
>                      RefSetNull -> [String] -> Doc
texts ["on", String
t, "set", "null"]
>                      RefSetDefault -> [String] -> Doc
texts ["on", String
t, "set", "default"]
>                      RefRestrict -> [String] -> Doc
texts ["on", String
t, "restrict"]
>                      RefNoAction -> [String] -> Doc
texts ["on", String
t, "no", "action"]

> alterTableAction :: Dialect -> AlterTableAction -> Doc
> alterTableAction :: Dialect -> AlterTableAction -> Doc
alterTableAction d :: Dialect
d (AddColumnDef cd :: ColumnDef
cd) =
>     [String] -> Doc
texts ["add", "column"] Doc -> Doc -> Doc
<+> Dialect -> ColumnDef -> Doc
columnDef Dialect
d ColumnDef
cd

> alterTableAction d :: Dialect
d (AlterColumnSetDefault n :: Name
n v :: ScalarExpr
v) =
>     [String] -> Doc
texts ["alter", "column"]
>     Doc -> Doc -> Doc
<+> Name -> Doc
name Name
n
>     Doc -> Doc -> Doc
<+> [String] -> Doc
texts ["set","default"] Doc -> Doc -> Doc
<+> Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
v
> alterTableAction _ (AlterColumnDropDefault n :: Name
n) =
>     [String] -> Doc
texts ["alter", "column"]
>     Doc -> Doc -> Doc
<+> Name -> Doc
name Name
n
>     Doc -> Doc -> Doc
<+> [String] -> Doc
texts ["drop","default"]

> alterTableAction _ (AlterColumnSetNotNull n :: Name
n) =
>     [String] -> Doc
texts ["alter", "column"]
>     Doc -> Doc -> Doc
<+> Name -> Doc
name Name
n
>     Doc -> Doc -> Doc
<+> [String] -> Doc
texts ["set","not","null"]

> alterTableAction _ (AlterColumnDropNotNull n :: Name
n) =
>     [String] -> Doc
texts ["alter", "column"]
>     Doc -> Doc -> Doc
<+> Name -> Doc
name Name
n
>     Doc -> Doc -> Doc
<+> [String] -> Doc
texts ["drop","not","null"]

> alterTableAction _ (AlterColumnSetDataType n :: Name
n t :: TypeName
t) =
>     [String] -> Doc
texts ["alter", "column"]
>     Doc -> Doc -> Doc
<+> Name -> Doc
name Name
n
>     Doc -> Doc -> Doc
<+> [String] -> Doc
texts ["set","data","Type"]
>     Doc -> Doc -> Doc
<+> TypeName -> Doc
typeName TypeName
t

> alterTableAction _ (DropColumn n :: Name
n b :: DropBehaviour
b) =
>     [String] -> Doc
texts ["drop", "column"]
>     Doc -> Doc -> Doc
<+> Name -> Doc
name Name
n
>     Doc -> Doc -> Doc
<+> DropBehaviour -> Doc
dropBehav DropBehaviour
b

> alterTableAction d :: Dialect
d (AddTableConstraintDef n :: Maybe [Name]
n con :: TableConstraint
con) =
>     String -> Doc
text "add"
>     Doc -> Doc -> Doc
<+> Doc -> ([Name] -> Doc) -> Maybe [Name] -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\s :: [Name]
s -> String -> Doc
text "constraint" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
s) Maybe [Name]
n
>     Doc -> Doc -> Doc
<+> Dialect -> TableConstraint -> Doc
tableConstraint Dialect
d TableConstraint
con

> alterTableAction _ (DropTableConstraintDef n :: [Name]
n b :: DropBehaviour
b) =
>     [String] -> Doc
texts ["drop", "constraint"]
>     Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
n
>     Doc -> Doc -> Doc
<+> DropBehaviour -> Doc
dropBehav DropBehaviour
b


> tableConstraint :: Dialect -> TableConstraint -> Doc
> tableConstraint :: Dialect -> TableConstraint -> Doc
tableConstraint _ (TableUniqueConstraint ns :: [Name]
ns) =
>          String -> Doc
text "unique" Doc -> Doc -> Doc
<+> Doc -> Doc
parens ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (Name -> Doc) -> [Name] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Doc
name [Name]
ns)
> tableConstraint _ (TablePrimaryKeyConstraint ns :: [Name]
ns) =
>         [String] -> Doc
texts ["primary","key"] Doc -> Doc -> Doc
<+> Doc -> Doc
parens ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (Name -> Doc) -> [Name] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Doc
name [Name]
ns)
> tableConstraint _ (TableReferencesConstraint cs :: [Name]
cs t :: [Name]
t tcs :: Maybe [Name]
tcs m :: ReferenceMatch
m u :: ReferentialAction
u del :: ReferentialAction
del) =
>         [String] -> Doc
texts ["foreign", "key"]
>         Doc -> Doc -> Doc
<+> Doc -> Doc
parens ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (Name -> Doc) -> [Name] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Doc
name [Name]
cs)
>         Doc -> Doc -> Doc
<+> String -> Doc
text "references"
>         Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
t
>         Doc -> Doc -> Doc
<+> Doc -> ([Name] -> Doc) -> Maybe [Name] -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\c' :: [Name]
c' -> Doc -> Doc
parens ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (Name -> Doc) -> [Name] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Doc
name [Name]
c')) Maybe [Name]
tcs
>         Doc -> Doc -> Doc
<+> ReferenceMatch -> Doc
refMatch ReferenceMatch
m
>         Doc -> Doc -> Doc
<+> String -> ReferentialAction -> Doc
refAct "update" ReferentialAction
u
>         Doc -> Doc -> Doc
<+> String -> ReferentialAction -> Doc
refAct "delete" ReferentialAction
del
> tableConstraint d :: Dialect
d (TableCheckConstraint v :: ScalarExpr
v) = String -> Doc
text "check" Doc -> Doc -> Doc
<+> Doc -> Doc
parens (Dialect -> ScalarExpr -> Doc
scalarExpr Dialect
d ScalarExpr
v)


> privAct :: PrivilegeAction -> Doc
> privAct :: PrivilegeAction -> Doc
privAct PrivAll = [String] -> Doc
texts ["all","privileges"]
> privAct (PrivSelect cs :: [Name]
cs) = String -> Doc
text "select" Doc -> Doc -> Doc
<+> [Name] -> Doc
maybeColList [Name]
cs
> privAct (PrivInsert cs :: [Name]
cs) = String -> Doc
text "insert" Doc -> Doc -> Doc
<+> [Name] -> Doc
maybeColList [Name]
cs
> privAct (PrivUpdate cs :: [Name]
cs) = String -> Doc
text "update" Doc -> Doc -> Doc
<+> [Name] -> Doc
maybeColList [Name]
cs
> privAct (PrivReferences cs :: [Name]
cs) = String -> Doc
text "references" Doc -> Doc -> Doc
<+> [Name] -> Doc
maybeColList [Name]
cs
> privAct PrivDelete = String -> Doc
text "delete"
> privAct PrivUsage = String -> Doc
text "usage"
> privAct PrivTrigger = String -> Doc
text "trigger"
> privAct PrivExecute = String -> Doc
text "execute"

> maybeColList :: [Name] -> Doc
> maybeColList :: [Name] -> Doc
maybeColList cs :: [Name]
cs =
>     if [Name] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Name]
cs
>     then Doc
empty
>     else Doc -> Doc
parens ([Doc] -> Doc
commaSep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (Name -> Doc) -> [Name] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Doc
name [Name]
cs)

> privObj :: PrivilegeObject -> Doc
> privObj :: PrivilegeObject -> Doc
privObj (PrivTable nm :: [Name]
nm) = [Name] -> Doc
names [Name]
nm
> privObj (PrivDomain nm :: [Name]
nm) = String -> Doc
text "domain" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
nm
> privObj (PrivType nm :: [Name]
nm) = String -> Doc
text "type" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
nm
> privObj (PrivSequence nm :: [Name]
nm) = String -> Doc
text "sequence" Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
nm
> privObj (PrivFunction nm :: [Name]
nm) = [String] -> Doc
texts ["specific", "function"] Doc -> Doc -> Doc
<+> [Name] -> Doc
names [Name]
nm

= utils

> commaSep :: [Doc] -> Doc
> commaSep :: [Doc] -> Doc
commaSep ds :: [Doc]
ds = [Doc] -> Doc
sep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ Doc -> [Doc] -> [Doc]
punctuate Doc
comma [Doc]
ds

> me :: (a -> Doc) -> Maybe a -> Doc
> me :: (a -> Doc) -> Maybe a -> Doc
me = Doc -> (a -> Doc) -> Maybe a -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty

> comment :: Comment -> Doc
> comment :: Comment -> Doc
comment (BlockComment str :: String
str) = String -> Doc
text "/*" Doc -> Doc -> Doc
<+> String -> Doc
text String
str Doc -> Doc -> Doc
<+> String -> Doc
text "*/"

> texts :: [String] -> Doc
> texts :: [String] -> Doc
texts ts :: [String]
ts = [Doc] -> Doc
sep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (String -> Doc) -> [String] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map String -> Doc
text [String]
ts