@@ -81,6 +81,9 @@ module Database.Beam.Postgres.PgSpecific
81
81
, arrayUpper_ , arrayLower_
82
82
, arrayUpperUnsafe_ , arrayLowerUnsafe_
83
83
, arrayLength_ , arrayLengthUnsafe_
84
+ , arrayAppend_ , arrayPrepend_ , arrayRemove_
85
+ , arrayReplace_ , arrayShuffle_ , arraySample_
86
+ , arrayToString_ , arrayToStringWithNull_
84
87
85
88
, isSupersetOf_ , isSubsetOf_
86
89
@@ -410,6 +413,152 @@ isSubsetOf_ (QExpr needles) (QExpr haystack) =
410
413
QExpr a ++. QExpr b =
411
414
QExpr (pgBinOp " ||" <$> a <*> b)
412
415
416
+ -- | Postgres array_append(value) function.
417
+ --
418
+ -- Appends an element to the end of an array. Equivalent to the
419
+ -- @anycompatiblearray || anycompatible@ operator.
420
+ --
421
+ -- Notes:
422
+ -- - The array must be empty or one-dimensional (per Postgres rules for
423
+ -- concatenating an element with an array).
424
+ -- - If the array is NULL, the result is NULL. If the element is NULL,
425
+ -- a NULL element is appended.
426
+ arrayAppend_
427
+ :: QGenExpr ctxt Postgres s (V. Vector a )
428
+ -> QGenExpr ctxt Postgres s a
429
+ -> QGenExpr ctxt Postgres s (V. Vector a )
430
+ arrayAppend_ (QExpr arr) (QExpr el) =
431
+ QExpr (PgExpressionSyntax . mappend (emit " array_append" ) . pgParens . mconcat <$> sequenceA
432
+ [ fromPgExpression <$> arr
433
+ , pure (emit " , " )
434
+ , fromPgExpression <$> el
435
+ ])
436
+
437
+ -- | Postgres array_prepend(value) function.
438
+ --
439
+ -- Prepends an element to the beginning of an array. Equivalent to the
440
+ -- @anycompatible || anycompatiblearray@ operator.
441
+ --
442
+ -- Notes:
443
+ -- - The array must be empty or one-dimensional.
444
+ -- - If the array is NULL, the result is NULL. If the element is NULL,
445
+ -- a NULL element is prepended.
446
+ arrayPrepend_
447
+ :: QGenExpr ctxt Postgres s a
448
+ -> QGenExpr ctxt Postgres s (V. Vector a )
449
+ -> QGenExpr ctxt Postgres s (V. Vector a )
450
+ arrayPrepend_ (QExpr el) (QExpr arr) =
451
+ QExpr (PgExpressionSyntax . mappend (emit " array_prepend" ) . pgParens . mconcat <$> sequenceA
452
+ [ fromPgExpression <$> el
453
+ , pure (emit " , " )
454
+ , fromPgExpression <$> arr
455
+ ])
456
+
457
+ -- | Postgres array_remove(value) function.
458
+ --
459
+ -- Removes all elements equal to the given value from the array.
460
+ -- Comparisons use @IS NOT DISTINCT FROM@ semantics, so this can remove NULLs.
461
+ --
462
+ -- Notes:
463
+ -- - The array must be one-dimensional.
464
+ -- - Returns NULL only if the array is NULL; if the value is not present,
465
+ -- the original array is returned unchanged.
466
+ arrayRemove_
467
+ :: QGenExpr ctxt Postgres s (V. Vector a )
468
+ -> QGenExpr ctxt Postgres s a
469
+ -> QGenExpr ctxt Postgres s (V. Vector a )
470
+ arrayRemove_ (QExpr arr) (QExpr el) =
471
+ QExpr (PgExpressionSyntax . mappend (emit " array_remove" ) . pgParens . mconcat <$> sequenceA
472
+ [ fromPgExpression <$> arr
473
+ , pure (emit " , " )
474
+ , fromPgExpression <$> el
475
+ ])
476
+
477
+ -- | Postgres array_replace(array, from, to) function.
478
+ --
479
+ -- Replaces each element equal to the second argument with the third.
480
+ --
481
+ -- Notes:
482
+ -- - Comparisons use IS NOT DISTINCT FROM semantics; can replace NULLs.
483
+ -- - The array must be one-dimensional.
484
+ --
485
+ -- Example:
486
+ --
487
+ -- @
488
+ -- select_ $ pure $ arrayReplace_ (val_ $ V.fromList [1::Int32,2,5,4]) (val_ 5) (val_ 3)
489
+ -- -- => {1,2,3,4}
490
+ -- @
491
+ arrayReplace_
492
+ :: QGenExpr ctxt Postgres s (V. Vector a )
493
+ -> QGenExpr ctxt Postgres s a
494
+ -> QGenExpr ctxt Postgres s a
495
+ -> QGenExpr ctxt Postgres s (V. Vector a )
496
+ arrayReplace_ (QExpr arr) (QExpr fromVal) (QExpr toVal) =
497
+ QExpr (PgExpressionSyntax . mappend (emit " array_replace" ) . pgParens . mconcat <$> sequenceA
498
+ [ fromPgExpression <$> arr
499
+ , pure (emit " , " )
500
+ , fromPgExpression <$> fromVal
501
+ , pure (emit " , " )
502
+ , fromPgExpression <$> toVal
503
+ ])
504
+
505
+ -- | Postgres array_shuffle(array) function.
506
+ -- Randomly shuffles the first dimension.
507
+ arrayShuffle_
508
+ :: QGenExpr ctxt Postgres s (V. Vector a )
509
+ -> QGenExpr ctxt Postgres s (V. Vector a )
510
+ arrayShuffle_ (QExpr arr) =
511
+ QExpr (PgExpressionSyntax . mappend (emit " array_shuffle" ) . pgParens . fromPgExpression <$> arr)
512
+
513
+ -- | Postgres array_sample(array, n) function.
514
+ -- Randomly selects @n@ items from the array. For multidimensional arrays,
515
+ -- an "item" is a slice with a given first subscript.
516
+ --
517
+ -- Precondition: @n@ must not exceed the length of the first dimension.
518
+ arraySample_
519
+ :: Integral n
520
+ => QGenExpr ctxt Postgres s (V. Vector a )
521
+ -> QGenExpr ctxt Postgres s n
522
+ -> QGenExpr ctxt Postgres s (V. Vector a )
523
+ arraySample_ (QExpr arr) (QExpr n) =
524
+ QExpr (PgExpressionSyntax . mappend (emit " array_sample" ) . pgParens . mconcat <$> sequenceA
525
+ [ fromPgExpression <$> arr
526
+ , pure (emit " , " )
527
+ , fromPgExpression <$> n
528
+ ])
529
+
530
+ -- | Postgres array_to_string(array, delimiter) function.
531
+ -- Converts each element to text and joins with the delimiter. NULLs are omitted.
532
+ arrayToString_
533
+ :: BeamSqlBackendIsString Postgres text
534
+ => QGenExpr ctxt Postgres s (V. Vector a )
535
+ -> QGenExpr ctxt Postgres s text
536
+ -> QGenExpr ctxt Postgres s text
537
+ arrayToString_ (QExpr arr) (QExpr delim) =
538
+ QExpr (PgExpressionSyntax . mappend (emit " array_to_string" ) . pgParens . mconcat <$> sequenceA
539
+ [ fromPgExpression <$> arr
540
+ , pure (emit " , " )
541
+ , fromPgExpression <$> delim
542
+ ])
543
+
544
+ -- | Postgres array_to_string(array, delimiter, null_string) function.
545
+ -- Converts each element to text and joins with the delimiter. NULLs are
546
+ -- represented by the provided @null_string@.
547
+ arrayToStringWithNull_
548
+ :: BeamSqlBackendIsString Postgres text
549
+ => QGenExpr ctxt Postgres s (V. Vector a )
550
+ -> QGenExpr ctxt Postgres s text
551
+ -> QGenExpr ctxt Postgres s text
552
+ -> QGenExpr ctxt Postgres s text
553
+ arrayToStringWithNull_ (QExpr arr) (QExpr delim) (QExpr nullStr) =
554
+ QExpr (PgExpressionSyntax . mappend (emit " array_to_string" ) . pgParens . mconcat <$> sequenceA
555
+ [ fromPgExpression <$> arr
556
+ , pure (emit " , " )
557
+ , fromPgExpression <$> delim
558
+ , pure (emit " , " )
559
+ , fromPgExpression <$> nullStr
560
+ ])
561
+
413
562
-- ** Array expressions
414
563
415
564
-- | An expression context that determines which types of expressions can be put
0 commit comments