001package gudusoft.gsqlparser.nodes;
002
003import gudusoft.gsqlparser.EExpressionType;
004import gudusoft.gsqlparser.TBaseType;
005import gudusoft.gsqlparser.TSourceToken;
006import gudusoft.gsqlparser.nodes.oracle.TErrorLoggingClause;
007
008import java.util.ArrayList;
009
010/**
011 * Interal used by parser.
012*/
013public class TInsertSqlNode extends TParseTreeNode {
014
015    private String fileFormat;
016    private String fileOptions = null;
017
018    public String getFileFormat() {
019        return fileFormat;
020    }
021
022    public String getFileOptions() {
023        return fileOptions;
024    }
025
026    public void setFileFormat(TDummy dummy){
027        fileFormat = dummy.st1.toString();
028        if (dummy.node1 != null){
029            fileOptions = dummy.node1.toString();
030        }
031    }
032
033    private TObjectName sourceValueTable;
034
035    public TObjectName getSourceValueTable() {
036        return sourceValueTable;
037    }
038
039    private boolean insertAll = false;
040    private boolean insertFirst = false;
041
042    public boolean isInsertAll() {
043        return insertAll;
044    }
045
046    public boolean isInsertFirst() {
047        return insertFirst;
048    }
049
050    public void setInsertAllOrFirst(TSourceToken st){
051        if (st == null) return;
052        insertAll = st.toString().equalsIgnoreCase("all");
053        insertFirst = st.toString().equalsIgnoreCase("first");
054    }
055
056    private TErrorLoggingClause errorLoggingClause;
057
058    public void setErrorLoggingClause(TErrorLoggingClause errorLoggingClause) {
059        this.errorLoggingClause = errorLoggingClause;
060    }
061
062    public TErrorLoggingClause getErrorLoggingClause() {
063
064        return errorLoggingClause;
065    }
066
067    public void setDirectoryName(TObjectName directoryName) {
068        this.directoryName = directoryName;
069    }
070
071
072    public void setOverwrite(TSourceToken overwrite) {
073        if (overwrite == null) return;
074        if (overwrite.toString().equalsIgnoreCase("overwrite")) {
075            this.hiveInsertType = EHiveInsertType.overwriteTable;
076        }
077        return;
078    }
079
080
081    public void setHiveInsertType(EHiveInsertType hiveInsertType) {
082
083        this.hiveInsertType = hiveInsertType;
084        this.valueType = TBaseType.vt_hive;
085    }
086
087    private EHiveInsertType hiveInsertType = EHiveInsertType.intoUnknown;
088    private TObjectName directoryName;
089
090    public EHiveInsertType getHiveInsertType() {
091        return hiveInsertType;
092    }
093
094    public TObjectName getDirectoryName() {
095        return directoryName;
096    }
097    //private THiveTableOrPartition tableOrPartition;
098
099    private TPTNodeList<TInsertCondition>  insertConditions;
100
101    public void init(Object arg1){
102            directoryName = (TObjectName)arg1;
103    }
104
105    public TCTEList getCteList() {
106        return cteList;
107    }
108
109    public TPTNodeList<TInsertCondition> getInsertConditions() {
110        return insertConditions;
111    }
112
113    public TPTNodeList<TInsertIntoValue> getInsertIntoValues() {
114        return insertIntoValues;
115    }
116
117    public void setInsertConditions(TPTNodeList<TInsertCondition> insertConditions) {
118        this.insertConditions = insertConditions;
119    }
120
121    private TPTNodeList<TInsertIntoValue> elseIntoValues;
122
123    public void setElseIntoValues(TPTNodeList<TInsertIntoValue> elseIntoValues) {
124        this.elseIntoValues = elseIntoValues;
125    }
126
127    public TPTNodeList<TInsertIntoValue> getElseIntoValues() {
128
129        return elseIntoValues;
130    }
131
132    private TPTNodeList<TInsertIntoValue> insertIntoValues;
133
134    public void setInsertIntoValues(TPTNodeList<TInsertIntoValue> insertIntoValues) {
135        this.insertIntoValues = insertIntoValues;
136    }
137
138    public void setInsertToken(TSourceToken insertToken) {
139        this.insertToken = insertToken;
140    }
141
142    public TSourceToken getInsertToken() {
143
144        return insertToken;
145    }
146
147    private TSourceToken insertToken = null;
148
149    public TSelectSqlNode getSubQueryNode() {
150        return subQueryNode;
151    }
152
153    public TMultiTargetList getValues() {
154
155        return values;
156    }
157
158    public void setSubQueryNode(TSelectSqlNode subQueryNode) {
159        this.subQueryNode = subQueryNode;
160        this.valueType = TBaseType.vt_query;
161        if (this.valueType == TBaseType.vt_hive){
162            this.valueType = TBaseType.vt_hive_query;
163        }
164    }
165
166    public int getValueType() {
167
168        return valueType;
169    }
170
171    public void setValueType(int valueType) {
172        this.valueType = valueType;
173    }
174
175    private int valueType = TBaseType.vt_values;
176
177    public TCTEList cteList = null;
178    private TTopClause topClause = null;
179
180    public TReturningClause getReturningClause() {
181        return returningClause;
182    }
183
184    public TObjectNameList getColumnList() {
185
186        return columnList;
187    }
188
189    public TOutputClause getOutputClause() {
190
191        return outputClause;
192    }
193
194    public TFromTable getTargetTable() {
195
196        return targetTable;
197    }
198
199    public TTopClause getTopClause() {
200
201        return topClause;
202    }
203
204    private TOutputClause outputClause = null;
205    private TFromTable targetTable = null;
206    private TObjectNameList columnList = null;
207    private TReturningClause returningClause = null;
208    private TResultColumnList onDuplicateKeyUpdate = null;
209
210    public void setOnDuplicateKeyUpdate(TResultColumnList onDuplicateKeyUpdate) {
211        this.onDuplicateKeyUpdate = onDuplicateKeyUpdate;
212    }
213
214    public TResultColumnList getOnDuplicateKeyUpdate() {
215
216        return onDuplicateKeyUpdate;
217    }
218
219    /**
220     * MySQL 8.0.20+ row alias for INSERT ... ON DUPLICATE KEY UPDATE
221     * Allows referencing the inserted row values without using VALUES() function.
222     * Example: INSERT INTO t1 (a,b,c) VALUES (1,2,3) AS new ON DUPLICATE KEY UPDATE c = new.a+new.b;
223     */
224    private TObjectName insertRowAlias = null;
225
226    /**
227     * MySQL 8.0.20+ optional column aliases for the row alias.
228     * Example: INSERT INTO t1 (a,b,c) VALUES (1,2,3) AS new(m,n,p) ON DUPLICATE KEY UPDATE c = m+n;
229     */
230    private TObjectNameList insertRowAliasColumnList = null;
231
232    public void setInsertRowAlias(TObjectName insertRowAlias) {
233        this.insertRowAlias = insertRowAlias;
234    }
235
236    public TObjectName getInsertRowAlias() {
237        return insertRowAlias;
238    }
239
240    public void setInsertRowAliasColumnList(TObjectNameList insertRowAliasColumnList) {
241        this.insertRowAliasColumnList = insertRowAliasColumnList;
242    }
243
244    public TObjectNameList getInsertRowAliasColumnList() {
245        return insertRowAliasColumnList;
246    }
247
248    /**
249     * Set row alias from grammar TDummy node.
250     * The dummy node contains: node1=alias name, objectNameList=column aliases (optional)
251     */
252    public void setInsertRowAlias(TDummy dummyNode) {
253        if (dummyNode == null) return;
254        this.insertRowAlias = (TObjectName) dummyNode.node1;
255        this.insertRowAliasColumnList = dummyNode.objectNameList;
256    }
257
258    private TOnConflictClause onConflictClause;
259
260    public TOnConflictClause getOnConflictClause() {
261        return onConflictClause;
262    }
263
264    public void setOnConflictClause(TOnConflictClause onConflictClause) {
265        this.onConflictClause = onConflictClause;
266    }
267
268    private TSourceToken ignore;
269    private TSourceToken priority_delayed;
270
271    public void setIgnore(TSourceToken ignore) {
272        this.ignore = ignore;
273    }
274
275    public void setPriority_delayed(TSourceToken priority_delayed) {
276        this.priority_delayed = priority_delayed;
277    }
278
279    public TSourceToken getIgnore() {
280
281        return ignore;
282    }
283
284    public TSourceToken getPriority_delayed() {
285        return priority_delayed;
286    }
287
288    public void setReturningClause(TReturningClause returningClause) {
289        this.returningClause = returningClause;
290    }
291
292    /*
293     * this function was used in teradata y file to support syntax like this:
294      * insert into t(v1,v2,v3) 
295     * @param columnList
296     */
297    public void setColumnListByResultColumnList(TResultColumnList columnList){
298        // expression in resultcolumn must be type TExpression.SimpleObject
299        this.columnList = new TObjectNameList();
300        TResultColumn column = null;
301        TExpression expr = null;
302        for(int i=0; i<columnList.size();i++){
303            column = columnList.getResultColumn(i);
304            expr = column.getExpr();
305            if (expr.getExpressionType() == EExpressionType.simple_object_name_t){
306                this.columnList.addObjectName(expr.getObjectOperand());
307            }
308        }
309    }
310
311    public void setColumnList(TObjectNameList columnList) {
312        this.columnList = columnList;
313    }
314
315    public void setTargetTable(TFromTable targetTable) {
316        this.targetTable = targetTable;
317    }
318
319    public void setTargetTable(TDummy targetTableAndColumns) {
320        this.targetTable = (TFromTable) targetTableAndColumns.node1;
321        if (targetTableAndColumns.objectNameList != null){
322            this.columnList = targetTableAndColumns.objectNameList;
323        }
324        if (targetTableAndColumns.node2 != null){
325            this.subQueryNode = (TSelectSqlNode) targetTableAndColumns.node2;
326            this.valueType = TBaseType.vt_query;
327        }
328    }
329
330    public void setOutputClause(TOutputClause outputClause) {
331        this.outputClause = outputClause;
332    }
333
334    public void setTopClause(TTopClause topClause) {
335        this.topClause = topClause;
336    }
337
338    private TMultiTargetList values = null;
339    private TSelectSqlNode subQueryNode = null;
340    private TExecuteSqlNode executeSqlNode = null;
341
342    public TFunctionCall getFunctionCall() {
343        return functionCall;
344    }
345
346    private TFunctionCall functionCall = null;
347
348    /*
349     * this function was used in teradata y file to support syntax like this:
350      * insert into t(v1,v2,v3)
351     */
352
353    public TObjectName getRecordName() {
354        return recordName;
355    }
356
357    private TObjectName recordName = null;
358
359    public void setValuesByMultiTarget(TMultiTarget mt){
360       this.valueType = TBaseType.vt_values;
361       values = new TMultiTargetList();
362       values.addMultiTarget(mt);
363    }
364
365    public void setValuesByMultiTargetList(TMultiTargetList mtList){
366        this.valueType = TBaseType.vt_values;
367        values = mtList;
368    }
369
370    private TResultColumnList setColumnValues = null;
371
372    public TResultColumnList getSetColumnValues() {
373        return setColumnValues;
374    }
375
376    private TIsolationClause isolationClause = null;
377
378    public void setIsolationClause(TIsolationClause isolationClause) {
379        this.isolationClause = isolationClause;
380    }
381
382    public TIsolationClause getIsolationClause() {
383        return isolationClause;
384    }
385
386    public TExecuteSqlNode getExecuteSqlNode() {
387        return executeSqlNode;
388    }
389
390    public void setValues(TDummy dummyNode){
391        valueType = dummyNode.int1;
392        switch(valueType){
393            case TBaseType.vt_values:
394                values = (TMultiTargetList)dummyNode.list1;
395                break;
396            case TBaseType.vt_query:
397                subQueryNode = (TSelectSqlNode)dummyNode.node1;
398                break;
399            case TBaseType.vt_default_values:
400                break;
401            case TBaseType.vt_execute:
402                executeSqlNode = (TExecuteSqlNode)dummyNode.node1;
403                break;
404            case TBaseType.vt_values_function:
405
406                functionCall = (TFunctionCall)dummyNode.node1;
407                break;
408            case TBaseType.vt_values_oracle_record:
409                recordName = (TObjectName)dummyNode.node1;
410                break;
411            case TBaseType.vt_values_empty:
412                break;
413            case TBaseType.vt_set_column_value:
414                this.setColumnValues = (TResultColumnList)dummyNode.list1;
415                break;
416            case TBaseType.vt_table:
417                this.sourceValueTable = (TObjectName) dummyNode.node1;
418                break;
419        }
420    }
421
422    private ArrayList<TInsertSqlNode> insertSqlNodes;
423
424    public ArrayList<TInsertSqlNode> getInsertSqlNodes() {
425        return insertSqlNodes;
426    }
427
428    public void setInsertSqlNodes(ArrayList<TInsertSqlNode> insertSqlNodes) {
429        this.insertSqlNodes = insertSqlNodes;
430    }
431
432    public static void setFromList(ArrayList<TInsertSqlNode> insertSqlNodes, TFromTableList fromTables){
433        for(int i=0;i<insertSqlNodes.size();i++){
434            TInsertSqlNode insertSqlNode = insertSqlNodes.get(i);
435            insertSqlNode.getSubQueryNode().setFromTableList(fromTables);
436        }
437    }
438
439    public static TInsertSqlNode getFirstInsertSqlNode(ArrayList<TInsertSqlNode> insertSqlNodes){
440        return  insertSqlNodes.get(0);
441    }
442}