Package org.apache.metamodel.query

Examples of org.apache.metamodel.query.CompiledQuery


    }

    public void testCompiledQueryParameterInWhereClause() throws Exception {
        DataContext dc = getDataContext();
        QueryParameter param1 = new QueryParameter();
        CompiledQuery compiledQuery = dc.query().from(table1).select("name").where(COLUMN_CONTRIBUTOR_COUNTRY)
                .eq(param1).compile();
        try {
            assertEquals(1, compiledQuery.getParameters().size());
            assertSame(param1, compiledQuery.getParameters().get(0));

            DataSet ds = dc.executeQuery(compiledQuery, "denmark");
            try {
                assertTrue(ds.next());
                assertEquals("Row[values=[kasper]]", ds.getRow().toString());
                assertTrue(ds.next());
                assertEquals("Row[values=[asbjorn]]", ds.getRow().toString());
                assertTrue(ds.next());
                assertEquals("Row[values=[jesper]]", ds.getRow().toString());
                assertFalse(ds.next());
            } finally {
                ds.close();
            }

            try {
                ds = dc.executeQuery(compiledQuery, "canada");
                assertTrue(ds.next());
                assertEquals("Row[values=[daniel]]", ds.getRow().toString());
                assertFalse(ds.next());
            } finally {
                ds.close();
            }
        } finally {
            compiledQuery.close();
        }
    }
View Full Code Here


        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 {
            assertEquals(1, compiledQuery.getParameters().size());
            assertSame(param1, compiledQuery.getParameters().get(0));

            DataSet ds = dc.executeQuery(compiledQuery, "denmark");
            List<Object[]> objectArrays = ds.toObjectArrays();
            assertEquals(3, objectArrays.size());

        } finally {
            compiledQuery.close();
        }
    }
View Full Code Here

        q.select(table.getColumns());
        q.from(table, "a");
        q.where(table.getColumnByName("CUSTOMERNUMBER"), OperatorType.EQUALS_TO, queryParameter);
        q.where(table.getColumnByName("CUSTOMERNAME"), OperatorType.EQUALS_TO, queryParameter);

        final CompiledQuery compiledQuery = dataContext.compileQuery(q);

        final JdbcCompiledQuery jdbcCompiledQuery = (JdbcCompiledQuery) compiledQuery;
        assertEquals(0, jdbcCompiledQuery.getActiveLeases());
        assertEquals(0, jdbcCompiledQuery.getIdleLeases());

        String compliedQueryString = compiledQuery.toSql();

        assertEquals(
                "SELECT a._CUSTOMERNUMBER_, a._CUSTOMERNAME_, a._CONTACTLASTNAME_, a._CONTACTFIRSTNAME_, a._PHONE_, a._ADDRESSLINE1_, a._ADDRESSLINE2_, a._CITY_, "
                        + "a._STATE_, a._POSTALCODE_, a._COUNTRY_, a._SALESREPEMPLOYEENUMBER_, a._CREDITLIMIT_ FROM PUBLIC._CUSTOMERS_ a WHERE a._CUSTOMERNUMBER_ = ? "
                        + "AND a._CUSTOMERNAME_ = ?", compliedQueryString.replace('\"', '_'));
        DataSet result1 = dataContext.executeQuery(compiledQuery, new Object[] { 103, "Atelier graphique" });
        assertTrue(result1.next());
        assertEquals(
                "Row[values=[103, Atelier graphique, Schmitt, Carine, 40.32.2555, 54, rue Royale, null, Nantes, null, 44000, France, 1370, 21000.0]]",
                result1.getRow().toString());
        assertFalse(result1.next());

        assertEquals(1, jdbcCompiledQuery.getActiveLeases());
        assertEquals(0, jdbcCompiledQuery.getIdleLeases());

        DataSet result2 = dataContext.executeQuery(compiledQuery, new Object[] { 103, "Atelier graphique" });

        assertEquals(2, jdbcCompiledQuery.getActiveLeases());
        assertEquals(0, jdbcCompiledQuery.getIdleLeases());

        result1.close();

        assertEquals(1, jdbcCompiledQuery.getActiveLeases());
        assertEquals(1, jdbcCompiledQuery.getIdleLeases());

        result2.close();

        assertEquals(0, jdbcCompiledQuery.getActiveLeases());
        assertEquals(2, jdbcCompiledQuery.getIdleLeases());

        compiledQuery.close();

        assertEquals(0, jdbcCompiledQuery.getActiveLeases());
        assertEquals(0, jdbcCompiledQuery.getIdleLeases());
    }
