001package gudusoft.gsqlparser.stmt.flink;
002
003import gudusoft.gsqlparser.*;
004import gudusoft.gsqlparser.nodes.*;
005
006/**
007 * Flink ALTER CATALOG statement.
008 *
009 * Syntax:
010 *   ALTER CATALOG catalog_name SET (key1=val1, ...)
011 *   ALTER CATALOG catalog_name RESET (key1, ...)
012 *   ALTER CATALOG catalog_name COMMENT 'new_comment'
013 *
014 * @since 3.2.0.0
015 */
016public class TFlinkAlterCatalogStmt extends TCustomSqlStatement {
017
018    public enum EAlterCatalogType {
019        actSet,
020        actReset,
021        actComment
022    }
023
024    private TObjectName catalogName;
025    private EAlterCatalogType alterType = EAlterCatalogType.actSet;
026    private TSourceToken comment;
027
028    public TFlinkAlterCatalogStmt(EDbVendor dbvendor) {
029        super(dbvendor);
030        sqlstatementtype = ESqlStatementType.sstaltercatalog;
031    }
032
033    public TObjectName getCatalogName() {
034        return catalogName;
035    }
036
037    public EAlterCatalogType getAlterType() {
038        return alterType;
039    }
040
041    public TSourceToken getComment() {
042        return comment;
043    }
044
045    public String getCommentString() {
046        if (comment == null) return null;
047        String s = comment.toString();
048        if (s.startsWith("'") && s.endsWith("'")) {
049            return s.substring(1, s.length() - 1);
050        }
051        return s;
052    }
053
054    @Override
055    public int doParseStatement(TCustomSqlStatement psql) {
056        super.doParseStatement(psql);
057        extractStatementDetails();
058        return 0;
059    }
060
061    private void extractStatementDetails() {
062        TSourceTokenList tokens = this.sourcetokenlist;
063        if (tokens == null) return;
064
065        boolean foundCatalog = false;
066
067        for (int i = 0; i < tokens.size(); i++) {
068            TSourceToken token = tokens.get(i);
069            if (token.tokentype == ETokenType.ttwhitespace) continue;
070
071            String tokenStr = token.toString().toUpperCase();
072
073            if ("CATALOG".equals(tokenStr)) {
074                foundCatalog = true;
075                continue;
076            }
077
078            if (foundCatalog && this.catalogName == null) {
079                if (token.tokentype == ETokenType.ttidentifier ||
080                    (token.tokencode > 0 && !isKeyword(tokenStr))) {
081                    this.catalogName = new TObjectName();
082                    this.catalogName.setString(token.toString());
083                    this.catalogName.setStartToken(token);
084                    this.catalogName.setEndToken(token);
085                    continue;
086                }
087            }
088
089            if ("SET".equals(tokenStr)) {
090                this.alterType = EAlterCatalogType.actSet;
091                break;
092            }
093
094            if ("RESET".equals(tokenStr)) {
095                this.alterType = EAlterCatalogType.actReset;
096                break;
097            }
098
099            if ("COMMENT".equals(tokenStr)) {
100                this.alterType = EAlterCatalogType.actComment;
101                TSourceToken nextSolid = tokens.nextsolidtoken(i, 1, true);
102                if (nextSolid != null && nextSolid.tokentype == ETokenType.ttsqstring) {
103                    this.comment = nextSolid;
104                }
105                break;
106            }
107        }
108    }
109
110    private boolean isKeyword(String str) {
111        String upper = str.toUpperCase();
112        return "ALTER".equals(upper) || "CATALOG".equals(upper) || "SET".equals(upper) ||
113               "RESET".equals(upper) || "COMMENT".equals(upper);
114    }
115
116    @Override
117    public void accept(TParseTreeVisitor v) {
118        v.preVisit(this);
119        v.postVisit(this);
120    }
121
122    @Override
123    public void acceptChildren(TParseTreeVisitor v) {
124        v.preVisit(this);
125        v.postVisit(this);
126    }
127}