Package org.apache.metamodel.data

Examples of org.apache.metamodel.data.DataSet


        Table table = dc.getDefaultSchema().getTables()[0];
        assertEquals("[i, greeting, greeter]", Arrays.toString(table.getColumnNames()));

        Query q = dc.query().from(table).select(table.getColumns()).toQuery();
        DataSet ds = dc.executeQuery(q);

        assertTrue(ds.next());
        assertEquals("[1, hello, world]", Arrays.toString(ds.getRow().getValues()));
        assertTrue(ds.next());
        assertEquals("[2, hi, there]", Arrays.toString(ds.getRow().getValues()));
        assertTrue(ds.next());
        assertEquals("[3, howdy, partner]", Arrays.toString(ds.getRow().getValues()));
        assertFalse(ds.next());
    }
View Full Code Here


        }

        int selectItemOffset = 0;
        List<Object[]> data = new ArrayList<Object[]>();
        for (int fromDataSetIndex = 0; fromDataSetIndex < fromDataSets.length; fromDataSetIndex++) {
            DataSet fromDataSet = fromDataSets[fromDataSetIndex];
            SelectItem[] fromSelectItems = fromDataSet.getSelectItems();
            if (fromDataSetIndex == 0) {
                while (fromDataSet.next()) {
                    Object[] values = fromDataSet.getRow().getValues();
                    Object[] row = new Object[selectItems.size()];
                    System.arraycopy(values, 0, row, selectItemOffset, values.length);
                    data.add(row);
                }
                fromDataSet.close();
            } else {
                List<Object[]> fromDataRows = new ArrayList<Object[]>();
                while (fromDataSet.next()) {
                    fromDataRows.add(fromDataSet.getRow().getValues());
                }
                fromDataSet.close();
                for (int i = 0; i < data.size(); i = i + fromDataRows.size()) {
                    Object[] originalRow = data.get(i);
                    data.remove(i);
                    for (int j = 0; j < fromDataRows.size(); j++) {
                        Object[] newRow = fromDataRows.get(j);
                        System.arraycopy(newRow, 0, originalRow, selectItemOffset, newRow.length);
                        data.add(i + j, originalRow.clone());
                    }
                }
            }
            selectItemOffset += fromSelectItems.length;
        }

        if (data.isEmpty()) {
            return new EmptyDataSet(selectItems);
        }

        final DataSetHeader header = new CachingDataSetHeader(selectItems);
        final List<Row> rows = new ArrayList<Row>(data.size());
        for (Object[] objects : data) {
            rows.add(new DefaultRow(header, objects, null));
        }

        DataSet result = new InMemoryDataSet(header, rows);
        if (whereItems != null) {
            DataSet filteredResult = getFiltered(result, whereItems);
            result = filteredResult;
        }
        return result;
    }
View Full Code Here

            exampleMap2.put("couch", "db");
            exampleList.add(exampleMap2);

            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);
View Full Code Here

    public static DataSet getGrouped(List<SelectItem> selectItems, DataSet dataSet, Collection<GroupByItem> groupByItems) {
        return getGrouped(selectItems, dataSet, groupByItems.toArray(new GroupByItem[groupByItems.size()]));
    }

    public static DataSet getGrouped(List<SelectItem> selectItems, DataSet dataSet, GroupByItem[] groupByItems) {
        DataSet result = dataSet;
        if (groupByItems != null && groupByItems.length > 0) {
            Map<Row, Map<SelectItem, List<Object>>> uniqueRows = new HashMap<Row, Map<SelectItem, List<Object>>>();

            final SelectItem[] groupBySelects = new SelectItem[groupByItems.length];
            for (int i = 0; i < groupBySelects.length; i++) {
View Full Code Here

                callback.insertInto(databaseName).value("foo", "bar").value("greeting", "hello").execute();
                callback.insertInto(databaseName).value("foo", "baz").value("greeting", "hi").execute();
            }
        });

        DataSet ds = dc.query().from(databaseName).select("_id", "foo", "greeting").execute();
        assertTrue(ds.next());
        assertNotNull(ds.getRow().getValue(0));
        assertEquals("bar", ds.getRow().getValue(1));
        assertEquals("hello", ds.getRow().getValue(2));
        assertTrue(ds.next());
        assertNotNull(ds.getRow().getValue(0));
        assertEquals("baz", ds.getRow().getValue(1));
        assertEquals("hi", ds.getRow().getValue(2));
        assertFalse(ds.next());
        ds.close();

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.update(databaseName).value("greeting", "howdy").where("foo").isEquals("baz").execute();

                callback.update(databaseName).value("foo", "foo").where("foo").isEquals("bar").execute();
            }
        });

        ds = dc.query().from(databaseName).select("_id", "foo", "greeting").execute();
        assertTrue(ds.next());
        assertNotNull(ds.getRow().getValue(0));
        assertEquals("foo", ds.getRow().getValue(1));
        assertEquals("hello", ds.getRow().getValue(2));
        assertTrue(ds.next());
        assertNotNull(ds.getRow().getValue(0));
        assertEquals("baz", ds.getRow().getValue(1));
        assertEquals("howdy", ds.getRow().getValue(2));
        assertFalse(ds.next());
        ds.close();
    }
