Package org.apache.metamodel

Examples of org.apache.metamodel.QueryPostprocessDataContext


                (String) description);
    }

    public void testHighColumnNameLineNumber() throws Exception {
        File file = new File("src/test/resources/csv_people.csv");
        QueryPostprocessDataContext dc = new CsvDataContext(file, new CsvConfiguration(3));
        Schema[] schemas = dc.getSchemas();
        assertEquals(2, schemas.length);
        Schema schema = dc.getDefaultSchema();
        assertEquals("resources", schema.getName());
        assertEquals(1, schema.getTableCount());
        Table table = schema.getTables()[0];
        assertEquals("csv_people.csv", table.getName());

        assertEquals(4, table.getColumnCount());
        assertEquals(0, table.getRelationshipCount());

        Column[] columns = table.getColumns();
        assertEquals("2", columns[0].getName());
        assertEquals("michael", columns[1].getName());
        assertEquals("male", columns[2].getName());
        assertEquals("19", columns[3].getName());

        Query query = dc.query().from(table).select(table.getColumnByName("michael")).toQuery();

        DataSet dataSet = dc.executeQuery(query);
        assertTrue(dataSet.next());
        assertEquals("peter", dataSet.getRow().getValue(0));
        assertTrue(dataSet.next());
        assertEquals("bob", dataSet.getRow().getValue(0));
        assertTrue(dataSet.next());
View Full Code Here


        assertEquals("barbara, barb", dataSet.getRow().getValue(0));
    }

    public void testNoColumnNames() throws Exception {
        File file = new File("src/test/resources/csv_people.csv");
        QueryPostprocessDataContext dc = new CsvDataContext(file, new CsvConfiguration(
                CsvConfiguration.NO_COLUMN_NAME_LINE));
        Schema[] schemas = dc.getSchemas();
        assertEquals(2, schemas.length);
        Schema schema = dc.getDefaultSchema();
        assertEquals("resources", schema.getName());
        assertEquals(1, schema.getTableCount());
        Table table = schema.getTables()[0];
        assertEquals("csv_people.csv", table.getName());

        assertEquals(4, table.getColumnCount());
        assertEquals(0, table.getRelationshipCount());

        Column[] columns = table.getColumns();
        assertEquals("A", columns[0].getName());
        assertEquals("B", columns[1].getName());
        assertEquals("C", columns[2].getName());
        assertEquals("D", columns[3].getName());

        Query query = dc.query().from(table).select(table.getColumnByName("B")).toQuery();

        DataSet dataSet = dc.executeQuery(query);
        assertTrue(dataSet.next());
        assertEquals("name", dataSet.getRow().getValue(0));
        assertTrue(dataSet.next());
        assertEquals("mike", dataSet.getRow().getValue(0));
        assertTrue(dataSet.next());
View Full Code Here

        assertEquals("michael", dataSet.getRow().getValue(0));
    }

    public void testGetSchemas() throws Exception {
        File file = new File("src/test/resources/csv_people.csv");
        QueryPostprocessDataContext dc = new CsvDataContext(file);
        Schema[] schemas = dc.getSchemas();
        assertEquals(2, schemas.length);
        Schema schema = dc.getDefaultSchema();
        assertEquals("resources", schema.getName());
        assertEquals(1, schema.getTableCount());
        Table table = schema.getTables()[0];
        assertEquals("csv_people.csv", table.getName());
View Full Code Here

        assertEquals("age", columns[3].getName());
    }

    public void testWhereItemNotInSelectClause() throws Exception {
        File file = new File("src/test/resources/csv_people.csv");
        QueryPostprocessDataContext dc = new CsvDataContext(file);
        Table table = dc.getDefaultSchema().getTableByName("csv_people.csv");

        Query q = new Query();
        q.from(table);
        q.where(new FilterItem(new SelectItem(table.getColumnByName("id")), OperatorType.EQUALS_TO, 1));
        q.select(table.getColumnByName("name"));
        DataSet data = dc.executeQuery(q);
        assertTrue(data.next());
        assertEquals("Row[values=[mike]]", data.getRow().toString());
        assertFalse(data.next());
    }
