|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2026 JSQLParser |
| 6 | + * %% |
| 7 | + * Dual licensed under GNU LGPL 2.1 or Apache License 2.0 |
| 8 | + * #L% |
| 9 | + */ |
| 10 | +package net.sf.jsqlparser.statement; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 16 | + |
| 17 | +import net.sf.jsqlparser.JSQLParserException; |
| 18 | +import net.sf.jsqlparser.expression.LongValue; |
| 19 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 20 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 21 | +import net.sf.jsqlparser.util.deparser.ExpressionDeParser; |
| 22 | +import net.sf.jsqlparser.util.deparser.SelectDeParser; |
| 23 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.ValueSource; |
| 27 | + |
| 28 | +class PostgreSqlCopyTest { |
| 29 | + @ParameterizedTest |
| 30 | + @ValueSource(strings = { |
| 31 | + "COPY users (email, name) FROM STDIN WITH (FORMAT csv, HEADER true)", |
| 32 | + "COPY users TO STDOUT WITH (FORMAT csv, DELIMITER ';', NULL '')", |
| 33 | + "COPY users TO STDOUT WITH (FORMAT csv, FORCE_QUOTE (email, name))", |
| 34 | + "COPY users TO STDOUT WITH (FORMAT csv, FORCE_QUOTE *)", |
| 35 | + "COPY (SELECT email FROM users) TO STDOUT WITH (FORMAT csv, HEADER true)", |
| 36 | + "COPY users FROM STDIN WITH (FORMAT csv, HEADER MATCH, FORCE_NULL (name))", |
| 37 | + "COPY users FROM STDIN WITH (FORMAT csv, ON_ERROR ignore, ENCODING 'UTF8')", |
| 38 | + "COPY users TO '/tmp/export.csv' WITH (FORMAT csv)", |
| 39 | + "COPY users TO STDOUT (FORMAT csv, HEADER true)", |
| 40 | + "COPY users FROM STDIN", |
| 41 | + "COPY users TO STDOUT WITH", |
| 42 | + "COPY (WITH c AS (SELECT email FROM users) SELECT email FROM c) TO STDOUT WITH (FORMAT csv)", |
| 43 | + "COPY (SELECT 1 UNION ALL SELECT 2) TO STDOUT WITH (FORMAT csv)" |
| 44 | + }) |
| 45 | + void preservesCopyOptionsAndWithKeyword(String sql) throws JSQLParserException { |
| 46 | + CopyStatement copy = parse(sql); |
| 47 | + assertEquals(sql.contains(" WITH ") || sql.endsWith(" WITH"), copy.isWithKeyword()); |
| 48 | + assertRoundTrip(copy); |
| 49 | + assertEquals(copy.toString(), CCJSqlParserUtil.parse(copy.toString()).toString()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void optionalWithKeywordCanBeEditedWithoutChangingTheOptions() throws JSQLParserException { |
| 54 | + CopyStatement copy = parse("COPY users TO STDOUT WITH (FORMAT csv, HEADER true)"); |
| 55 | + assertTrue(copy.isWithKeyword()); |
| 56 | + copy.setWithKeyword(false); |
| 57 | + assertEquals("COPY users TO STDOUT (FORMAT csv, HEADER true)", copy.toString()); |
| 58 | + assertFalse(parse(copy.toString()).isWithKeyword()); |
| 59 | + copy.withWithKeyword(true); |
| 60 | + assertRoundTrip(copy); |
| 61 | + copy.setOptions(null); |
| 62 | + assertEquals("COPY users TO STDOUT WITH", copy.toString()); |
| 63 | + assertRoundTrip(copy); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void passesQueryExpressionsToTheConfiguredDeparser() throws JSQLParserException { |
| 68 | + CopyStatement copy = parse("COPY (SELECT 7 FROM users WHERE id > 8) " |
| 69 | + + "TO STDOUT WITH (FORMAT csv)"); |
| 70 | + StringBuilder buffer = new StringBuilder(); |
| 71 | + ExpressionDeParser expressions = new ExpressionDeParser() { |
| 72 | + @Override |
| 73 | + public <S> StringBuilder visit(LongValue value, S context) { |
| 74 | + return getBuilder().append(value.getValue() + 100); |
| 75 | + } |
| 76 | + }; |
| 77 | + copy.accept(new StatementDeParser(expressions, new SelectDeParser(), buffer), null); |
| 78 | + assertEquals("COPY (SELECT 107 FROM users WHERE id > 108) TO STDOUT WITH (FORMAT csv)", |
| 79 | + buffer.toString()); |
| 80 | + assertRoundTrip(parse(buffer.toString())); |
| 81 | + } |
| 82 | + |
| 83 | + @ParameterizedTest |
| 84 | + @ValueSource(strings = {"WITH ()", "WITH (FORMAT csv,)", "WITH (,FORMAT csv)", |
| 85 | + "WITH FORMAT csv", "WITH (FORMAT csv,, HEADER true)", "WITH (FORCE_QUOTE (email)"}) |
| 86 | + void rejectsMalformedWithOptions(String options) { |
| 87 | + assertThrows(JSQLParserException.class, () -> parse("COPY users TO STDOUT " + options)); |
| 88 | + } |
| 89 | + |
| 90 | + private static CopyStatement parse(String sql) throws JSQLParserException { |
| 91 | + return (CopyStatement) CCJSqlParserUtil.parse(sql, p -> p.withDialect(Dialect.POSTGRESQL)); |
| 92 | + } |
| 93 | + |
| 94 | + private static void assertRoundTrip(CopyStatement copy) throws JSQLParserException { |
| 95 | + StringBuilder buffer = new StringBuilder(); |
| 96 | + copy.accept(new StatementDeParser(buffer), null); |
| 97 | + assertEquals(copy.toString(), buffer.toString()); |
| 98 | + assertEquals(copy.toString(), parse(buffer.toString()).toString()); |
| 99 | + } |
| 100 | +} |
0 commit comments