001package gudusoft.gsqlparser.stmt.flink;
002
003import gudusoft.gsqlparser.*;
004import gudusoft.gsqlparser.nodes.*;
005
006/**
007 * Flink DROP CATALOG statement.
008 *
009 * Syntax:
010 *   DROP CATALOG [IF EXISTS] catalog_name
011 *
012 * @since 3.2.0.0
013 */
014public class TFlinkDropCatalogStmt extends TCustomSqlStatement {
015
016    private TObjectName catalogName;
017    private boolean ifExists = false;
018
019    public TFlinkDropCatalogStmt(EDbVendor dbvendor) {
020        super(dbvendor);
021        sqlstatementtype = ESqlStatementType.sstdropcatalog;
022    }
023
024    public TObjectName getCatalogName() {
025        return catalogName;
026    }
027
028    public boolean isIfExists() {
029        return ifExists;
030    }
031
032    @Override
033    public int doParseStatement(TCustomSqlStatement psql) {
034        super.doParseStatement(psql);
035        extractStatementDetails();
036        return 0;
037    }
038
039    private void extractStatementDetails() {
040        TSourceTokenList tokens = this.sourcetokenlist;
041        if (tokens == null) return;
042
043        boolean foundCatalog = false;
044
045        for (int i = 0; i < tokens.size(); i++) {
046            TSourceToken token = tokens.get(i);
047            if (token.tokentype == ETokenType.ttwhitespace) continue;
048
049            String tokenStr = token.toString().toUpperCase();
050
051            if ("CATALOG".equals(tokenStr)) {
052                foundCatalog = true;
053                continue;
054            }
055
056            if ("IF".equals(tokenStr)) {
057                TSourceToken nextSolid = tokens.nextsolidtoken(i, 1, true);
058                if (nextSolid != null && "EXISTS".equals(nextSolid.toString().toUpperCase())) {
059                    this.ifExists = true;
060                }
061                continue;
062            }
063
064            if ("EXISTS".equals(tokenStr)) {
065                continue;
066            }
067
068            if (foundCatalog && this.catalogName == null) {
069                if (token.tokentype == ETokenType.ttidentifier ||
070                    (token.tokencode > 0 && !isKeyword(tokenStr))) {
071                    this.catalogName = new TObjectName();
072                    this.catalogName.setString(token.toString());
073                    this.catalogName.setStartToken(token);
074                    this.catalogName.setEndToken(token);
075                    break;
076                }
077            }
078        }
079    }
080
081    private boolean isKeyword(String str) {
082        String upper = str.toUpperCase();
083        return "DROP".equals(upper) || "CATALOG".equals(upper) || "IF".equals(upper) || "EXISTS".equals(upper);
084    }
085
086    @Override
087    public void accept(TParseTreeVisitor v) {
088        v.preVisit(this);
089        v.postVisit(this);
090    }
091
092    @Override
093    public void acceptChildren(TParseTreeVisitor v) {
094        v.preVisit(this);
095        v.postVisit(this);
096    }
097}