View Full Code Here

     * @return a row object representing the single row returned from the query
     * @throws MetaModelException
     *             if less or more than one Row is returned from the query
     */
    public static Row executeSingleRowQuery(DataContext dataContext, Query query) throws MetaModelException {
        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();
        return row;
    }
View Full Code Here

            // 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);
View Full Code Here

        System.arraycopy(ds1selects, 0, leftOrderedSelects, 0, ds1selects.length);
        System.arraycopy(ds2selects, 0, leftOrderedSelects, ds1selects.length, ds2selects.length);

        // We will reuse the left join algorithm (but switch the datasets
        // around)
        DataSet dataSet = getLeftJoin(ds2, ds1, onConditions);

        dataSet = getSelection(leftOrderedSelects, dataSet);
        return dataSet;
    }
View Full Code Here

        assertTrue(idColumn.isPrimaryKey());

        assertEquals("Column[name=_rev,columnNumber=1,type=VARCHAR,nullable=false,nativeType=null,columnSize=null]",
                schema.getTableByName(getDatabaseName()).getColumnByName("_rev").toString());

        DataSet ds;

        ds = dc.query().from(getDatabaseName()).select("name").and("age").execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[John Doe, 30]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[Jane Doe, null]]", ds.getRow().toString());
        assertFalse(ds.next());
        ds.close();

        ds = dc.query().from(getDatabaseName()).select("name").and("gender").where("age").isNull().execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[Jane Doe, F]]", ds.getRow().toString());
        assertFalse(ds.next());
        ds.close();

        ds = dc.query().from(getDatabaseName()).select("_id").where("name").eq("Jane Doe").execute();
        assertTrue(ds.next());
        Object pkValue = ds.getRow().getValue(0);
        assertFalse(ds.next());
        ds.close();

        // test primary key lookup query
        ds = dc.query().from(getDatabaseName()).select("name").and("gender").where("_id").eq(pkValue).execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[Jane Doe, F]]", ds.getRow().toString());
        assertFalse(ds.next());
        ds.close();

        // test primary key null
        ds = dc.query().from(getDatabaseName()).select("name").and("gender").where("_id").isNull().execute();
        assertFalse(ds.next());
        ds.close();

        // test primary key not found
        ds = dc.query().from(getDatabaseName()).select("name").and("gender").where("_id").eq("this id does not exist")
                .execute();
        assertFalse(ds.next());
        ds.close();
    }
View Full Code Here

    for (int i = 0; i < tables.length; i++) {
      Table table = tables[i];
      Column[] columns = table.getColumns();
      Query query = mainDataContext.query().from(table).select(columns)
          .toQuery();
      DataSet dataSet = mainDataContext.executeQuery(query);

      DataSet comparedDataSet = null;
      if (comparedDataContext != null) {
        Table comparedTable = comparedTables[i];
        assertEquals(comparedTable.getName(), table.getName());
        assertEquals(comparedTable.getColumnCount(),
            table.getColumnCount());

        Column[] comparedColumns = comparedTable.getColumns();
        for (int j = 0; j < comparedColumns.length; j++) {
          assertEquals(columns[j].getColumnNumber(),
              comparedColumns[j].getColumnNumber());
        }

        Query comparedQuery = comparedDataContext.query()
            .from(comparedTable).select(comparedColumns).toQuery();
        comparedDataSet = comparedDataContext
            .executeQuery(comparedQuery);
      }

      while (dataSet.next()) {
        Row row = dataSet.getRow();
        assertNotNull(row);
        Object[] values = row.getValues();

        assertEquals(values.length, table.getColumnCount());

        if (comparedDataSet != null) {
          boolean next = comparedDataSet.next();
          assertTrue("No comparable row exists for: " + row, next);
          Row comparedRow = comparedDataSet.getRow();
          assertNotNull(comparedRow);
          Object[] comparedValues = comparedRow.getValues();
          assertEquals(comparedValues.length, table.getColumnCount());

          for (int j = 0; j < comparedValues.length; j++) {
            assertEquals(comparedValues[j], values[j]);
          }

          // compare styles
          for (int j = 0; j < comparedValues.length; j++) {
            Style style1 = comparedRow.getStyle(j);
            Style style2 = row.getStyle(j);
            assertEquals("Diff in style on row: " + row
                + " (value index = " + j + ")\nStyle 1: "
                + style1 + "\nStyle 2: " + style2 + ". ",
                style1, style2);
          }
        }
      }
      dataSet.close();

      if (comparedDataSet != null) {
        assertFalse(comparedDataSet.next());
        comparedDataSet.close();
      }
    }
  }
View Full Code Here

TOP

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

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.