Examples of JdbcDataContext


Examples of org.apache.metamodel.jdbc.JdbcDataContext

    public void testQueryRewriterQuoteAliases() throws Exception {
        if (!isConfigured()) {
            return;
        }
        JdbcDataContext dc = new JdbcDataContext(getConnection(), TableType.DEFAULT_TABLE_TYPES, DATABASE_NAME);
        IQueryRewriter queryRewriter = dc.getQueryRewriter();
        assertSame(SQLServerQueryRewriter.class, queryRewriter.getClass());

        Schema schema = dc.getSchemaByName("Sales");
        Table customersTable = schema.getTableByName("CUSTOMER");

        Query q = new Query().from(customersTable, "cus-tomers").select(
                new SelectItem(customersTable.getColumnByName("AccountNumber")).setAlias("c|o|d|e"));
        q.setMaxRows(5);

        assertEquals("SELECT cus-tomers.\"AccountNumber\" AS c|o|d|e FROM Sales.\"Customer\" cus-tomers", q.toString());

        String queryString = queryRewriter.rewriteQuery(q);
        assertEquals(
                "SELECT TOP 5 \"cus-tomers\".\"AccountNumber\" AS \"c|o|d|e\" FROM Sales.\"Customer\" \"cus-tomers\"",
                queryString);

        // We have to test that no additional quoting characters are added every
        // time we run the rewriting
        queryString = queryRewriter.rewriteQuery(q);
        queryString = queryRewriter.rewriteQuery(q);
        assertEquals(
                "SELECT TOP 5 \"cus-tomers\".\"AccountNumber\" AS \"c|o|d|e\" FROM Sales.\"Customer\" \"cus-tomers\"",
                queryString);

        // Test that the original query is still the same (ie. it has been
        // cloned for execution)
        assertEquals("SELECT cus-tomers.\"AccountNumber\" AS c|o|d|e FROM Sales.\"Customer\" cus-tomers", q.toString());

        DataSet data = dc.executeQuery(q);
        assertNotNull(data);
        data.close();
    }
View Full Code Here

