Skip to content

Commit

Permalink
formatting fixes via spotlessApply
Browse files Browse the repository at this point in the history
  • Loading branch information
nicky6s committed Aug 4, 2024
1 parent 1de2a39 commit 3f60913
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public DeleteDeParser(ExpressionVisitor<StringBuilder> expressionVisitor,
public void deParse(Delete delete) {
if (delete.getWithItemsList() != null && !delete.getWithItemsList().isEmpty()) {
buffer.append("WITH ");
for (Iterator<WithItem<?>> iter = delete.getWithItemsList().iterator(); iter.hasNext();) {
for (Iterator<WithItem<?>> iter = delete.getWithItemsList().iterator(); iter
.hasNext();) {
WithItem<?> withItem = iter.next();
buffer.append(withItem);
if (iter.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public InsertDeParser(ExpressionVisitor<StringBuilder> expressionVisitor,
public void deParse(Insert insert) {
if (insert.getWithItemsList() != null && !insert.getWithItemsList().isEmpty()) {
buffer.append("WITH ");
for (Iterator<WithItem<?>> iter = insert.getWithItemsList().iterator(); iter.hasNext();) {
for (Iterator<WithItem<?>> iter = insert.getWithItemsList().iterator(); iter
.hasNext();) {
WithItem<?> withItem = iter.next();
withItem.accept(this.selectVisitor, null);
if (iter.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public UpdateDeParser(ExpressionVisitor<StringBuilder> expressionVisitor,
public void deParse(Update update) {
if (update.getWithItemsList() != null && !update.getWithItemsList().isEmpty()) {
buffer.append("WITH ");
for (Iterator<WithItem<?>> iter = update.getWithItemsList().iterator(); iter.hasNext();) {
for (Iterator<WithItem<?>> iter = update.getWithItemsList().iterator(); iter
.hasNext();) {
WithItem<?> withItem = iter.next();
buffer.append(withItem);
if (iter.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,4 @@ public void visit(Values values) {
visit(values, null);
}

}
}
20 changes: 14 additions & 6 deletions src/test/java/net/sf/jsqlparser/statement/delete/DeleteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,13 @@ public void testWith() throws JSQLParserException {
List<WithItem<?>> withItems = delete.getWithItemsList();
assertEquals("cfe.instrument_ref", delete.getTable().getFullyQualifiedName());
assertEquals(2, withItems.size());
SelectItem selectItem1 = withItems.get(0).getSelect().getPlainSelect().getSelectItems().get(0);
SelectItem selectItem1 =
withItems.get(0).getSelect().getPlainSelect().getSelectItems().get(0);
assertEquals("1", selectItem1.getExpression().toString());
assertEquals(" id_instrument_ref", selectItem1.getAlias().toString());
assertEquals(" a", withItems.get(0).getAlias().toString());
SelectItem selectItem2 = withItems.get(1).getSelect().getPlainSelect().getSelectItems().get(0);
SelectItem selectItem2 =
withItems.get(1).getSelect().getPlainSelect().getSelectItems().get(0);
assertEquals("1", selectItem2.getExpression().toString());
assertEquals(" id_instrument_ref", selectItem2.getAlias().toString());
assertEquals(" b", withItems.get(1).getAlias().toString());
Expand Down Expand Up @@ -326,9 +328,12 @@ void testDeleteAndInsertWithin2Ctes() throws JSQLParserException {
assertEquals(" deleted", withItems.get(0).getAlias().toString());
Insert insert = withItems.get(1).getInsert().getInsert();
assertEquals("x", insert.getTable().toString());
assertEquals("SELECT bar FROM b WHERE y IN (SELECT y FROM deleted)", insert.getSelect().toString());
assertEquals("SELECT bar FROM b WHERE y IN (SELECT y FROM deleted)",
insert.getSelect().toString());
assertEquals(" RETURNING w", insert.getReturningClause().toString());
assertEquals("INSERT INTO x (foo) SELECT bar FROM b WHERE y IN (SELECT y FROM deleted) RETURNING w", insert.toString());
assertEquals(
"INSERT INTO x (foo) SELECT bar FROM b WHERE y IN (SELECT y FROM deleted) RETURNING w",
insert.toString());
assertEquals(" inserted", withItems.get(1).getAlias().toString());
}

Expand Down Expand Up @@ -357,9 +362,12 @@ void testSelectAndInsertWithin2Ctes() throws JSQLParserException {
assertEquals(" selection", withItems.get(0).getAlias().toString());
Insert insert = withItems.get(1).getInsert().getInsert();
assertEquals("x", insert.getTable().toString());
assertEquals("SELECT bar FROM b WHERE y IN (SELECT y FROM selection)", insert.getSelect().toString());
assertEquals("SELECT bar FROM b WHERE y IN (SELECT y FROM selection)",
insert.getSelect().toString());
assertEquals(" RETURNING w", insert.getReturningClause().toString());
assertEquals("INSERT INTO x (foo) SELECT bar FROM b WHERE y IN (SELECT y FROM selection) RETURNING w", insert.toString());
assertEquals(
"INSERT INTO x (foo) SELECT bar FROM b WHERE y IN (SELECT y FROM selection) RETURNING w",
insert.toString());
assertEquals(" inserted", withItems.get(1).getAlias().toString());
}

Expand Down
55 changes: 37 additions & 18 deletions src/test/java/net/sf/jsqlparser/statement/insert/InsertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,25 +279,29 @@ public void testInsertSelect() throws JSQLParserException {

@Test
public void testInsertWithSelect() throws JSQLParserException {
String sqlStr1 = "INSERT INTO mytable (mycolumn) WITH a AS (SELECT mycolumn FROM mytable) SELECT mycolumn FROM a";
String sqlStr1 =
"INSERT INTO mytable (mycolumn) WITH a AS (SELECT mycolumn FROM mytable) SELECT mycolumn FROM a";
Insert insert1 = (Insert) assertSqlCanBeParsedAndDeparsed(sqlStr1, true);
List<WithItem<?>> insertWithItems1 = insert1.getWithItemsList();
List<WithItem<?>> selectWithItems1 = insert1.getSelect().getWithItemsList();
assertEquals("mytable", insert1.getTable().getFullyQualifiedName());
assertNull(insertWithItems1);
assertEquals(1, selectWithItems1.size());
assertEquals("SELECT mycolumn FROM mytable", selectWithItems1.get(0).getSelect().getPlainSelect().toString());
assertEquals("SELECT mycolumn FROM mytable",
selectWithItems1.get(0).getSelect().getPlainSelect().toString());
assertEquals(" a", selectWithItems1.get(0).getAlias().toString());

String sqlStr2 = "INSERT INTO mytable (mycolumn) (WITH a AS (SELECT mycolumn FROM mytable) SELECT mycolumn FROM a)";
String sqlStr2 =
"INSERT INTO mytable (mycolumn) (WITH a AS (SELECT mycolumn FROM mytable) SELECT mycolumn FROM a)";
Insert insert2 = (Insert) assertSqlCanBeParsedAndDeparsed(sqlStr2, true);
List<WithItem<?>> insertWithItems2 = insert2.getWithItemsList();
assertEquals("mytable", insert2.getTable().getFullyQualifiedName());
assertNull(insertWithItems2);
ParenthesedSelect select = (ParenthesedSelect) insert2.getSelect();
List<WithItem<?>> selectWithItems2 = select.getSelect().getWithItemsList();
assertEquals(1, selectWithItems2.size());
assertEquals("SELECT mycolumn FROM mytable", selectWithItems2.get(0).getSelect().getPlainSelect().toString());
assertEquals("SELECT mycolumn FROM mytable",
selectWithItems2.get(0).getSelect().getPlainSelect().toString());
assertEquals(" a", selectWithItems2.get(0).getAlias().toString());
}

Expand Down Expand Up @@ -364,15 +368,17 @@ public void testKeywordPrecisionIssue363() throws JSQLParserException {

@Test
public void testWithDeparsingIssue406() throws JSQLParserException {
String sqlStr = "insert into mytab3 (a,b,c) select a,b,c from mytab where exists(with t as (select * from mytab2) select * from t)";
String sqlStr =
"insert into mytab3 (a,b,c) select a,b,c from mytab where exists(with t as (select * from mytab2) select * from t)";
Insert insert = (Insert) assertSqlCanBeParsedAndDeparsed(sqlStr, true);
List<WithItem<?>> insertWithItems = insert.getWithItemsList();
List<WithItem<?>> selectWithItems = insert.getSelect().getWithItemsList();
assertEquals("mytab3", insert.getTable().getFullyQualifiedName());
assertNull(insertWithItems);
assertNull(selectWithItems);
ExistsExpression exists = (ExistsExpression) insert.getPlainSelect().getWhere();
assertEquals("(WITH t AS (SELECT * FROM mytab2) SELECT * FROM t)", exists.getRightExpression().toString());
assertEquals("(WITH t AS (SELECT * FROM mytab2) SELECT * FROM t)",
exists.getRightExpression().toString());
}

@Test
Expand Down Expand Up @@ -412,12 +418,14 @@ public void testInsertKeyWordIntervalIssue682() throws JSQLParserException {

@Test
public void testWithAtFront() throws JSQLParserException {
String sqlStr = "WITH foo AS ( SELECT attr FROM bar ) INSERT INTO lalelu (attr) SELECT attr FROM foo";
String sqlStr =
"WITH foo AS ( SELECT attr FROM bar ) INSERT INTO lalelu (attr) SELECT attr FROM foo";
Insert insert = (Insert) assertSqlCanBeParsedAndDeparsed(sqlStr, true);
List<WithItem<?>> insertWithItems = insert.getWithItemsList();
assertEquals("lalelu", insert.getTable().getFullyQualifiedName());
assertEquals(1, insertWithItems.size());
assertEquals("SELECT attr FROM bar", insertWithItems.get(0).getSelect().getPlainSelect().toString());
assertEquals("SELECT attr FROM bar",
insertWithItems.get(0).getSelect().getPlainSelect().toString());
assertEquals(" foo", insertWithItems.get(0).getAlias().toString());
assertEquals("SELECT attr FROM foo", insert.getSelect().toString());
assertEquals("foo", insert.getSelect().getPlainSelect().getFromItem().toString());
Expand Down Expand Up @@ -456,12 +464,14 @@ public void testDisableKeywordIssue945() throws JSQLParserException {

@Test
public void testWithListIssue282() throws JSQLParserException {
String sqlStr = "WITH myctl AS (SELECT a, b FROM mytable) INSERT INTO mytable SELECT a, b FROM myctl";
String sqlStr =
"WITH myctl AS (SELECT a, b FROM mytable) INSERT INTO mytable SELECT a, b FROM myctl";
Insert insert = (Insert) assertSqlCanBeParsedAndDeparsed(sqlStr, true);
List<WithItem<?>> insertWithItems = insert.getWithItemsList();
assertEquals("mytable", insert.getTable().getFullyQualifiedName());
assertEquals(1, insertWithItems.size());
assertEquals("SELECT a, b FROM mytable", insertWithItems.get(0).getSelect().getPlainSelect().toString());
assertEquals("SELECT a, b FROM mytable",
insertWithItems.get(0).getSelect().getPlainSelect().toString());
assertEquals(" myctl", insertWithItems.get(0).getAlias().toString());
assertEquals("SELECT a, b FROM myctl", insert.getSelect().toString());
assertEquals("myctl", insert.getSelect().getPlainSelect().getFromItem().toString());
Expand Down Expand Up @@ -510,10 +520,12 @@ public void testInsertUnionSelectIssue1491() throws JSQLParserException {
@Test
public void testWithSelectFromDual() throws JSQLParserException {
String sqlStr = "(with a as (select * from dual) select * from a)";
ParenthesedSelect parenthesedSelect = (ParenthesedSelect) assertSqlCanBeParsedAndDeparsed(sqlStr, true);
ParenthesedSelect parenthesedSelect =
(ParenthesedSelect) assertSqlCanBeParsedAndDeparsed(sqlStr, true);
List<WithItem<?>> withItems = parenthesedSelect.getSelect().getWithItemsList();
assertEquals(1, withItems.size());
assertEquals("SELECT * FROM dual", withItems.get(0).getSelect().getPlainSelect().toString());
assertEquals("SELECT * FROM dual",
withItems.get(0).getSelect().getPlainSelect().toString());
assertEquals(" a", withItems.get(0).getAlias().toString());
assertEquals("a", parenthesedSelect.getPlainSelect().getFromItem().toString());
assertEquals("[*]", parenthesedSelect.getPlainSelect().getSelectItems().toString());
Expand Down Expand Up @@ -578,7 +590,8 @@ public void insertOnConflictObjectsTest() throws JSQLParserException {
List<WithItem<?>> withItems = insert.getWithItemsList();
assertEquals("test", insert.getTable().getFullyQualifiedName());
assertEquals(1, withItems.size());
assertEquals("[1, 2, 3]", withItems.get(0).getSelect().getPlainSelect().getSelectItems().toString());
assertEquals("[1, 2, 3]",
withItems.get(0).getSelect().getPlainSelect().getSelectItems().toString());
assertEquals(" a", withItems.get(0).getAlias().toString());

Expression whereExpression = CCJSqlParserUtil.parseExpression("a=1", false);
Expand Down Expand Up @@ -647,7 +660,7 @@ void testMultiColumnConflictTargetIssue955() throws JSQLParserException {
@Test
public void testDefaultValues() throws JSQLParserException {
String statement = "INSERT INTO mytable DEFAULT VALUES";
//assertSqlCanBeParsedAndDeparsed(statement);
// assertSqlCanBeParsedAndDeparsed(statement);
Insert insert = (Insert) parserManager.parse(new StringReader(statement));
assertEquals("mytable", insert.getTable().getFullyQualifiedName());
assertEquals("INSERT INTO MYTABLE DEFAULT VALUES", insert.toString().toUpperCase());
Expand Down Expand Up @@ -782,9 +795,12 @@ void testDeleteAndInsertWithin2Ctes() throws JSQLParserException {
assertEquals(" deleted", withItems.get(0).getAlias().toString());
Insert innerInsert = withItems.get(1).getInsert().getInsert();
assertEquals("x", innerInsert.getTable().toString());
assertEquals("SELECT bar FROM b WHERE y IN (SELECT y FROM deleted)", innerInsert.getSelect().toString());
assertEquals("SELECT bar FROM b WHERE y IN (SELECT y FROM deleted)",
innerInsert.getSelect().toString());
assertEquals(" RETURNING w", innerInsert.getReturningClause().toString());
assertEquals("INSERT INTO x (foo) SELECT bar FROM b WHERE y IN (SELECT y FROM deleted) RETURNING w", innerInsert.toString());
assertEquals(
"INSERT INTO x (foo) SELECT bar FROM b WHERE y IN (SELECT y FROM deleted) RETURNING w",
innerInsert.toString());
assertEquals(" inserted", withItems.get(1).getAlias().toString());
}

Expand Down Expand Up @@ -812,9 +828,12 @@ void testSelectAndInsertWithin2Ctes() throws JSQLParserException {
assertEquals(" selection", withItems.get(0).getAlias().toString());
Insert innerInsert = withItems.get(1).getInsert().getInsert();
assertEquals("x", innerInsert.getTable().toString());
assertEquals("SELECT bar FROM b WHERE y IN (SELECT y FROM selection)", innerInsert.getSelect().toString());
assertEquals("SELECT bar FROM b WHERE y IN (SELECT y FROM selection)",
innerInsert.getSelect().toString());
assertEquals(" RETURNING w", innerInsert.getReturningClause().toString());
assertEquals("INSERT INTO x (foo) SELECT bar FROM b WHERE y IN (SELECT y FROM selection) RETURNING w", innerInsert.toString());
assertEquals(
"INSERT INTO x (foo) SELECT bar FROM b WHERE y IN (SELECT y FROM selection) RETURNING w",
innerInsert.toString());
assertEquals(" inserted", withItems.get(1).getAlias().toString());
}

Expand Down
Loading

0 comments on commit 3f60913

Please sign in to comment.