forked from haskellari/postgresql-simple
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterval.hs
More file actions
241 lines (210 loc) · 7.19 KB
/
Interval.hs
File metadata and controls
241 lines (210 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
{-# LANGUAGE LexicalNegation #-}
{-# LANGUAGE NumDecimals #-}
module Database.PostgreSQL.Simple.Interval where
import qualified Control.Applicative as Applicative
import qualified Control.Monad as Monad
import qualified Data.Attoparsec.ByteString.Char8 as A
import qualified Data.Bits as Bits
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Builder as Builder
import qualified Data.Function as Function
import qualified Data.Int as Int
import qualified Data.Scientific as Scientific
import qualified Database.PostgreSQL.Simple.FromField as Postgres
import qualified Database.PostgreSQL.Simple.ToField as Postgres
import qualified Database.PostgreSQL.Simple.TypeInfo.Static as Postgres
data Interval = MkInterval
{ months :: !Int.Int32,
days :: !Int.Int32,
microseconds :: !Int.Int64
}
deriving (Eq, Show)
instance Postgres.FromField Interval where
fromField = Postgres.attoFieldParser (== Postgres.intervalOid) parse
instance Postgres.ToField Interval where
toField = Postgres.Plain . render
zero :: Interval
zero = MkInterval 0 0 0
fromMicroseconds :: Int.Int64 -> Interval
fromMicroseconds x = zero {microseconds = x}
fromMilliseconds :: Int.Int64 -> Maybe Interval
fromMilliseconds =
fmap fromMicroseconds
. Bits.toIntegralSized
. (*) 1e3
. toInteger
fromSeconds :: Int.Int64 -> Maybe Interval
fromSeconds =
fmap fromMicroseconds
. Bits.toIntegralSized
. (*) 1e6
. toInteger
fromMinutes :: Int.Int64 -> Maybe Interval
fromMinutes =
fmap fromMicroseconds
. Bits.toIntegralSized
. (*) 60e6
. toInteger
fromHours :: Int.Int64 -> Maybe Interval
fromHours =
fmap fromMicroseconds
. Bits.toIntegralSized
. (*) 3600e6
. toInteger
fromDays :: Int.Int32 -> Interval
fromDays x = zero {days = x}
fromWeeks :: Int.Int32 -> Maybe Interval
fromWeeks =
fmap fromDays
. Bits.toIntegralSized
. (*) 7
. toInteger
fromMonths :: Int.Int32 -> Interval
fromMonths x = zero {months = x}
fromYears :: Int.Int32 -> Maybe Interval
fromYears =
fmap fromMonths
. Bits.toIntegralSized
. (*) 12
. toInteger
add :: Interval -> Interval -> Maybe Interval
add x y =
let safeAdd :: (Bits.Bits a, Integral a) => a -> a -> Maybe a
safeAdd n = Bits.toIntegralSized . Function.on (+) toInteger n
in MkInterval
<$> Function.on safeAdd months x y
<*> Function.on safeAdd days x y
<*> Function.on safeAdd microseconds x y
render :: Interval -> Builder.Builder
render x =
let signed :: (Num a, Ord a) => (a -> Builder.Builder) -> a -> Builder.Builder
signed f n = (if n > 0 then "+" else "") <> f n
in "interval '"
<> signed Builder.int32Dec (months x)
<> " months "
<> signed Builder.int32Dec (days x)
<> " days "
<> signed Builder.int64Dec (microseconds x)
<> " microseconds'"
parse :: A.Parser Interval
parse =
-- Start with parsers that have non-empty prefixes, in order to avoid
-- ambiguity. Neither of the `postgres` nor `sql_standard` interval styles
-- have a prefix (or suffix), so whichever one is attempted first needs to
-- make sure it has consumed all of the input.
A.choice $
parseInfinities
: fmap
(fromComponents =<<)
[ parseIso8601,
parsePostgresVerbose,
parsePostgres <* A.endOfInput,
parseSqlStandard
]
parseInfinities :: A.Parser Interval
parseInfinities =
-- Both `-infinity` and `infinity` are new as of PostgreSQL 17.0.
-- https://www.postgresql.org/message-id/E1r2rB1-005PHm-UL%40gemulon.postgresql.org
A.choice
[ MkInterval minBound minBound minBound <$ "-infinity",
MkInterval maxBound maxBound maxBound <$ "infinity"
]
parseIso8601 :: A.Parser [Component]
parseIso8601 = do
Monad.void "P"
dates <-
A.many' $
A.choice
[ Years <$> A.signed A.decimal <* "Y",
Months <$> A.signed A.decimal <* "M",
Days <$> A.signed A.decimal <* "D"
]
times <- A.option [] $ do
Monad.void "T"
A.many' $
A.choice
[ Hours <$> A.signed A.decimal <* "H",
Minutes <$> A.signed A.decimal <* "M",
Microseconds <$> A.signed A.scientific <* "S"
]
pure $ dates <> times
parsePostgresVerbose :: A.Parser [Component]
parsePostgresVerbose = do
Monad.void "@ "
components <-
flip A.sepBy " " $
A.choice
[ Years <$> A.signed A.decimal <* maybePlural " year",
Months <$> A.signed A.decimal <* maybePlural " mon",
Days <$> A.signed A.decimal <* maybePlural " day",
Hours <$> A.signed A.decimal <* maybePlural " hour",
Minutes <$> A.signed A.decimal <* maybePlural " min",
Microseconds <$> A.signed A.scientific <* A.option "" (maybePlural " sec")
]
ago <- A.option "" " ago"
pure $ negateComponentsWhen (not $ ByteString.null ago) components
parsePostgres :: A.Parser [Component]
parsePostgres = do
dates <-
flip A.sepBy " " $
A.choice
[ Years <$> A.signed A.decimal <* maybePlural " year",
Months <$> A.signed A.decimal <* maybePlural " mon",
Days <$> A.signed A.decimal <* maybePlural " day"
]
time <- A.option [] $ A.skipSpace *> parseTime
pure $ dates <> time
parseSqlStandard :: A.Parser [Component]
parseSqlStandard = do
let parseYearsAndMonths = do
sign <- parseSign
years <- Years <$> A.decimal <* "-"
months_ <- Months <$> A.decimal
pure $ negateComponentsWhen (sign == "-") [years, months_]
let parseDays = (: []) . Days <$> A.signed A.decimal
let parsers = [parseYearsAndMonths, parseTime, parseDays]
mconcat <$> A.sepBy1 (A.choice parsers) " "
parseTime :: A.Parser [Component]
parseTime = do
sign <- parseSign
hours <- Hours <$> A.decimal <* ":"
minutes <- Minutes <$> A.decimal <* ":"
micros <- Microseconds <$> A.scientific
pure $ negateComponentsWhen (sign == "-") [hours, minutes, micros]
parseSign :: A.Parser ByteString.ByteString
parseSign = A.choice ["-", "+", ""]
maybePlural :: ByteString.ByteString -> A.Parser ByteString.ByteString
maybePlural word = (<>) <$> A.string word <*> A.option "" "s"
data Component
= Years !Integer
| Months !Integer
| Days !Integer
| Hours !Integer
| Minutes !Integer
| Microseconds !Scientific.Scientific
deriving (Eq, Show)
fromComponent :: Component -> Maybe Interval
fromComponent c = case c of
Years y -> fromYears =<< Bits.toIntegralSized y
Months m -> fromMonths <$> Bits.toIntegralSized m
Days d -> fromDays <$> Bits.toIntegralSized d
Hours h -> fromHours =<< Bits.toIntegralSized h
Minutes m -> fromMinutes =<< Bits.toIntegralSized m
Microseconds u -> fromMicroseconds <$> Scientific.toBoundedInteger (u * 1e6)
fromComponents ::
(Applicative.Alternative f, Traversable t) =>
t Component ->
f Interval
fromComponents =
maybe Applicative.empty pure
. (Monad.foldM add zero Monad.<=< traverse fromComponent)
negateComponent :: Component -> Component
negateComponent c = case c of
Years y -> Years -y
Months m -> Months -m
Days d -> Days -d
Hours h -> Hours -h
Minutes m -> Minutes -m
Microseconds u -> Microseconds -u
negateComponentsWhen :: (Functor f) => Bool -> f Component -> f Component
negateComponentsWhen p = if p then fmap negateComponent else id