PostgreSQLSelectParser.java 3.9 KB
Newer Older
T
terrymanu 已提交
1
/*
2 3
 * Copyright 1999-2015 dangdang.com.
 * <p>
T
terrymanu 已提交
4 5 6 7 8 9 10 11 12 13 14
 * Licensed 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
 *
 *      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.
15
 * </p>
T
terrymanu 已提交
16 17
 */

T
terrymanu 已提交
18
package com.dangdang.ddframe.rdb.sharding.parsing.parser.dialect.postgresql;
T
terrymanu 已提交
19

20
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
T
terrymanu 已提交
21
import com.dangdang.ddframe.rdb.sharding.parsing.lexer.LexerEngine;
T
terrymanu 已提交
22
import com.dangdang.ddframe.rdb.sharding.parsing.lexer.dialect.postgresql.PostgreSQLKeyword;
T
terrymanu 已提交
23
import com.dangdang.ddframe.rdb.sharding.parsing.lexer.token.DefaultKeyword;
T
terrymanu 已提交
24
import com.dangdang.ddframe.rdb.sharding.parsing.lexer.token.Keyword;
T
terrymanu 已提交
25
import com.dangdang.ddframe.rdb.sharding.parsing.parser.exception.SQLParsingUnsupportedException;
26
import com.dangdang.ddframe.rdb.sharding.parsing.parser.sql.AbstractOrderBySQLParser;
T
terrymanu 已提交
27
import com.dangdang.ddframe.rdb.sharding.parsing.parser.sql.DistinctSQLParser;
28
import com.dangdang.ddframe.rdb.sharding.parsing.parser.sql.GroupBySQLParser;
T
terrymanu 已提交
29
import com.dangdang.ddframe.rdb.sharding.parsing.parser.sql.HavingSQLParser;
30
import com.dangdang.ddframe.rdb.sharding.parsing.parser.sql.SelectListSQLParser;
T
terrymanu 已提交
31
import com.dangdang.ddframe.rdb.sharding.parsing.parser.sql.WhereSQLParser;
T
terrymanu 已提交
32
import com.dangdang.ddframe.rdb.sharding.parsing.parser.statement.dql.select.AbstractSelectParser;
33
import com.dangdang.ddframe.rdb.sharding.parsing.parser.statement.dql.select.SelectStatement;
34

T
terrymanu 已提交
35 36 37 38 39 40
/**
 * PostgreSQL Select语句解析器.
 *
 * @author zhangliang
 */
public final class PostgreSQLSelectParser extends AbstractSelectParser {
T
terrymanu 已提交
41
    
T
terrymanu 已提交
42 43
    private final DistinctSQLParser distinctSQLParser;
    
44 45 46 47
    private final SelectListSQLParser selectListSQLParser;
    
    private final GroupBySQLParser groupBySQLParser;
    
T
terrymanu 已提交
48 49
    private final HavingSQLParser havingSQLParser;
    
50 51
    private final AbstractOrderBySQLParser orderBySQLParser;
    
T
terrymanu 已提交
52 53
    private  final PostgreSQLLimitSQLParser limitSQLParser;
    
54 55
    public PostgreSQLSelectParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) {
        super(shardingRule, lexerEngine, new WhereSQLParser(lexerEngine));
T
terrymanu 已提交
56
        distinctSQLParser = new DistinctSQLParser(lexerEngine);
57 58
        selectListSQLParser = new SelectListSQLParser(shardingRule, lexerEngine);
        groupBySQLParser = new GroupBySQLParser(lexerEngine);
T
terrymanu 已提交
59
        havingSQLParser = new HavingSQLParser(lexerEngine);
60
        orderBySQLParser = new PostgreSQLOrderBySQLParser(lexerEngine);
T
terrymanu 已提交
61
        limitSQLParser = new PostgreSQLLimitSQLParser(lexerEngine);
T
terrymanu 已提交
62 63
    }
    
T
terrymanu 已提交
64
    @Override
65
    protected void parseInternal(final SelectStatement selectStatement) {
T
terrymanu 已提交
66
        distinctSQLParser.parse();
67
        selectListSQLParser.parse(selectStatement, getItems());
68 69
        parseFrom(selectStatement);
        parseWhere(selectStatement);
70
        groupBySQLParser.parse(selectStatement);
T
terrymanu 已提交
71
        havingSQLParser.parse();
72
        orderBySQLParser.parse(selectStatement);
T
terrymanu 已提交
73
        limitSQLParser.parse(selectStatement);
T
terrymanu 已提交
74 75
        parseFor();
        parseRest();
T
terrymanu 已提交
76 77
    }
    
T
terrymanu 已提交
78
    private void parseFor() {
T
terrymanu 已提交
79
        if (!getLexerEngine().skipIfEqual(DefaultKeyword.FOR)) {
80 81
            return;
        }
T
terrymanu 已提交
82 83
        getLexerEngine().skipIfEqual(DefaultKeyword.UPDATE, PostgreSQLKeyword.SHARE);
        if (getLexerEngine().equalAny(DefaultKeyword.OF)) {
84
            throw new SQLParsingUnsupportedException(DefaultKeyword.OF);
T
terrymanu 已提交
85
        }
T
terrymanu 已提交
86
        getLexerEngine().skipIfEqual(PostgreSQLKeyword.NOWAIT);
T
terrymanu 已提交
87 88 89 90 91 92
    }
    
    @Override
    protected Keyword[] getUnsupportedKeywordsRest() {
        return new Keyword[] {PostgreSQLKeyword.WINDOW, DefaultKeyword.FETCH};
    }
T
terrymanu 已提交
93
}