Package org.apache.metamodel

Examples of org.apache.metamodel.DataContext


  public void testQueryWithSingleQuote() throws Exception {
      if (!isConfigured()) {
            return;
        }
       
    DataContext dc = new JdbcDataContext(getConnection(), TableType.DEFAULT_TABLE_TYPES, "sakila");
    Query q = dc.query().from("category").selectCount().where("name").eq("kasper's horror movies").toQuery();
    DataSet ds = dc.executeQuery(q);
    assertTrue(ds.next());
    assertEquals(0, ((Number) ds.getRow().getValue(0)).intValue());
    assertFalse(ds.next());
  }
View Full Code Here


    public void testDifferentOperators() throws Exception {
        JdbcTestTemplates.differentOperatorsTest(_connection);
    }

    public void testGetSchemas() throws Exception {
        DataContext dc = new JdbcDataContext(_connection);
        String[] schemaNames = dc.getSchemaNames();
        assertEquals("[null]", Arrays.toString(schemaNames));

        Schema schema = dc.getDefaultSchema();
        assertNotNull(schema);
        assertNull(schema.getName());

        Table[] tables = schema.getTables();
        assertEquals("[Table[name=system,type=TABLE,remarks=null], "
View Full Code Here

        assertEquals("[anonymous, 12]", Arrays.toString(data.get(0)));
        assertEquals("[authenticated, 4]", Arrays.toString(data.get(1)));
    }

    public void testQualifiedLabel() throws Exception {
        DataContext dc = new JdbcDataContext(_connection);
        Schema schema = dc.getDefaultSchema();
        Table wikiTable = schema.getTableByName("WIKI");
        assertEquals("wiki", wikiTable.getQualifiedLabel());
        Column nameColumn = wikiTable.getColumnByName("name");
        assertEquals("wiki.name", nameColumn.getQualifiedLabel());

        assertEquals(
                "Column[name=name,columnNumber=0,type=VARCHAR,nullable=true,nativeType=TEXT,columnSize=2000000000]", dc
                        .getColumnByQualifiedLabel("wiki.name").toString());
        assertEquals("Table[name=wiki,type=TABLE,remarks=null]", dc.getTableByQualifiedLabel("WIKI").toString());
    }
View Full Code Here

    public void testGetDefaultSchema() throws Exception {
        if (!isConfigured()) {
            return;
        }

        DataContext dc = new JdbcDataContext(getConnection());
        Schema schema = dc.getDefaultSchema();
        assertEquals("public", schema.getName());
    }
View Full Code Here

    public void testGetSchema() throws Exception {
        if (!isConfigured()) {
            return;
        }

        DataContext dc = new JdbcDataContext(getConnection());
        Schema[] schemas = dc.getSchemas();
        assertTrue(schemas.length >= 3);

        assertNotNull(dc.getSchemaByName("information_schema"));
        assertNotNull(dc.getSchemaByName("pg_catalog"));
        assertNotNull(dc.getSchemaByName("public"));

        Schema schema = dc.getSchemaByName("public");

        assertEquals("[Table[name=categories,type=TABLE,remarks=null], "
                + "Table[name=cust_hist,type=TABLE,remarks=null], " + "Table[name=customers,type=TABLE,remarks=null], "
                + "Table[name=inventory,type=TABLE,remarks=null], "
                + "Table[name=orderlines,type=TABLE,remarks=null], " + "Table[name=orders,type=TABLE,remarks=null], "
View Full Code Here

    public void testExecuteQueryInPublicSchema() throws Exception {
        if (!isConfigured()) {
            return;
        }

        DataContext dc = new JdbcDataContext(getConnection());
        Query q = new Query();
        Schema schema = dc.getSchemaByName("public");
        Table productsTable = schema.getTableByName("products");
        q.from(productsTable);

        Column titleColumn = productsTable.getColumnByName("title");
        Column productPriceColumn = productsTable.getColumnByName("price");
        q.select(titleColumn, productPriceColumn);
        q.getSelectClause().getItem(0).setAlias("product-title");

        DataSet data = dc.executeQuery(q);
        TableModel tableModel = new DataSetTableModel(data);
        assertEquals(2, tableModel.getColumnCount());
        assertEquals(10000, tableModel.getRowCount());

        assertEquals("ACADEMY ACADEMY", tableModel.getValueAt(0, 0).toString());
        assertEquals("25.99", tableModel.getValueAt(0, 1).toString());

        assertEquals("ACADEMY HORN", tableModel.getValueAt(432, 0).toString());
        assertEquals("16.99", tableModel.getValueAt(6346, 1).toString());

        assertEquals("ALADDIN ZORRO", tableModel.getValueAt(9999, 0).toString());
        assertEquals("10.99", tableModel.getValueAt(9999, 1).toString());

        data = null;
        tableModel = null;

        Column prodIdColumn = productsTable.getColumnByName("prod_id");
        Table orderlinesTable = schema.getTableByName("orderlines");
        Column commonProdIdColumn = orderlinesTable.getColumnByName("prod_id");
        Column quantityColumn = orderlinesTable.getColumnByName("quantity");

        q.from(orderlinesTable);
        q.where(new FilterItem(new SelectItem(prodIdColumn), OperatorType.EQUALS_TO, new SelectItem(commonProdIdColumn)));
        q.groupBy(titleColumn);
        q.getSelectClause().removeItem(q.getSelectClause().getSelectItem(productPriceColumn));
        SelectItem quantitySum = new SelectItem(FunctionType.SUM, quantityColumn).setAlias("orderAmount");
        q.select(quantitySum);
        q.having(new FilterItem(quantitySum, OperatorType.GREATER_THAN, 25));
        q.orderBy(new OrderByItem(q.getSelectClause().getItem(0)));

        assertEquals("SELECT \"products\".\"title\" AS product-title, SUM(\"orderlines\".\"quantity\") AS orderAmount "
                + "FROM public.\"products\", public.\"orderlines\" "
                + "WHERE \"products\".\"prod_id\" = \"orderlines\".\"prod_id\" " + "GROUP BY \"products\".\"title\" "
                + "HAVING SUM(\"orderlines\".\"quantity\") > 25 " + "ORDER BY \"products\".\"title\" ASC", q.toString());
        data = dc.executeQuery(q);
        tableModel = new DataSetTableModel(data);
        assertEquals(2, tableModel.getColumnCount());
        assertEquals(136, tableModel.getRowCount());

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

        if (!"true".equalsIgnoreCase(getProperties().getProperty(PROPERTY_LONGRUNNINGTESTS))) {
            return;
        }

        DataContext dc = new JdbcDataContext(getConnection());
        Query q = new Query();
        Schema schema = dc.getSchemaByName("public");
        Table productsTable = schema.getTableByName("products");
        Table customerTable = schema.getTableByName("customers");
        q.from(productsTable, "p").from(customerTable, "c");

        Column titleColumn = productsTable.getColumnByName("title");
View Full Code Here

        assertNotNull(data);
        data.close();
    }

    public void testQualifiedLabel() throws Exception {
        DataContext dc = new JdbcDataContext(_connection);

        Column column = dc.getDefaultSchema().getTableByName("PRODUCTS").getColumnByName("PRODUCTCODE");
        assertEquals("PUBLIC.PRODUCTS.PRODUCTCODE", column.getQualifiedLabel());
        assertEquals("Table[name=PRODUCTS,type=TABLE,remarks=null]", dc.getTableByQualifiedLabel("PUBLIC.PRODUCTS").toString());
        assertEquals("Table[name=PRODUCTS,type=TABLE,remarks=null]", dc.getTableByQualifiedLabel("PRODUCTS").toString());
        assertEquals("Column[name=PRODUCTCODE,columnNumber=0,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50]", dc
                .getColumnByQualifiedLabel("PUBLIC.PRODUCTS.PRODUCTCODE").toString());
        assertEquals("Column[name=PRODUCTCODE,columnNumber=0,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50]", dc
                .getColumnByQualifiedLabel("PRODUCTS.PRODUCTCODE").toString());
    }
View Full Code Here

        row = MetaModelHelper.executeSingleRowQuery(dc, q);
        assertEquals(1, ((Number) row.getValue(0)).intValue());
    }

    public void testWhereColInValues() throws Exception {
        DataContext dc = new JdbcDataContext(_connection);
        DataSet ds = dc.query().from("QUADRANT_ACTUALS").select("REGION").and("DEPARTMENT").where("DEPARTMENT")
                .in("Sales", "Finance").execute();

        assertTrue(ds.next());
        assertEquals("Row[values=[Central, Sales]]", ds.getRow().toString());
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

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.