Package org.apache.metamodel.schema

Examples of org.apache.metamodel.schema.Table


        // if from clause only contains a main schema table
        List<FromItem> fromItems = query.getFromClause().getItems();
        if (fromItems.size() == 1 && fromItems.get(0).getTable() != null
                && fromItems.get(0).getTable().getSchema() == _schema) {
            final Table table = fromItems.get(0).getTable();

            // if GROUP BY, HAVING and ORDER BY clauses are not specified
            if (query.getGroupByClause().isEmpty() && query.getHavingClause().isEmpty()
                    && query.getOrderByClause().isEmpty()) {
View Full Code Here


        assertFalse(executed.get().booleanValue());

        builder.withColumn("foo").ofType(ColumnType.VARCHAR).asPrimaryKey().ofNativeType("vch").ofSize(1234)
                .nullable(true);
        builder.withColumn("bar").withColumn("baz").nullable(false);
        Table table = builder.execute();

        assertTrue(executed.get().booleanValue());

        assertEquals("tablename", table.getName());
        assertEquals(3, table.getColumnCount());
        assertEquals("Column[name=foo,columnNumber=0,type=VARCHAR,nullable=true,nativeType=vch,columnSize=1234]",
                table.getColumns()[0].toString());
        assertEquals("Column[name=bar,columnNumber=1,type=null,nullable=null,nativeType=null,columnSize=null]",
                table.getColumns()[1].toString());
        assertEquals("Column[name=baz,columnNumber=2,type=null,nullable=false,nativeType=null,columnSize=null]",
                table.getColumns()[2].toString());

        assertEquals("CREATE TABLE schema.tablename (foo VARCHAR(1234) PRIMARY KEY,bar,baz NOT NULL)", builder.toSql());
    }
View Full Code Here

                null).setPrimaryKey(true));
        likeTable.addColumn(new MutableColumn("bar"));
        likeTable.addColumn(new MutableColumn("baz"));

        builder.like(likeTable);
        Table table = builder.execute();

        assertTrue(executed.get().booleanValue());

        assertEquals("tablename", table.getName());
        assertEquals(3, table.getColumnCount());
        assertEquals("Column[name=foo,columnNumber=0,type=VARCHAR,nullable=true,nativeType=vch,columnSize=1234]",
                table.getColumns()[0].toString());
        assertTrue(table.getColumns()[0].isPrimaryKey());

        assertEquals("Column[name=bar,columnNumber=1,type=null,nullable=null,nativeType=null,columnSize=null]",
                table.getColumns()[1].toString());
        assertEquals("Column[name=baz,columnNumber=2,type=null,nullable=null,nativeType=null,columnSize=null]",
                table.getColumns()[2].toString());
    }
View Full Code Here

public class QueryTest extends MetaModelTestCase {

    private Schema _schema = getExampleSchema();

    public void testSimpleQuery() throws Exception {
        Table contributorTable = _schema.getTableByName(TABLE_CONTRIBUTOR);

        Query q = new Query();
        q.selectCount().from(contributorTable);
        assertEquals("SELECT COUNT(*) FROM MetaModelSchema.contributor", q.toString());
    }
View Full Code Here

