Search in sources :

Example 56 with Row

use of com.google.api.ads.admanager.jaxws.v202205.Row in project molgenis-emx2 by molgenis.

the class SqlTable method getWhereConditionForBatchDelete.

private Condition getWhereConditionForBatchDelete(Collection<Row> rows) {
    List<Condition> conditions = new ArrayList<>();
    for (Row r : rows) {
        List<Condition> rowCondition = new ArrayList<>();
        if (getMetadata().getPrimaryKeys().isEmpty()) {
            // when no key, use all columns as id
            for (Column keyPart : getMetadata().getStoredColumns()) {
                rowCondition.add(getColumnCondition(r, keyPart));
            }
        } else {
            for (Column keyPart : getMetadata().getPrimaryKeyColumns()) {
                rowCondition.add(getColumnCondition(r, keyPart));
            }
        }
        conditions.add(and(rowCondition));
    }
    return or(conditions);
}
Also used : Row(org.molgenis.emx2.Row)

Example 57 with Row

use of com.google.api.ads.admanager.jaxws.v202205.Row in project molgenis-emx2 by molgenis.

the class SqlTable method insertBatch.

private static int insertBatch(SqlTable table, List<Row> rows, boolean updateOnConflict, Set<String> updateColumns) {
    boolean inherit = table.getMetadata().getInherit() != null;
    if (inherit) {
        SqlTable inheritedTable = table.getInheritedTable();
        inheritedTable.insertBatch(inheritedTable, rows, updateOnConflict, updateColumns);
    }
    // get metadata
    Set<Column> columns = table.getColumnsToBeUpdated(updateColumns);
    // check that columns exist for validation
    checkForMissingVariablesColumns(columns);
    List<Column> allColumns = table.getMetadata().getMutationColumns();
    List<Field> insertFields = columns.stream().map(c -> c.getJooqField()).collect(Collectors.toList());
    if (!inherit) {
        insertFields.add(field(name(MG_INSERTEDBY)));
        insertFields.add(field(name(MG_INSERTEDON)));
        insertFields.add(field(name(MG_UPDATEDBY)));
        insertFields.add(field(name(MG_UPDATEDON)));
    }
    // define the insert step
    InsertValuesStepN<org.jooq.Record> step = table.getJooq().insertInto(table.getJooqTable(), insertFields.toArray(new Field[0]));
    // add all the rows as steps
    String user = table.getSchema().getDatabase().getActiveUser();
    if (user == null) {
        user = ADMIN_USER;
    }
    LocalDateTime now = LocalDateTime.now();
    for (Row row : rows) {
        // get values
        Map values = SqlTypeUtils.getValuesAsMap(row, columns);
        if (!inherit) {
            values.put(MG_INSERTEDBY, user);
            values.put(MG_INSERTEDON, now);
            values.put(MG_UPDATEDBY, user);
            values.put(MG_UPDATEDON, now);
        }
        // when insert, we should include all columns, not only 'updateColumns'
        if (!row.isDraft()) {
            checkRequired(row, allColumns);
            checkValidation(values, columns);
        }
        step.values(values.values());
    }
    // optionally, add conflict clause
    if (updateOnConflict) {
        InsertOnDuplicateSetStep<org.jooq.Record> step2 = step.onConflict(table.getMetadata().getPrimaryKeyFields().toArray(new Field[0])).doUpdate();
        for (Column column : columns) {
            step2.set(column.getJooqField(), (Object) field(unquotedName("excluded.\"" + column.getName() + "\"")));
        }
        if (!inherit) {
            step2.set(field(name(MG_UPDATEDBY)), user);
            step2.set(field(name(MG_UPDATEDON)), now);
        }
    }
    return step.execute();
}
Also used : EvaluateExpressions.checkValidation(org.molgenis.emx2.sql.EvaluateExpressions.checkValidation) java.util(java.util) DSL(org.jooq.impl.DSL) Logger(org.slf4j.Logger) LocalDateTime(java.time.LocalDateTime) LoggerFactory(org.slf4j.LoggerFactory) Constants(org.molgenis.emx2.Constants) org.molgenis.emx2(org.molgenis.emx2) Collectors(java.util.stream.Collectors) EvaluateExpressions.checkForMissingVariablesColumns(org.molgenis.emx2.sql.EvaluateExpressions.checkForMissingVariablesColumns) ADMIN_USER(org.molgenis.emx2.sql.SqlDatabase.ADMIN_USER) Query(org.molgenis.emx2.Query) MutationType(org.molgenis.emx2.MutationType) StringReader(java.io.StringReader) Table(org.molgenis.emx2.Table) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) org.jooq(org.jooq) Writer(java.io.Writer) BaseConnection(org.postgresql.core.BaseConnection) CopyManager(org.postgresql.copy.CopyManager) SqlTypeUtils.getTypedValue(org.molgenis.emx2.sql.SqlTypeUtils.getTypedValue) Row(org.molgenis.emx2.Row) LocalDateTime(java.time.LocalDateTime) Row(org.molgenis.emx2.Row)

Example 58 with Row

use of com.google.api.ads.admanager.jaxws.v202205.Row in project molgenis-emx2 by molgenis.

the class TestReadWriteStores method executeTest.

