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