Package org.apache.metamodel.data

Examples of org.apache.metamodel.data.Row


            header = new SimpleDataSetHeader(workSelectItems);
        }

        final List<Row> resultRows = new ArrayList<Row>();
        while (dataSet.next()) {
            final Row inputRow = dataSet.getRow();
            for (SelectItem item : functionItems) {
                final AggregateBuilder<?> aggregateBuilder = aggregateBuilders.get(item);
                final Column column = item.getColumn();
                if (column != null) {
                    Object value = inputRow.getValue(new SelectItem(column));
                    aggregateBuilder.add(value);
                } else if (SelectItem.isCountAllItem(item)) {
                    // Just use the empty string, since COUNT(*) don't
                    // evaluate values (but null values should be prevented)
                    aggregateBuilder.add("");
                } else {
                    throw new IllegalArgumentException("Expression function not supported: " + item);
                }
            }

            // If the result should also contain non-aggregated values, we
            // will keep those in the rows list
            if (!onlyAggregates) {
                final Object[] values = new Object[header.size()];
                for (int i = 0; i < header.size(); i++) {
                    final Object value = inputRow.getValue(header.getSelectItem(i));
                    if (value != null) {
                        values[i] = value;
                    }
                }
                resultRows.add(new DefaultRow(header, values));
            }
        }
        dataSet.close();

        // Collect the aggregates
        Map<SelectItem, Object> functionResult = new HashMap<SelectItem, Object>();
        for (SelectItem item : functionItems) {
            AggregateBuilder<?> aggregateBuilder = aggregateBuilders.get(item);
            Object result = aggregateBuilder.getAggregate();
            functionResult.put(item, result);
        }

        // if there are no result rows (no matching records at all), we still
        // need to return a record with the aggregates
        final boolean noResultRows = resultRows.isEmpty();

        if (onlyAggregates || noResultRows) {
            // We will only create a single row with all the aggregates
            Object[] values = new Object[header.size()];
            for (int i = 0; i < header.size(); i++) {
                values[i] = functionResult.get(header.getSelectItem(i));
            }
            Row row = new DefaultRow(header, values);
            resultRows.add(row);
        } else {
            // We will create the aggregates as well as regular values
            for (int i = 0; i < resultRows.size(); i++) {
                Row row = resultRows.get(i);
                Object[] values = row.getValues();
                for (Entry<SelectItem, Object> entry : functionResult.entrySet()) {
                    SelectItem item = entry.getKey();
                    int itemIndex = row.indexOf(item);
                    if (itemIndex != -1) {
                        Object value = entry.getValue();
                        values[itemIndex] = value;
                    }
                }
View Full Code Here


        DataSet dataSet = dataContext.executeQuery(query);
        boolean next = dataSet.next();
        if (!next) {
            throw new MetaModelException("No rows returned from query: " + query);
        }
        Row row = dataSet.getRow();
        next = dataSet.next();
        if (next) {
            throw new MetaModelException("More than one row returned from query: " + query);
        }
        dataSet.close();
View Full Code Here

        while (ds1.next()) {

            // Construct a single-row dataset for making a carthesian product
            // against ds2
            Row ds1row = ds1.getRow();
            List<Row> ds1rows = new ArrayList<Row>();
            ds1rows.add(ds1row);

            DataSet carthesianProduct = getCarthesianProduct(new DataSet[] {
                    new InMemoryDataSet(new CachingDataSetHeader(si1), ds1rows),
                    new InMemoryDataSet(new CachingDataSetHeader(si2), ds2data) }, onConditions);
            List<Row> carthesianRows = readDataSetFull(carthesianProduct);
            if (carthesianRows.size() > 0) {
                resultRows.addAll(carthesianRows);
            } else {
                Object[] values = ds1row.getValues();
                Object[] row = new Object[selectItems.length];
                System.arraycopy(values, 0, row, 0, values.length);
                resultRows.add(new DefaultRow(header, row));
            }
        }
View Full Code Here

            dc.executeUpdate(new InsertInto(table).value("id", 1).value("foo", exampleMap).value("bar", exampleList));

            DataSet ds = dc.query().from(table).select("id","foo","bar").execute();
            assertTrue(ds.next());
            Row row = ds.getRow();
            assertFalse(ds.next());
            ds.close();

            assertEquals(
                    "Row[values=[1, {hello=[world, welt, verden], foo=bar}, [{}, {meta=model, couch=db}]]]",
                    row.toString());
            assertTrue(row.getValue(0) instanceof Integer);
            assertTrue(row.getValue(1) instanceof Map);
            assertTrue(row.getValue(2) instanceof List);

        } finally {
            dc.executeUpdate(new DropTable(table));
        }
View Full Code Here

        assertTrue("Class: " + ds2.getClass().getName(), ds2 instanceof CouchDbDataSet);

        assertTrue(ds1.next());
        assertTrue(ds2.next());

        final Row row1 = ds1.getRow();
        final Row row2 = ds2.getRow();

        assertFalse(ds1.next());
        assertFalse(ds2.next());

        assertEquals("Row[values=[Jane Doe, null]]", row1.toString());
        assertEquals("Row[values=[John Doe, 30]]", row2.toString());

        ds1.close();
        ds2.close();
    }
