-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathPgSpecific.hs
More file actions
1548 lines (1301 loc) · 61.1 KB
/
PgSpecific.hs
File metadata and controls
1548 lines (1301 loc) · 61.1 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE CPP #-}
-- | Postgres-specific types, functions, and operators
module Database.Beam.Postgres.PgSpecific
( -- ** Full-text search
-- $full-text-search
-- *** @TSVECTOR@ data type
TsVectorConfig, TsVector(..)
, toTsVector, english
-- *** @TSQUERY@ data type
, TsQuery(..), (@@)
, toTsQuery
-- ** @JSON@ and @JSONB@ data types
-- $json
, PgJSON(..), PgJSONB(..)
, IsPgJSON(..)
, PgJSONEach(..), PgJSONKey(..), PgJSONElement(..)
, (@>), (<@), (->#), (->$)
, (->>#), (->>$), (#>), (#>>)
, (?), (?|), (?&), (||.)
, withoutKey, withoutIdx
, withoutKeys
, pgJsonArrayLength
, pgJsonbUpdate, pgJsonbSet
, pgJsonbPretty
-- ** @MONEY@ data type
, PgMoney(..), pgMoney
, pgScaleMoney_
, pgDivideMoney_, pgDivideMoneys_
, pgAddMoney_, pgSubtractMoney_
, pgSumMoneyOver_, pgAvgMoneyOver_
, pgSumMoney_, pgAvgMoney_
-- ** Geometry types (not PostGIS)
, PgPoint(..), PgLine(..), PgLineSegment(..)
, PgBox(..), PgPath(..), PgPolygon(..)
, PgCircle(..)
-- ** Regular expressions
, PgRegex(..), pgRegex_
, (~.), (~*.), (!~.), (!~*.)
, pgRegexpReplace_, pgRegexpMatch_
, pgRegexpSplitToTable, pgRegexpSplitToArray
-- ** Set-valued functions
-- $set-valued-funs
, PgSetOf, pgUnnest
, pgUnnestArray, pgUnnestArrayWithOrdinality
-- ** @ARRAY@ types
-- $arrays
, PgArrayValueContext, PgIsArrayContext
-- *** Building @ARRAY@s
, array_, arrayOf_, (++.)
, pgArrayAgg, pgArrayAggOver
-- *** Array operators and functions
, (!.), arrayDims_
, arrayUpper_, arrayLower_
, arrayUpperUnsafe_, arrayLowerUnsafe_
, arrayLength_, arrayLengthUnsafe_
, isSupersetOf_, isSubsetOf_
-- ** @RANGE@ types
-- $ranges
, PgRange(..), PgRangeBound(..), PgBoundType(..)
, PgIsRange(..)
, PgInt4Range, PgInt8Range, PgNumRange
, PgTsRange, PgTsTzRange, PgDateRange
-- *** Building ranges from expressions
, range_
-- *** Building @PgRangeBound@s
, inclusive, exclusive, unbounded
-- *** Range operators and functions
, (-@>-), (-@>), (-<@-), (<@-)
, (-&&-), (-<<-), (->>-)
, (-&<-), (-&>-), (--|--)
, (-+-), (-*-), (-.-)
, rLower_, rUpper_, isEmpty_
, lowerInc_, upperInc_, lowerInf_, upperInf_
, rangeMerge_
-- ** Postgres functions and aggregates
, pgBoolOr, pgBoolAnd, pgStringAgg, pgStringAggOver
, pgNubBy_
, now_, ilike_
)
where
import Database.Beam hiding ((||.), char, double)
import Database.Beam.Backend.SQL
import Database.Beam.Migrate ( HasDefaultSqlDataType(..) )
import Database.Beam.Postgres.Syntax
import Database.Beam.Postgres.Types
import Database.Beam.Query.Internal
import Database.Beam.Schema.Tables
import Control.Monad.Free
import Control.Monad.State.Strict (evalState, put, get)
import Data.Aeson
import Data.Attoparsec.ByteString
import Data.Attoparsec.ByteString.Char8
import Data.ByteString (ByteString)
import Data.ByteString.Builder
import qualified Data.ByteString.Char8 as BC
import qualified Data.ByteString.Lazy as BL
import Data.Foldable
import Data.Hashable
import qualified Data.List.NonEmpty as NE
import Data.Proxy
import Data.Scientific (Scientific, formatScientific, FPFormat(Fixed))
import Data.String
import qualified Data.Text as T
import Data.Time (LocalTime)
import Data.Type.Bool
import qualified Data.Vector as V
#if !MIN_VERSION_base(4, 11, 0)
import Data.Semigroup
#endif
import qualified Database.PostgreSQL.Simple.FromField as Pg
import qualified Database.PostgreSQL.Simple.ToField as Pg
import qualified Database.PostgreSQL.Simple.TypeInfo.Static as Pg
import qualified Database.PostgreSQL.Simple.Range as Pg
import GHC.TypeLits
import GHC.Exts hiding (toList)
-- ** Postgres-specific functions
-- | Postgres @NOW()@ function. Returns the server's timestamp
now_ :: QExpr Postgres s LocalTime
now_ = QExpr (\_ -> PgExpressionSyntax (emit "NOW()"))
-- | Postgres @ILIKE@ operator. A case-insensitive version of 'like_'.
ilike_ :: BeamSqlBackendIsString Postgres text
=> QExpr Postgres s text
-> QExpr Postgres s text
-> QExpr Postgres s Bool
ilike_ (QExpr a) (QExpr b) = QExpr (pgBinOp "ILIKE" <$> a <*> b)
-- ** TsVector type
-- | The type of a document preprocessed for full-text search. The contained
-- 'ByteString' is the Postgres representation of the @TSVECTOR@ type. Use
-- 'toTsVector' to construct these on-the-fly from strings.
--
-- When this field is embedded in a beam table, 'defaultMigratableDbSettings'
-- will give the column the postgres @TSVECTOR@ type.
newtype TsVector = TsVector ByteString
deriving (Show, Eq, Ord)
-- | The identifier of a Postgres text search configuration.
--
-- Use the 'IsString' instance to construct new values of this type
newtype TsVectorConfig = TsVectorConfig ByteString
deriving (Show, Eq, Ord, IsString)
instance Pg.FromField TsVector where
fromField field d =
if Pg.typeOid field /= Pg.typoid pgTsVectorTypeInfo
then Pg.returnError Pg.Incompatible field ""
else case d of
Just d' -> pure (TsVector d')
Nothing -> Pg.returnError Pg.UnexpectedNull field ""
instance Pg.ToField TsVector where
toField (TsVector d) =
Pg.Many [ Pg.Plain "($$"
, Pg.Plain (byteString d)
, Pg.Plain "$$::tsvector)" ]
instance FromBackendRow Postgres TsVector
instance HasSqlEqualityCheck Postgres TsVectorConfig
instance HasSqlQuantifiedEqualityCheck Postgres TsVectorConfig
instance HasSqlEqualityCheck Postgres TsVector
instance HasSqlQuantifiedEqualityCheck Postgres TsVector
-- | A full-text search configuration with sensible defaults for english
english :: TsVectorConfig
english = TsVectorConfig "english"
-- | The Postgres @to_tsvector@ function. Given a configuration and string,
-- return the @TSVECTOR@ that represents the contents of the string.
toTsVector :: BeamSqlBackendIsString Postgres str
=> Maybe TsVectorConfig -> QGenExpr context Postgres s str
-> QGenExpr context Postgres s TsVector
toTsVector Nothing (QExpr x) =
QExpr (fmap (\(PgExpressionSyntax x') ->
PgExpressionSyntax $
emit "to_tsvector(" <> x' <> emit ")") x)
toTsVector (Just (TsVectorConfig configNm)) (QExpr x) =
QExpr (fmap (\(PgExpressionSyntax x') -> PgExpressionSyntax $
emit "to_tsvector('" <> escapeString configNm <> emit "', " <> x' <> emit ")") x)
-- | Determine if the given @TSQUERY@ matches the document represented by the
-- @TSVECTOR@. Behaves exactly like the similarly-named operator in postgres.
(@@) :: QGenExpr context Postgres s TsVector
-> QGenExpr context Postgres s TsQuery
-> QGenExpr context Postgres s Bool
QExpr vec @@ QExpr q =
QExpr (pgBinOp "@@" <$> vec <*> q)
-- ** TsQuery type
-- | A query that can be run against a document contained in a 'TsVector'.
--
-- When this field is embedded in a beam table, 'defaultMigratableDbSettings'
-- will give the column the postgres @TSVECTOR@ type
newtype TsQuery = TsQuery ByteString
deriving (Show, Eq, Ord)
instance HasSqlEqualityCheck Postgres TsQuery
instance HasSqlQuantifiedEqualityCheck Postgres TsQuery
instance Pg.FromField TsQuery where
fromField field d =
if Pg.typeOid field /= Pg.typoid pgTsQueryTypeInfo
then Pg.returnError Pg.Incompatible field ""
else case d of
Just d' -> pure (TsQuery d')
Nothing -> Pg.returnError Pg.UnexpectedNull field ""
instance FromBackendRow Postgres TsQuery
-- | The Postgres @to_tsquery@ function. Given a configuration and string,
-- return the @TSQUERY@ that represents the contents of the string.
toTsQuery :: BeamSqlBackendIsString Postgres str
=> Maybe TsVectorConfig -> QGenExpr context Postgres s str
-> QGenExpr context Postgres s TsQuery
toTsQuery Nothing (QExpr x) =
QExpr (fmap (\(PgExpressionSyntax x') ->
PgExpressionSyntax $
emit "to_tsquery(" <> x' <> emit ")") x)
toTsQuery (Just (TsVectorConfig configNm)) (QExpr x) =
QExpr (fmap (\(PgExpressionSyntax x') -> PgExpressionSyntax $
emit "to_tsquery('" <> escapeString configNm <> emit "', " <> x' <> emit ")") x)
-- ** Array operators
-- TODO this should be robust to slices
-- | Index into the given array. This translates to the @<array>[<index>]@
-- syntax in postgres. The beam operator name has been chosen to match the
-- 'Data.Vector.(!)' operator.
(!.) :: Integral ix
=> QGenExpr context Postgres s (V.Vector a)
-> QGenExpr context Postgres s ix
-> QGenExpr context Postgres s a
QExpr v !. QExpr ix =
QExpr (index <$> v <*> ix)
where
index (PgExpressionSyntax v') (PgExpressionSyntax ix') =
PgExpressionSyntax (emit "(" <> v' <> emit ")[" <> ix' <> emit "]")
-- | Postgres @array_dims()@ function. Returns a textual representation of the
-- dimensions of the array.
arrayDims_ :: BeamSqlBackendIsString Postgres text
=> QGenExpr context Postgres s (V.Vector a)
-> QGenExpr context Postgres s text
arrayDims_ (QExpr v) = QExpr (fmap (\(PgExpressionSyntax v') -> PgExpressionSyntax (emit "array_dims(" <> v' <> emit ")")) v)
type family CountDims (v :: *) :: Nat where
CountDims (V.Vector a) = 1 + CountDims a
CountDims a = 0
type family WithinBounds (dim :: Nat) (v :: *) :: Constraint where
WithinBounds dim v =
If ((dim <=? CountDims v) && (1 <=? dim))
(() :: Constraint)
(TypeError ( ('Text "Dimension " ':<>: 'ShowType dim ':<>: 'Text " is out of bounds.") ':$$:
('Text "The type " ':<>: 'ShowType v ':<>: 'Text " has " ':<>: 'ShowType (CountDims v) ':<>: 'Text " dimension(s).") ':$$:
('Text "Hint: The dimension should be a natural between 1 and " ':<>: 'ShowType (CountDims v)) ))
-- | Return the upper or lower bound of the given array at the given dimension
-- (statically supplied as a type application on a 'GHC.TypeLits.Nat'). Note
-- that beam will attempt to statically determine if the dimension is in range.
-- GHC errors will be thrown if this cannot be proved.
--
-- For example, to get the upper bound of the 2nd-dimension of an array:
--
-- @
-- arrayUpper_ @2 vectorValuedExpression
-- @
arrayUpper_, arrayLower_
:: forall (dim :: Nat) context num v s.
(KnownNat dim, WithinBounds dim (V.Vector v), Integral num)
=> QGenExpr context Postgres s (V.Vector v)
-> QGenExpr context Postgres s num
arrayUpper_ v =
unsafeRetype (arrayUpperUnsafe_ v (val_ (natVal (Proxy @dim) :: Integer)) :: QGenExpr context Postgres s (Maybe Integer))
arrayLower_ v =
unsafeRetype (arrayLowerUnsafe_ v (val_ (natVal (Proxy @dim) :: Integer)) :: QGenExpr context Postgres s (Maybe Integer))
-- | These functions can be used to find the lower and upper bounds of an array
-- where the dimension number is not known until run-time. They are marked
-- unsafe because they may cause query processing to fail at runtime, even if
-- they typecheck successfully.
arrayUpperUnsafe_, arrayLowerUnsafe_
:: (Integral dim, Integral length)
=> QGenExpr context Postgres s (V.Vector v)
-> QGenExpr context Postgres s dim
-> QGenExpr context Postgres s (Maybe length)
arrayUpperUnsafe_ (QExpr v) (QExpr dim) =
QExpr (fmap (PgExpressionSyntax . mconcat) . sequenceA $
[ pure (emit "array_upper(")
, fromPgExpression <$> v
, pure (emit ", ")
, fromPgExpression <$> dim
, pure (emit ")") ])
arrayLowerUnsafe_ (QExpr v) (QExpr dim) =
QExpr (fmap (PgExpressionSyntax . mconcat) . sequenceA $
[ pure (emit "array_lower(")
, fromPgExpression <$> v
, pure (emit ", ")
, fromPgExpression <$> dim
, pure (emit ")") ])
-- | Get the size of the array at the given (statically known) dimension,
-- provided as a type-level 'Nat'. Like the 'arrayUpper_' and 'arrayLower_'
-- functions,throws a compile-time error if the dimension is out of bounds.
arrayLength_
:: forall (dim :: Nat) ctxt num v s.
(KnownNat dim, WithinBounds dim (V.Vector v), Integral num)
=> QGenExpr ctxt Postgres s (V.Vector v)
-> QGenExpr ctxt Postgres s num
arrayLength_ v =
unsafeRetype (arrayLengthUnsafe_ v (val_ (natVal (Proxy @dim) :: Integer)) :: QGenExpr ctxt Postgres s (Maybe Integer))
-- | Get the size of an array at a dimension not known until run-time. Marked
-- unsafe as this may cause runtime errors even if it type checks.
arrayLengthUnsafe_
:: (Integral dim, Integral num)
=> QGenExpr ctxt Postgres s (V.Vector v)
-> QGenExpr ctxt Postgres s dim
-> QGenExpr ctxt Postgres s (Maybe num)
arrayLengthUnsafe_ (QExpr a) (QExpr dim) =
QExpr $ fmap (PgExpressionSyntax . mconcat) $ sequenceA $
[ pure (emit "array_length(")
, fromPgExpression <$> a
, pure (emit ", ")
, fromPgExpression <$> dim
, pure (emit ")") ]
-- | The Postgres @@>@ operator. Returns true if every member of the second
-- array is present in the first.
isSupersetOf_ :: QGenExpr ctxt Postgres s (V.Vector a)
-> QGenExpr ctxt Postgres s (V.Vector a)
-> QGenExpr ctxt Postgres s Bool
isSupersetOf_ (QExpr haystack) (QExpr needles) =
QExpr (pgBinOp "@>" <$> haystack <*> needles)
-- | The Postgres @<@@ operator. Returns true if every member of the first
-- array is present in the second.
isSubsetOf_ :: QGenExpr ctxt Postgres s (V.Vector a)
-> QGenExpr ctxt Postgres s (V.Vector a)
-> QGenExpr ctxt Postgres s Bool
isSubsetOf_ (QExpr needles) (QExpr haystack) =
QExpr (pgBinOp "<@" <$> needles <*> haystack)
-- | Postgres @||@ operator. Concatenates two vectors and returns their result.
(++.) :: QGenExpr ctxt Postgres s (V.Vector a)
-> QGenExpr ctxt Postgres s (V.Vector a)
-> QGenExpr ctxt Postgres s (V.Vector a)
QExpr a ++. QExpr b =
QExpr (pgBinOp "||" <$> a <*> b)
-- ** Array expressions
-- | An expression context that determines which types of expressions can be put
-- inside an array element. Any scalar, aggregate, or window expression can be
-- placed within an array.
data PgArrayValueContext
-- | If you are extending beam-postgres and provide another expression context
-- that can be represented in an array, provide an empty instance of this class.
class PgIsArrayContext ctxt where
mkArraySyntax :: Proxy ctxt -> PgSyntax -> PgSyntax
mkArraySyntax _ s = emit "ARRAY" <> s
instance PgIsArrayContext PgArrayValueContext where
mkArraySyntax _ = id
instance PgIsArrayContext QValueContext
instance PgIsArrayContext QAggregateContext
instance PgIsArrayContext QWindowingContext
-- | Build a 1-dimensional postgres array from an arbitrary 'Foldable'
-- containing expressions.
array_ :: forall context f s a.
(PgIsArrayContext context, Foldable f)
=> f (QGenExpr context Postgres s a)
-> QGenExpr context Postgres s (V.Vector a)
array_ vs =
QExpr $ fmap (PgExpressionSyntax . mkArraySyntax (Proxy @context) . mconcat) $
sequenceA [ pure (emit "[")
, pgSepBy (emit ", ") <$> mapM (\(QExpr e) -> fromPgExpression <$> e) (toList vs)
, pure (emit "]") ]
-- | Build a 1-dimensional postgres array from a subquery
arrayOf_ :: Q Postgres db s (QExpr Postgres s a)
-> QGenExpr context Postgres s (V.Vector a)
arrayOf_ q =
let QExpr sub = subquery_ q
in QExpr (\t -> let PgExpressionSyntax sub' = sub t
in PgExpressionSyntax (emit "ARRAY(" <> sub' <> emit ")"))
-- ** Ranges
-- | Represents the types of bounds a range can have. A range can and often does have mis-matched
-- bound types.
data PgBoundType
= Inclusive
| Exclusive
deriving (Show, Generic)
instance Hashable PgBoundType
lBound :: PgBoundType -> ByteString
lBound Inclusive = "["
lBound Exclusive = "("
uBound :: PgBoundType -> ByteString
uBound Inclusive = "]"
uBound Exclusive = ")"
-- | Represents a single bound on a Range. A bound always has a type, but may not have a value
-- (the absense of a value represents unbounded).
data PgRangeBound a = PgRangeBound PgBoundType (Maybe a) deriving (Show, Generic)
inclusive :: a -> PgRangeBound a
inclusive = PgRangeBound Inclusive . Just
exclusive :: a -> PgRangeBound a
exclusive = PgRangeBound Exclusive . Just
unbounded :: PgRangeBound a
unbounded = PgRangeBound Exclusive Nothing
-- | A range of a given Haskell type (represented by @a@) stored as a given Postgres Range Type
-- (represented by @n@).
--
-- A reasonable example might be @Range PgInt8Range Int64@.
-- This represents a range of Haskell @Int64@ values stored as a range of 'bigint' in Postgres.
data PgRange (n :: *) a
= PgEmptyRange
| PgRange (PgRangeBound a) (PgRangeBound a)
deriving (Show, Generic)
instance Hashable a => Hashable (PgRangeBound a)
instance Hashable a => Hashable (PgRange n a)
-- | A class representing Postgres Range types and how to refer to them when speaking to the
-- database.
--
-- For custom Range types, create an uninhabited type, and make it an instance of this class.
class PgIsRange n where
-- | The range type name in the database.
rangeName :: ByteString
data PgInt4Range
instance PgIsRange PgInt4Range where
rangeName = "int4range"
data PgInt8Range
instance PgIsRange PgInt8Range where
rangeName = "int8range"
data PgNumRange
instance PgIsRange PgNumRange where
rangeName = "numrange"
data PgTsRange
instance PgIsRange PgTsRange where
rangeName = "tsrange"
data PgTsTzRange
instance PgIsRange PgTsTzRange where
rangeName = "tstzrange"
data PgDateRange
instance PgIsRange PgDateRange where
rangeName = "daterange"
instance (Pg.FromField a, Typeable a, Typeable n, Ord a) => Pg.FromField (PgRange n a) where
fromField field d = do
pgR :: Pg.PGRange a <- Pg.fromField field d
if Pg.isEmpty pgR
then pure PgEmptyRange
else let Pg.PGRange lRange rRange = pgR
in pure $ PgRange (boundConv lRange) (boundConv rRange)
-- According to Postgres docs, there is no such thing as an inclusive infinite bound.
-- https://www.postgresql.org/docs/10/static/rangetypes.html#RANGETYPES-INFINITE
boundConv :: Pg.RangeBound a -> PgRangeBound a
boundConv Pg.NegInfinity = PgRangeBound Exclusive Nothing
boundConv Pg.PosInfinity = PgRangeBound Exclusive Nothing
boundConv (Pg.Inclusive a) = PgRangeBound Inclusive (Just a)
boundConv (Pg.Exclusive a) = PgRangeBound Exclusive (Just a)
instance (Pg.ToField (Pg.PGRange a)) => Pg.ToField (PgRange n a) where
toField PgEmptyRange = Pg.toField (Pg.empty :: Pg.PGRange a)
toField (PgRange (PgRangeBound lt lb) (PgRangeBound ut ub)) = Pg.toField r'
where
r' = Pg.PGRange lb' ub'
lb' = case (lt, lb) of (_, Nothing) -> Pg.NegInfinity
(Inclusive, Just a) -> Pg.Inclusive a
(Exclusive, Just a) -> Pg.Exclusive a
ub' = case (ut, ub) of (_, Nothing) -> Pg.PosInfinity
(Inclusive, Just a) -> Pg.Inclusive a
(Exclusive, Just a) -> Pg.Exclusive a
instance HasSqlEqualityCheck Postgres (PgRange n a)
instance HasSqlQuantifiedEqualityCheck Postgres (PgRange n a)
instance (Pg.FromField a, Typeable a, Typeable n, Ord a) => FromBackendRow Postgres (PgRange n a)
instance (HasSqlValueSyntax PgValueSyntax a, PgIsRange n) =>
HasSqlValueSyntax PgValueSyntax (PgRange n a) where
sqlValueSyntax PgEmptyRange =
PgValueSyntax $
emit "'empty'::" <> escapeIdentifier (rangeName @n)
sqlValueSyntax (PgRange (PgRangeBound lbt mlBound) (PgRangeBound rbt muBound)) =
PgValueSyntax $
escapeIdentifier (rangeName @n) <> pgParens (pgSepBy (emit ", ") [lb, rb, bounds])
where
lb = sqlValueSyntax' mlBound
rb = sqlValueSyntax' muBound
bounds = emit "'" <> emit (lBound lbt <> uBound rbt) <> emit "'"
sqlValueSyntax' = fromPgValue . sqlValueSyntax
binOpDefault :: ByteString
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s b
-> QGenExpr context Postgres s c
binOpDefault symbol (QExpr r1) (QExpr r2) = QExpr (pgBinOp symbol <$> r1 <*> r2)
(-@>-) :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
(-@>-) = binOpDefault "@>"
(-@>) :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s Bool
(-@>) = binOpDefault "@>"
(-<@-) :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
(-<@-) = binOpDefault "<@"
(<@-) :: QGenExpr context Postgres s a
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
(<@-) = binOpDefault "<@"
(-&&-) :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
(-&&-) = binOpDefault "&&"
(-<<-) :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
(-<<-) = binOpDefault "<<"
(->>-) :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
(->>-) = binOpDefault ">>"
(-&<-) :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
(-&<-) = binOpDefault "&<"
(-&>-) :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
(-&>-) = binOpDefault "&>"
(--|--) :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
(--|--) = binOpDefault "-|-"
(-+-) :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
(-+-) = binOpDefault "+"
(-*-) :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
(-*-) = binOpDefault "*"
-- | The postgres range operator @-@ .
(-.-) :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
(-.-) = binOpDefault "-"
defUnaryFn :: ByteString
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s b
defUnaryFn fn (QExpr s) = QExpr (pgExprFrom <$> s)
where
pgExprFrom s' = PgExpressionSyntax (emit fn <> emit "(" <> fromPgExpression s' <> emit ")")
rLower_ :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (Maybe a)
rLower_ = defUnaryFn "LOWER"
rUpper_ :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (Maybe a)
rUpper_ = defUnaryFn "UPPER"
isEmpty_ :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
isEmpty_ = defUnaryFn "ISEMPTY"
lowerInc_ :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
lowerInc_ = defUnaryFn "LOWER_INC"
upperInc_ :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
upperInc_ = defUnaryFn "UPPER_INC"
lowerInf_ :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
lowerInf_ = defUnaryFn "LOWER_INF"
upperInf_ :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
upperInf_ = defUnaryFn "UPPER_INF"
rangeMerge_ :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
rangeMerge_ (QExpr r1) (QExpr r2) = QExpr (pgExprFrom <$> r1 <*> r2)
where
pgExprFrom r1' r2' =
PgExpressionSyntax
(emit "RANGE_MERGE(" <>
fromPgExpression r1' <>
emit ", " <>
fromPgExpression r2' <>
emit ")")
range_ :: forall n a context s. PgIsRange n
=> PgBoundType -- ^ Lower bound type
-> PgBoundType -- ^ Upper bound type
-> QGenExpr context Postgres s (Maybe a) -- ^. Lower bound value
-> QGenExpr context Postgres s (Maybe a) -- ^. Upper bound value
-> QGenExpr context Postgres s (PgRange n a)
range_ lbt ubt (QExpr e1) (QExpr e2) = QExpr (pgExprFrom <$> e1 <*> e2)
where
bounds = emit "'" <> emit (lBound lbt <> uBound ubt) <> emit "'"
pgExprFrom e1' e2' =
PgExpressionSyntax
(escapeIdentifier (rangeName @n) <>
pgParens (pgSepBy (emit ", ") [fromPgExpression e1', fromPgExpression e2', bounds]))
-- ** JSON
-- | The Postgres @JSON@ type, which stores textual values that represent JSON
-- objects. The type parameter indicates the Haskell type which the JSON
-- encodes. This type must be a member of 'FromJSON' and 'ToJSON' in order for
-- deserialization and serialization to work as expected.
--
-- The 'defaultMigratableDbSettings' function automatically assigns the postgres
-- @JSON@ type to fields with this type.
newtype PgJSON a = PgJSON a
deriving ( Show, Eq, Ord, Hashable, Monoid, Semigroup )
instance HasSqlEqualityCheck Postgres (PgJSON a)
instance HasSqlQuantifiedEqualityCheck Postgres (PgJSON a)
instance (Typeable x, FromJSON x) => Pg.FromField (PgJSON x) where
fromField field d =
if Pg.typeOid field /= Pg.typoid Pg.json
then Pg.returnError Pg.Incompatible field ""
else case decodeStrict =<< d of
Just d' -> pure (PgJSON d')
Nothing -> Pg.returnError Pg.UnexpectedNull field ""
instance (Typeable a, FromJSON a) => FromBackendRow Postgres (PgJSON a)
instance ToJSON a => HasSqlValueSyntax PgValueSyntax (PgJSON a) where
sqlValueSyntax (PgJSON a) =
PgValueSyntax $
emit "'" <> escapeString (BL.toStrict (encode a)) <> emit "'::json"
-- | The Postgres @JSONB@ type, which stores JSON-encoded data in a
-- postgres-specific binary format. Like 'PgJSON', the type parameter indicates
-- the Haskell type which the JSON encodes.
--
-- Fields with this type are automatically given the Postgres @JSONB@ type
newtype PgJSONB a = PgJSONB a
deriving ( Show, Eq, Ord, Hashable, Monoid, Semigroup )
instance HasSqlEqualityCheck Postgres (PgJSONB a)
instance HasSqlQuantifiedEqualityCheck Postgres (PgJSONB a)
instance (Typeable x, FromJSON x) => Pg.FromField (PgJSONB x) where
fromField field d =
if Pg.typeOid field /= Pg.typoid Pg.jsonb
then Pg.returnError Pg.Incompatible field ""
else case decodeStrict =<< d of
Just d' -> pure (PgJSONB d')
Nothing -> Pg.returnError Pg.UnexpectedNull field ""
instance (Typeable a, FromJSON a) => FromBackendRow Postgres (PgJSONB a)
instance ToJSON a => HasSqlValueSyntax PgValueSyntax (PgJSONB a) where
sqlValueSyntax (PgJSONB a) =
PgValueSyntax $
emit "'" <> escapeString (BL.toStrict (encode a)) <> emit "'::jsonb"
-- | Key-value pair, used as output of 'pgJsonEachText' and 'pgJsonEach'
data PgJSONEach valType f
= PgJSONEach
{ pgJsonEachKey :: C f T.Text
, pgJsonEachValue :: C f valType
} deriving Generic
instance Beamable (PgJSONEach valType)
-- | Output row of 'pgJsonKeys'
data PgJSONKey f = PgJSONKey { pgJsonKey :: C f T.Text }
deriving Generic
instance Beamable PgJSONKey
-- | Output row of 'pgJsonArrayElements' and 'pgJsonArrayElementsText'
data PgJSONElement a f = PgJSONElement { pgJsonElement :: C f a }
deriving Generic
instance Beamable (PgJSONElement a)
-- | Postgres provides separate @json_@ and @jsonb_@ functions. However, we know
-- what we're dealing with based on the type of data, so we can be less obtuse.
--
-- For more information on how these functions behave, see the Postgres manual
-- section on
-- <https://www.postgresql.org/docs/current/static/functions-json.html JSON>.
--
class IsPgJSON (json :: * -> *) where
-- | The @json_each@ or @jsonb_each@ function. Values returned as @json@ or
-- @jsonb@ respectively. Use 'pgUnnest' to join against the result
pgJsonEach :: QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONEach (json Value)))
-- | Like 'pgJsonEach', but returning text values instead
pgJsonEachText :: QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONEach T.Text))
-- | The @json_object_keys@ and @jsonb_object_keys@ function. Use 'pgUnnest'
-- to join against the result.
pgJsonKeys :: QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s (PgSetOf PgJSONKey)
-- | The @json_array_elements@ and @jsonb_array_elements@ function. Use
-- 'pgUnnest' to join against the result
pgJsonArrayElements :: QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONElement (json Value)))
-- | Like 'pgJsonArrayElements', but returning the values as 'T.Text'
pgJsonArrayElementsText :: QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONElement T.Text))
-- pgJsonToRecord
-- pgJsonToRecordSet
-- | The @json_typeof@ or @jsonb_typeof@ function
pgJsonTypeOf :: QGenExpr ctxt Postgres s (json a) -> QGenExpr ctxt Postgres s T.Text
-- | The @json_strip_nulls@ or @jsonb_strip_nulls@ function.
pgJsonStripNulls :: QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s (json b)
-- | The @json_agg@ or @jsonb_agg@ aggregate.
pgJsonAgg :: QExpr Postgres s a -> QAgg Postgres s (json a)
-- | The @json_object_agg@ or @jsonb_object_agg@. The first argument gives the
-- key source and the second the corresponding values.
pgJsonObjectAgg :: QExpr Postgres s key -> QExpr Postgres s value
-> QAgg Postgres s (json a)
instance IsPgJSON PgJSON where
pgJsonEach (QExpr a) =
QExpr $ fmap (PgExpressionSyntax . mappend (emit "json_each") . pgParens . fromPgExpression) a
pgJsonEachText (QExpr a) =
QExpr $ fmap (PgExpressionSyntax . mappend (emit "json_each_text") . pgParens . fromPgExpression) a
pgJsonKeys (QExpr a) =
QExpr $ fmap (PgExpressionSyntax . mappend (emit "json_object_keys") . pgParens . fromPgExpression) a
pgJsonArrayElements (QExpr a) =
QExpr $ fmap (PgExpressionSyntax . mappend (emit "json_array_elements") . pgParens . fromPgExpression) a
pgJsonArrayElementsText (QExpr a) =
QExpr $ fmap (PgExpressionSyntax . mappend (emit "json_array_elements_text") . pgParens . fromPgExpression) a
pgJsonTypeOf (QExpr a) =
QExpr $ fmap (PgExpressionSyntax . mappend (emit "json_typeof") . pgParens . fromPgExpression) a
pgJsonStripNulls (QExpr a) =
QExpr $ fmap (PgExpressionSyntax . mappend (emit "json_strip_nulls") . pgParens . fromPgExpression) a
pgJsonAgg (QExpr a) =
QExpr $ fmap (PgExpressionSyntax . mappend (emit "json_agg") . pgParens . fromPgExpression) a
pgJsonObjectAgg (QExpr keys) (QExpr values) =
QExpr $ fmap (PgExpressionSyntax . mappend (emit "json_object_agg") . pgParens . mconcat) $
sequenceA $ [ fromPgExpression <$> keys, pure (emit ", ")
, fromPgExpression <$> values ]
instance IsPgJSON PgJSONB where
pgJsonEach (QExpr a) =
QExpr $ fmap (PgExpressionSyntax . mappend (emit "jsonb_each") . pgParens . fromPgExpression) a
pgJsonEachText (QExpr a) =
QExpr $ fmap (PgExpressionSyntax . mappend (emit "jsonb_each_text") . pgParens . fromPgExpression) a
pgJsonKeys (QExpr a) =
QExpr $ fmap (PgExpressionSyntax . mappend (emit "jsonb_object_keys") . pgParens . fromPgExpression) a
pgJsonArrayElements (QExpr a) =
QExpr $ fmap (PgExpressionSyntax . mappend (emit "jsonb_array_elements") . pgParens . fromPgExpression) a
pgJsonArrayElementsText (QExpr a) =
QExpr $ fmap (PgExpressionSyntax . mappend (emit "jsonb_array_elements_text") . pgParens . fromPgExpression) a
pgJsonTypeOf (QExpr a) =
QExpr $ fmap (PgExpressionSyntax . mappend (emit "jsonb_typeof") . pgParens . fromPgExpression) a
pgJsonStripNulls (QExpr a) =
QExpr $ fmap (PgExpressionSyntax . mappend (emit "jsonb_strip_nulls") . pgParens . fromPgExpression) a
pgJsonAgg (QExpr a) =
QExpr $ fmap (PgExpressionSyntax . mappend (emit "jsonb_agg") . pgParens . fromPgExpression) a
pgJsonObjectAgg (QExpr keys) (QExpr values) =
QExpr $ fmap (PgExpressionSyntax . mappend (emit "jsonb_object_agg") . pgParens . mconcat) $
sequenceA $ [ fromPgExpression <$> keys, pure (emit ", ")
, fromPgExpression <$> values ]
-- | Postgres @@>@ and @<@@ operators for JSON. Return true if the
-- json object pointed to by the arrow is completely contained in the other. See
-- the Postgres documentation for more in formation on what this means.
(@>), (<@) :: IsPgJSON json
=> QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s (json b)
-> QGenExpr ctxt Postgres s Bool
QExpr a @> QExpr b =
QExpr (pgBinOp "@>" <$> a <*> b)
QExpr a <@ QExpr b =
QExpr (pgBinOp "<@" <$> a <*> b)
-- | Access a JSON array by index. Corresponds to the Postgres @->@ operator.
-- See '(->$)' for the corresponding operator for object access.
(->#) :: IsPgJSON json
=> QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s Int
-> QGenExpr ctxt Postgres s (json b)
QExpr a -># QExpr b =
QExpr (pgBinOp "->" <$> a <*> b)
-- | Acces a JSON object by key. Corresponds to the Postgres @->@ operator. See
-- '(->#)' for the corresponding operator for arrays.
(->$) :: IsPgJSON json
=> QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s T.Text
-> QGenExpr ctxt Postgres s (json b)
QExpr a ->$ QExpr b =
QExpr (pgBinOp "->" <$> a <*> b)
-- | Access a JSON array by index, returning the embedded object as a string.
-- Corresponds to the Postgres @->>@ operator. See '(->>$)' for the
-- corresponding operator on objects.
(->>#) :: IsPgJSON json
=> QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s Int
-> QGenExpr ctxt Postgres s T.Text
QExpr a ->># QExpr b =
QExpr (pgBinOp "->>" <$> a <*> b)
-- | Access a JSON object by key, returning the embedded object as a string.
-- Corresponds to the Postgres @->>@ operator. See '(->>#)' for the
-- corresponding operator on arrays.
(->>$) :: IsPgJSON json
=> QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s T.Text
-> QGenExpr ctxt Postgres s T.Text
QExpr a ->>$ QExpr b =
QExpr (pgBinOp "->>" <$> a <*> b)
-- | Access a deeply nested JSON object. The first argument is the JSON object
-- to look within, the second is the path of keys from the first argument to the
-- target. Returns the result as a new json value. Note that the postgres
-- function allows etiher string keys or integer indices, but this function only
-- allows string keys. PRs to improve this functionality are welcome.
(#>) :: IsPgJSON json
=> QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s (V.Vector T.Text)
-> QGenExpr ctxt Postgres s (json b)
QExpr a #> QExpr b =
QExpr (pgBinOp "#>" <$> a <*> b)
-- | Like '(#>)' but returns the result as a string.
(#>>) :: IsPgJSON json
=> QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s (V.Vector T.Text)
-> QGenExpr ctxt Postgres s T.Text
QExpr a #>> QExpr b =
QExpr (pgBinOp "#>>" <$> a <*> b)
-- | Postgres @?@ operator. Checks if the given string exists as top-level key
-- of the json object.
(?) :: IsPgJSON json
=> QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s T.Text
-> QGenExpr ctxt Postgres s Bool
QExpr a ? QExpr b =
QExpr (pgBinOp "?" <$> a <*> b)
-- | Postgres @?|@ and @?&@ operators. Check if any or all of the given strings
-- exist as top-level keys of the json object respectively.
(?|), (?&) :: IsPgJSON json
=> QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s (V.Vector T.Text)
-> QGenExpr ctxt Postgres s Bool
QExpr a ?| QExpr b =
QExpr (pgBinOp "?|" <$> a <*> b)
QExpr a ?& QExpr b =
QExpr (pgBinOp "?&" <$> a <*> b)
-- | Postgres @||@ operator. Concatenates two jsonb values into a new jsonb value.
(||.) :: QGenExpr ctxt Postgres s (PgJSONB a)
-> QGenExpr ctxt Postgres s (PgJSONB a)
-> QGenExpr ctxt Postgres s (PgJSONB a)
QExpr a ||. QExpr b =
QExpr (pgBinOp "||" <$> a <*> b)
-- | Postgres @-@ operator on json objects. Returns the supplied json object
-- with the supplied key deleted. See 'withoutIdx' for the corresponding
-- operator on arrays.
withoutKey :: IsPgJSON json
=> QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s T.Text
-> QGenExpr ctxt Postgres s (json b)
QExpr a `withoutKey` QExpr b =
QExpr (pgBinOp "-" <$> a <*> b)
-- | Postgres @-@ operator on json arrays. See 'withoutKey' for the
-- corresponding operator on objects.
withoutIdx :: IsPgJSON json
=> QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s Int
-> QGenExpr ctxt Postgres s (json b)
QExpr a `withoutIdx` QExpr b =
QExpr (pgBinOp "-" <$> a <*> b)
-- | Postgres @#-@ operator. Removes all the keys specificied from the JSON
-- object and returns the result.
withoutKeys :: IsPgJSON json
=> QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s (V.Vector T.Text)
-> QGenExpr ctxt Postgres s (json b)
QExpr a `withoutKeys` QExpr b =
QExpr (pgBinOp "#-" <$> a <*> b)
-- | Postgres @json_array_length@ function. The supplied json object should be
-- an array, but this isn't checked at compile-time.
pgJsonArrayLength :: IsPgJSON json => QGenExpr ctxt Postgres s (json a)