001package gudusoft.gsqlparser.stmt;
002
003import gudusoft.gsqlparser.*;
004import gudusoft.gsqlparser.nodes.*;
005
006
007public class TSetStmt extends TCustomSqlStatement {
008    private TObjectName variableName;
009    private TObjectNameList variableNameList;
010    private TExpression variableValue;
011    private TExpressionList variableValueList;
012    private ESetStatementType setStatementType;
013
014    private TObjectName characterSetName;
015    private TObjectName collationName;
016    private TObjectName userName;
017    private TObjectName password;
018    private ETransactionIsolationLevel isolationLevel;
019
020    public TObjectName getCharacterSetName() {
021        return characterSetName;
022    }
023
024    public TObjectName getCollationName() {
025        return collationName;
026    }
027
028    public TObjectName getUserName() {
029        return userName;
030    }
031
032    public TObjectName getPassword() {
033        return password;
034    }
035
036    public ETransactionIsolationLevel getIsolationLevel() {
037        return isolationLevel;
038    }
039
040    public ESetStatementType getSetStatementType() {
041        return setStatementType;
042    }
043
044    public TObjectName getVariableName() {
045        return variableName;
046    }
047
048    public TObjectNameList getVariableNameList() {
049        return variableNameList;
050    }
051
052    public TExpression getVariableValue() {
053        return variableValue;
054    }
055
056    public TExpressionList getVariableValueList() {
057        return variableValueList;
058    }
059
060    public TSetStmt(EDbVendor dbvendor) {
061        super(dbvendor);
062        sqlstatementtype = ESqlStatementType.sstset;
063    }
064
065    private TPTNodeList<TSetAssignment> assignments;
066
067    public TPTNodeList<TSetAssignment> getAssignments() {
068        return assignments;
069    }
070
071    public int doParseStatement(TCustomSqlStatement psql) {
072        if (rootNode == null) return -1;
073        super.doParseStatement(psql);
074        if (!(rootNode instanceof TSetSqlNode)) {
075            TSourceToken errorToken = rootNode.getStartToken();
076            if ((errorToken == null) && (sourcetokenlist.size() > 0)) {
077                errorToken = sourcetokenlist.get(0);
078            }
079            if (errorToken != null) {
080                parseerrormessagehandle(new TSyntaxError(errorToken,
081                        "invalid SET statement", EErrorType.sperror,
082                        TBaseType.MSG_ERROR_SYNTAX_ERROR, this));
083            } else {
084                parseerrormessagehandle(new TSyntaxError("SET", 0, 0,
085                        "invalid SET statement", EErrorType.sperror,
086                        TBaseType.MSG_ERROR_SYNTAX_ERROR, this, -1));
087            }
088            return TBaseType.MSG_ERROR_SYNTAX_ERROR;
089        }
090        TSetSqlNode node = (TSetSqlNode)rootNode;
091        setStatementType = node.getSetStatementType();
092
093        switch (dbvendor){
094            case dbvmysql:
095                parseMySqlSet(node);
096                break;
097            case dbvoceanbase:
098                // MySQL/system tenant: the MySQL SET (Mantis 4813); Oracle tenant keeps the default reading
099                if (isMySQLDialect()){
100                    parseMySqlSet(node);
101                }else{
102                    parseGenericSet(node);
103                }
104                break;
105            case dbvgreenplum:
106            case dbvpostgresql:
107            case dbvgaussdb:
108            case dbvedb:
109                switch (setStatementType){
110                    case variable:
111                        variableName = node.getVariableName();
112                        variableNameList = node.getVariableNameList();
113                        variableValue = node.getVariableValue();
114                        variableValueList = node.getVariableValueList();
115                        if (variableName.toString().equalsIgnoreCase("search_path")){
116                            if (getSqlEnv() != null)  {
117                                getSqlEnv().setDefaultSchemaName(variableValueList.getExpression(0).toString());
118                            }
119                        }
120                        break;
121                    case reset:
122                        variableName = node.getVariableName();
123                        variableName.setDbObjectType(EDbObjectType.variable);
124                        break;
125                    default:
126                        break;
127                }
128
129                break;
130            case dbvbigquery:
131                variableName = node.getVariableName();
132                variableValue = node.getVariableValue();
133                variableValue.doParse(this,ESqlClause.unknown);
134
135                variableNameList = node.getVariableNameList();
136                variableValueList = node.getVariableValueList();
137                if (variableValueList != null){
138                    variableValueList.doParse(this,ESqlClause.unknown);
139                }
140
141                break;
142            case dbvsnowflake:
143                variableName = node.getVariableName();
144                if (variableName != null){
145                    variableValue = node.getVariableValue();
146                    variableValue.doParse(this,ESqlClause.unknown);
147                }
148
149                variableNameList = node.getVariableNameList();
150                if (variableNameList != null){
151                    variableValueList = node.getVariableValueList();
152                    if (variableValueList != null){
153                        variableValueList.doParse(this,ESqlClause.unknown);
154                    }
155                }
156
157                break;
158            default:
159                parseGenericSet(node);
160                break;
161        }
162
163
164        return 0;
165    }
166
167    /** MySQL SET: variable assignments, character set, collation, user password, isolation level. */
168    private void parseMySqlSet(TSetSqlNode node){
169        assignments = node.getAssignments();
170        if (setStatementType == ESetStatementType.variable){
171            for (int i=0;i<assignments.size();i++){
172                assignments.getElement(i).getParameterValue().doParse(this,ESqlClause.setVariable);
173            }
174        }
175
176        characterSetName = node.getCharacterSetName();
177        collationName = node.getCollationName();
178        userName = node.getUserName();
179        password = node.getPassword();
180        isolationLevel = node.getIsolationLevel();
181    }
182
183    /** SET of the other dialects: one variable (list) and its value (list). */
184    private void parseGenericSet(TSetSqlNode node){
185        variableName = node.getVariableName();
186        variableNameList = node.getVariableNameList();
187        variableValue = node.getVariableValue();
188        variableValueList = node.getVariableValueList();
189        if (variableValue != null) {
190            variableValue.doParse(this, ESqlClause.unknown);
191        }
192        if (variableValueList != null) {
193            variableValueList.doParse(this, ESqlClause.unknown);
194        }
195    }
196    public void accept(TParseTreeVisitor v){
197        v.preVisit(this);
198        v.postVisit(this);
199    }
200    public void acceptChildren(TParseTreeVisitor v){
201        v.preVisit(this);
202        if (variableValue != null) {
203            variableValue.acceptChildren(v);
204        }
205        if (variableValueList != null) {
206            variableValueList.acceptChildren(v);
207        }
208        if (assignments != null) {
209            assignments.acceptChildren(v);
210        }
211        v.postVisit(this);
212    }
213
214}