View Full Code Here

        assertFalse(ds.next());
        ds.close();

        ds = dc.executeQuery(q);

        Row row;

        assertTrue(ds.next());
        row = ds.getRow();
        assertEquals("TismmerswerskisMFSTLandsmeers                                                          ", row.getValue(0)
                .toString());

        assertTrue(ds.next());
        row = ds.getRow();
        assertEquals("-\"t\" \"houetismfsthueiss\"", row.getValue(0).toString());

        assertTrue(ds.next());
        row = ds.getRow();
        assertEquals("TismmerswerskisMFSTLandsmeers                                                          ", row.getValue(0)
                .toString());

        assertFalse(ds.next());
        ds.close();
    }
View Full Code Here

        assertSingleRowResult("Row[values=[0, null]]", dc.query().from(emptyTable).selectCount().and("foo").execute());
    }

    private void assertSingleRowResult(String rowStr, DataSet ds) {
        assertTrue("DataSet had no rows", ds.next());
        Row row = ds.getRow();
        assertEquals(rowStr, row.toString());
        assertFalse("DataSet had more than a single row!", ds.next());
        ds.close();
    }
View Full Code Here

        DataContext dc = getDataContext();
        Query q = new Query();
        q.from(table1);
        q.selectCount();

        Row row = MetaModelHelper.executeSingleRowQuery(dc, q);
        assertEquals("6", row.getValue(0).toString());
    }
View Full Code Here

        Query q = new Query();
        q.from(table1);
        q.select(table1.getColumns());
        DataSet dataSet = dc.executeQuery(q);
        assertTrue(dataSet.next());
        Row row = dataSet.getRow();
        assertEquals("Row[values=[1, kasper, denmark]]", row.toString());
        assertTrue(dataSet.next());
        assertTrue(dataSet.next());
        assertTrue(dataSet.next());
        assertTrue(dataSet.next());
        assertTrue(dataSet.next());
View Full Code Here

        try {
            ds.next();
            fail("Exception expected");
        } catch (InconsistentRowLengthException e) {
            assertEquals("Inconsistent length of row no. 3. Expected 2 columns but found 3.", e.getMessage());
            Row proposedRow = e.getProposedRow();
            assertEquals("[5, 6]", Arrays.toString(proposedRow.getValues()));

            String[] sourceLine = e.getSourceLine();
            assertEquals("[5, 6, 7]", Arrays.toString(sourceLine));
        }

        assertTrue(ds.next());

        try {
            ds.next();
            fail("Exception expected");
        } catch (InconsistentRowLengthException e) {
            assertEquals("Inconsistent length of row no. 5. Expected 2 columns but found 1.", e.getMessage());
            Row proposedRow = e.getProposedRow();
            assertEquals("[10, null]", Arrays.toString(proposedRow.getValues()));

            String[] sourceLine = e.getSourceLine();
            assertEquals("[10]", Arrays.toString(sourceLine));
        }
View Full Code Here

TOP

Related Classes of org.apache.metamodel.data.Row

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.