Package org.apache.metamodel

Examples of org.apache.metamodel.DataContext


        Arrays.toString(table.getColumns()));
  }

  public void testExecuteQuery() throws Exception {
    File file = new File("src/test/resources/xml_input_eobjects.xml");
    DataContext dc = new XmlDomDataContext(file, false);
    Schema schema = dc.getDefaultSchema();
    Table table = schema.getTableByName("projects_project");
    Query q = new Query().select(table.getColumns()).from(table, "p");
    assertEquals(
        "SELECT p.id, p.project, p.name FROM xml_input_eobjects.xml.projects_project p",
        q.toString());
    DataSet data = dc.executeQuery(q);
    TableModel tableModel = new DataSetTableModel(data);
    assertEquals(3, tableModel.getColumnCount());
    assertEquals(2, tableModel.getRowCount());

    // ID
    assertEquals(1, tableModel.getValueAt(0, 0));
    assertEquals(2, tableModel.getValueAt(1, 0));

    // Project (text content)
    assertEquals("Some description", tableModel.getValueAt(0, 1));
    assertNull(tableModel.getValueAt(1, 1));

    // Name
    assertEquals("datacleaner", tableModel.getValueAt(0, 2));
    assertEquals("MetaModel", tableModel.getValueAt(1, 2));

    // Make a new query that joins the normalized tables together
    table = schema.getTableByName("contributors_person_address");
    Relationship relationShip = table.getRelationships()[0];
    q = new Query().select(relationShip.getPrimaryTable().getColumns())
        .select(relationShip.getForeignTable().getColumns())
        .from(new FromItem(JoinType.INNER, relationShip));

    assertEquals(
        "SELECT contributors_person.id, contributors_person_address.id, "
            + "contributors_person_address.contributors_person_id, contributors_person_address.address "
            + "FROM xml_input_eobjects.xml.contributors_person INNER JOIN xml_input_eobjects.xml.contributors_person_address "
            + "ON contributors_person.id = contributors_person_address.contributors_person_id",
        q.toString());

    data = dc.executeQuery(q);
    tableModel = new DataSetTableModel(data);
    assertEquals(4, tableModel.getColumnCount());
    assertEquals(4, tableModel.getRowCount());

    assertEquals("1", tableModel.getValueAt(0, 0).toString());
View Full Code Here


    assertEquals("2007", data.getRow().getValue(0));
    assertFalse(data.next());
  }

  public void testAutoflattenTicket229() throws Exception {
    DataContext dc = new XmlDomDataContext(new File(
        "src/test/resources/xml_input_ticket_229.xml"));
    Schema schema = dc.getDefaultSchema();
    assertNotNull(schema);

    assertEquals(
        "[VirtualBox, Machine, Machine_ExtraData_ExtraDataItem, Machine_Hardware_Boot_Order, "
            + "Machine_Hardware_Display, Machine_Hardware_RemoteDisplay, Machine_Hardware_BIOS_Logo, "
View Full Code Here

        TableDataProvider<?> provider1 = new ObjectTableDataProvider<Person>("persons", Person.class, persons);
        TableDataProvider<?> provider2 = new MapTableDataProvider(new SimpleTableDef("titles", new String[] { "name",
                "title" }), titles);

        DataContext dc = new PojoDataContext(Arrays.<TableDataProvider<?>> asList(provider1, provider2));

        DataSet dataSet = dc.query().from("persons").innerJoin("titles").on("name", "name").selectAll().execute();

        assertTrue(dataSet.next());
        assertEquals("Row[values=[Elvis Presley, 42, Elvis Presley, The King]]", dataSet.getRow().toString());
        assertFalse(dataSet.next());
    }
View Full Code Here

public class JdbcDataContextTest extends JdbcTestCase {

    public void testGetDefaultSchema() throws Exception {
        Connection con = getTestDbConnection();
        DataContext dc = new JdbcDataContext(con);
        Schema schema = dc.getDefaultSchema();
        assertEquals("PUBLIC", schema.getName());
        con.close();
    }
View Full Code Here

        con.close();
    }

    public void testGetNonExistingSchema() throws Exception {
        Connection con = getTestDbConnection();
        DataContext dc = new JdbcDataContext(con);
        Schema schema = dc.getSchemaByName("foobar");
        assertNull("found schema: " + schema, schema);
    }
View Full Code Here

     *
     * @throws Exception
     */
    public void testCsvQueryResultComparison() throws Exception {
        Connection connection = getTestDbConnection();
        final DataContext dataContext1 = new JdbcDataContext(connection);
        final DataContext dataContext2 = new QueryPostprocessDataContext() {
            @Override
            public DataSet materializeMainSchemaTable(Table table, Column[] columns, int maxRows) {
                Query q = new Query();
                q.from(table, "a");
                q.select(columns);
                return dataContext1.executeQuery(q);
            }

            @Override
            protected Schema getMainSchema() throws MetaModelException {
                throw new UnsupportedOperationException();
            }

            @Override
            protected String getMainSchemaName() throws MetaModelException {
                return "PUBLIC";
            }
        };

        Schema schema2 = dataContext1.getDefaultSchema();
        Table customersTable = schema2.getTableByName("CUSTOMERS");
        Table employeeTable = schema2.getTableByName("EMPLOYEES");
        assertEquals(
                "[Column[name=CUSTOMERNUMBER,columnNumber=0,type=INTEGER,nullable=false,nativeType=INTEGER,columnSize=0], "
                        + "Column[name=CUSTOMERNAME,columnNumber=1,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50], "
                        + "Column[name=CONTACTLASTNAME,columnNumber=2,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50], "
                        + "Column[name=CONTACTFIRSTNAME,columnNumber=3,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50], "
                        + "Column[name=PHONE,columnNumber=4,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50], "
                        + "Column[name=ADDRESSLINE1,columnNumber=5,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50], "
                        + "Column[name=ADDRESSLINE2,columnNumber=6,type=VARCHAR,nullable=true,nativeType=VARCHAR,columnSize=50], "
                        + "Column[name=CITY,columnNumber=7,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50], "
                        + "Column[name=STATE,columnNumber=8,type=VARCHAR,nullable=true,nativeType=VARCHAR,columnSize=50], "
                        + "Column[name=POSTALCODE,columnNumber=9,type=VARCHAR,nullable=true,nativeType=VARCHAR,columnSize=15], "
                        + "Column[name=COUNTRY,columnNumber=10,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50], "
                        + "Column[name=SALESREPEMPLOYEENUMBER,columnNumber=11,type=INTEGER,nullable=true,nativeType=INTEGER,columnSize=0], "
                        + "Column[name=CREDITLIMIT,columnNumber=12,type=NUMERIC,nullable=true,nativeType=NUMERIC,columnSize=17]]",
                Arrays.toString(customersTable.getColumns()));
        assertEquals(
                "[Column[name=EMPLOYEENUMBER,columnNumber=0,type=INTEGER,nullable=false,nativeType=INTEGER,columnSize=0], "
                        + "Column[name=LASTNAME,columnNumber=1,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50], "
                        + "Column[name=FIRSTNAME,columnNumber=2,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50], "
                        + "Column[name=EXTENSION,columnNumber=3,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=10], "
                        + "Column[name=EMAIL,columnNumber=4,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=100], "
                        + "Column[name=OFFICECODE,columnNumber=5,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=20], "
                        + "Column[name=REPORTSTO,columnNumber=6,type=INTEGER,nullable=true,nativeType=INTEGER,columnSize=0], "
                        + "Column[name=JOBTITLE,columnNumber=7,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50]]",
                Arrays.toString(employeeTable.getColumns()));

        Column employeeNumberColumn1 = customersTable.getColumnByName("SALESREPEMPLOYEENUMBER");
        Column countryColumn = customersTable.getColumnByName("COUNTRY");
        Column employeeNumberColumn2 = employeeTable.getColumnByName("EMPLOYEENUMBER");
        Column creditLimitColumn = customersTable.getColumnByName("CREDITLIMIT");

        Query q = new Query();
        q.from(customersTable, "c");
        q.from(employeeTable, "o");
        SelectItem countrySelect = new SelectItem(countryColumn);
        q.select(countrySelect, new SelectItem(FunctionType.SUM, creditLimitColumn));
        q.groupBy(countryColumn);
        q.orderBy(new OrderByItem(countrySelect));
        q.where(new FilterItem(new SelectItem(employeeNumberColumn1), OperatorType.EQUALS_TO, new SelectItem(
                employeeNumberColumn2)));

        assertEquals(
                "SELECT c.\"COUNTRY\", SUM(c.\"CREDITLIMIT\") FROM PUBLIC.\"CUSTOMERS\" c, PUBLIC.\"EMPLOYEES\" o WHERE c.\"SALESREPEMPLOYEENUMBER\" = o.\"EMPLOYEENUMBER\" GROUP BY c.\"COUNTRY\" ORDER BY c.\"COUNTRY\" ASC",
                q.toString());

        DataSet data1 = dataContext1.executeQuery(q);
        assertTrue(data1.next());
        assertEquals("Row[values=[Australia, 430300.0]]", data1.getRow().toString());
        DataSet data2 = dataContext2.executeQuery(q);
        assertTrue(data2.next());
        assertEquals("Row[values=[Australia, 430300.0]]", data2.getRow().toString());
        assertEquals(new DataSetTableModel(data1), new DataSetTableModel(data2));
    }
View Full Code Here

        assertSame(file, dc.getFile());
    }

    public void testEmptyFileNoHeaderLine() throws Exception {
        DataContext dc = new ExcelDataContext(new File("src/test/resources/empty_file.xls"), new ExcelConfiguration(
                ExcelConfiguration.NO_COLUMN_NAME_LINE, false, false));
        assertEquals(1, dc.getDefaultSchema().getTableCount());

        Table table = dc.getDefaultSchema().getTables()[0];
        assertEquals("sheet", table.getName());
        assertEquals(0, table.getColumnCount());
    }
