Package org.apache.metamodel.query

Examples of org.apache.metamodel.query.SelectItem


            public DataSet materializeMainSchemaTable(Table table, Column[] columns, int maxRows) {
                if (table == table1) {
                    Column[] columns1 = table1.getColumns();
                    SelectItem[] selectItems = new SelectItem[columns1.length];
                    for (int i = 0; i < selectItems.length; i++) {
                        SelectItem selectItem = new SelectItem(columns1[i]);
                        selectItems[i] = selectItem;
                    }
                    List<Object[]> data = new ArrayList<Object[]>();
                    data.add(new Object[] { 1, "kasper", "denmark" });
                    data.add(new Object[] { 2, "asbjorn", "denmark" });
                    data.add(new Object[] { 3, "johny", "israel" });
                    data.add(new Object[] { 4, "daniel", "canada" });
                    data.add(new Object[] { 5, "sasidhar", "unknown" });
                    data.add(new Object[] { 6, "jesper", "denmark" });
                    if (maxRows != -1) {
                        for (int i = data.size() - 1; i >= maxRows; i--) {
                            data.remove(i);
                        }
                    }
                    return createDataSet(selectItems, data);
                } else if (table == table2) {
                    Column[] columns2 = table2.getColumns();
                    SelectItem[] selectItems = new SelectItem[columns2.length];
                    for (int i = 0; i < selectItems.length; i++) {
                        SelectItem selectItem = new SelectItem(columns2[i]);
                        selectItems[i] = selectItem;
                    }
                    List<Object[]> data = new ArrayList<Object[]>();
                    data.add(new Object[] { 1, 1, "founder" });
                    data.add(new Object[] { 1, 1, "developer" });
View Full Code Here


    public void testOrderByWithoutSelecting() throws Exception {
        Query q = new Query();
        q.from(new FromItem(table2).setAlias("r"));
        Column roleColumn = table2.getColumnByName(COLUMN_ROLE_ROLE_NAME);
        Column projectIdColumn = table2.getColumnByName(COLUMN_ROLE_PROJECT_ID);
        q.select(new SelectItem(projectIdColumn));
        q.orderBy(roleColumn);
        assertEquals("SELECT r.project_id FROM MetaModelSchema.role r ORDER BY r.name ASC", q.toString());

        DataContext dc = getDataContext();
        DataSet data = dc.executeQuery(q);
View Full Code Here

    public void testGroupByWithoutSelecting() throws Exception {
        Query q = new Query();
        q.from(new FromItem(table2).setAlias("r"));
        Column roleColumn = table2.getColumnByName(COLUMN_ROLE_ROLE_NAME);
        Column projectIdColumn = table2.getColumnByName(COLUMN_ROLE_PROJECT_ID);
        q.select(new SelectItem(FunctionType.SUM, projectIdColumn));
        q.groupBy(new GroupByItem(new SelectItem(roleColumn)));
        q.orderBy(roleColumn);
        assertEquals("SELECT SUM(r.project_id) FROM MetaModelSchema.role r GROUP BY r.name ORDER BY r.name ASC",
                q.toString());

        DataContext dc = getDataContext();
View Full Code Here

    public void testSimpleGroupBy() throws Exception {
        Query q = new Query();
        q.from(new FromItem(table2).setAlias("r"));
        Column roleColumn = table2.getColumnByName(COLUMN_ROLE_ROLE_NAME);
        q.select(new SelectItem(roleColumn));
        q.groupBy(new GroupByItem(new SelectItem(roleColumn)));
        assertEquals("SELECT r.name FROM MetaModelSchema.role r GROUP BY r.name", q.toString());

        DataContext dc = getDataContext();
        DataSet data = dc.executeQuery(q);
        assertEquals(1, data.getSelectItems().length);
        assertEquals("r.name", data.getSelectItems()[0].toString());
        TableModel tableModel = new DataSetTableModel(data);
        assertEquals(3, tableModel.getRowCount());

        q.select(new SelectItem(FunctionType.COUNT, "*", "c"));
        q.where(new FilterItem(new SelectItem(roleColumn), OperatorType.EQUALS_TO, "founder"));
        data = dc.executeQuery(q);
        assertEquals(2, data.getSelectItems().length);
        assertEquals("r.name", data.getSelectItems()[0].toString());
        assertEquals("COUNT(*) AS c", data.getSelectItems()[1].toString());
        tableModel = new DataSetTableModel(data);
        assertEquals(1, tableModel.getRowCount());
        assertEquals("founder", tableModel.getValueAt(0, 0));
        assertEquals(2l, tableModel.getValueAt(0, 1));

        q.select(new SelectItem(FunctionType.SUM, table2.getColumns()[0]));
        assertEquals(
                "SELECT r.name, COUNT(*) AS c, SUM(r.contributor_id) FROM MetaModelSchema.role r WHERE r.name = 'founder' GROUP BY r.name",
                q.toString());
        data = dc.executeQuery(q);
        assertEquals(3, data.getSelectItems().length);
View Full Code Here

        q.from(table2, "c");
        Column roleColumn = table2.getColumnByName(COLUMN_ROLE_ROLE_NAME);
        Column contributorIdColumn = table2.getColumnByName(COLUMN_ROLE_CONTRIBUTOR_ID);

        q.groupBy(roleColumn);
        SelectItem countSelectItem = new SelectItem(FunctionType.COUNT, contributorIdColumn).setAlias("my_count");
        q.select(new SelectItem(roleColumn), countSelectItem);
        q.having(new FilterItem(countSelectItem, OperatorType.GREATER_THAN, 1));
        q.orderBy(new OrderByItem(countSelectItem));
        assertEquals(
                "SELECT c.name, COUNT(c.contributor_id) AS my_count FROM MetaModelSchema.role c GROUP BY c.name HAVING COUNT(c.contributor_id) > 1 ORDER BY COUNT(c.contributor_id) ASC",
                q.toString());
View Full Code Here

        q.from(table2, "c");
        Column roleColumn = table2.getColumnByName(COLUMN_ROLE_ROLE_NAME);
        Column contributorIdColumn = table2.getColumnByName(COLUMN_ROLE_CONTRIBUTOR_ID);

        q.groupBy(roleColumn);
        SelectItem countSelectItem = new SelectItem(FunctionType.COUNT, contributorIdColumn).setAlias("my_count");
        q.select(new SelectItem(roleColumn));
        q.having(new FilterItem(countSelectItem, OperatorType.GREATER_THAN, 3));
        assertEquals("SELECT c.name FROM MetaModelSchema.role c GROUP BY c.name HAVING COUNT(c.contributor_id) > 3",
                q.toString());

        DataSet data = getDataContext().executeQuery(q);
View Full Code Here

        final QueryParameter param1 = new QueryParameter();
        final Query subQuery = dc.query().from(table1).select("name").where(COLUMN_CONTRIBUTOR_COUNTRY).eq(param1)
                .toQuery();

        final FromItem subQueryFromItem = new FromItem(subQuery);
        final Query query = new Query().select(new SelectItem(subQuery.getSelectClause().getItem(0), subQueryFromItem))
                .from(subQueryFromItem);

        final CompiledQuery compiledQuery = dc.compileQuery(query);

        try {
View Full Code Here

    public void testSimpleWhere() throws Exception {
        DataContext dc = getDataContext();
        Query q = new Query();
        q.from(table1);
        q.select(table1.getColumns());
        SelectItem countrySelectItem = q.getSelectClause().getSelectItem(
                table1.getColumnByName(COLUMN_CONTRIBUTOR_COUNTRY));
        q.where(new FilterItem(countrySelectItem, OperatorType.EQUALS_TO, "denmark"));

        DataSet data = dc.executeQuery(q);
        for (int i = 0; i < 3; i++) {
View Full Code Here

    }

    public void testCarthesianProductWithWhere() throws Exception {
        DataContext dc = getDataContext();

        SelectItem s1 = new SelectItem(table1.getColumnByName(COLUMN_CONTRIBUTOR_NAME));
        SelectItem s2 = new SelectItem(table2.getColumnByName(COLUMN_ROLE_ROLE_NAME));
        FromItem f1 = new FromItem(table1);
        FromItem f2 = new FromItem(table2);

        Query q = new Query();
        q.select(s1);
        q.select(s2);
        q.from(f1);
        q.from(f2);
        SelectItem s3 = new SelectItem(table1.getColumnByName(COLUMN_CONTRIBUTOR_CONTRIBUTOR_ID));
        SelectItem s4 = new SelectItem(table2.getColumnByName(COLUMN_ROLE_CONTRIBUTOR_ID));
        q.where(new FilterItem(s3, OperatorType.EQUALS_TO, s4));
        assertEquals(
                "SELECT contributor.name, role.name FROM MetaModelSchema.contributor, MetaModelSchema.role WHERE contributor.contributor_id = role.contributor_id",
                q.toString());
View Full Code Here

    }

    public void testSubSelectionAndInnerJoin() throws Exception {
        DataContext dc = getDataContext();

        SelectItem s1 = new SelectItem(table1.getColumnByName(COLUMN_CONTRIBUTOR_NAME));
        SelectItem s2 = new SelectItem(table2.getColumnByName(COLUMN_ROLE_ROLE_NAME));
        FromItem fromItem = new FromItem(JoinType.INNER, table1.getRelationships(table2)[0]);

        Query q = new Query();
        q.select(s1);
        q.select(s2);
View Full Code Here

TOP

Related Classes of org.apache.metamodel.query.SelectItem

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.