View Full Code Here

        assertFalse(data.next());
    }

    public void testWhereColumnInValues() throws Exception {
        File file = new File("src/test/resources/csv_people.csv");
        QueryPostprocessDataContext dc = new CsvDataContext(file, new CsvConfiguration(1, true, true));
        Table table = dc.getDefaultSchema().getTableByName("csv_people.csv");

        Query q = dc.query().from(table).as("t").select("name").and("age").where("age").in("18", "20").toQuery();
        assertEquals("SELECT t.name, t.age FROM resources.csv_people.csv t WHERE t.age IN ('18' , '20')", q.toSql());

        DataSet ds = dc.executeQuery(q);
        assertTrue(ds.next());
        assertEquals("Row[values=[mike, 18]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[peter, 18]]", ds.getRow().toString());
        assertTrue(ds.next());
View Full Code Here

        assertEquals(5, tableModel.getRowCount());
    }

    public void testQueryOnlyAggregate() throws Exception {
        File file = new File("src/test/resources/csv_people.csv");
        QueryPostprocessDataContext dc = new CsvDataContext(file);
        Table table = dc.getDefaultSchema().getTables()[0];

        Query q = new Query().selectCount().from(table);
        assertEquals("SELECT COUNT(*) FROM resources.csv_people.csv", q.toString());

        List<Object[]> data = dc.executeQuery(q).toObjectArrays();
        assertEquals(1, data.size());
        Object[] row = data.get(0);
        assertEquals(1, row.length);
        assertEquals("[9]", Arrays.toString(row));

        q.select(table.getColumns()[0]);
        assertEquals("SELECT COUNT(*), csv_people.csv.id FROM resources.csv_people.csv", q.toString());
        data = dc.executeQuery(q).toObjectArrays();
        assertEquals(9, data.size());
        row = data.get(0);
        assertEquals(2, row.length);
        assertEquals("[9, 1]", Arrays.toString(row));

View Full Code Here

        assertEquals(
                "SELECT persons.column_number FROM s.persons WHERE (persons.name = 'kasper' OR persons.role = 'user')",
                q.toString());

        DataContext dc = new QueryPostprocessDataContext() {

            @Override
            public DataSet materializeMainSchemaTable(Table table, Column[] columns, int maxRows) {
                assertEquals(3, columns.length);
                assertEquals("column_number", columns[0].getName());
                assertEquals("name", columns[1].getName());
                assertEquals("role", columns[2].getName());
                SelectItem[] selectItems = new SelectItem[] { new SelectItem(col1), new SelectItem(col2),
                        new SelectItem(col3) };
                DataSetHeader header = new CachingDataSetHeader(selectItems);
                List<Row> rows = new LinkedList<Row>();
                rows.add(new DefaultRow(header, new Object[] { "foo", "bar", 1 }));
                rows.add(new DefaultRow(header, new Object[] { "kasper", "developer", 2 }));
                rows.add(new DefaultRow(header, new Object[] { "admin", "admin", 3 }));
                rows.add(new DefaultRow(header, new Object[] { "elikeon", "user", 4 }));
                rows.add(new DefaultRow(header, new Object[] { "someuser", "user", 5 }));
                rows.add(new DefaultRow(header, new Object[] { "hmm", "what-the", 6 }));

                return new InMemoryDataSet(header, rows);
            }

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

            @Override
            protected Schema getMainSchema() throws MetaModelException {
                return schema;
            }
        };

        DataSet result = dc.executeQuery(q);
        List<Object[]> objectArrays = result.toObjectArrays();
        assertEquals(3, objectArrays.size());
        assertEquals(2, objectArrays.get(0)[0]);
        assertEquals(4, objectArrays.get(1)[0]);
        assertEquals(5, objectArrays.get(2)[0]);
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

                (String) description);
    }

    public void testHighColumnNameLineNumber() throws Exception {
        File file = new File("src/test/resources/csv_people.csv");
        QueryPostprocessDataContext dc = new CsvDataContext(file, new CsvConfiguration(3));
        Schema[] schemas = dc.getSchemas();
        assertEquals(2, schemas.length);
        Schema schema = dc.getDefaultSchema();
        assertEquals("resources", schema.getName());
        assertEquals(1, schema.getTableCount());
        Table table = schema.getTables()[0];
        assertEquals("csv_people.csv", table.getName());

        assertEquals(4, table.getColumnCount());
        assertEquals(0, table.getRelationshipCount());

        Column[] columns = table.getColumns();
        assertEquals("2", columns[0].getName());
        assertEquals("michael", columns[1].getName());
        assertEquals("male", columns[2].getName());
        assertEquals("19", columns[3].getName());

        Query query = dc.query().from(table).select(table.getColumnByName("michael")).toQuery();

        DataSet dataSet = dc.executeQuery(query);
        assertTrue(dataSet.next());
        assertEquals("peter", dataSet.getRow().getValue(0));
        assertTrue(dataSet.next());
        assertEquals("bob", dataSet.getRow().getValue(0));
        assertTrue(dataSet.next());
View Full Code Here

        assertEquals("barbara, barb", dataSet.getRow().getValue(0));
    }

    public void testNoColumnNames() throws Exception {
        File file = new File("src/test/resources/csv_people.csv");
        QueryPostprocessDataContext dc = new CsvDataContext(file, new CsvConfiguration(
                CsvConfiguration.NO_COLUMN_NAME_LINE));
        Schema[] schemas = dc.getSchemas();
        assertEquals(2, schemas.length);
        Schema schema = dc.getDefaultSchema();
        assertEquals("resources", schema.getName());
        assertEquals(1, schema.getTableCount());
        Table table = schema.getTables()[0];
        assertEquals("csv_people.csv", table.getName());

        assertEquals(4, table.getColumnCount());
        assertEquals(0, table.getRelationshipCount());

        Column[] columns = table.getColumns();
        assertEquals("A", columns[0].getName());
        assertEquals("B", columns[1].getName());
        assertEquals("C", columns[2].getName());
        assertEquals("D", columns[3].getName());

        Query query = dc.query().from(table).select(table.getColumnByName("B")).toQuery();

        DataSet dataSet = dc.executeQuery(query);
        assertTrue(dataSet.next());
        assertEquals("name", dataSet.getRow().getValue(0));
        assertTrue(dataSet.next());
        assertEquals("mike", dataSet.getRow().getValue(0));
        assertTrue(dataSet.next());
View Full Code Here

TOP

Related Classes of org.apache.metamodel.QueryPostprocessDataContext

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.