View Full Code Here

        assertEquals("sheet", table.getName());
        assertEquals(0, table.getColumnCount());
    }

    public void testUnexistingHeaderLine() throws Exception {
        DataContext dc = new ExcelDataContext(new File("src/test/resources/xls_people.xls"), new ExcelConfiguration(20, true,
                false));
        assertEquals(1, dc.getDefaultSchema().getTableCount());

        Table table = dc.getDefaultSchema().getTables()[0];
        assertEquals("xls_people", table.getName());
        assertEquals(0, table.getColumnCount());
    }
View Full Code Here

        assertEquals("[bar, 4, 2010-01-04 00:00:00]", Arrays.toString(objectArrays.get(3)));
    }

    public void testConfigurationWithoutHeader() throws Exception {
        File file = new File("src/test/resources/xls_people.xls");
        DataContext dc = new ExcelDataContext(file, new ExcelConfiguration(ExcelConfiguration.NO_COLUMN_NAME_LINE, true, true));
        Table table = dc.getDefaultSchema().getTables()[0];

        String[] columnNames = table.getColumnNames();
        assertEquals("[A, B, C, D]", Arrays.toString(columnNames));

        Query q = dc.query().from(table).select(table.getColumnByName("A")).toQuery();
        assertEquals("SELECT xls_people.A FROM xls_people.xls.xls_people", q.toSql());

        DataSet dataSet = dc.executeQuery(q);
        assertTrue(dataSet.next());
        assertEquals("id", dataSet.getRow().getValue(0));
        for (int i = 1; i <= 9; i++) {
            assertTrue(dataSet.next());
            assertEquals(i + "", dataSet.getRow().getValue(0));
View Full Code Here

        assertFalse(dataSet.next());
    }

    public void testConfigurationNonDefaultColumnNameLineNumber() throws Exception {
        File file = new File("src/test/resources/xls_people.xls");
        DataContext dc = new ExcelDataContext(file, new ExcelConfiguration(2, true, true));
        Table table = dc.getDefaultSchema().getTables()[0];

        String[] columnNames = table.getColumnNames();
        assertEquals("[1, mike, male, 18]", Arrays.toString(columnNames));

        Query q = dc.query().from(table).select(table.getColumnByName("1")).toQuery();
        assertEquals("SELECT xls_people.1 FROM xls_people.xls.xls_people", q.toSql());

        DataSet dataSet = dc.executeQuery(q);
        assertTrue(dataSet.next());
        assertEquals("2", dataSet.getRow().getValue(0));
        for (int i = 3; i <= 9; i++) {
            assertTrue(dataSet.next());
            assertEquals(i + "", dataSet.getRow().getValue(0));
View Full Code Here

TOP

Related Classes of org.apache.metamodel.DataContext

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.