Skip to content

Commit 3b41c51

Browse files
committed
Add support for posgresql arrays
1 parent c93d0f2 commit 3b41c51

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/formatter.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub(crate) fn format(
7676
formatter.format_no_change(token, &mut formatted_query);
7777
continue;
7878
}
79+
7980
match token.kind {
8081
TokenKind::Whitespace => {
8182
// ignore (we do our own whitespace formatting)

src/lib.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,10 @@ mod tests {
488488
#[test]
489489
fn it_formats_type_specifiers() {
490490
let input = "SELECT id, ARRAY [] :: UUID [] FROM UNNEST($1 :: UUID []) WHERE $1::UUID[] IS NOT NULL;";
491-
let options = FormatOptions::default();
491+
let options = FormatOptions {
492+
dialect: Dialect::PostgreSql,
493+
..Default::default()
494+
};
492495
let expected = indoc!(
493496
"
494497
SELECT
@@ -503,6 +506,51 @@ mod tests {
503506
assert_eq!(format(input, &QueryParams::None, &options), expected);
504507
}
505508

509+
#[test]
510+
fn it_formats_arrays_as_function_arguments() {
511+
let input =
512+
"SELECT array_position(ARRAY['sun','mon','tue', 'wed', 'thu','fri', 'sat'], 'mon');";
513+
let options = FormatOptions {
514+
dialect: Dialect::PostgreSql,
515+
..Default::default()
516+
};
517+
let expected = indoc!(
518+
"
519+
SELECT
520+
array_position(
521+
ARRAY['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'],
522+
'mon'
523+
);"
524+
);
525+
526+
assert_eq!(format(input, &QueryParams::None, &options), expected);
527+
}
528+
529+
#[test]
530+
fn it_formats_arrays_as_values() {
531+
let input = " INSERT INTO t VALUES('a', ARRAY[0, 1,2,3], ARRAY[['a','b'], ['c' ,'d']]);";
532+
let options = FormatOptions {
533+
dialect: Dialect::PostgreSql,
534+
max_inline_block: 10,
535+
max_inline_top_level: Some(50),
536+
..Default::default()
537+
};
538+
let expected = indoc!(
539+
"
540+
INSERT INTO t
541+
VALUES (
542+
'a',
543+
ARRAY[0, 1, 2, 3],
544+
ARRAY[
545+
['a', 'b'],
546+
['c', 'd']
547+
]
548+
);"
549+
);
550+
551+
assert_eq!(format(input, &QueryParams::None, &options), expected);
552+
}
553+
506554
#[test]
507555
fn it_formats_limit_of_single_value_and_offset() {
508556
let input = "LIMIT 5 OFFSET 8;";

0 commit comments

Comments
 (0)