Package org.apache.metamodel.query

Examples of org.apache.metamodel.query.SelectItem


                        // multiplicity)
                        boolean uniqueForeignKeys = true;

                        Column[] foreignColumns = foreignKeyRelationship.getForeignColumns();

                        SelectItem countAllItem = SelectItem.getCountAllItem();
                        Query q = new Query().select(foreignColumns).select(countAllItem).from(table).groupBy(foreignColumns);
                        DataSet data = executeQuery(q);
                        Comparable<Object> comparable = NumberComparator.getComparable(1);
                        while (data.next()) {
                            Object value = data.getRow().getValue(countAllItem);
View Full Code Here


                    sb.append(" ORDER BY ");
                } else {
                    sb.append(", ");
                }

                final SelectItem selectItem = orderByItem.getSelectItem();
                validateSoqlSupportedSelectItem(selectItem);

                final Column column = selectItem.getColumn();
                sb.append(column.getName());
                sb.append(' ');
                sb.append(orderByItem.getDirection());

                i++;
View Full Code Here

            }
            sb.append(')');
            return;
        }

        final SelectItem selectItem = filterItem.getSelectItem();
        validateSoqlSupportedSelectItem(selectItem);

        final Column column = selectItem.getColumn();
        sb.append(column.getName());
        sb.append(' ');

        final OperatorType operator = filterItem.getOperator();
        if (operator == OperatorType.IN) {
            throw new UnsupportedOperationException("IN operator not supported: " + filterItem);
        }
        sb.append(operator.toSql());
        sb.append(' ');

        final Object operand = filterItem.getOperand();
        if (operand == null) {
            sb.append("null");
        } else if (operand instanceof String) {
            sb.append('\'');

            String str = operand.toString();
            str = str.replaceAll("\'", "\\\\'");
            str = str.replaceAll("\"", "\\\\\"");
            str = str.replaceAll("\r", "\\\\r");
            str = str.replaceAll("\n", "\\\\n");
            str = str.replaceAll("\t", "\\\\t");

            sb.append(str);
            sb.append('\'');
        } else if (operand instanceof Number) {
            sb.append(operand);
        } else if (operand instanceof Date) {
            final SimpleDateFormat dateFormat;
            ColumnType expectedColumnType = selectItem.getExpectedColumnType();
            if (expectedColumnType == ColumnType.DATE) {
                // note: we don't apply the timezone for DATE fields, since they
                // don't contain time-of-day information.
                dateFormat = new SimpleDateFormat(SOQL_DATE_FORMAT_OUT);
            } else if (expectedColumnType == ColumnType.TIME) {
                dateFormat = new SimpleDateFormat(SOQL_TIME_FORMAT_OUT, Locale.ENGLISH);
                dateFormat.setTimeZone(SOQL_TIMEZONE);
            } else {
                dateFormat = new SimpleDateFormat(SOQL_DATE_TIME_FORMAT_OUT, Locale.ENGLISH);
                dateFormat.setTimeZone(SOQL_TIMEZONE);
            }

            String str = dateFormat.format((Date) operand);
            logger.debug("Date '{}' formatted as: {}", operand, str);
            sb.append(str);
        } else if (operand instanceof Column) {
            sb.append(((Column) operand).getName());
        } else if (operand instanceof SelectItem) {
            SelectItem operandSelectItem = (SelectItem) operand;
            validateSoqlSupportedSelectItem(operandSelectItem);
            sb.append(operandSelectItem.getColumn().getName());
        } else {
            throw new UnsupportedOperationException("Unsupported operand: " + operand);
        }
    }
View Full Code Here

        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

    boolean hasConverter = false;
    SelectItem[] selectItems = dataSet.getSelectItems();
    TypeConverter<?, ?>[] converterArray = new TypeConverter[selectItems.length];
    for (int i = 0; i < selectItems.length; i++) {
      SelectItem selectItem = selectItems[i];
      Column column = selectItem.getColumn();
      if (column != null && selectItem.getFunction() == null) {
        TypeConverter<?, ?> converter = converters.get(column);
        if (converter != null) {
          hasConverter = true;
          converterArray[i] = converter;
        }
View Full Code Here

    public void testApproximatedCountHugeFile() throws Exception {
        DataContext dc = new CsvDataContext(getHugeFile());

        Table table = dc.getDefaultSchema().getTables()[0];
        Query q = dc.query().from(table).selectCount().toQuery();
        SelectItem selectItem = q.getSelectClause().getItem(0);
        selectItem.setFunctionApproximationAllowed(true);

        DataSet ds = dc.executeQuery(q);
        assertTrue(ds.next());
        Object[] values = ds.getRow().getValues();
        assertEquals(1, values.length);
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

    public String rewriteFilterItem(FilterItem item) {
        if (item.isCompoundFilter()) {
            return super.rewriteFilterItem(item);
        }

        final SelectItem selectItem = item.getSelectItem();
        final Object operand = item.getOperand();
        final OperatorType operator = item.getOperator();

        if (selectItem == null || operand == null || operator == null) {
            return super.rewriteFilterItem(item);
        }

        final Column column = selectItem.getColumn();
        if (column == null) {
            return super.rewriteFilterItem(item);
        }

        if (operand instanceof Date) {
            final String nativeType = column.getNativeType();
            if ("TIMESTAMP".equalsIgnoreCase(nativeType) || "DATETIME".equalsIgnoreCase(nativeType)) {
                final StringBuilder sb = new StringBuilder();
                sb.append(selectItem.getSameQueryAlias(true));

                FilterItem.appendOperator(sb, operand, operator);

                final Date date = (Date) operand;
View Full Code Here

    }

    @Override
    public String rewriteFilterItem(FilterItem item) {
        if (!item.isCompoundFilter()) {
            final SelectItem selectItem = item.getSelectItem();
            final Column column = selectItem.getColumn();
            if (column != null) {
                if (column.getType() == ColumnType.TIMESTAMP) {
                    // HSQLDB does not treat (TIMESTAMP 'yyyy-MM-dd hh:mm:ss')
                    // tokens correctly
                    String result = super.rewriteFilterItem(item);
View Full Code Here

            throw new IllegalStateException(
                    "Salesforce only allows deletion of records by their specific IDs. Violated by operator in where item: "
                            + whereItem);
        }

        final SelectItem selectItem = whereItem.getSelectItem();
        final Column column = selectItem.getColumn();

        final Object operand = whereItem.getOperand();

        if (column == null || operand == null || selectItem.getFunction() != null) {
            throw new IllegalStateException(
                    "Salesforce only allows deletion of records by their specific IDs. Violated by where item: "
                            + whereItem);
        }
        if (!column.isPrimaryKey()) {
View Full Code Here

TOP

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

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.