001package gudusoft.gsqlparser.stmt.oceanbase;
002
003import gudusoft.gsqlparser.EDbVendor;
004import gudusoft.gsqlparser.ESqlStatementType;
005import gudusoft.gsqlparser.TCustomSqlStatement;
006import gudusoft.gsqlparser.nodes.TObjectName;
007import gudusoft.gsqlparser.nodes.TParseTreeVisitor;
008import gudusoft.gsqlparser.nodes.oceanbase.TOceanbaseCreateResourcePoolSqlNode;
009import gudusoft.gsqlparser.nodes.oceanbase.TOceanbaseResourceOption;
010
011import java.util.ArrayList;
012
013/**
014 * OceanBase {@code CREATE RESOURCE POOL} statement (Phase 4 Batch 2).
015 *
016 * <p>Exposes the pool name and the parsed resource option list (things
017 * like {@code UNIT = 'u_large'}, {@code UNIT_NUM = 2},
018 * {@code ZONE_LIST = ('z1','z2','z3')}). Typed convenience accessors
019 * scan the option list on demand so the core is not coupled to the
020 * exhaustive option vocabulary.
021 *
022 * <p>Tagged {@link ESqlStatementType#sstoceanbase_create_resource_pool}.
023 *
024 * @since 4.0.1.4
025 */
026public class TCreateResourcePoolSqlStatement extends TCustomSqlStatement {
027
028    private TObjectName poolName;
029    private ArrayList<TOceanbaseResourceOption> resourceOptions
030            = new ArrayList<TOceanbaseResourceOption>();
031
032    public TCreateResourcePoolSqlStatement(EDbVendor dbvendor) {
033        super(dbvendor);
034        this.sqlstatementtype = ESqlStatementType.sstoceanbase_create_resource_pool;
035    }
036
037    public TObjectName getPoolName() {
038        return poolName;
039    }
040
041    public ArrayList<TOceanbaseResourceOption> getResourceOptions() {
042        return resourceOptions;
043    }
044
045    /**
046     * @return the first value of the {@code UNIT} option (the unit
047     *         spec name this pool binds to), or {@code null} when not
048     *         specified.
049     */
050    public TObjectName getUnit() {
051        return firstValueOfOption("UNIT");
052    }
053
054    /**
055     * @return the {@code UNIT_NUM} value, or {@code null}.
056     */
057    public TObjectName getUnitNum() {
058        return firstValueOfOption("UNIT_NUM");
059    }
060
061    /**
062     * @return the list of zones inside
063     *         {@code ZONE_LIST = ('z1', 'z2', ...)}, or an empty list.
064     */
065    public ArrayList<TObjectName> getZoneList() {
066        for (TOceanbaseResourceOption opt : resourceOptions) {
067            if (opt == null || opt.getName() == null) continue;
068            if ("ZONE_LIST".equals(opt.getName())) {
069                return opt.getValues();
070            }
071        }
072        return new ArrayList<TObjectName>();
073    }
074
075    private TObjectName firstValueOfOption(String canonicalName) {
076        for (TOceanbaseResourceOption opt : resourceOptions) {
077            if (opt == null || opt.getName() == null) continue;
078            if (canonicalName.equals(opt.getName())) {
079                return opt.getFirstValue();
080            }
081        }
082        return null;
083    }
084
085    @Override
086    public int doParseStatement(TCustomSqlStatement psql) {
087        if (rootNode == null) return -1;
088        super.doParseStatement(psql);
089        TOceanbaseCreateResourcePoolSqlNode node =
090                (TOceanbaseCreateResourcePoolSqlNode) rootNode;
091        this.poolName = node.getPoolName();
092        this.resourceOptions = node.getResourceOptions();
093        return 0;
094    }
095
096    @Override
097    public void accept(TParseTreeVisitor v) {
098        v.preVisit(this);
099        v.postVisit(this);
100    }
101
102    @Override
103    public void acceptChildren(TParseTreeVisitor v) {
104        v.preVisit(this);
105        v.postVisit(this);
106    }
107}