Skip to content

Commit 5c0de00

Browse files
committed
Add support for posgresql arrays
1 parent acbf5e7 commit 5c0de00

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
@@ -491,7 +491,10 @@ mod tests {
491491
#[test]
492492
fn it_formats_type_specifiers() {
493493
let input = "SELECT id, ARRAY [] :: UUID [] FROM UNNEST($1 :: UUID []) WHERE $1::UUID[] IS NOT NULL;";
494-
let options = FormatOptions::default();
494+
let options = FormatOptions {
495+
dialect: Dialect::PostgreSql,
496+
..Default::default()
497+
};
495498
let expected = indoc!(
496499
"
497500
SELECT
@@ -506,6 +509,51 @@ mod tests {
506509
assert_eq!(format(input, &QueryParams::None, &options), expected);
507510
}
508511

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

0 commit comments

Comments
 (0)