Skip to content

Commit

Permalink
feat: StructType syntax sugar
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
  • Loading branch information
manticore-projects committed Apr 23, 2024
1 parent e426c5a commit 6e9bf42
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/StructType.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public StructType(Dialect dialect, List<Map.Entry<String, ColDataType>> paramete
this.arguments = arguments;
}

public StructType(Dialect dialect, List<SelectItem<?>> arguments) {
this.dialect = dialect;
this.arguments = arguments;
}

public Dialect getDialect() {
return dialect;
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/select/SelectItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
package net.sf.jsqlparser.statement.select;

import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.expression.DoubleValue;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;

public class SelectItem<T extends Expression> extends ASTNodeAccessImpl {
Expand All @@ -28,6 +31,22 @@ public SelectItem(T expression, String aliasName) {
this.alias = new Alias(aliasName);
}

public SelectItem(Long expression, String aliasName) {
this((T) new LongValue(expression), aliasName);
}

public SelectItem(Integer expression, String aliasName) {
this((T) new LongValue(expression), aliasName);
}

public SelectItem(Double expression, String aliasName) {
this((T) new DoubleValue(expression), aliasName);
}

public SelectItem(String expression, String aliasName) {
this((T) new StringValue(expression), aliasName);
}

public SelectItem() {
this(null, (Alias) null);
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/net/sf/jsqlparser/expression/StructTypeTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package net.sf.jsqlparser.expression;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.SelectItem;
import net.sf.jsqlparser.test.TestUtils;
import org.junit.jupiter.api.Test;

import java.util.List;

class StructTypeTest {
@Test
void testStructTypeBigQuery() throws JSQLParserException {
Expand Down Expand Up @@ -40,6 +44,19 @@ void testStructTypeDuckDB() throws JSQLParserException {
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}

@Test
void testStructTypeConstructorDuckDB() throws JSQLParserException {
// @todo: check why the white-space after the "{" is needed?!
String sqlStr = "SELECT { t:'abc',len:5}";
List<SelectItem<?>> selectItems = List.of(
new SelectItem<>("abc", "t")
, new SelectItem<>(5, "len")
);
StructType struct = new StructType(StructType.Dialect.DUCKDB, selectItems);
PlainSelect select = new PlainSelect().withSelectItems( new SelectItem<>(struct));
TestUtils.assertStatementCanBeDeparsedAs(select, sqlStr, true);
}

@Test
void testStructTypeWithArgumentsDuckDB() throws JSQLParserException {
// @todo: check why the white-space after the "{" is needed?!
Expand Down

0 comments on commit 6e9bf42

Please sign in to comment.