001package gudusoft.gsqlparser.stmt.oceanbase;
002
003import gudusoft.gsqlparser.EDbVendor;
004import gudusoft.gsqlparser.ESqlStatementType;
005import gudusoft.gsqlparser.TCustomSqlStatement;
006import gudusoft.gsqlparser.nodes.TBaseTablePartition;
007import gudusoft.gsqlparser.nodes.TObjectName;
008import gudusoft.gsqlparser.nodes.TParseTreeVisitor;
009import gudusoft.gsqlparser.nodes.oceanbase.TOceanbaseCreateTablegroupSqlNode;
010import gudusoft.gsqlparser.nodes.oceanbase.TOceanbaseTablegroupOption;
011
012import java.util.ArrayList;
013
014/**
015 * OceanBase {@code CREATE TABLEGROUP} statement (Phase 4 Batch 6).
016 *
017 * <p>Exposes the tablegroup name, {@code IF NOT EXISTS} flag, and the
018 * full tablegroup option list. Typed convenience accessors scan the
019 * option list on demand.
020 *
021 * <p>Tagged {@link ESqlStatementType#sstoceanbase_create_tablegroup}.
022 *
023 * @since 4.0.1.4
024 */
025public class TCreateTablegroupSqlStatement extends TCustomSqlStatement {
026
027    private TObjectName tablegroupName;
028    private boolean ifNotExists;
029    private ArrayList<TOceanbaseTablegroupOption> tablegroupOptions
030            = new ArrayList<TOceanbaseTablegroupOption>();
031    private TBaseTablePartition tablePartition;
032
033    public TCreateTablegroupSqlStatement(EDbVendor dbvendor) {
034        super(dbvendor);
035        this.sqlstatementtype = ESqlStatementType.sstoceanbase_create_tablegroup;
036    }
037
038    public TObjectName getTablegroupName() {
039        return tablegroupName;
040    }
041
042    public boolean isIfNotExists() {
043        return ifNotExists;
044    }
045
046    public ArrayList<TOceanbaseTablegroupOption> getTablegroupOptions() {
047        return tablegroupOptions;
048    }
049
050    /**
051     * @return the trailing {@code PARTITION BY ...} clause, or
052     *         {@code null} if no partition clause was specified.
053     */
054    public TBaseTablePartition getTablePartition() {
055        return tablePartition;
056    }
057
058    /**
059     * @return the {@code PRIMARY_ZONE} value, or {@code null}.
060     */
061    public TObjectName getPrimaryZone() {
062        return firstValueOfOption("PRIMARY_ZONE");
063    }
064
065    /**
066     * @return the {@code LOCALITY} value, or {@code null}.
067     */
068    public TObjectName getLocality() {
069        return firstValueOfOption("LOCALITY");
070    }
071
072    /**
073     * @return the {@code BINDING} value (typically {@code TRUE} /
074     *         {@code FALSE}), or {@code null}.
075     */
076    public TObjectName getBinding() {
077        return firstValueOfOption("BINDING");
078    }
079
080    private TObjectName firstValueOfOption(String canonicalName) {
081        for (TOceanbaseTablegroupOption opt : tablegroupOptions) {
082            if (opt == null || opt.getName() == null) continue;
083            if (canonicalName.equals(opt.getName())) {
084                return opt.getFirstValue();
085            }
086        }
087        return null;
088    }
089
090    @Override
091    public int doParseStatement(TCustomSqlStatement psql) {
092        if (rootNode == null) return -1;
093        super.doParseStatement(psql);
094        TOceanbaseCreateTablegroupSqlNode node =
095                (TOceanbaseCreateTablegroupSqlNode) rootNode;
096        this.tablegroupName = node.getTablegroupName();
097        this.ifNotExists = node.isIfNotExists();
098        this.tablegroupOptions = node.getTablegroupOptions();
099        this.tablePartition = node.getTablePartition();
100        return 0;
101    }
102
103    @Override
104    public void accept(TParseTreeVisitor v) {
105        v.preVisit(this);
106        v.postVisit(this);
107    }
108
109    @Override
110    public void acceptChildren(TParseTreeVisitor v) {
111        v.preVisit(this);
112        v.postVisit(this);
113    }
114}