Package org.apache.metamodel.jdbc.dialects

Examples of org.apache.metamodel.jdbc.dialects.IQueryRewriter


  protected String createSqlStatement() {
    return createSqlStatement(getTable());
  }

  private String createSqlStatement(Table table) {
    final IQueryRewriter queryRewriter = getUpdateCallback().getDataContext().getQueryRewriter();
    final StringBuilder sb = new StringBuilder();
    sb.append("CREATE TABLE ");
    final Schema schema = getSchema();
    if (schema != null && schema.getName() != null) {
      sb.append(schema.getQualifiedLabel());
      sb.append(".");
    }
    sb.append(getUpdateCallback().quoteIfNescesary(table.getName()));
    sb.append(" (");
    final Column[] columns = table.getColumns();
    for (int i = 0; i < columns.length; i++) {
      final Column column = columns[i];
      if (i != 0) {
        sb.append(", ");
      }
      sb.append(getUpdateCallback().quoteIfNescesary(column.getName()));
      sb.append(' ');
      final String nativeType = column.getNativeType();
      if (nativeType == null) {
        ColumnType columnType = column.getType();
        if (columnType == null) {
          columnType = ColumnType.VARCHAR;
        }

        final String columnTypeString = queryRewriter.rewriteColumnType(columnType);

        sb.append(columnTypeString);
      } else {
        sb.append(nativeType);
      }
View Full Code Here


        if (!isConfigured()) {
            return;
        }
       
        JdbcDataContext strategy = new JdbcDataContext(getConnection(), TableType.DEFAULT_TABLE_TYPES, DATABASE_NAME);
        IQueryRewriter queryRewriter = strategy.getQueryRewriter();
        assertSame(SQLServerQueryRewriter.class, queryRewriter.getClass());

        Schema schema = strategy.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
View Full Code Here

        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();

        assertEquals(
                "SELECT \"Product\".\"Name\" FROM Production.\"Product\" Product WHERE Product.\"Color\" = 'R''ed'",
                queryRewriter.rewriteQuery(q));
    }
View Full Code Here

    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
View Full Code Here

    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(
                "SELECT Production.\"Product\".\"Name\" FROM Production.\"Product\" WHERE Production.\"Product\".\"Color\" = 'R''ed'",
                queryRewriter.rewriteQuery(q));
    }
View Full Code Here

        realStatement.close();
    }

    public void testMaxRowsRewrite() throws Exception {
        JdbcDataContext dc = new JdbcDataContext(getTestDbConnection(), new TableType[] { TableType.TABLE }, null);
        IQueryRewriter rewriter = new DefaultQueryRewriter(dc) {
            @Override
            public String rewriteQuery(Query query) {
                return "SELECT COUNT(*) FROM PUBLIC.CUSTOMERS";
            }
        };
View Full Code Here

        ds.close();
    }

    public void testQueryRewriterQuoteAliases() throws Exception {
        JdbcDataContext dc = new JdbcDataContext(_connection);
        IQueryRewriter queryRewriter = dc.getQueryRewriter();
        assertSame(HsqldbQueryRewriter.class, queryRewriter.getClass());

        Schema schema = dc.getSchemaByName("PUBLIC");
        Table productsTable = schema.getTableByName("PRODUCTS");

        Query q = new Query().from(productsTable, "pro-ducts").select(
                new SelectItem(productsTable.getColumnByName("PRODUCTCODE")).setAlias("c|o|d|e"));
        q.setMaxRows(5);

        assertEquals("SELECT pro-ducts.\"PRODUCTCODE\" AS c|o|d|e FROM PUBLIC.\"PRODUCTS\" pro-ducts", q.toString());

        String queryString = queryRewriter.rewriteQuery(q);
        assertEquals("SELECT TOP 5 \"pro-ducts\".\"PRODUCTCODE\" AS \"c|o|d|e\" FROM PUBLIC.\"PRODUCTS\" \"pro-ducts\"",
                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 \"pro-ducts\".\"PRODUCTCODE\" AS \"c|o|d|e\" FROM PUBLIC.\"PRODUCTS\" \"pro-ducts\"",
                queryString);

        // Test that the original query is still the same (ie. it has been
        // cloned for execution)
View Full Code Here

    }

    public void testQueryRewriter() throws Exception {
        final JdbcDataContext dc = new JdbcDataContext(conn);

        final IQueryRewriter queryRewriter = dc.getQueryRewriter();
        assertEquals("H2QueryRewriter", queryRewriter.getClass().getSimpleName());

        assertTrue(queryRewriter.isFirstRowSupported());
        assertTrue(queryRewriter.isMaxRowsSupported());
    }
View Full Code Here

                Arrays.toString(schemas[0].getTableByName("CUSTOMERS").getColumns()));
    }

    public void testQueryRewriterQuoteAliases() throws Exception {
        JdbcDataContext dc = new JdbcDataContext(_connection, new TableType[] { TableType.TABLE, TableType.VIEW }, null);
        IQueryRewriter queryRewriter = dc.getQueryRewriter();
        assertSame(DefaultQueryRewriter.class, queryRewriter.getClass());

        Schema schema = dc.getSchemaByName("APP");
        Table customersTable = schema.getTableByName("CUSTOMERS");

        Query q = dc.query().from(customersTable).as("cus-tomers").select("CUSTOMERNAME").as("c|o|d|e").toQuery();
        assertEquals(25000, dc.getFetchSizeCalculator().getFetchSize(q));

        q.setMaxRows(5);

        assertEquals("SELECT cus-tomers.\"CUSTOMERNAME\" AS c|o|d|e FROM APP.\"CUSTOMERS\" cus-tomers", q.toString());

        String queryString = queryRewriter.rewriteQuery(q);
        assertEquals("SELECT \"cus-tomers\".\"CUSTOMERNAME\" AS \"c|o|d|e\" FROM APP.\"CUSTOMERS\" \"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 \"cus-tomers\".\"CUSTOMERNAME\" AS \"c|o|d|e\" FROM APP.\"CUSTOMERS\" \"cus-tomers\"",
                queryString);

        // Test that the original query is still the same (ie. it has been
        // cloned for execution)
View Full Code Here

    assertEquals("Schema[name=dbo]", strategy.getDefaultSchema().toString());
  }

  public void testQueryRewriterQuoteAliases() throws Exception {
    JdbcDataContext strategy = new JdbcDataContext(_connection, TableType.DEFAULT_TABLE_TYPES, _databaseName);
    IQueryRewriter queryRewriter = strategy.getQueryRewriter();
    assertSame(SQLServerQueryRewriter.class, queryRewriter.getClass());

    Schema schema = strategy.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
View Full Code Here

TOP

Related Classes of org.apache.metamodel.jdbc.dialects.IQueryRewriter

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.