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 boolean invalidColumnList = false; 208 private TReturningClause returningClause = null; 209 private TResultColumnList onDuplicateKeyUpdate = null; 210 211 public void setOnDuplicateKeyUpdate(TResultColumnList onDuplicateKeyUpdate) { 212 this.onDuplicateKeyUpdate = onDuplicateKeyUpdate; 213 } 214 215 public TResultColumnList getOnDuplicateKeyUpdate() { 216 217 return onDuplicateKeyUpdate; 218 } 219 220 /** 221 * MySQL 8.0.20+ row alias for INSERT ... ON DUPLICATE KEY UPDATE 222 * Allows referencing the inserted row values without using VALUES() function. 223 * Example: INSERT INTO t1 (a,b,c) VALUES (1,2,3) AS new ON DUPLICATE KEY UPDATE c = new.a+new.b; 224 */ 225 private TObjectName insertRowAlias = null; 226 227 /** 228 * MySQL 8.0.20+ optional column aliases for the row alias. 229 * Example: INSERT INTO t1 (a,b,c) VALUES (1,2,3) AS new(m,n,p) ON DUPLICATE KEY UPDATE c = m+n; 230 */ 231 private TObjectNameList insertRowAliasColumnList = null; 232 233 public void setInsertRowAlias(TObjectName insertRowAlias) { 234 this.insertRowAlias = insertRowAlias; 235 } 236 237 public TObjectName getInsertRowAlias() { 238 return insertRowAlias; 239 } 240 241 public void setInsertRowAliasColumnList(TObjectNameList insertRowAliasColumnList) { 242 this.insertRowAliasColumnList = insertRowAliasColumnList; 243 } 244 245 public TObjectNameList getInsertRowAliasColumnList() { 246 return insertRowAliasColumnList; 247 } 248 249 /** 250 * Set row alias from grammar TDummy node. 251 * The dummy node contains: node1=alias name, objectNameList=column aliases (optional) 252 */ 253 public void setInsertRowAlias(TDummy dummyNode) { 254 if (dummyNode == null) return; 255 this.insertRowAlias = (TObjectName) dummyNode.node1; 256 this.insertRowAliasColumnList = dummyNode.objectNameList; 257 } 258 259 private TOnConflictClause onConflictClause; 260 261 public TOnConflictClause getOnConflictClause() { 262 return onConflictClause; 263 } 264 265 public void setOnConflictClause(TOnConflictClause onConflictClause) { 266 this.onConflictClause = onConflictClause; 267 } 268 269 private TSourceToken ignore; 270 private TSourceToken priority_delayed; 271 272 public void setIgnore(TSourceToken ignore) { 273 this.ignore = ignore; 274 } 275 276 public void setPriority_delayed(TSourceToken priority_delayed) { 277 this.priority_delayed = priority_delayed; 278 } 279 280 public TSourceToken getIgnore() { 281 282 return ignore; 283 } 284 285 public TSourceToken getPriority_delayed() { 286 return priority_delayed; 287 } 288 289 public void setReturningClause(TReturningClause returningClause) { 290 this.returningClause = returningClause; 291 } 292 293 /* 294 * this function was used in teradata y file to support syntax like this: 295 * insert into t(v1,v2,v3) 296 * @param columnList 297 */ 298 public void setColumnListByResultColumnList(TResultColumnList columnList){ 299 // expression in resultcolumn must be type TExpression.SimpleObject 300 this.columnList = new TObjectNameList(); 301 invalidColumnList = (columnList == null) || (columnList.size() == 0); 302 if (invalidColumnList) { 303 return; 304 } 305 for(int i=0; i<columnList.size();i++){ 306 TResultColumn column = columnList.getResultColumn(i); 307 TExpression expr = column == null ? null : column.getExpr(); 308 if ((expr != null) 309 && (expr.getExpressionType() == EExpressionType.simple_object_name_t) 310 && (expr.getObjectOperand() != null)){ 311 this.columnList.addObjectName(expr.getObjectOperand()); 312 } else { 313 invalidColumnList = true; 314 } 315 } 316 } 317 318 /** 319 * Whether Teradata column-list recovery produced a missing or non-name 320 * element that cannot be used as an INSERT target column. 321 */ 322 public boolean hasInvalidColumnList() { 323 return invalidColumnList; 324 } 325 326 public void setColumnList(TObjectNameList columnList) { 327 this.columnList = columnList; 328 } 329 330 public void setTargetTable(TFromTable targetTable) { 331 this.targetTable = targetTable; 332 } 333 334 public void setTargetTable(TDummy targetTableAndColumns) { 335 this.targetTable = (TFromTable) targetTableAndColumns.node1; 336 if (targetTableAndColumns.objectNameList != null){ 337 this.columnList = targetTableAndColumns.objectNameList; 338 } 339 if (targetTableAndColumns.node2 != null){ 340 this.subQueryNode = (TSelectSqlNode) targetTableAndColumns.node2; 341 this.valueType = TBaseType.vt_query; 342 } 343 } 344 345 public void setOutputClause(TOutputClause outputClause) { 346 this.outputClause = outputClause; 347 } 348 349 public void setTopClause(TTopClause topClause) { 350 this.topClause = topClause; 351 } 352 353 private TMultiTargetList values = null; 354 private TSelectSqlNode subQueryNode = null; 355 private TExecuteSqlNode executeSqlNode = null; 356 357 public TFunctionCall getFunctionCall() { 358 return functionCall; 359 } 360 361 private TFunctionCall functionCall = null; 362 363 /* 364 * this function was used in teradata y file to support syntax like this: 365 * insert into t(v1,v2,v3) 366 */ 367 368 public TObjectName getRecordName() { 369 return recordName; 370 } 371 372 private TObjectName recordName = null; 373 374 public void setValuesByMultiTarget(TMultiTarget mt){ 375 this.valueType = TBaseType.vt_values; 376 values = new TMultiTargetList(); 377 values.addMultiTarget(mt); 378 } 379 380 public void setValuesByMultiTargetList(TMultiTargetList mtList){ 381 this.valueType = TBaseType.vt_values; 382 values = mtList; 383 } 384 385 private TResultColumnList setColumnValues = null; 386 387 public TResultColumnList getSetColumnValues() { 388 return setColumnValues; 389 } 390 391 private TIsolationClause isolationClause = null; 392 393 public void setIsolationClause(TIsolationClause isolationClause) { 394 this.isolationClause = isolationClause; 395 } 396 397 public TIsolationClause getIsolationClause() { 398 return isolationClause; 399 } 400 401 public TExecuteSqlNode getExecuteSqlNode() { 402 return executeSqlNode; 403 } 404 405 public void setValues(TDummy dummyNode){ 406 valueType = dummyNode.int1; 407 switch(valueType){ 408 case TBaseType.vt_values: 409 values = (TMultiTargetList)dummyNode.list1; 410 break; 411 case TBaseType.vt_query: 412 subQueryNode = (TSelectSqlNode)dummyNode.node1; 413 break; 414 case TBaseType.vt_default_values: 415 break; 416 case TBaseType.vt_execute: 417 executeSqlNode = (TExecuteSqlNode)dummyNode.node1; 418 break; 419 case TBaseType.vt_values_function: 420 421 functionCall = (TFunctionCall)dummyNode.node1; 422 break; 423 case TBaseType.vt_values_oracle_record: 424 recordName = (TObjectName)dummyNode.node1; 425 break; 426 case TBaseType.vt_values_empty: 427 break; 428 case TBaseType.vt_set_column_value: 429 this.setColumnValues = (TResultColumnList)dummyNode.list1; 430 break; 431 case TBaseType.vt_table: 432 this.sourceValueTable = (TObjectName) dummyNode.node1; 433 break; 434 } 435 } 436 437 private ArrayList<TInsertSqlNode> insertSqlNodes; 438 439 public ArrayList<TInsertSqlNode> getInsertSqlNodes() { 440 return insertSqlNodes; 441 } 442 443 public void setInsertSqlNodes(ArrayList<TInsertSqlNode> insertSqlNodes) { 444 this.insertSqlNodes = insertSqlNodes; 445 } 446 447 public static void setFromList(ArrayList<TInsertSqlNode> insertSqlNodes, TFromTableList fromTables){ 448 for(int i=0;i<insertSqlNodes.size();i++){ 449 TInsertSqlNode insertSqlNode = insertSqlNodes.get(i); 450 insertSqlNode.getSubQueryNode().setFromTableList(fromTables); 451 } 452 } 453 454 public static TInsertSqlNode getFirstInsertSqlNode(ArrayList<TInsertSqlNode> insertSqlNodes){ 455 return insertSqlNodes.get(0); 456 } 457}