        q.selectCount().from(contributorTable);
        assertEquals("SELECT COUNT(*) FROM MetaModelSchema.contributor", q.toString());
    }

    public void testCloneGroupBy() throws Exception {
        Table table = _schema.getTableByName(TABLE_PROJECT);
        Column column = table.getColumnByName(COLUMN_PROJECT_NAME);
        Query q = new Query().from(table).selectCount().select(column).groupBy(column);
        assertEquals(q.toString(), q.clone().toString());

        q.having(new FilterItem(SelectItem.getCountAllItem(), OperatorType.GREATER_THAN, 20));
        assertEquals(q.toString(), q.clone().toString());
View Full Code Here

        assertEquals(q.toString(), q.clone().toString());
    }

    public void testFromItemAlias() throws Exception {
        Query q = new Query();
        Table contributorTable = _schema.getTableByName(TABLE_CONTRIBUTOR);
        Column nameColumn = contributorTable.getColumnByName(COLUMN_CONTRIBUTOR_NAME);
        Column countryColumn = contributorTable.getColumnByName(COLUMN_CONTRIBUTOR_COUNTRY);

        FromItem fromContributor = new FromItem(contributorTable);
        q.from(fromContributor);
        q.select(nameColumn, countryColumn);
        assertEquals("SELECT contributor.name, contributor.country FROM MetaModelSchema.contributor", q.toString());

        fromContributor.setAlias("c");

        assertEquals("SELECT c.name, c.country FROM MetaModelSchema.contributor c", q.toString());

        q.groupBy(new GroupByItem(q.getSelectClause().getSelectItem(nameColumn)));
        q.groupBy(new GroupByItem(q.getSelectClause().getSelectItem(countryColumn)));
        q.select(new SelectItem(FunctionType.COUNT, "*", "total"));
        assertEquals(2, q.getGroupByClause().getItems().size());
        assertEquals(
                "SELECT c.name, c.country, COUNT(*) AS total FROM MetaModelSchema.contributor c GROUP BY c.name, c.country",
                q.toString());

        Column contributorIdColumn = contributorTable.getColumnByName(COLUMN_CONTRIBUTOR_CONTRIBUTOR_ID);
        q.where(contributorIdColumn, OperatorType.EQUALS_TO, 1);
        assertEquals(
                "SELECT c.name, c.country, COUNT(*) AS total FROM MetaModelSchema.contributor c WHERE c.contributor_id = 1 GROUP BY c.name, c.country",
                q.toString());

View Full Code Here

                "SELECT c.name, c.country, COUNT(*) AS total FROM MetaModelSchema.contributor c WHERE c.contributor_id = 1 AND c.contributor_id <> c.name GROUP BY c.name, c.country",
                q.toString());
    }

    public void testAddOrderBy() throws Exception {
        Table contributorTable = _schema.getTableByName(TABLE_CONTRIBUTOR);
        Column nameColumn = contributorTable.getColumnByName(COLUMN_CONTRIBUTOR_NAME);
        Column countryColumn = contributorTable.getColumnByName(COLUMN_CONTRIBUTOR_COUNTRY);
        FromItem fromContributor = new FromItem(contributorTable);
        fromContributor.setAlias("a");

        Query q = new Query();
        q.select(nameColumn, countryColumn).from(fromContributor).orderBy(nameColumn)
View Full Code Here

                q.toString());
    }

    public void testCloneJoinAndOrderBy() throws Exception {
        Query q1 = new Query();
        Table contributorTable = _schema.getTableByName(TABLE_CONTRIBUTOR);
        Table roleTable = _schema.getTableByName(TABLE_ROLE);
        FromItem fromItem = new FromItem(JoinType.INNER, contributorTable.getRelationships(roleTable)[0]);
        q1.from(fromItem);

        Column nameColumn = contributorTable.getColumnByName(COLUMN_CONTRIBUTOR_NAME);
        Column countryColumn = contributorTable.getColumnByName(COLUMN_CONTRIBUTOR_COUNTRY);
        Column roleNameColumn = roleTable.getColumnByName(COLUMN_ROLE_ROLE_NAME);
        q1.select(nameColumn, countryColumn, roleNameColumn);
        q1.orderBy(roleNameColumn);
        String q1string = q1.toString();
        assertEquals(
                "SELECT contributor.name, contributor.country, role.name FROM MetaModelSchema.contributor INNER JOIN MetaModelSchema.role ON contributor.contributor_id = role.contributor_id ORDER BY role.name ASC",
View Full Code Here

    assertEquals("SELECT COUNT(*) FROM foobar", new Query().selectCount().from(
        "foobar").toString());
  }

  public void testRelationJoinToString() throws Exception {
    Table contributorTable = _schema.getTableByName(TABLE_CONTRIBUTOR);
    Table roleTable = _schema.getTableByName(TABLE_ROLE);
    Relationship[] relationships = roleTable
        .getRelationships(contributorTable);
    FromItem from = new FromItem(JoinType.INNER, relationships[0]);
    assertEquals(
        "MetaModelSchema.contributor INNER JOIN MetaModelSchema.role ON contributor.contributor_id = role.contributor_id",
        from.toString());
View Full Code Here

        "(MetaModelSchema.contributor a INNER JOIN MetaModelSchema.role ON a.contributor_id = role.contributor_id) myJoin",
        from.toString());
  }

  public void testSubQueryJoinToString() throws Exception {
    Table projectTable = _schema.getTableByName(TABLE_PROJECT);
    Table roleTable = _schema.getTableByName(TABLE_ROLE);

    Column projectIdColumn = projectTable
        .getColumnByName(COLUMN_PROJECT_PROJECT_ID);

    FromItem leftSide = new FromItem(projectTable);
    leftSide.setAlias("a");
    SelectItem[] leftOn = new SelectItem[] { new SelectItem(projectIdColumn) };

    Column[] columns = roleTable.getColumns();

    Query subQuery = new Query();
    FromItem subQueryFrom = new FromItem(roleTable);
    subQuery.from(subQueryFrom);
    subQuery.select(columns);
View Full Code Here

TOP

Related Classes of org.apache.metamodel.schema.Table

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.