Skip to content

Commit df66569

Browse files
feat: serialize WithItem
- fixes #2144 Signed-off-by: Andreas Reichel <andreas@manticore-projects.com> Signed-off-by: manticore-projects <andreas@manticore-projects.com>
1 parent 0ab1319 commit df66569

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/main/java/net/sf/jsqlparser/statement/select/WithItem.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
import net.sf.jsqlparser.statement.insert.ParenthesedInsert;
1616
import net.sf.jsqlparser.statement.update.ParenthesedUpdate;
1717

18+
import java.io.Serializable;
1819
import java.util.ArrayList;
1920
import java.util.Collection;
2021
import java.util.Collections;
2122
import java.util.List;
2223
import java.util.Optional;
2324

24-
public class WithItem<T extends ParenthesedStatement> {
25-
25+
public class WithItem<T extends ParenthesedStatement> implements Serializable {
2626
private T statement;
2727
private Alias alias;
2828
private List<SelectItem<?>> withItemList;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package net.sf.jsqlparser.statement;
2+
3+
import net.sf.jsqlparser.JSQLParserException;
4+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
5+
import net.sf.jsqlparser.statement.select.PlainSelect;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.io.ByteArrayInputStream;
10+
import java.io.ByteArrayOutputStream;
11+
import java.io.IOException;
12+
import java.io.ObjectInputStream;
13+
import java.io.ObjectOutputStream;
14+
15+
public class SerializationTest {
16+
@Test
17+
void serializeWithItem() throws JSQLParserException, IOException, ClassNotFoundException {
18+
String sqlStr =
19+
"with sample_data(day, value) as (values ((0, 13), (1, 12), (2, 15), (3, 4), (4, 8), (5, 16))), test2 as (values (1,2,3)) \n"
20+
+ "select day, value from sample_data as a";
21+
22+
// Parse the SQL string into a PlainSelect object
23+
PlainSelect originalSelect = (PlainSelect) CCJSqlParserUtil.parse(sqlStr);
24+
25+
// Serialize the object to a byte array
26+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
27+
try (ObjectOutputStream out = new ObjectOutputStream(byteArrayOutputStream)) {
28+
out.writeObject(originalSelect);
29+
}
30+
31+
// Deserialize the object from the byte array
32+
PlainSelect deserializedSelect;
33+
try (ObjectInputStream in = new ObjectInputStream(
34+
new ByteArrayInputStream(byteArrayOutputStream.toByteArray()))) {
35+
deserializedSelect = (PlainSelect) in.readObject();
36+
}
37+
38+
// Verify that the original and deserialized objects are equal
39+
Assertions.assertEquals(originalSelect.toString(), deserializedSelect.toString(),
40+
"The deserialized object should be equal to the original");
41+
}
42+
43+
}

0 commit comments

Comments
 (0)