OrderByValue.java 5.9 KB
Newer Older
1
/*
2 3 4 5 6 7
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
8 9 10 11 12 13 14 15 16 17
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

18
package org.apache.shardingsphere.sharding.merge.dql.orderby;
19

ShardingSphere's avatar
ShardingSphere 已提交
20
import com.google.common.base.Preconditions;
21
import lombok.Getter;
22
import org.apache.shardingsphere.infra.metadata.schema.model.physical.PhysicalColumnMetaData;
23
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
24
import org.apache.shardingsphere.infra.metadata.schema.model.physical.PhysicalTableMetaData;
25 26
import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByItem;
import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
T
tristaZero 已提交
27 28 29 30
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.ColumnOrderByItemSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.IndexOrderByItemSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.OrderByItemSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
31
import org.apache.shardingsphere.infra.executor.sql.QueryResult;
32 33 34

import java.sql.SQLException;
import java.util.ArrayList;
T
terrymanu 已提交
35
import java.util.Collection;
36 37
import java.util.Collections;
import java.util.List;
38
import java.util.Map;
39 40 41 42 43 44 45 46 47

/**
 * Order by value.
 */
public final class OrderByValue implements Comparable<OrderByValue> {
    
    @Getter
    private final QueryResult queryResult;
    
T
terrymanu 已提交
48
    private final Collection<OrderByItem> orderByItems;
49
    
50 51
    private final List<Boolean> orderValuesCaseSensitive;
    
52 53
    private List<Comparable<?>> orderValues;
    
54
    public OrderByValue(final QueryResult queryResult, final Collection<OrderByItem> orderByItems, 
55
                        final SelectStatementContext selectStatementContext, final ShardingSphereSchema schema) throws SQLException {
56 57
        this.queryResult = queryResult;
        this.orderByItems = orderByItems;
58
        orderValuesCaseSensitive = getOrderValuesCaseSensitive(selectStatementContext, schema);
59 60
    }
    
61
    private List<Boolean> getOrderValuesCaseSensitive(final SelectStatementContext selectStatementContext, final ShardingSphereSchema schema) throws SQLException {
62
        List<Boolean> result = new ArrayList<>(orderByItems.size());
63
        for (OrderByItem eachOrderByItem : orderByItems) {
64
            result.add(getOrderValuesCaseSensitiveFromTables(selectStatementContext, schema, eachOrderByItem));
65 66 67 68
        }
        return result;
    }
    
69
    private boolean getOrderValuesCaseSensitiveFromTables(final SelectStatementContext selectStatementContext,
70
                                                          final ShardingSphereSchema schema, final OrderByItem eachOrderByItem) throws SQLException {
71 72
        for (SimpleTableSegment eachSimpleTableSegment : selectStatementContext.getAllTables()) {
            String tableName = eachSimpleTableSegment.getTableName().getIdentifier().getValue();
73
            PhysicalTableMetaData tableMetaData = schema.get(tableName);
74
            Map<String, PhysicalColumnMetaData> columns = tableMetaData.getColumns();
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
            OrderByItemSegment orderByItemSegment = eachOrderByItem.getSegment();
            if (orderByItemSegment instanceof ColumnOrderByItemSegment) {
                String columnName = ((ColumnOrderByItemSegment) orderByItemSegment).getColumn().getIdentifier().getValue();
                if (columns.containsKey(columnName)) {
                    return columns.get(columnName).isCaseSensitive();
                }
            } else if (orderByItemSegment instanceof IndexOrderByItemSegment) {
                int columnIndex = ((IndexOrderByItemSegment) orderByItemSegment).getColumnIndex();
                String columnName = queryResult.getColumnName(columnIndex);
                if (columns.containsKey(columnName)) {
                    return columns.get(columnName).isCaseSensitive();
                }
            } else {
                return false;
            }
        }
        return false;
    }
    
94 95 96 97 98 99 100 101
    /**
     * iterate next data.
     *
     * @return has next data
     * @throws SQLException SQL Exception
     */
    public boolean next() throws SQLException {
        boolean result = queryResult.next();
102
        orderValues = result ? getOrderValues() : Collections.emptyList();
103 104 105 106 107
        return result;
    }
    
    private List<Comparable<?>> getOrderValues() throws SQLException {
        List<Comparable<?>> result = new ArrayList<>(orderByItems.size());
T
terrymanu 已提交
108
        for (OrderByItem each : orderByItems) {
109 110 111 112 113 114 115 116 117
            Object value = queryResult.getValue(each.getIndex(), Object.class);
            Preconditions.checkState(null == value || value instanceof Comparable, "Order by value must implements Comparable");
            result.add((Comparable<?>) value);
        }
        return result;
    }
    
    @Override
    public int compareTo(final OrderByValue o) {
T
terrymanu 已提交
118 119
        int i = 0;
        for (OrderByItem each : orderByItems) {
120 121
            int result = CompareUtil.compareTo(orderValues.get(i), o.orderValues.get(i), each.getSegment().getOrderDirection(),
                each.getSegment().getNullOrderDirection(), orderValuesCaseSensitive.get(i));
122 123 124
            if (0 != result) {
                return result;
            }
T
terrymanu 已提交
125
            i++;
126 127 128 129
        }
        return 0;
    }
}