use of com.helger.genericode.v10.Row in project googleads-java-lib by googleads.
the class PqlTest method testCombineResultSet.
@Test
public void testCombineResultSet() {
Row row1 = new Row();
row1.setValues(new Value[] { textValue1, booleanValue1, numberValue1 });
Row row2 = new Row();
row2.setValues(new Value[] { textValue2, booleanValue2, numberValue2 });
Row row3 = new Row();
row3.setValues(new Value[] { textValue3, booleanValue3, numberValue3 });
ResultSet resultSet1 = new ResultSet();
resultSet1.setColumnTypes(new ColumnType[] { column1, column2, column3 });
resultSet1.setRows(new Row[] { row1, row2 });
ResultSet resultSet2 = new ResultSet();
resultSet2.setColumnTypes(new ColumnType[] { column1, column2, column3 });
resultSet2.setRows(new Row[] { row3 });
ResultSet combinedResultSet = Pql.combineResultSets(resultSet1, resultSet2);
assertEquals(3, combinedResultSet.getRows().length);
assertArrayEquals(new ColumnType[] { column1, column2, column3 }, combinedResultSet.getColumnTypes());
assertArrayEquals(new Value[] { textValue1, booleanValue1, numberValue1 }, combinedResultSet.getRows()[0].getValues());
assertArrayEquals(new Value[] { textValue2, booleanValue2, numberValue2 }, combinedResultSet.getRows()[1].getValues());
assertArrayEquals(new Value[] { textValue3, booleanValue3, numberValue3 }, combinedResultSet.getRows()[2].getValues());
}
use of com.helger.genericode.v10.Row in project googleads-java-lib by googleads.
the class PqlTest method testGetRowStringValues.
@Test
public void testGetRowStringValues() {
Row row = new Row();
row.setValues(new Value[] { textValue1, booleanValue1, numberValue2, numberSetValue });
assertEquals(Lists.newArrayList(new String[] { "value1", "false", "1.02", "1,-1" }), Pql.getRowStringValues(row));
}
use of com.helger.genericode.v10.Row in project googleads-java-lib by googleads.
the class Pql method resultSetToStringArrayList.
/**
* Gets the result set as list of string arrays, which can be transformed to a CSV using {@code
* CsvFiles} such as
*
* <pre>
* <code>
* ResultSet combinedResultSet = Pql.combineResultSet(resultSet1, resultSet2);
* //...
* combinedResultSet = Pql.combineResultSet(combinedResultSet, resultSet3);
* CsvFiles.writeCsv(Pql.resultSetToStringArrayList(combinedResultSet), filePath);
* </code>
* </pre>
*
* @param resultSet the result set to convert to a CSV compatible format
* @return a list of string arrays representing the result set
*/
public static List<String[]> resultSetToStringArrayList(ResultSet resultSet) {
List<String[]> stringArrayList = Lists.newArrayList();
stringArrayList.add(getColumnLabels(resultSet).toArray(new String[] {}));
if (resultSet.getRows() != null) {
for (Row row : resultSet.getRows()) {
try {
stringArrayList.add(getRowStringValues(row).toArray(new String[] {}));
} catch (IllegalArgumentException e) {
throw new IllegalStateException("Cannot convert result set to string array list", e);
}
}
}
return stringArrayList;
}
use of com.helger.genericode.v10.Row in project googleads-java-lib by googleads.
the class Pql method combineResultSets.
/**
* Combines the first and second result sets, if and only if, the columns of both result sets
* match.
*
* @throws IllegalArgumentException if the columns of the first result set don't match the second
*/
public static ResultSet combineResultSets(ResultSet first, ResultSet second) {
Function<ColumnType, String> columnTypeToString = new Function<ColumnType, String>() {
@Override
public String apply(ColumnType input) {
return input.getLabelName();
}
};
List<String> firstColumns = Lists.transform(Lists.newArrayList(first.getColumnTypes()), columnTypeToString);
List<String> secondColumns = Lists.transform(Lists.newArrayList(second.getColumnTypes()), columnTypeToString);
if (!firstColumns.equals(secondColumns)) {
throw new IllegalArgumentException(String.format("First result set columns [%s] do not match second columns [%s]", Joiner.on(",").join(firstColumns), Joiner.on(",").join(secondColumns)));
}
List<Row> combinedRows = Lists.newArrayList(first.getRows());
if (second.getRows() != null) {
Collections.addAll(combinedRows, second.getRows());
}
ResultSet combinedResultSet = new ResultSet();
combinedResultSet.setColumnTypes(first.getColumnTypes());
combinedResultSet.setRows(combinedRows.toArray(new Row[] {}));
return combinedResultSet;
}
use of com.helger.genericode.v10.Row in project molgenis-emx2 by molgenis.
the class PetStoreLoader method loadExampleData.
private void loadExampleData(Schema schema) {
final String shopviewer = "shopviewer";
final String shopmanager = "shopmanager";
final String shopowner = "shopowner";
// initual user
schema.addMember(shopmanager, "Manager");
schema.getDatabase().setUserPassword(shopmanager, shopmanager);
schema.addMember(shopviewer, "Viewer");
schema.getDatabase().setUserPassword(shopviewer, shopviewer);
schema.addMember(shopowner, "Owner");
schema.getDatabase().setUserPassword(shopowner, shopowner);
schema.getTable(CATEGORY).insert(new Row().set(NAME, "cat"), new Row().set(NAME, "dog"));
schema.getTable(TAG).insert(new Row().set(NAME, COLORS), new Row().set(NAME, "red").set(PARENT, COLORS), new Row().set(NAME, "green").set(PARENT, COLORS), new Row().set(NAME, SPECIES), new Row().set(NAME, MAMMALS).set(PARENT, SPECIES), new Row().set(NAME, "carnivorous mammals").set(PARENT, MAMMALS), new Row().set(NAME, "herbivorous mammals").set(PARENT, MAMMALS), new Row().set(NAME, "birds").set(PARENT, SPECIES));
schema.getTable(PET).insert(new Row().set(CATEGORY_COLUMN, "cat").set("name", "pooky").set(STATUS, "available").set(WEIGHT, 9.4), new Row().set(CATEGORY_COLUMN, "dog").set("name", "spike").set(STATUS, "sold").set("tags", "red,green").set(WEIGHT, 15.7));
schema.getTable(ORDER).insert(new Row().set(ORDER_ID, "1").set("pet", "pooky").set(QUANTITY, 1l).set(PRICE, 9.99).set(COMPLETE, true).set(STATUS, "delivered"), new Row().set(ORDER_ID, "2").set("pet", "spike").set(PRICE, 14.99).set(QUANTITY, 7l).set(COMPLETE, false).set(STATUS, "approved"));
schema.getTable(USER).insert(new Row().set("username", "bofke").set("pets", "spike,pooky"));
schema.addMember(SqlDatabase.ANONYMOUS, Privileges.VIEWER.toString());
}
Aggregations