public static void executeTest(TableStore store) throws IOException, MolgenisException {
    List<Row> rows = new ArrayList<>();
    int count = 10;
    for (int i = 1; i <= count; i++) {
        rows.add(new Row().setString("stringCol", "test" + i).setInt("intCol", i).setDecimal("decimalCol", Double.valueOf(i / 2)).setUuid("uuidCol", UUID.randomUUID()).setDate("dateCol", LocalDate.of(2019, 12, 12)).setDateTime("datetimeCol", LocalDateTime.now()).setBool("boolCol", true).setStringArray("stringarrayCol", new String[] { "a", "b,including comma," }).setIntArray("intarrayCol", new Integer[] { 1, 2 }).setDecimalArray("doubleArrayCol", new Double[] { 1.0, 2.0 }).setDecimalArray("doubleArrayCol", new Double[] { 1.0, 2.0 }).setDateArray("dateArray", new LocalDate[] { LocalDate.of(2019, 12, 12), LocalDate.of(2019, 12, 12) }).setDateTimeArray("datetimeArrayCol", new LocalDateTime[] { LocalDateTime.now(), LocalDateTime.now() }).setBoolArray("booleanArrayCol", new Boolean[] { true, false }));
    }
    StopWatch.start("created some rows");
    // write them
    store.writeTable("test", List.of(), rows);
    store.writeTable("test2", List.of(), rows);
    StopWatch.print("wrote them to " + store.getClass().getSimpleName(), count);
    List<Row> rows2 = StreamSupport.stream(store.readTable("test2").spliterator(), false).collect(Collectors.toList());
    // for (Row r : rows2) System.out.println(r);
    StopWatch.print("fromReader them back from " + store.getClass().getSimpleName(), count);
    // compare
    CompareTools.assertEquals(rows, rows2);
    // write another one
    store.writeTable("test3", List.of(), rows);
    StopWatch.print("wrote them to " + store.getClass().getSimpleName(), count);
    rows2 = StreamSupport.stream(store.readTable("test3").spliterator(), false).collect(Collectors.toList());
    // for (Row r : rows2) System.out.println(r);
    StopWatch.print("fromReader them back from " + store.getClass().getSimpleName(), count);
    // compare
    CompareTools.assertEquals(rows, rows2);
    StopWatch.print("compared succesfully");
    // write empty
    store.writeTable("test4", List.of("empty"), new ArrayList<>());
    // test that reading store that doesn't exist errors properly
    try {
        store.readTable("fake");
        fail("should have failed");
    } catch (MolgenisException me) {
        System.out.println("errored correctly:" + me);
    }
}
Also used : ArrayList(java.util.ArrayList) MolgenisException(org.molgenis.emx2.MolgenisException) Row(org.molgenis.emx2.Row) LocalDate(java.time.LocalDate)

Example 59 with Row

use of com.google.api.ads.admanager.jaxws.v202205.Row in project molgenis-emx2 by molgenis.

the class TestTypesImport method testTsvTypeCast.

@Test
public void testTsvTypeCast() {
    ClassLoader classLoader = getClass().getClassLoader();
    Path path = new File(classLoader.getResource("TypeTest.tsv").getFile()).toPath();
    TableStore store = new TableStoreForCsvFile(path);
    for (Row r : store.readTable("Sheet1")) {
        assertEquals("1", r.getString("column1"));
        assertEquals("2", r.getString("column2"));
    }
}
Also used : Path(java.nio.file.Path) TableStoreForCsvFile(org.molgenis.emx2.io.tablestore.TableStoreForCsvFile) Row(org.molgenis.emx2.Row) TableStoreForCsvFile(org.molgenis.emx2.io.tablestore.TableStoreForCsvFile) File(java.io.File) TableStoreForXlsxFile(org.molgenis.emx2.io.tablestore.TableStoreForXlsxFile) TableStore(org.molgenis.emx2.io.tablestore.TableStore) Test(org.junit.Test)

Example 60 with Row

use of com.google.api.ads.admanager.jaxws.v202205.Row in project molgenis-emx2 by molgenis.

the class CatalogueSiteMapTest method buildSiteMap.

@Test
public void buildSiteMap() {
    Schema schema = mock(Schema.class);
    Table table = mock(Table.class);
    Query query = mock(Query.class);
    List<Row> row = Collections.singletonList(new Row("pid", "my-cohort-pid"));
    when(schema.getTable("Cohorts")).thenReturn(table);
    when(table.select(any())).thenReturn(query);
    when(query.retrieveRows()).thenReturn(row);
    CatalogueSiteMap catalogueSiteMap = new CatalogueSiteMap(schema, "https://my/base/url");
    String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" >\n" + "  <url>\n" + "    <loc>https://my/base/url/ssr-catalogue/Cohorts/my-cohort-pid</loc>\n" + "  </url>\n" + "</urlset>";
    assertEquals(expected, catalogueSiteMap.buildSiteMap());
}
Also used : Table(org.molgenis.emx2.Table) Query(org.molgenis.emx2.Query) Schema(org.molgenis.emx2.Schema) Row(org.molgenis.emx2.Row) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)68 Row (org.molgenis.emx2.Row)43 Row (com.google.bigtable.v2.Row)22 ArrayList (java.util.ArrayList)17 ByteString (com.google.protobuf.ByteString)14 Table (org.molgenis.emx2.Table)11 Function (com.google.common.base.Function)10 Row (org.hypertrace.entity.query.service.v1.Row)10 Map (java.util.Map)9 ResultSetChunk (org.hypertrace.entity.query.service.v1.ResultSetChunk)8 Schema (org.molgenis.emx2.Schema)8 Row (com.google.api.ads.admanager.axis.v202108.Row)6 Family (com.google.bigtable.v2.Family)6 List (java.util.List)6 ByteKey (org.apache.beam.sdk.io.range.ByteKey)6 Row (com.google.api.ads.admanager.axis.v202105.Row)5 Row (com.google.api.ads.admanager.axis.v202111.Row)5 Row (com.google.api.ads.admanager.axis.v202202.Row)5 Row (com.google.api.ads.admanager.axis.v202205.Row)5 Row (com.google.api.ads.admanager.jaxws.v202105.Row)5