Package org.apache.metamodel.query

Examples of org.apache.metamodel.query.FilterItem


        timestampColumn.setNativeType("DATETIME");
        Query q = new Query()
                .from(table)
                .select(column)
                .select(timestampColumn)
                .where(new FilterItem(new SelectItem(timestampColumn), OperatorType.LESS_THAN, TimeComparator
                        .toDate("2014-06-28 14:06:00")));

        assertEquals(
                "SELECT MY_SCHEMA.\"foo\".\"bar\", timestamp FROM MY_SCHEMA.\"foo\" WHERE timestamp < CAST('2014-06-28 14:06:00' AS DATETIME)",
                qr.rewriteQuery(q));
View Full Code Here


public class AbstractQueryRewriterTest extends TestCase {

    public void testRewriteQuery() throws Exception {
        Query q = new Query().selectCount().from(new MutableTable("foobar"))
                .where(new MutableColumn("foob"), OperatorType.EQUALS_TO, null).groupBy(new MutableColumn("col1"))
                .having(new FilterItem(new SelectItem(new MutableColumn("col2")), OperatorType.GREATER_THAN, 40))
                .orderBy(new MutableColumn("bla"));
        assertEquals("SELECT COUNT(*) FROM foobar WHERE foob IS NULL GROUP BY col1 HAVING col2 > 40 ORDER BY bla ASC",
                q.toString());

        AbstractQueryRewriter rewriter = new DefaultQueryRewriter(null) {
View Full Code Here

            if (operand instanceof String) {
                String str = (String) operand;
                // escape single quotes
                if (str.indexOf('\'') != -1) {
                    str = escapeQuotes(str);
                    FilterItem replacementFilterItem = new FilterItem(item.getSelectItem(), item.getOperator(), str);
                    return super.rewriteFilterItem(replacementFilterItem);
                }
            } else if (operand instanceof Iterable || operand.getClass().isArray()) {
                // operand is a set of values (typically in combination with an
                // IN operator). Each individual element must be escaped.

                assert item.getOperator() == OperatorType.IN;

                @SuppressWarnings("unchecked")
                final List<Object> elements = (List<Object>) CollectionUtils.toList(operand);

                for (ListIterator<Object> it = elements.listIterator(); it.hasNext();) {
                    Object next = it.next();
                    if (next == null) {
                        logger.warn("element in IN list is NULL, which isn't supported by SQL. Stripping the element from the list: {}", item);
                        it.remove();
                    } else if (next instanceof String) {
                        String str = (String) next;
                        if (str.indexOf('\'') != -1) {
                            str = escapeQuotes(str);
                            it.set(str);
                        }
                    }
                }

                FilterItem replacementFilterItem = new FilterItem(item.getSelectItem(), item.getOperator(), elements);
                return super.rewriteFilterItem(replacementFilterItem);
            }
        }
        return super.rewriteFilterItem(item);
    }
View Full Code Here

        StringBuilder sb = new StringBuilder();
        if (havingClause.getItemCount() > 0) {
            sb.append(AbstractQueryClause.PREFIX_HAVING);
            List<FilterItem> items = havingClause.getItems();
            for (int i = 0; i < items.size(); i++) {
                FilterItem item = items.get(i);
                if (i != 0) {
                    sb.append(AbstractQueryClause.DELIM_AND);
                }
                sb.append(rewriteFilterItem(item));
            }
View Full Code Here

        StringBuilder sb = new StringBuilder();
        if (whereClause.getItemCount() > 0) {
            sb.append(AbstractQueryClause.PREFIX_WHERE);
            List<FilterItem> items = whereClause.getItems();
            for (int i = 0; i < items.size(); i++) {
                FilterItem item = items.get(i);
                if (i != 0) {
                    sb.append(AbstractQueryClause.DELIM_AND);
                }
                sb.append(rewriteFilterItem(item));
            }
View Full Code Here

        if (item.isCompoundFilter()) {
            FilterItem[] childItems = item.getChildItems();
            StringBuilder sb = new StringBuilder();
            sb.append('(');
            for (int i = 0; i < childItems.length; i++) {
                FilterItem child = childItems[i];
                if (i != 0) {
                    sb.append(' ');
                    sb.append(item.getLogicalOperator().toString());
                    sb.append(' ');
                }
                sb.append(rewriteFilterItem(child));
            }
            sb.append(')');
            return sb.toString();
        }

        final String primaryFilterSql = item.toSql(isSchemaIncludedInColumnPaths());

        final OperatorType operator = item.getOperator();
        if (operator == OperatorType.DIFFERENT_FROM) {
            final Object operand = item.getOperand();
            if (operand != null) {
                // special case in SQL where NULL is not treated as a value -
                // see Ticket #1058

                FilterItem isNullFilter = new FilterItem(item.getSelectItem(), OperatorType.EQUALS_TO, null);
                final String secondaryFilterSql = rewriteFilterItem(isNullFilter);

                return '(' + primaryFilterSql + " OR " + secondaryFilterSql + ')';
            }
        }
View Full Code Here

    }

    @Override
    public WhereBuilder<GroupedQueryBuilder> or(Column column) {
        if (_parentOrFilter == null) {
            _parentOrFilter = new FilterItem(_orFilters);
        }
        return new WhereBuilderImpl(column, _query, _parentOrFilter, _orFilters, getQueryBuilder());
    }
View Full Code Here

    q.select(actorTable.getColumns());
    q.getSelectClause().getItem(0).setAlias("foo-bar");
    assertEquals(
        "SELECT a.`actor_id` AS foo-bar, a.`first_name`, a.`last_name`, a.`last_update` FROM sakila.`actor` a",
        q.toString());
    FilterItem f1 = new FilterItem(q.getSelectClause().getItem(0), OperatorType.EQUALS_TO, 5);
    FilterItem f2 = new FilterItem(q.getSelectClause().getItem(0), OperatorType.EQUALS_TO, 8);
    q.where(new FilterItem(f1, f2));

    DataSet dataSet = dc.executeQuery(q);
    TableModel tableModel = new DataSetTableModel(dataSet);
    assertEquals(4, tableModel.getColumnCount());
    assertEquals(2, tableModel.getRowCount());
View Full Code Here

        QueryPostprocessDataContext dc = new CsvDataContext(file);
        Table table = dc.getDefaultSchema().getTableByName("csv_people.csv");

        Query q = new Query();
        q.from(table);
        q.where(new FilterItem(new SelectItem(table.getColumnByName("id")), OperatorType.EQUALS_TO, 1));
        q.select(table.getColumnByName("name"));
        DataSet data = dc.executeQuery(q);
        assertTrue(data.next());
        assertEquals("Row[values=[mike]]", data.getRow().toString());
        assertFalse(data.next());
View Full Code Here

        sb.append(" AND ");
      }
      if (!inlineValues) {
        if (isPreparedParameterCandidate(whereItem)) {
          // replace operator with parameter
          whereItem = new FilterItem(whereItem.getSelectItem(),
              whereItem.getOperator(), new QueryParameter());
        }
      }
      final String whereItemLabel = queryRewriter
          .rewriteFilterItem(whereItem);
View Full Code Here

TOP

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

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.