module PostgresqlSyntax.Ast.ConstDatetime where
import PostgresqlSyntax.Algebra
import PostgresqlSyntax.Ast.Timezone
import qualified PostgresqlSyntax.Extras.TextBuilder as TextBuilder
import qualified PostgresqlSyntax.Helpers.Parsers as Parsers
import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders
import PostgresqlSyntax.Prelude
import qualified Test.QuickCheck as Qc
data ConstDatetime
= TimestampConstDatetime (Maybe Int64) (Maybe Timezone)
| TimeConstDatetime (Maybe Int64) (Maybe Timezone)
deriving (Int -> ConstDatetime -> ShowS
[ConstDatetime] -> ShowS
ConstDatetime -> String
(Int -> ConstDatetime -> ShowS)
-> (ConstDatetime -> String)
-> ([ConstDatetime] -> ShowS)
-> Show ConstDatetime
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ConstDatetime -> ShowS
showsPrec :: Int -> ConstDatetime -> ShowS
$cshow :: ConstDatetime -> String
show :: ConstDatetime -> String
$cshowList :: [ConstDatetime] -> ShowS
showList :: [ConstDatetime] -> ShowS
Show, (forall x. ConstDatetime -> Rep ConstDatetime x)
-> (forall x. Rep ConstDatetime x -> ConstDatetime)
-> Generic ConstDatetime
forall x. Rep ConstDatetime x -> ConstDatetime
forall x. ConstDatetime -> Rep ConstDatetime x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ConstDatetime -> Rep ConstDatetime x
from :: forall x. ConstDatetime -> Rep ConstDatetime x
$cto :: forall x. Rep ConstDatetime x -> ConstDatetime
to :: forall x. Rep ConstDatetime x -> ConstDatetime
Generic, ConstDatetime -> ConstDatetime -> Bool
(ConstDatetime -> ConstDatetime -> Bool)
-> (ConstDatetime -> ConstDatetime -> Bool) -> Eq ConstDatetime
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ConstDatetime -> ConstDatetime -> Bool
== :: ConstDatetime -> ConstDatetime -> Bool
$c/= :: ConstDatetime -> ConstDatetime -> Bool
/= :: ConstDatetime -> ConstDatetime -> Bool
Eq, Eq ConstDatetime
Eq ConstDatetime =>
(ConstDatetime -> ConstDatetime -> Ordering)
-> (ConstDatetime -> ConstDatetime -> Bool)
-> (ConstDatetime -> ConstDatetime -> Bool)
-> (ConstDatetime -> ConstDatetime -> Bool)
-> (ConstDatetime -> ConstDatetime -> Bool)
-> (ConstDatetime -> ConstDatetime -> ConstDatetime)
-> (ConstDatetime -> ConstDatetime -> ConstDatetime)
-> Ord ConstDatetime
ConstDatetime -> ConstDatetime -> Bool
ConstDatetime -> ConstDatetime -> Ordering
ConstDatetime -> ConstDatetime -> ConstDatetime
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: ConstDatetime -> ConstDatetime -> Ordering
compare :: ConstDatetime -> ConstDatetime -> Ordering
$c< :: ConstDatetime -> ConstDatetime -> Bool
< :: ConstDatetime -> ConstDatetime -> Bool
$c<= :: ConstDatetime -> ConstDatetime -> Bool
<= :: ConstDatetime -> ConstDatetime -> Bool
$c> :: ConstDatetime -> ConstDatetime -> Bool
> :: ConstDatetime -> ConstDatetime -> Bool
$c>= :: ConstDatetime -> ConstDatetime -> Bool
>= :: ConstDatetime -> ConstDatetime -> Bool
$cmax :: ConstDatetime -> ConstDatetime -> ConstDatetime
max :: ConstDatetime -> ConstDatetime -> ConstDatetime
$cmin :: ConstDatetime -> ConstDatetime -> ConstDatetime
min :: ConstDatetime -> ConstDatetime -> ConstDatetime
Ord, Typeable ConstDatetime
Typeable ConstDatetime =>
(forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ConstDatetime -> c ConstDatetime)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ConstDatetime)
-> (ConstDatetime -> Constr)
-> (ConstDatetime -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ConstDatetime))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ConstDatetime))
-> ((forall b. Data b => b -> b) -> ConstDatetime -> ConstDatetime)
-> (forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ConstDatetime -> r)
-> (forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ConstDatetime -> r)
-> (forall u. (forall d. Data d => d -> u) -> ConstDatetime -> [u])
-> (forall u.
Int -> (forall d. Data d => d -> u) -> ConstDatetime -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> ConstDatetime -> m ConstDatetime)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ConstDatetime -> m ConstDatetime)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ConstDatetime -> m ConstDatetime)
-> Data ConstDatetime
ConstDatetime -> Constr
ConstDatetime -> DataType
(forall b. Data b => b -> b) -> ConstDatetime -> ConstDatetime
forall a.
Typeable a =>
(forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> ConstDatetime -> u
forall u. (forall d. Data d => d -> u) -> ConstDatetime -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ConstDatetime -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ConstDatetime -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> ConstDatetime -> m ConstDatetime
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ConstDatetime -> m ConstDatetime
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ConstDatetime
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ConstDatetime -> c ConstDatetime
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ConstDatetime)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ConstDatetime)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ConstDatetime -> c ConstDatetime
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ConstDatetime -> c ConstDatetime
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ConstDatetime
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ConstDatetime
$ctoConstr :: ConstDatetime -> Constr
toConstr :: ConstDatetime -> Constr
$cdataTypeOf :: ConstDatetime -> DataType
dataTypeOf :: ConstDatetime -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ConstDatetime)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ConstDatetime)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ConstDatetime)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ConstDatetime)
$cgmapT :: (forall b. Data b => b -> b) -> ConstDatetime -> ConstDatetime
gmapT :: (forall b. Data b => b -> b) -> ConstDatetime -> ConstDatetime
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ConstDatetime -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ConstDatetime -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ConstDatetime -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ConstDatetime -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> ConstDatetime -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> ConstDatetime -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> ConstDatetime -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> ConstDatetime -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> ConstDatetime -> m ConstDatetime
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> ConstDatetime -> m ConstDatetime
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ConstDatetime -> m ConstDatetime
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ConstDatetime -> m ConstDatetime
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ConstDatetime -> m ConstDatetime
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ConstDatetime -> m ConstDatetime
Data)
instance IsAst ConstDatetime where
toTextBuilder :: Settings -> ConstDatetime -> TextBuilder
toTextBuilder Settings
settings = \case
TimestampConstDatetime Maybe Int64
a Maybe Timezone
b ->
[Maybe TextBuilder] -> TextBuilder
TextBuilders.optLexemes
[ TextBuilder -> Maybe TextBuilder
forall a. a -> Maybe a
Just TextBuilder
"TIMESTAMP",
(Int64 -> TextBuilder) -> Maybe Int64 -> Maybe TextBuilder
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (TextBuilder -> TextBuilder
TextBuilders.renderInParens (TextBuilder -> TextBuilder)
-> (Int64 -> TextBuilder) -> Int64 -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Int64 -> TextBuilder
TextBuilder.int64Dec) Maybe Int64
a,
(Timezone -> TextBuilder) -> Maybe Timezone -> Maybe TextBuilder
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Settings -> Timezone -> TextBuilder
forall a. IsAst a => Settings -> a -> TextBuilder
toTextBuilder Settings
settings) Maybe Timezone
b
]
TimeConstDatetime Maybe Int64
a Maybe Timezone
b ->
[Maybe TextBuilder] -> TextBuilder
TextBuilders.optLexemes
[ TextBuilder -> Maybe TextBuilder
forall a. a -> Maybe a
Just TextBuilder
"TIME",
(Int64 -> TextBuilder) -> Maybe Int64 -> Maybe TextBuilder
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (TextBuilder -> TextBuilder
TextBuilders.renderInParens (TextBuilder -> TextBuilder)
-> (Int64 -> TextBuilder) -> Int64 -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Int64 -> TextBuilder
TextBuilder.int64Dec) Maybe Int64
a,
(Timezone -> TextBuilder) -> Maybe Timezone -> Maybe TextBuilder
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Settings -> Timezone -> TextBuilder
forall a. IsAst a => Settings -> a -> TextBuilder
toTextBuilder Settings
settings) Maybe Timezone
b
]
parser :: Settings -> Parser ConstDatetime
parser Settings
settings =
[Parser ConstDatetime] -> Parser ConstDatetime
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum
[ do
Text -> HeadedParsec Void Text Text
forall {s} {e}.
(Tokens s ~ Text, Token s ~ Char, Ord e, Stream s) =>
Text -> HeadedParsec e s Text
Parsers.keyword Text
"timestamp"
Maybe Int64
a <- HeadedParsec Void Text Int64
-> HeadedParsec Void Text (Maybe Int64)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (HeadedParsec Void Text ()
forall err strm.
(Ord err, Stream strm, Token strm ~ Char) =>
HeadedParsec err strm ()
Parsers.space1 HeadedParsec Void Text ()
-> HeadedParsec Void Text Int64 -> HeadedParsec Void Text Int64
forall a b.
HeadedParsec Void Text a
-> HeadedParsec Void Text b -> HeadedParsec Void Text b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> HeadedParsec Void Text Int64 -> HeadedParsec Void Text Int64
forall a. Parser a -> Parser a
Parsers.inParens HeadedParsec Void Text Int64
forall err strm decimal.
(Ord err, Stream strm, Token strm ~ Char, Integral decimal) =>
HeadedParsec err strm decimal
Parsers.decimal)
Maybe Timezone
b <- HeadedParsec Void Text Timezone
-> HeadedParsec Void Text (Maybe Timezone)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (HeadedParsec Void Text ()
forall err strm.
(Ord err, Stream strm, Token strm ~ Char) =>
HeadedParsec err strm ()
Parsers.space1 HeadedParsec Void Text ()
-> HeadedParsec Void Text Timezone
-> HeadedParsec Void Text Timezone
forall a b.
HeadedParsec Void Text a
-> HeadedParsec Void Text b -> HeadedParsec Void Text b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Settings -> HeadedParsec Void Text Timezone
forall a. IsAst a => Settings -> Parser a
parser Settings
settings)
return (Maybe Int64 -> Maybe Timezone -> ConstDatetime
TimestampConstDatetime Maybe Int64
a Maybe Timezone
b),
do
Text -> HeadedParsec Void Text Text
forall {s} {e}.
(Tokens s ~ Text, Token s ~ Char, Ord e, Stream s) =>
Text -> HeadedParsec e s Text
Parsers.keyword Text
"time"
Maybe Int64
a <- HeadedParsec Void Text Int64
-> HeadedParsec Void Text (Maybe Int64)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (HeadedParsec Void Text ()
forall err strm.
(Ord err, Stream strm, Token strm ~ Char) =>
HeadedParsec err strm ()
Parsers.space1 HeadedParsec Void Text ()
-> HeadedParsec Void Text Int64 -> HeadedParsec Void Text Int64
forall a b.
HeadedParsec Void Text a
-> HeadedParsec Void Text b -> HeadedParsec Void Text b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> HeadedParsec Void Text Int64 -> HeadedParsec Void Text Int64
forall a. Parser a -> Parser a
Parsers.inParens HeadedParsec Void Text Int64
forall err strm decimal.
(Ord err, Stream strm, Token strm ~ Char, Integral decimal) =>
HeadedParsec err strm decimal
Parsers.decimal)
Maybe Timezone
b <- HeadedParsec Void Text Timezone
-> HeadedParsec Void Text (Maybe Timezone)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (HeadedParsec Void Text ()
forall err strm.
(Ord err, Stream strm, Token strm ~ Char) =>
HeadedParsec err strm ()
Parsers.space1 HeadedParsec Void Text ()
-> HeadedParsec Void Text Timezone
-> HeadedParsec Void Text Timezone
forall a b.
HeadedParsec Void Text a
-> HeadedParsec Void Text b -> HeadedParsec Void Text b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Settings -> HeadedParsec Void Text Timezone
forall a. IsAst a => Settings -> Parser a
parser Settings
settings)
return (Maybe Int64 -> Maybe Timezone -> ConstDatetime
TimeConstDatetime Maybe Int64
a Maybe Timezone
b)
]
instance Qc.Arbitrary ConstDatetime where
shrink :: ConstDatetime -> [ConstDatetime]
shrink = ConstDatetime -> [ConstDatetime]
forall a.
(Generic a, RecursivelyShrink (Rep a), GSubterms (Rep a) a) =>
a -> [a]
Qc.genericShrink
arbitrary :: Gen ConstDatetime
arbitrary =
[Gen ConstDatetime] -> Gen ConstDatetime
forall a. HasCallStack => [Gen a] -> Gen a
Qc.oneof
[ Maybe Int64 -> Maybe Timezone -> ConstDatetime
TimestampConstDatetime (Maybe Int64 -> Maybe Timezone -> ConstDatetime)
-> Gen (Maybe Int64) -> Gen (Maybe Timezone -> ConstDatetime)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (Maybe Int64)
forall {a}. (Random a, Num a, Bounded a) => Gen (Maybe a)
nonNegativeMaybeInt64 Gen (Maybe Timezone -> ConstDatetime)
-> Gen (Maybe Timezone) -> Gen ConstDatetime
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen (Maybe Timezone)
forall a. Arbitrary a => Gen a
Qc.arbitrary,
Maybe Int64 -> Maybe Timezone -> ConstDatetime
TimeConstDatetime (Maybe Int64 -> Maybe Timezone -> ConstDatetime)
-> Gen (Maybe Int64) -> Gen (Maybe Timezone -> ConstDatetime)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (Maybe Int64)
forall {a}. (Random a, Num a, Bounded a) => Gen (Maybe a)
nonNegativeMaybeInt64 Gen (Maybe Timezone -> ConstDatetime)
-> Gen (Maybe Timezone) -> Gen ConstDatetime
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen (Maybe Timezone)
forall a. Arbitrary a => Gen a
Qc.arbitrary
]
where
nonNegativeMaybeInt64 :: Gen (Maybe a)
nonNegativeMaybeInt64 = [Gen (Maybe a)] -> Gen (Maybe a)
forall a. HasCallStack => [Gen a] -> Gen a
Qc.oneof [Maybe a -> Gen (Maybe a)
forall a. a -> Gen a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing, a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> Gen a -> Gen (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen a
forall {a}. (Random a, Num a, Bounded a) => Gen a
nonNegativeInt64]
nonNegativeInt64 :: Gen a
nonNegativeInt64 = (Int -> Gen a) -> Gen a
forall a. (Int -> Gen a) -> Gen a
Qc.sized (\Int
n -> (a, a) -> Gen a
forall a. Random a => (a, a) -> Gen a
Qc.choose (a
0, Int -> a
forall {b} {a}. (Bounded a, Integral b, Num a) => b -> a
cap Int
n))
cap :: b -> a
cap b
n
| b
n b -> b -> Bool
forall a. Ord a => a -> a -> Bool
>= b
62 = a
forall a. Bounded a => a
maxBound
| Bool
otherwise = a
2 a -> b -> a
forall a b. (Num a, Integral b) => a -> b -> a
^ b
n