com.hazelcast.org.apache.calcite.sql.SqlInsert Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.org.apache.calcite.sql;
import com.hazelcast.org.apache.calcite.sql.parser.SqlParserPos;
import com.hazelcast.org.apache.calcite.sql.validate.SqlValidator;
import com.hazelcast.org.apache.calcite.sql.validate.SqlValidatorScope;
import com.hazelcast.org.apache.calcite.util.ImmutableNullableList;
import com.hazelcast.org.checkerframework.checker.nullness.qual.Nullable;
import com.hazelcast.org.checkerframework.dataflow.qual.Pure;
import java.util.List;
/**
* A SqlInsert
is a node of a parse tree which represents an INSERT
* statement.
*/
public class SqlInsert extends SqlCall {
public static final SqlSpecialOperator OPERATOR =
new SqlSpecialOperator("INSERT", SqlKind.INSERT) {
@SuppressWarnings("argument.type.incompatible")
@Override public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
SqlParserPos pos,
@Nullable SqlNode... operands) {
return new SqlInsert(
pos,
(SqlNodeList) operands[0],
operands[1],
operands[2],
(SqlNodeList) operands[3]);
}
};
SqlNodeList keywords;
SqlNode targetTable;
SqlNode source;
@Nullable SqlNodeList columnList;
//~ Constructors -----------------------------------------------------------
public SqlInsert(SqlParserPos pos,
SqlNodeList keywords,
SqlNode targetTable,
SqlNode source,
@Nullable SqlNodeList columnList) {
super(pos);
this.keywords = keywords;
this.targetTable = targetTable;
this.source = source;
this.columnList = columnList;
assert keywords != null;
}
//~ Methods ----------------------------------------------------------------
@Override public SqlKind getKind() {
return SqlKind.INSERT;
}
@Override public SqlOperator getOperator() {
return OPERATOR;
}
@SuppressWarnings("nullness")
@Override public List getOperandList() {
return ImmutableNullableList.of(keywords, targetTable, source, columnList);
}
/** Returns whether this is an UPSERT statement.
*
* In SQL, this is represented using the {@code UPSERT} keyword rather than
* {@code INSERT}; in the abstract syntax tree, an UPSERT is indicated by the
* presence of a {@link SqlInsertKeyword#UPSERT} keyword. */
public final boolean isUpsert() {
return getModifierNode(SqlInsertKeyword.UPSERT) != null;
}
@SuppressWarnings("assignment.type.incompatible")
@Override public void setOperand(int i, @Nullable SqlNode operand) {
switch (i) {
case 0:
keywords = (SqlNodeList) operand;
break;
case 1:
assert operand instanceof SqlIdentifier;
targetTable = operand;
break;
case 2:
source = operand;
break;
case 3:
columnList = (SqlNodeList) operand;
break;
default:
throw new AssertionError(i);
}
}
/**
* Return the identifier for the target table of the insertion.
*/
public SqlNode getTargetTable() {
return targetTable;
}
/**
* Returns the source expression for the data to be inserted.
*/
public SqlNode getSource() {
return source;
}
public void setSource(SqlSelect source) {
this.source = source;
}
/**
* Returns the list of target column names, or null for all columns in the
* target table.
*/
@Pure
public @Nullable SqlNodeList getTargetColumnList() {
return columnList;
}
public final @Nullable SqlNode getModifierNode(SqlInsertKeyword modifier) {
for (SqlNode keyword : keywords) {
SqlInsertKeyword keyword2 =
((SqlLiteral) keyword).symbolValue(SqlInsertKeyword.class);
if (keyword2 == modifier) {
return keyword;
}
}
return null;
}
@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
writer.startList(SqlWriter.FrameTypeEnum.SELECT);
writer.sep(isUpsert() ? "UPSERT INTO" : "INSERT INTO");
final int opLeft = getOperator().getLeftPrec();
final int opRight = getOperator().getRightPrec();
targetTable.unparse(writer, opLeft, opRight);
if (columnList != null) {
columnList.unparse(writer, opLeft, opRight);
}
writer.newlineAndIndent();
source.unparse(writer, 0, 0);
}
@Override public void validate(SqlValidator validator, SqlValidatorScope scope) {
validator.validateInsert(this);
}
}