Examples of org.apache.metamodel.jdbc.JdbcDataContext

    public void testQuotedString() throws Exception {
        if (!isConfigured()) {
            return;
        }
        JdbcDataContext dc = new JdbcDataContext(getConnection(), TableType.DEFAULT_TABLE_TYPES, DATABASE_NAME);
        IQueryRewriter queryRewriter = dc.getQueryRewriter();
        assertSame(SQLServerQueryRewriter.class, queryRewriter.getClass());

        Query q = dc.query().from("Production", "Product").select("Name").where("Color").eq("R'ed").toQuery();

        DataSet ds = dc.executeQuery(q);
        assertNotNull(ds);
        assertFalse(ds.next());
        ds.close();

        assertEquals(
View Full Code Here

Examples of org.apache.metamodel.jdbc.JdbcDataContext

  public void testGetSchemas() throws Exception {
      if (!isConfigured()) {
          return;
      }
    JdbcDataContext dataContext = getDataContext();
        Schema[] schemas = dataContext.getSchemas();
    assertEquals(1, schemas.length);
    Schema schema = dataContext.getDefaultSchema();
    assertEquals("{JdbcTable[name=COUNTRY,type=TABLE,remarks=<null>],"
        + "JdbcTable[name=CUSTOMER,type=TABLE,remarks=<null>],"
        + "JdbcTable[name=DEPARTMENT,type=TABLE,remarks=<null>],"
        + "JdbcTable[name=EMPLOYEE,type=TABLE,remarks=<null>],"
        + "JdbcTable[name=EMPLOYEE_PROJECT,type=TABLE,remarks=<null>],"
View Full Code Here

Examples of org.apache.metamodel.jdbc.JdbcDataContext

  public void testExecuteQuery() throws Exception {
      if (!isConfigured()) {
            return;
        }
    JdbcDataContext dataContext = getDataContext();
        Schema schema = dataContext.getDefaultSchema();
    Table departmentTable = schema.getTableByName("DEPARTMENT");
    Table employeeTable = schema.getTableByName("EMPLOYEE");
    Query q = new Query().from(new FromItem(JoinType.INNER, departmentTable.getRelationships(employeeTable)[0]));
    q.select(departmentTable.getColumns()[1]);
    q.select(new SelectItem(employeeTable.getColumns()[4]).setAlias("hire-date"));
    assertEquals(
        "SELECT \"DEPARTMENT\".\"DEPARTMENT\", \"EMPLOYEE\".\"HIRE_DATE\" AS hire-date FROM \"EMPLOYEE\" INNER JOIN \"DEPARTMENT\" ON \"EMPLOYEE\".\"EMP_NO\" = \"DEPARTMENT\".\"MNGR_NO\"",
        q.toString());

    DataSet data = dataContext.executeQuery(q);
    assertNotNull(data);

    TableModel tableModel = new DataSetTableModel(data);
    assertEquals(2, tableModel.getColumnCount());
    assertEquals(17, tableModel.getRowCount());
View Full Code Here

Examples of org.apache.metamodel.jdbc.JdbcDataContext

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

        Schema schema = new JdbcDataContext(getConnection(), new TableType[] { TableType.TABLE }, null)
                .getSchemaByName("SYS");
        assertEquals(12, schema.getTableCount());
    }
View Full Code Here

Examples of org.apache.metamodel.jdbc.JdbcDataContext

    public void testGetSchemaNames() throws Exception {
        if (!isConfigured()) {
            return;
        }
        DataContext dc = new JdbcDataContext(getConnection());
        String[] schemaNames = dc.getSchemaNames();

        String concatSchemas = Arrays.toString(schemaNames);

        // In order to allow the database to be used for other purposes than
        // this integration test, we will not make an exact assertion as to
        // which schema names exist, but just assert that HR and the default
        // oracle schemas exist.
        assertTrue(concatSchemas.indexOf("foobar_schema_that_does_not_exist") == -1);
        assertTrue(concatSchemas.indexOf("HR") != -1);
        assertTrue(concatSchemas.indexOf("SYSTEM") != -1);
        assertTrue(concatSchemas.indexOf("XDB") != -1);
        assertTrue(schemaNames.length > 8);

        Schema schema = dc.getDefaultSchema();
        assertEquals("HR", schema.getName());
    }
View Full Code Here

Examples of org.apache.metamodel.jdbc.JdbcDataContext

       
        return _connection;
    }
   
    protected JdbcDataContext getDataContext() {
        return new JdbcDataContext(getConnection());
    }
View Full Code Here

Examples of org.apache.metamodel.jdbc.JdbcDataContext

      // do nothing
    }

    assertFalse(getConnection().isReadOnly());

    JdbcDataContext dc = new JdbcDataContext(getConnection());
    final Schema schema = dc.getDefaultSchema();
    assertEquals("sakila", schema.getName());

    dc.executeUpdate(new UpdateScript() {
      @Override
      public void run(UpdateCallback cb) {
        Table table = cb.createTable(schema, "test_table").withColumn("id").ofType(ColumnType.INTEGER)
            .asPrimaryKey().withColumn("birthdate").ofType(ColumnType.DATE).execute();

        cb.insertInto(table).value("id", "1").execute();
        cb.insertInto(table).value("id", 2).value("birthdate", "2011-12-21").execute();
      }
    });

    assertTrue(dc.getColumnByQualifiedLabel("test_table.id").isPrimaryKey());
    assertFalse(dc.getColumnByQualifiedLabel("test_table.birthdate").isPrimaryKey());

    DataSet ds = dc.query().from("test_table").select("id").and("birthdate").execute();
    assertTrue(ds.next());
    assertEquals("Row[values=[1, null]]", ds.getRow().toString());
    assertEquals("java.lang.Integer", ds.getRow().getValue(0).getClass().getName());
    assertTrue(ds.next());
    assertEquals("Row[values=[2, 2011-12-21]]", ds.getRow().toString());
    assertEquals("java.sql.Date", ds.getRow().getValue(1).getClass().getName());
    assertFalse(ds.next());
    ds.close();

    dc.executeUpdate(new UpdateScript() {
      @Override
      public void run(UpdateCallback callback) {
        callback.dropTable("test_table").execute();
      }
    });
View Full Code Here

Examples of org.apache.metamodel.jdbc.JdbcDataContext

  public void testAlternativeConnectionString() throws Exception {
      if (!isConfigured()) {
            return;
        }
       
    DataContext dc = new JdbcDataContext(getConnection(), TableType.DEFAULT_TABLE_TYPES, "sakila");
    Schema[] schemas = dc.getSchemas();
    assertEquals("[Schema[name=mysql], Schema[name=performance_schema], Schema[name=portal], "
        + "Schema[name=sakila], Schema[name=world]]", Arrays.toString(schemas));

    Table table = dc.getSchemaByName("sakila").getTableByName("film");
    Query q = new Query().from(table).select(table.getColumns());
    DataSet data = dc.executeQuery(q);
    TableModel tableModel = new DataSetTableModel(data);
    assertEquals(13, tableModel.getColumnCount());
    assertEquals(1000, tableModel.getRowCount());
  }
View Full Code Here

Examples of org.apache.metamodel.jdbc.JdbcDataContext

  public void testGetCatalogNames() throws Exception {
      if (!isConfigured()) {
            return;
        }
       
    JdbcDataContext dataContext = new JdbcDataContext(getConnection());
    assertTrue(dataContext.getQueryRewriter() instanceof MysqlQueryRewriter);
    String[] catalogNames = dataContext.getCatalogNames();
    assertEquals("[information_schema, mysql, performance_schema, portal, sakila, world]",
        Arrays.toString(catalogNames));
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.