Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update LegendTdsResultParser to handle columns without relationalType #2331

Merged
merged 10 commits into from
Oct 3, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,23 @@
import java.util.Objects;
import static java.util.Objects.requireNonNull;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LegendTdsResultParser
{
public static final String BUILDER = "builder";
public static final String TYPE = "type";
public static final String COLUMNS = "columns";
public static final String NAME = "name";
public static final String RELATIONAL_TYPE = "relationalType";
public static final String RESULT = "result";
public static final String ROWS = "rows";
public static final String VALUES = "values";
private static final List<JsonToken> SKIP_CHILDREN_TOKEN
= Collections.unmodifiableList(Arrays.asList(JsonToken.START_ARRAY, JsonToken.START_OBJECT));

private static final Logger LOGGER = LoggerFactory.getLogger(LegendTdsResultParser.class);

public static final String _TYPE = "_type";
private final JsonParser parser;
private List<LegendColumn> legendColumns;
Expand Down Expand Up @@ -96,14 +100,11 @@ private void parserHeader() throws IOException
acceptNextToken(JsonToken.START_ARRAY);
while (parser.nextToken() != JsonToken.END_ARRAY)
{


//parse column
acceptCurrent(JsonToken.START_OBJECT);
String columnName = parseNextTextField(NAME);
String type = parseNextTextField(TYPE);
String relationType = parseNextTextField(RELATIONAL_TYPE);
acceptNextToken(JsonToken.END_OBJECT);
skipUntilToken(JsonToken.END_OBJECT);
legendColumns.add(new LegendColumn(columnName, type));
}
acceptNextToken(JsonToken.END_OBJECT);
Expand Down Expand Up @@ -190,6 +191,19 @@ private void skipChildren() throws IOException
parser.skipChildren();
}

private void skipUntilToken(JsonToken expectedToken) throws IOException
{
parser.nextToken();
while (!expectedToken.equals(parser.currentToken()))
{
if (LOGGER.isDebugEnabled())
{
LOGGER.debug("Skipping token :" + parser.currentToken());
}
parser.nextToken();
}
}

private void acceptNextToken(JsonToken expectedToken) throws IOException
{
JsonToken jsonToken = parser.nextToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,17 @@ public void testMetadata() throws SQLException
)
{
ResultSetMetaData resultSetMetaData = statement.getMetaData();
Assert.assertEquals(3, resultSetMetaData.getColumnCount());
Assert.assertEquals(5, resultSetMetaData.getColumnCount());
Assert.assertEquals("Id", resultSetMetaData.getColumnName(1));
Assert.assertEquals("Name", resultSetMetaData.getColumnName(2));
Assert.assertEquals("Employee Type", resultSetMetaData.getColumnName(3));
Assert.assertEquals("Full Name", resultSetMetaData.getColumnName(4));
Assert.assertEquals("Derived Name", resultSetMetaData.getColumnName(5));
Assert.assertEquals("int4", resultSetMetaData.getColumnTypeName(1));
Assert.assertEquals("varchar", resultSetMetaData.getColumnTypeName(2));
Assert.assertEquals("varchar", resultSetMetaData.getColumnTypeName(3));
Assert.assertEquals("varchar", resultSetMetaData.getColumnTypeName(4));
Assert.assertEquals("varchar", resultSetMetaData.getColumnTypeName(5));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,33 @@ public void testParseDataValidateResults() throws IOException
LOGGER.info("Row: {}, Column: {}: Value: {}", row, column, value);
column++;
}
Assert.assertEquals("Verify number of column", 8, column);
Assert.assertEquals("Verify number of columns", 8, column);
}
Assert.assertEquals("Verify number of rows", 2, row);
}
}

@Test
public void testParseDataNoRelationalTypeValidateResults() throws IOException
{
try (InputStream pureProjectInputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("legendTdsResultNoRelationalType.json");)
{
LegendTdsResultParser parser = new LegendTdsResultParser(pureProjectInputStream);
List<LegendColumn> legendColumns = parser.getLegendColumns();
int row = 0;
int column = 0;
while (parser.hasNext())
{
row++;
column = 0;
List<Object> nextRow = parser.next();
for (int i = 0; i < legendColumns.size(); i++)
{
Object value = nextRow.get(i);
LOGGER.info("Row: {}, Column: {}: Value: {}", row, column, value);
column++;
}
Assert.assertEquals("Verify number of columns", 2, column);
}
Assert.assertEquals("Verify number of rows", 2, row);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"builder": {
"_type": "tdsBuilder",
"columns": [
{
"name": "stringColumn1",
"type": "String"
},
{
"name": "stringColumn2",
"type": "String",
"relationalType": "sqlType"
}
]
},
"activities": [
{
"_type": "relational",
"sql": "select ..."
}
],
"result": {
"columns": [
"stringColumn1",
"stringColumn2"
],
"rows": [
{
"values": [
"foo",
"bar"
]
},
{
"values": [
"foo1",
"bar2"
]
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,16 @@ Service demo::H2PersonService
[
x|$x.id,
x|$x.name,
x|$x.type
x|$x.type,
x|$x.name + ' Smith',
x|$x.derivedName
],
[
'Id',
'Name',
'Employee Type'
'Employee Type',
'Full Name',
'Derived Name'
]
);
mapping: demo::DemoRelationalMapping;
Expand Down Expand Up @@ -171,6 +175,7 @@ Class demo::employee
name: String[1];
type: demo::employeeType[1];
startDate: Date[1];
derivedName() {$this.name+'_derived'} : String[1];
}


Expand Down