View Full Code Here

        q.select(table.getColumns());
        q.from(table, "a");
        q.where(table.getColumnByName("CUSTOMERNUMBER"), OperatorType.EQUALS_TO, queryParameter);
        q.where(table.getColumnByName("CUSTOMERNAME"), OperatorType.EQUALS_TO, queryParameter);

        final CompiledQuery compiledQuery = dataContext.compileQuery(q);

        final JdbcCompiledQuery jdbcCompiledQuery = (JdbcCompiledQuery) compiledQuery;
        assertEquals(0, jdbcCompiledQuery.getActiveLeases());
        assertEquals(0, jdbcCompiledQuery.getIdleLeases());

        String compliedQueryString = compiledQuery.toSql();

        assertEquals(
                "SELECT a._CUSTOMERNUMBER_, a._CUSTOMERNAME_, a._CONTACTLASTNAME_, a._CONTACTFIRSTNAME_, a._PHONE_, a._ADDRESSLINE1_, a._ADDRESSLINE2_, a._CITY_, "
                        + "a._STATE_, a._POSTALCODE_, a._COUNTRY_, a._SALESREPEMPLOYEENUMBER_, a._CREDITLIMIT_ FROM PUBLIC._CUSTOMERS_ a WHERE a._CUSTOMERNUMBER_ = ? "
                        + "AND a._CUSTOMERNAME_ = ?", compliedQueryString.replace('\"', '_'));
        DataSet result1 = dataContext.executeQuery(compiledQuery, new Object[] { 103, "Atelier graphique" });
        assertTrue(result1.next());
        assertEquals(
                "Row[values=[103, Atelier graphique, Schmitt, Carine, 40.32.2555, 54, rue Royale, null, Nantes, null, 44000, France, 1370, 21000.0]]",
                result1.getRow().toString());
        assertFalse(result1.next());

        assertEquals(1, jdbcCompiledQuery.getActiveLeases());
        assertEquals(0, jdbcCompiledQuery.getIdleLeases());

        DataSet result2 = dataContext.executeQuery(compiledQuery, new Object[] { 103, "Atelier graphique" });

        assertEquals(2, jdbcCompiledQuery.getActiveLeases());
        assertEquals(0, jdbcCompiledQuery.getIdleLeases());

        result1.close();

        assertEquals(1, jdbcCompiledQuery.getActiveLeases());
        assertEquals(1, jdbcCompiledQuery.getIdleLeases());

        result2.close();

        assertEquals(0, jdbcCompiledQuery.getActiveLeases());
        assertEquals(2, jdbcCompiledQuery.getIdleLeases());

        compiledQuery.close();

        assertEquals(0, jdbcCompiledQuery.getActiveLeases());
        assertEquals(0, jdbcCompiledQuery.getIdleLeases());
    }
View Full Code Here

    }

    public void testCompiledQueryParameterInWhereClause() throws Exception {
        DataContext dc = getDataContext();
        QueryParameter param1 = new QueryParameter();
        CompiledQuery compiledQuery = dc.query().from(table1).select("name").where(COLUMN_CONTRIBUTOR_COUNTRY)
                .eq(param1).compile();
        try {
            assertEquals(1, compiledQuery.getParameters().size());
            assertSame(param1, compiledQuery.getParameters().get(0));

            DataSet ds = dc.executeQuery(compiledQuery, "denmark");
            try {
                assertTrue(ds.next());
                assertEquals("Row[values=[kasper]]", ds.getRow().toString());
                assertTrue(ds.next());
                assertEquals("Row[values=[asbjorn]]", ds.getRow().toString());
                assertTrue(ds.next());
                assertEquals("Row[values=[jesper]]", ds.getRow().toString());
                assertFalse(ds.next());
            } finally {
                ds.close();
            }

            try {
                ds = dc.executeQuery(compiledQuery, "canada");
                assertTrue(ds.next());
                assertEquals("Row[values=[daniel]]", ds.getRow().toString());
                assertFalse(ds.next());
            } finally {
                ds.close();
            }
        } finally {
            compiledQuery.close();
        }
    }
View Full Code Here

        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 {
            assertEquals(1, compiledQuery.getParameters().size());
            assertSame(param1, compiledQuery.getParameters().get(0));

            DataSet ds = dc.executeQuery(compiledQuery, "denmark");
            List<Object[]> objectArrays = ds.toObjectArrays();
            assertEquals(3, objectArrays.size());

        } finally {
            compiledQuery.close();
        }
    }
View Full Code Here

TOP

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

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.